Skip to content

Commit

Permalink
STY: Fix various style issues (#742)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma authored Apr 12, 2022
1 parent 8b40f6b commit 1f00794
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
- name: Test with flake8
run: |
flake8 . --ignore E,F,I,SIM,C,PT,N,ASS,A,P,R,W
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
if: matrix.python-version != '2.7'

- name: Test with pytest
Expand Down
14 changes: 7 additions & 7 deletions PyPDF2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,17 @@ def decode(self):
http://www.rasip.fer.hr/research/compress/algorithms/fund/lz/lzw.html
and the PDFReference
"""
cW = self.CLEARDICT;
cW = self.CLEARDICT
baos=""
while True:
pW = cW;
cW = self.nextCode();
pW = cW
cW = self.nextCode()
if cW == -1:
raise PdfReadError("Missed the stop code in LZWDecode!")
if cW == self.STOP:
break;
break
elif cW == self.CLEARDICT:
self.resetDict();
self.resetDict()
elif pW == self.CLEARDICT:
baos+=self.dict[cW]
else:
Expand All @@ -255,7 +255,7 @@ def decode(self):
else:
p=self.dict[pW]+self.dict[pW][0]
baos+=p
self.dict[self.dictlen] = p;
self.dict[self.dictlen] = p
self.dictlen+=1
if (self.dictlen >= (1 << self.bitspercode) - 1 and
self.bitspercode < 12):
Expand All @@ -281,7 +281,7 @@ def decode(data, decodeParms=None):
if len(retval) == 0 and c == "<" and data[x+1] == "~":
x += 2
continue
#elif c.isspace():
# elif c.isspace():
# x += 1
# continue
elif c == 'z':
Expand Down
13 changes: 6 additions & 7 deletions PyPDF2/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def readObject(stream, pdf):
# number object OR indirect reference
peek = stream.read(20)
stream.seek(-len(peek), 1) # reset to start
if IndirectPattern.match(peek) != None:
if IndirectPattern.match(peek) is not None:
return IndirectObject.readFromStream(stream, pdf)
else:
return NumberObject.readFromStream(stream)
Expand Down Expand Up @@ -181,7 +181,7 @@ def __repr__(self):

def __eq__(self, other):
return (
other != None and
other is not None and
isinstance(other, IndirectObject) and
self.idnum == other.idnum and
self.generation == other.generation and
Expand Down Expand Up @@ -529,7 +529,7 @@ def __getitem__(self, key):
# return None if no metadata was found on the document root.
def getXmpMetadata(self):
metadata = self.get("/Metadata", None)
if metadata == None:
if metadata is None:
return None
metadata = metadata.getObject()
from . import xmp
Expand Down Expand Up @@ -614,7 +614,7 @@ def readFromStream(stream, pdf):
stream.seek(t, 0)
data["__streamdata__"] = stream.read(length)
if debug: print("here")
#if debug: print(binascii.hexlify(data["__streamdata__"]))
# if debug: print(binascii.hexlify(data["__streamdata__"]))
e = readNonWhitespace(stream)
ndstream = stream.read(8)
if (e + ndstream) != b_("endstream"):
Expand All @@ -631,7 +631,6 @@ def readFromStream(stream, pdf):
# we found it by looking back one character further.
data["__streamdata__"] = data["__streamdata__"][:-1]
else:
if debug: print(("E", e, ndstream, debugging.toHex(end)))
stream.seek(pos, 0)
raise utils.PdfReadError("Unable to find 'endstream' marker after stream at byte %s." % utils.hexStr(stream.tell()))
else:
Expand Down Expand Up @@ -706,9 +705,9 @@ def removeChild(self, child):
cur = curRef.getObject()
lastRef = self[NameObject('/Last')]
last = lastRef.getObject()
while cur != None:
while cur is not None:
if cur == childObj:
if prev == None:
if prev is None:
if NameObject('/Next') in cur:
# Removing first tree node
nextRef = cur[NameObject('/Next')]
Expand Down
30 changes: 15 additions & 15 deletions PyPDF2/merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def merge(self, position, fileobj, bookmark=None, pages=None, import_bookmarks=T
pdfr._decryption_key = decryption_key

# Find the range of pages to merge.
if pages == None:
if pages is None:
pages = (0, pdfr.getNumPages())
elif isinstance(pages, PageRange):
pages = pages.indices(pdfr.getNumPages())
Expand Down Expand Up @@ -219,8 +219,8 @@ def write(self, fileobj):
for page in self.pages:
self.output.addPage(page.pagedata)
page.out_pagedata = self.output.getReference(self.output._pages.getObject()["/Kids"][-1].getObject())
#idnum = self.output._objects.index(self.output._pages.getObject()["/Kids"][-1].getObject()) + 1
#page.out_pagedata = IndirectObject(idnum, 0, self.output)
# idnum = self.output._objects.index(self.output._pages.getObject()["/Kids"][-1].getObject()) + 1
# page.out_pagedata = IndirectObject(idnum, 0, self.output)

# Once all pages are added, create bookmarks to point at those pages
self._write_dests()
Expand Down Expand Up @@ -358,12 +358,12 @@ def _write_dests(self):
pageno = i
pdf = p.src
break
if pageno != None:
if pageno is not None:
self.output.addNamedDestinationObject(v)

def _write_bookmarks(self, bookmarks=None, parent=None):

if bookmarks == None:
if bookmarks is None:
bookmarks = self.bookmarks

last_added = None
Expand All @@ -377,10 +377,10 @@ def _write_bookmarks(self, bookmarks=None, parent=None):
if '/Page' in b:
for i, p in enumerate(self.pages):
if p.id == b['/Page']:
#b[NameObject('/Page')] = p.out_pagedata
# b[NameObject('/Page')] = p.out_pagedata
args = [NumberObject(p.id), NameObject(b['/Type'])]
#nothing more to add
#if b['/Type'] == '/Fit' or b['/Type'] == '/FitB'
# nothing more to add
# if b['/Type'] == '/Fit' or b['/Type'] == '/FitB'
if b['/Type'] == '/FitH' or b['/Type'] == '/FitBH':
if '/Top' in b and not isinstance(b['/Top'], NullObject):
args.append(FloatObject(b['/Top']))
Expand Down Expand Up @@ -431,7 +431,7 @@ def _write_bookmarks(self, bookmarks=None, parent=None):
pageno = i
pdf = p.src
break
if pageno != None:
if pageno is not None:
del b['/Page'], b['/Type']
last_added = self.output.addBookmarkDict(b, parent)

Expand All @@ -447,13 +447,13 @@ def _associate_dests_to_pages(self, pages):
if np.getObject() == p.pagedata.getObject():
pageno = p.id

if pageno != None:
if pageno is not None:
nd[NameObject('/Page')] = NumberObject(pageno)
else:
raise ValueError("Unresolved named destination '%s'" % (nd['/Title'],))

def _associate_bookmarks_to_pages(self, pages, bookmarks=None):
if bookmarks == None:
if bookmarks is None:
bookmarks = self.bookmarks

for b in bookmarks:
Expand All @@ -471,13 +471,13 @@ def _associate_bookmarks_to_pages(self, pages, bookmarks=None):
if bp.getObject() == p.pagedata.getObject():
pageno = p.id

if pageno != None:
if pageno is not None:
b[NameObject('/Page')] = NumberObject(pageno)
else:
raise ValueError("Unresolved bookmark '%s'" % (b['/Title'],))

def findBookmark(self, bookmark, root=None):
if root == None:
if root is None:
root = self.bookmarks

for i, b in enumerate(root):
Expand All @@ -499,7 +499,7 @@ def addBookmark(self, title, pagenum, parent=None):
:param parent: A reference to a parent bookmark to create nested
bookmarks.
"""
if parent == None:
if parent is None:
iloc = [len(self.bookmarks)-1]
elif isinstance(parent, list):
iloc = parent
Expand All @@ -508,7 +508,7 @@ def addBookmark(self, title, pagenum, parent=None):

dest = Bookmark(TextStringObject(title), NumberObject(pagenum), NameObject('/FitH'), NumberObject(826))

if parent == None:
if parent is None:
self.bookmarks.append(dest)
else:
bmparent = self.bookmarks
Expand Down
6 changes: 3 additions & 3 deletions PyPDF2/pagerange.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ def to_slice(self):
def __str__(self):
""" A string like "1:2:3". """
s = self._slice
if s.step == None:
if s.start != None and s.stop == s.start + 1:
if s.step is None:
if s.start is not None and s.stop == s.start + 1:
return str(s.start)

indices = s.start, s.stop
else:
indices = s.start, s.stop, s.step
return ':'.join("" if i == None else str(i) for i in indices)
return ':'.join("" if i is None else str(i) for i in indices)

def __repr__(self):
""" A string like "PageRange('1:2:3')". """
Expand Down
Loading

0 comments on commit 1f00794

Please sign in to comment.