Skip to content

Commit

Permalink
Reimplement custom warnings with overwriteWarnings flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mstamy2 committed Feb 26, 2014
1 parent 6a3d034 commit 0233dd5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
15 changes: 13 additions & 2 deletions PyPDF2/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
import codecs
from .generic import *
from .utils import readNonWhitespace, readUntilWhitespace, ConvertFunctionsToVirtualList
from .utils import b_, u_, ord_, chr_, str_, string_type
from .utils import b_, u_, ord_, chr_, str_, string_type, formatWarning

if version_info < ( 2, 4 ):
from sets import ImmutableSet as frozenset
Expand Down Expand Up @@ -772,7 +772,18 @@ def setPageMode(self, mode):
# also causes some correctable problems to be fatal. Defaults
# to True.
class PdfFileReader(object):
def __init__(self, stream, strict=True, warndest = None):
def __init__(self, stream, strict=True, warndest = None, overwriteWarnings = True):
if overwriteWarnings:
# 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(formatWarning(message, category, filename, lineno, line))
except IOError:
pass
warnings.showwarning = _showwarning
self.strict = strict
self.flattenedPages = None
self.resolvedObjects = {}
Expand Down
5 changes: 5 additions & 0 deletions PyPDF2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
__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 0233dd5

Please sign in to comment.