Skip to content

Commit

Permalink
Fix issue #92, PdfReader.readpages() recursion error
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaupin authored May 4, 2017
1 parent f948261 commit 388f254
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
4 changes: 4 additions & 0 deletions pdfrw/objects/pdfarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ def sort(self, *args, **kw):
def pop(self, *args):
self._resolve()
return list.pop(self, *args)

def __reversed__(self):
self._resolve()
return list.__reversed__(self)
34 changes: 17 additions & 17 deletions pdfrw/pdfreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,24 +454,24 @@ def readpages(self, node):
typename = PdfName.Type
kidname = PdfName.Kids

# PDFs can have arbitrarily nested Pages/Page
# dictionary structures.
def readnode(node):
nodetype = node[typename]
if nodetype == pagename:
yield node
elif nodetype == pagesname:
for node in node[kidname]:
for node in readnode(node):
yield node
elif nodetype == catalogname:
for node in readnode(node[pagesname]):
yield node
else:
log.error('Expected /Page or /Pages dictionary, got %s' %
repr(node))
try:
return list(readnode(node))
result = []
stack = [node]
append = result.append
pop = stack.pop
while stack:
node = pop()
nodetype = node[typename]
if nodetype == pagename:
append(node)
elif nodetype == pagesname:
stack.extend(reversed(node[kidname]))
elif nodetype == catalogname:
stack.append(node[pagesname])
else:
log.error('Expected /Page or /Pages dictionary, got %s' %
repr(node))
return result
except (AttributeError, TypeError) as s:
log.error('Invalid page tree: %s' % s)
return []
Expand Down

0 comments on commit 388f254

Please sign in to comment.