From 188217832f421b37494910561dd10cf052afcf0c Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Sun, 1 May 2022 13:50:26 +0200 Subject: [PATCH] DEP: overwriteWarnings parameter of reader/merger PdfFileReader and PdfFileMerger no longer have the `overwriteWarnings`. The new behavior is `overwriteWarnings=False`. Additionally, PyPDF2.utils.formatWarning was removed --- CHANGELOG | 3 +++ PyPDF2/_reader.py | 32 ++------------------------------ PyPDF2/merger.py | 10 ++-------- PyPDF2/utils.py | 6 ------ 4 files changed, 7 insertions(+), 44 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index aa211a377a..ab63aa9d51 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,7 +3,10 @@ Version 2.0.0 Deprecations (DEP): - PyPDF2 2.0 requires Python 3.6+. Python 2.7 and 3.5 support were dropped. +- PdfFileReader and PdfFileMerger no longer have the `overwriteWarnings` + parameter. The new behavior is `overwriteWarnings=False`. - utils: + * `formatWarning` was removed * `isInt(obj)`: Use `instance(obj, int)` instead * `u_(s)`: Use `s` directly * `chr_(c)`: Use `chr(c)` instead diff --git a/PyPDF2/_reader.py b/PyPDF2/_reader.py index fcc46c95c9..b251a2cbe2 100644 --- a/PyPDF2/_reader.py +++ b/PyPDF2/_reader.py @@ -64,12 +64,7 @@ readNonWhitespace, readObject, ) -from PyPDF2.utils import ( - ConvertFunctionsToVirtualList, - b_, - formatWarning, - readUntilWhitespace, -) +from PyPDF2.utils import ConvertFunctionsToVirtualList, b_, readUntilWhitespace def convertToInt(d, size): @@ -188,32 +183,9 @@ class PdfFileReader(object): Defaults to ``True``. :param warndest: Destination for logging warnings (defaults to ``sys.stderr``). - :param bool overwriteWarnings: Determines whether to override Python's - ``warnings.py`` module with a custom implementation (defaults to - ``True``). """ - 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: - # It is possible for sys.stderr to be defined as None, most commonly in the case that the script - # is being run vida pythonw.exe on Windows. In this case, just swallow the warning. - # See also https://docs.python.org/3/library/sys.html# sys.__stderr__ - if file is not None: - file.write( - formatWarning(message, category, filename, lineno, line) - ) - except IOError: - pass - - warnings.showwarning = _showwarning + def __init__(self, stream, strict=True, warndest=None): self.strict = strict self.flattenedPages = None self.resolvedObjects = {} diff --git a/PyPDF2/merger.py b/PyPDF2/merger.py index 03f9afccb8..b790b7b553 100644 --- a/PyPDF2/merger.py +++ b/PyPDF2/merger.py @@ -63,12 +63,9 @@ class PdfFileMerger(object): :param bool strict: Determines whether user should be warned of all problems and also causes some correctable problems to be fatal. Defaults to ``True``. - :param bool overwriteWarnings: Determines whether to override Python's - ``warnings.py`` module with a custom implementation (defaults to - ``True``). """ - def __init__(self, strict=True, overwriteWarnings=True): + def __init__(self, strict=True): self.inputs = [] self.pages = [] self.output = PdfFileWriter() @@ -76,7 +73,6 @@ def __init__(self, strict=True, overwriteWarnings=True): self.named_dests = [] self.id_count = 0 self.strict = strict - self.overwriteWarnings = overwriteWarnings def merge( self, position, fileobj, bookmark=None, pages=None, import_bookmarks=True @@ -139,9 +135,7 @@ def merge( # Create a new PdfFileReader instance using the stream # (either file or BytesIO or StringIO) created above - pdfr = PdfFileReader( - fileobj, strict=self.strict, overwriteWarnings=self.overwriteWarnings - ) + pdfr = PdfFileReader(fileobj, strict=self.strict) if decryption_key is not None: pdfr._decryption_key = decryption_key diff --git a/PyPDF2/utils.py b/PyPDF2/utils.py index e7b5e1d439..12ffb4f69b 100644 --- a/PyPDF2/utils.py +++ b/PyPDF2/utils.py @@ -39,12 +39,6 @@ bytes_type = type(bytes()) # Works the same in Python 2.X and 3.X -def formatWarning(message, category, filename, lineno, line=None): - """custom implementation of warnings.formatwarning""" - 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.