Skip to content

Commit

Permalink
STY: Remove debug code
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma committed Apr 27, 2022
1 parent f90a9d7 commit 1b94dec
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 45 deletions.
8 changes: 0 additions & 8 deletions PyPDF2/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,14 +493,11 @@ def writeToStream(self, stream, encryption_key):

@staticmethod
def readFromStream(stream, pdf):
debug = False
if debug: print((stream.tell()))
name = stream.read(1)
if name != NameObject.surfix:
raise PdfReadError("name read error")
name += utils.readUntilRegex(stream, NameObject.delimiterPattern,
ignore_eof=True)
if debug: print(name)
try:
try:
ret=name.decode('utf-8')
Expand Down Expand Up @@ -575,7 +572,6 @@ def writeToStream(self, stream, encryption_key):

@staticmethod
def readFromStream(stream, pdf):
debug = False
tmp = stream.read(2)
if tmp != b_("<<"):
raise PdfReadError("Dictionary read error at byte %s: stream must begin with '<<'" % utils.hexStr(stream.tell()))
Expand All @@ -591,7 +587,6 @@ def readFromStream(stream, pdf):
if not tok:
raise PdfStreamError(STREAM_TRUNCATED_PREMATURELY)

if debug: print(("Tok:", tok))
if tok == b_(">"):
stream.read(1)
break
Expand Down Expand Up @@ -628,14 +623,11 @@ def readFromStream(stream, pdf):
# this is a stream object, not a dictionary
assert SA.LENGTH in data
length = data[SA.LENGTH]
if debug: print(data)
if isinstance(length, IndirectObject):
t = stream.tell()
length = pdf.getObject(length)
stream.seek(t, 0)
data["__streamdata__"] = stream.read(length)
if debug: print("here")
# if debug: print(binascii.hexlify(data["__streamdata__"]))
e = readNonWhitespace(stream)
ndstream = stream.read(8)
if (e + ndstream) != b_("endstream"):
Expand Down
33 changes: 0 additions & 33 deletions PyPDF2/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,6 @@ def write(self, stream):
"""
if hasattr(stream, 'mode') and 'b' not in stream.mode:
warnings.warn("File <%s> to write to is not in binary mode. It may not be written to correctly." % stream.name)
debug = False

if not self._root:
self._root = self._addObject(self._root_object)
Expand All @@ -517,7 +516,6 @@ def write(self, stream):
externalReferenceMap[data.pdf][data.generation][data.idnum] = IndirectObject(objIndex + 1, 0, self)

self.stack = []
if debug: print(("ERM:", externalReferenceMap, "root:", self._root))
self._sweepIndirectReferences(externalReferenceMap, self._root)
del self.stack

Expand Down Expand Up @@ -585,8 +583,6 @@ def addMetadata(self, infos):
self.getObject(self._info).update(args)

def _sweepIndirectReferences(self, externMap, data):
debug = False
if debug: print((data, "TYPE", data.__class__.__name__))
if isinstance(data, DictionaryObject):
for key, value in list(data.items()):
value = self._sweepIndirectReferences(externMap, value)
Expand Down Expand Up @@ -707,7 +703,6 @@ def addBookmarkDestination(self, dest, parent=None):
parent = outlineRef

parent = parent.getObject()
# print parent.__class__.__name__
parent.addChild(destRef, self)

return destRef
Expand Down Expand Up @@ -1669,11 +1664,8 @@ def _flatten(self, pages=None, inherit=None, indirectRef=None):
def _getObjectFromStream(self, indirectReference):
# indirect reference to object in object stream
# read the entire object stream into memory
debug = False
stmnum, idx = self.xref_objStm[indirectReference.idnum]
if debug: print(("Here1: %s %s"%(stmnum, idx)))
objStm = IndirectObject(stmnum, 0, self).getObject()
if debug: print(("Here2: objStm=%s.. stmnum=%s data=%s"%(objStm, stmnum, objStm.getData())))
# This is an xref to a stream, so its type better be a stream
assert objStm['/Type'] == '/ObjStm'
# /N is the number of indirect objects in the stream
Expand All @@ -1694,13 +1686,6 @@ def _getObjectFromStream(self, indirectReference):
if self.strict and idx != i:
raise PdfReadError("Object is in wrong index.")
streamData.seek(objStm['/First']+offset, 0)
if debug:
pos = streamData.tell()
streamData.seek(0, 0)
lines = streamData.readlines()
for i in range(0, len(lines)):
print(lines[i])
streamData.seek(pos, 0)
try:
obj = readObject(streamData, self)
except PdfStreamError as e:
Expand All @@ -1720,8 +1705,6 @@ def _getObjectFromStream(self, indirectReference):
return NullObject()

def getObject(self, indirectReference):
debug = False
if debug: print(("looking at:", indirectReference.idnum, indirectReference.generation))
retval = self.cacheGetIndirectObject(indirectReference.generation, indirectReference.idnum)
if retval is not None:
return retval
Expand All @@ -1731,7 +1714,6 @@ def getObject(self, indirectReference):
elif indirectReference.generation in self.xref and \
indirectReference.idnum in self.xref[indirectReference.generation]:
start = self.xref[indirectReference.generation][indirectReference.idnum]
if debug: print((" Uncompressed Object", indirectReference.idnum, indirectReference.generation, ":", start))
self.stream.seek(start, 0)
idnum, generation = self.readObjectHeader(self.stream)
if idnum != indirectReference.idnum and self.xrefIndex:
Expand Down Expand Up @@ -1810,10 +1792,7 @@ def readObjectHeader(self, stream):
return int(idnum), int(generation)

def cacheGetIndirectObject(self, generation, idnum):
debug = False
out = self.resolvedObjects.get((generation, idnum))
if debug and out: print(("cache hit: %d %d"%(idnum, generation)))
elif debug: print(("cache miss: %d %d"%(idnum, generation)))
return out

def cacheIndirectObject(self, generation, idnum, obj):
Expand All @@ -1826,8 +1805,6 @@ def cacheIndirectObject(self, generation, idnum, obj):
return obj

def read(self, stream):
debug = False
if debug: print(">>read", stream)
# start at the end:
stream.seek(-1, 2)
if not stream.tell():
Expand All @@ -1844,7 +1821,6 @@ def read(self, stream):
if stream.tell() < last1M:
raise PdfReadError("EOF marker not found")
line = self.readNextEndLine(stream)
if debug: print(" line:",line)

# find startxref entry - the location of the xref table
line = self.readNextEndLine(stream)
Expand Down Expand Up @@ -1957,7 +1933,6 @@ def read(self, stream):
# Index pairs specify the subsections in the dictionary. If
# none create one subsection that spans everything.
idx_pairs = xrefstream.get("/Index", [0, xrefstream.get("/Size")])
if debug: print(("read idx_pairs=%s"%list(self._pairs(idx_pairs))))
entrySizes = xrefstream.get("/W")
assert len(entrySizes) >= 3
if self.strict and len(entrySizes) > 3:
Expand Down Expand Up @@ -2087,24 +2062,18 @@ def _pairs(self, array):
break

def readNextEndLine(self, stream, limit_offset=0):
debug = False
if debug: print(">>readNextEndLine")
line_parts = []
while True:
# Prevent infinite loops in malformed PDFs
if stream.tell() == 0 or stream.tell() == limit_offset:
raise PdfReadError("Could not read malformed PDF file")
x = stream.read(1)
if debug: print((" x:", x, "%x"%ord(x)))
if stream.tell() < 2:
raise PdfReadError("EOL marker not found")
stream.seek(-2, 1)
if x == b_('\n') or x == b_('\r'): ## \n = LF; \r = CR
crlf = False
while x == b_('\n') or x == b_('\r'):
if debug:
if ord(x) == 0x0D: print(" x is CR 0D")
elif ord(x) == 0x0A: print(" x is LF 0A")
x = stream.read(1)
if x == b_('\n') or x == b_('\r'): # account for CR+LF
stream.seek(-1, 1)
Expand All @@ -2115,9 +2084,7 @@ def readNextEndLine(self, stream, limit_offset=0):
stream.seek(2 if crlf else 1, 1) # if using CR+LF, go back 2 bytes, else 1
break
else:
if debug: print(" x is neither")
line_parts.append(x)
if debug: print("leaving RNEL")
line_parts.reverse()
return b"".join(line_parts)

Expand Down
3 changes: 1 addition & 2 deletions Tests/test_basic_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ def test_basic_features():
reader = PdfFileReader(pdf_path)
writer = PdfFileWriter()

# print how many pages input1 has:
print("document1.pdf has %d pages." % reader.getNumPages())
reader.getNumPages()

# add page 1 from input1 to output document, unchanged
writer.addPage(reader.getPage(0))
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

from PyPDF2.errors import PdfReadError, PdfStreamError
from PyPDF2.filters import ASCIIHexDecode, FlateDecode, ASCII85Decode
from PyPDF2.filters import ASCII85Decode, ASCIIHexDecode, FlateDecode

filter_inputs = (
# "", '', """""",
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_page.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
import json
import os

import pytest

Expand Down

0 comments on commit 1b94dec

Please sign in to comment.