Skip to content

Commit

Permalink
MAINT: Use decorators for @staticmethod (#775)
Browse files Browse the repository at this point in the history
This drops support for Python 2.3 and older.

As Python 2.3 had it's last release in 2008 - 14 years ago! - I will not make a major version bump.
  • Loading branch information
MartinThoma committed Apr 18, 2022
1 parent f92e975 commit 316f5e6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
14 changes: 7 additions & 7 deletions PyPDF2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def compress(data):


class FlateDecode(object):
@staticmethod
def decode(data, decodeParms):
data = decompress(data)
predictor = 1
Expand Down Expand Up @@ -182,14 +183,14 @@ def decode(data, decodeParms):
# unsupported predictor
raise PdfReadError("Unsupported flatedecode predictor %r" % predictor)
return data
decode = staticmethod(decode) # type: ignore

@staticmethod
def encode(data):
return compress(data)
encode = staticmethod(encode) # type: ignore


class ASCIIHexDecode(object):
@staticmethod
def decode(data, decodeParms=None):
retval = ""
char = ""
Expand All @@ -208,7 +209,6 @@ def decode(data, decodeParms=None):
x += 1
assert char == ""
return retval
decode = staticmethod(decode) # type: ignore


class LZWDecode(object):
Expand Down Expand Up @@ -291,6 +291,7 @@ def decode(data, decodeParms=None):


class ASCII85Decode(object):
@staticmethod
def decode(data, decodeParms=None):
if version_info < ( 3, 0 ):
retval = ""
Expand Down Expand Up @@ -364,19 +365,19 @@ def decode(data, decodeParms=None):
out += struct.pack(b'>L',b)[:n-1]
break
return bytes(out)
decode = staticmethod(decode) # type: ignore

class DCTDecode(object):
@staticmethod
def decode(data, decodeParms=None):
return data
decode = staticmethod(decode) # type: ignore

class JPXDecode(object):
@staticmethod
def decode(data, decodeParms=None):
return data
decode = staticmethod(decode) # type: ignore

class CCITTFaxDecode(object):
@staticmethod
def decode(data, decodeParms=None, height=0):
k = 1
width = 0
Expand Down Expand Up @@ -416,7 +417,6 @@ def decode(data, decodeParms=None, height=0):

return tiffHeader + data

decode = staticmethod(decode) # type: ignore

def decodeStreamData(stream):
from .generic import NameObject
Expand Down
18 changes: 8 additions & 10 deletions PyPDF2/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ class NullObject(PdfObject):
def writeToStream(self, stream, encryption_key):
stream.write(b_("null"))

@staticmethod
def readFromStream(stream):
nulltxt = stream.read(4)
if nulltxt != b_("null"):
raise PdfReadError("Could not read Null object")
return NullObject()
readFromStream = staticmethod(readFromStream) # type: ignore


class BooleanObject(PdfObject):
Expand All @@ -137,6 +137,7 @@ def writeToStream(self, stream, encryption_key):
else:
stream.write(b_("false"))

@staticmethod
def readFromStream(stream):
word = stream.read(4)
if word == b_("true"):
Expand All @@ -146,7 +147,6 @@ def readFromStream(stream):
return BooleanObject(False)
else:
raise PdfReadError('Could not read Boolean object')
readFromStream = staticmethod(readFromStream) # type: ignore


class ArrayObject(list, PdfObject):
Expand All @@ -157,6 +157,7 @@ def writeToStream(self, stream, encryption_key):
data.writeToStream(stream, encryption_key)
stream.write(b_(" ]"))

@staticmethod
def readFromStream(stream, pdf):
arr = ArrayObject()
tmp = stream.read(1)
Expand All @@ -176,8 +177,6 @@ def readFromStream(stream, pdf):
# read and append obj
arr.append(readObject(stream, pdf))
return arr
readFromStream = staticmethod(readFromStream) # type: ignore


class IndirectObject(PdfObject):
def __init__(self, idnum, generation, pdf):
Expand Down Expand Up @@ -206,6 +205,7 @@ def __ne__(self, other):
def writeToStream(self, stream, encryption_key):
stream.write(b_("%s %s R" % (self.idnum, self.generation)))

@staticmethod
def readFromStream(stream, pdf):
idnum = b_("")
while True:
Expand All @@ -229,7 +229,6 @@ def readFromStream(stream, pdf):
if r != b_("R"):
raise PdfReadError("Error reading indirect object reference at byte %s" % utils.hexStr(stream.tell()))
return IndirectObject(int(idnum), int(generation), pdf)
readFromStream = staticmethod(readFromStream) # type: ignore


class FloatObject(decimal.Decimal, PdfObject):
Expand Down Expand Up @@ -274,13 +273,13 @@ def as_numeric(self):
def writeToStream(self, stream, encryption_key):
stream.write(b_(repr(self)))

@staticmethod
def readFromStream(stream):
num = utils.readUntilRegex(stream, NumberObject.NumberPattern)
if num.find(NumberObject.ByteDot) != -1:
return FloatObject(num)
else:
return NumberObject(num)
readFromStream = staticmethod(readFromStream) # type: ignore


def createStringObject(string):
Expand Down Expand Up @@ -483,6 +482,7 @@ class NameObject(str, PdfObject):
def writeToStream(self, stream, encryption_key):
stream.write(b_(self))

@staticmethod
def readFromStream(stream, pdf):
debug = False
if debug: print((stream.tell()))
Expand All @@ -507,8 +507,6 @@ def readFromStream(stream, pdf):
else:
raise PdfReadError("Illegal character in Name Object")

readFromStream = staticmethod(readFromStream) # type: ignore


class DictionaryObject(dict, PdfObject):
def raw_get(self, key):
Expand Down Expand Up @@ -566,6 +564,7 @@ def writeToStream(self, stream, encryption_key):
stream.write(b_("\n"))
stream.write(b_(">>"))

@staticmethod
def readFromStream(stream, pdf):
debug = False
tmp = stream.read(2)
Expand Down Expand Up @@ -652,7 +651,6 @@ def readFromStream(stream, pdf):
retval = DictionaryObject()
retval.update(data)
return retval
readFromStream = staticmethod(readFromStream) # type: ignore


class TreeObject(DictionaryObject):
Expand Down Expand Up @@ -802,6 +800,7 @@ def writeToStream(self, stream, encryption_key):
stream.write(data)
stream.write(b_("\nendstream"))

@staticmethod
def initializeFromDictionary(data):
if SA.FILTER in data:
retval = EncodedStreamObject()
Expand All @@ -812,7 +811,6 @@ def initializeFromDictionary(data):
del data[SA.LENGTH]
retval.update(data)
return retval
initializeFromDictionary = staticmethod(initializeFromDictionary) # type: ignore

def flateEncode(self):
if SA.FILTER in self:
Expand Down
12 changes: 6 additions & 6 deletions PyPDF2/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1694,7 +1694,7 @@ def _getObjectFromStream(self, indirectReference):
streamData.seek(pos, 0)
try:
obj = readObject(streamData, self)
except utils.PdfStreamError as e:
except PdfStreamError as e:
# Stream object cannot be read. Normally, a critical error, but
# Adobe Reader doesn't complain, so continue (in strict mode?)
e = sys.exc_info()[1]
Expand Down Expand Up @@ -2273,6 +2273,7 @@ def __init__(self, pdf=None, indirectRef=None):
self.pdf = pdf
self.indirectRef = indirectRef

@staticmethod
def createBlankPage(pdf=None, width=None, height=None):
"""
Returns a new blank page.
Expand Down Expand Up @@ -2306,7 +2307,6 @@ def createBlankPage(pdf=None, width=None, height=None):
RectangleObject([0, 0, width, height]))

return page
createBlankPage = staticmethod(createBlankPage) # type: ignore

def rotateClockwise(self, angle):
"""
Expand Down Expand Up @@ -2337,6 +2337,7 @@ def _rotate(self, angle):
currentAngle = rotateObj if isinstance(rotateObj, int) else rotateObj.getObject()
self[NameObject("/Rotate")] = NumberObject(currentAngle + angle)

@staticmethod
def _mergeResources(res1, res2, resource):
newRes = DictionaryObject()
newRes.update(res1.get(resource, DictionaryObject()).getObject())
Expand All @@ -2350,8 +2351,8 @@ def _mergeResources(res1, res2, resource):
elif key not in newRes:
newRes[key] = page2Res.raw_get(key)
return newRes, renameRes
_mergeResources = staticmethod(_mergeResources) # type: ignore

@staticmethod
def _contentStreamRename(stream, rename, pdf):
if not rename:
return stream
Expand All @@ -2370,8 +2371,8 @@ def _contentStreamRename(stream, rename, pdf):
else:
raise KeyError ("type of operands is %s" % type (operands))
return stream
_contentStreamRename = staticmethod(_contentStreamRename) # type: ignore

@staticmethod
def _pushPopGS(contents, pdf):
# adds a graphics state "push" and "pop" to the beginning and end
# of a content stream. This isolates it from changes such as
Expand All @@ -2380,8 +2381,8 @@ def _pushPopGS(contents, pdf):
stream.operations.insert(0, [[], "q"])
stream.operations.append([[], "Q"])
return stream
_pushPopGS = staticmethod(_pushPopGS) # type: ignore

@staticmethod
def _addTransformationMatrix(contents, pdf, ctm):
# adds transformation matrix at the beginning of the given
# contents stream.
Expand All @@ -2391,7 +2392,6 @@ def _addTransformationMatrix(contents, pdf, ctm):
FloatObject(c), FloatObject(d), FloatObject(e),
FloatObject(f)], " cm"])
return contents
_addTransformationMatrix = staticmethod(_addTransformationMatrix) # type: ignore

def getContents(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion PyPDF2/xmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def _getText(self, element):
def _converter_string(value):
return value

@staticmethod
def _converter_date(value):
m = iso8601.match(value)
year = int(m.group("year"))
Expand All @@ -115,7 +116,6 @@ def _converter_date(value):
tzd_minutes *= -1
dt = dt + datetime.timedelta(hours=tzd_hours, minutes=tzd_minutes)
return dt
_test_converter_date = staticmethod(_converter_date)

def _getter_bag(namespace, name, converter):
def get(self):
Expand Down

0 comments on commit 316f5e6

Please sign in to comment.