Navigation Menu

Skip to content

Commit

Permalink
Corrected error in getHeight() (Simon Kaempflein), removed warning me…
Browse files Browse the repository at this point in the history
…thod overwrites, added a more specific exception in case getNumPages fails on an encrypted PDF file.
  • Loading branch information
mstamy2 committed Feb 18, 2014
1 parent 4f3311e commit 89a29ef
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 22 deletions.
2 changes: 1 addition & 1 deletion PyPDF2/generic.py
Expand Up @@ -921,7 +921,7 @@ def getWidth(self):
return self.getUpperRight_x() - self.getLowerLeft_x()

def getHeight(self):
return self.getUpperRight_y() - self.getLowerLeft_x()
return self.getUpperRight_y() - self.getLowerLeft_y()

lowerLeft = property(getLowerLeft, setLowerLeft, None, None)
lowerRight = property(getLowerRight, setLowerRight, None, None)
Expand Down
20 changes: 4 additions & 16 deletions PyPDF2/pdf.py
Expand Up @@ -71,8 +71,6 @@
else:
from hashlib import md5

warnings.formatwarning = utils._formatwarning

##
# This class supports writing PDF files out, given pages produced by another
# class (typically {@link #PdfFileReader PdfFileReader}).
Expand Down Expand Up @@ -773,20 +771,8 @@ def setPageMode(self, mode):
# @param strict Determines whether user should be warned of all problems and
# also causes some correctable problems to be fatal. Defaults
# to True.
# @param warndest Allows redirection of warnings to any open file/stream. Defauls to
# the warnings default (sys.stderr)
class PdfFileReader(object):
def __init__(self, stream, strict=True, warndest=None):
# have to dynamically override the default showwarning since there are no
# public methods that specify the 'file' parameter
def _showwarning(message, category, filename, lineno, file=warndest, line=None):
if file is None:
file = sys.stderr
try:
file.write(warnings.formatwarning(message, category, filename, lineno, line))
except IOError:
pass
warnings.showwarning = _showwarning
def __init__(self, stream, strict=True):
self.strict = strict
self.flattenedPages = None
self.resolvedObjects = {}
Expand Down Expand Up @@ -861,6 +847,8 @@ def getNumPages(self):
try:
self._override_encryption = True
return self.trailer["/Root"]["/Pages"]["/Count"]
except:
raise utils.PdfReadError("File has not been decrypted")
finally:
self._override_encryption = False
else:
Expand Down Expand Up @@ -1171,7 +1159,7 @@ def getObject(self, indirectReference):
if not self._override_encryption and self.isEncrypted:
# if we don't have the encryption key:
if not hasattr(self, '_decryption_key'):
raise Exception("file has not been decrypted")
raise utils.PdfReadError("file has not been decrypted")
# otherwise, decrypt here...
import struct
pack1 = struct.pack("<i", indirectReference.idnum)[:3]
Expand Down
5 changes: 0 additions & 5 deletions PyPDF2/utils.py
Expand Up @@ -33,11 +33,6 @@
__author__ = "Mathieu Fenniak"
__author_email__ = "biziqe@mathieu.fenniak.net"

#custom implementation of warnings.formatwarning
def _formatwarning(message, category, filename, lineno, line=None):
file = filename.replace("/", "\\").rsplit("\\", 1)[1] # find the file name
return "%s: %s [%s:%s]\n" % (category.__name__, message, file, lineno)

def readUntilWhitespace(stream, maxchars=None):
"""
Reads non-whitespace characters and returns them.
Expand Down

0 comments on commit 89a29ef

Please sign in to comment.