From b2aae74044a639b5902f6cbd56ebd720f8640b44 Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Wed, 13 Apr 2022 19:21:04 +0200 Subject: [PATCH] STY: Fix style issues --- .github/workflows/unit-tests.yaml | 2 +- PyPDF2/__init__.py | 10 +++++++++- PyPDF2/filters.py | 2 +- PyPDF2/generic.py | 4 ++-- PyPDF2/merger.py | 5 ++--- PyPDF2/pdf.py | 12 ++++++------ PyPDF2/utils.py | 2 +- PyPDF2/xmp.py | 1 - Tests/test_merger.py | 1 - Tests/test_reader.py | 2 +- Tests/test_writer.py | 2 +- 11 files changed, 24 insertions(+), 19 deletions(-) diff --git a/.github/workflows/unit-tests.yaml b/.github/workflows/unit-tests.yaml index d28659fd8..e7e405a88 100644 --- a/.github/workflows/unit-tests.yaml +++ b/.github/workflows/unit-tests.yaml @@ -43,7 +43,7 @@ jobs: - name: Test with flake8 run: | - flake8 . --ignore=E203,W503,E501,F405,E226,E128,E225,F403,E201,E202,E231,W504,E241,F401,E261,E302,E211,E701,E228,E111,F841,E117,E127,E251,E266,E + flake8 . --ignore=E203,W503,W504,E,F403,F405 if: matrix.python-version != '2.7' - name: Test with pytest diff --git a/PyPDF2/__init__.py b/PyPDF2/__init__.py index bace8bf72..5d85cbc4f 100644 --- a/PyPDF2/__init__.py +++ b/PyPDF2/__init__.py @@ -3,4 +3,12 @@ from .pagerange import PageRange, parse_filename_page_ranges from ._version import __version__ -__all__ = ["pdf", "PdfFileMerger"] +__all__ = [ + "__version__", + "PageRange", + "parse_filename_page_ranges", + "pdf", + "PdfFileMerger", + "PdfFileReader", + "PdfFileWriter", +] diff --git a/PyPDF2/filters.py b/PyPDF2/filters.py index d0b98786f..361de77f9 100644 --- a/PyPDF2/filters.py +++ b/PyPDF2/filters.py @@ -55,7 +55,7 @@ def compress(data): # Unable to import zlib. Attempt to use the System.IO.Compression # library from the .NET framework. (IronPython only) import System - from System import IO, Collections, Array + from System import IO, Array def _string_to_bytearr(buf): retval = Array.CreateInstance(System.Byte, len(buf)) diff --git a/PyPDF2/generic.py b/PyPDF2/generic.py index 75d098621..800208f0c 100644 --- a/PyPDF2/generic.py +++ b/PyPDF2/generic.py @@ -483,10 +483,10 @@ def readFromStream(stream, pdf): try: try: ret=name.decode('utf-8') - except (UnicodeEncodeError, UnicodeDecodeError) as e: + except (UnicodeEncodeError, UnicodeDecodeError): ret=name.decode('gbk') return NameObject(ret) - except (UnicodeEncodeError, UnicodeDecodeError) as e: + except (UnicodeEncodeError, UnicodeDecodeError): # Name objects should represent irregular characters # with a '#' followed by the symbol's hex number if not pdf.strict: diff --git a/PyPDF2/merger.py b/PyPDF2/merger.py index eae26ed18..00393bbb8 100644 --- a/PyPDF2/merger.py +++ b/PyPDF2/merger.py @@ -311,7 +311,6 @@ def _trim_dests(self, pdf, dests, pages): page set. """ new_dests = [] - prev_header_added = True for k, o in list(dests.items()): for j in range(*pages): if pdf.getPage(j).getObject() == o['/Page'].getObject(): @@ -356,7 +355,7 @@ def _write_dests(self): if p.id == v['/Page']: v[NameObject('/Page')] = p.out_pagedata pageno = i - pdf = p.src + pdf = p.src # noqa: F841 break if pageno is not None: self.output.addNamedDestinationObject(v) @@ -429,7 +428,7 @@ def _write_bookmarks(self, bookmarks=None, parent=None): b[NameObject('/A')] = DictionaryObject({NameObject('/S'): NameObject('/GoTo'), NameObject('/D'): ArrayObject(args)}) pageno = i - pdf = p.src + pdf = p.src # noqa: F841 break if pageno is not None: del b['/Page'], b['/Type'] diff --git a/PyPDF2/pdf.py b/PyPDF2/pdf.py index fbe097fc2..f727f4fe4 100644 --- a/PyPDF2/pdf.py +++ b/PyPDF2/pdf.py @@ -41,7 +41,6 @@ __maintainer__ = "Phaseit, Inc." __maintainer_email = "PyPDF2@phaseit.net" -import string import math import struct import sys @@ -57,7 +56,6 @@ else: from io import BytesIO -from . import filters from . import utils import warnings import codecs @@ -543,7 +541,6 @@ def _sweepIndirectReferences(self, externMap, data): if debug: print((data, "TYPE", data.__class__.__name__)) if isinstance(data, DictionaryObject): for key, value in list(data.items()): - origvalue = value value = self._sweepIndirectReferences(externMap, value) if isinstance(value, StreamObject): # a dictionary value is a stream. streams must be indirect @@ -1752,7 +1749,10 @@ def readObjectHeader(self, stream): idnum = readUntilWhitespace(stream) extra |= utils.skipOverWhitespace(stream); stream.seek(-1, 1) generation = readUntilWhitespace(stream) - obj = stream.read(3) + + # although it's not used, it might still be necessary to read + _obj = stream.read(3) # noqa: F841 + readNonWhitespace(stream) stream.seek(-1, 1) if (extra and self.strict): @@ -1938,8 +1938,8 @@ def used_before(num, generation): # The rest of the elements depend on the xref_type if xref_type == 0: # linked list of free objects - next_free_object = getEntry(1) - next_generation = getEntry(2) + next_free_object = getEntry(1) # noqa: F841 + next_generation = getEntry(2) # noqa: F841 elif xref_type == 1: # objects that are in use but are not compressed byte_offset = getEntry(1) diff --git a/PyPDF2/utils.py b/PyPDF2/utils.py index cb392a18b..5c027c5b4 100644 --- a/PyPDF2/utils.py +++ b/PyPDF2/utils.py @@ -242,7 +242,7 @@ def b_(s): if len(s) < 2: bc[s] = r return r - except Exception as e: + except Exception: print(s) r = s.encode('utf-8') if len(s) < 2: diff --git a/PyPDF2/xmp.py b/PyPDF2/xmp.py index 3670f264a..9aec5e017 100644 --- a/PyPDF2/xmp.py +++ b/PyPDF2/xmp.py @@ -2,7 +2,6 @@ import datetime import decimal from .generic import PdfObject -from xml.dom import getDOMImplementation from xml.dom.minidom import parseString from .utils import u_ diff --git a/Tests/test_merger.py b/Tests/test_merger.py index ca2a40fc3..91ac6a777 100644 --- a/Tests/test_merger.py +++ b/Tests/test_merger.py @@ -1,5 +1,4 @@ import os -import binascii import sys import PyPDF2 diff --git a/Tests/test_reader.py b/Tests/test_reader.py index 96cddeb8b..070f69bd0 100644 --- a/Tests/test_reader.py +++ b/Tests/test_reader.py @@ -2,7 +2,7 @@ import os import pytest import PyPDF2.utils -from PyPDF2.filters import decodeStreamData, _xobj_to_image +from PyPDF2.filters import _xobj_to_image TESTS_ROOT = os.path.abspath(os.path.dirname(__file__)) PROJECT_ROOT = os.path.dirname(TESTS_ROOT) diff --git a/Tests/test_writer.py b/Tests/test_writer.py index 8c39258ff..3a14fb3a1 100644 --- a/Tests/test_writer.py +++ b/Tests/test_writer.py @@ -3,7 +3,7 @@ from PyPDF2 import PdfFileReader, PdfFileWriter from PyPDF2.utils import PageSizeNotDefinedError -from PyPDF2.generic import IndirectObject, RectangleObject +from PyPDF2.generic import RectangleObject TESTS_ROOT = os.path.abspath(os.path.dirname(__file__)) PROJECT_ROOT = os.path.dirname(TESTS_ROOT)