mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-03-13 17:25:49 +03:00
convert load_* functions to pure functions with no state
This commit is contained in:
parent
156972874d
commit
a9396ab412
|
@ -319,7 +319,7 @@ class ImageFileDirectory_v2(collections.MutableMapping):
|
|||
data = self._tagdata[tag]
|
||||
typ = self.tagtype[tag]
|
||||
size, handler = self._load_dispatch[typ]
|
||||
self[tag] = handler(self, data) # check type
|
||||
self[tag] = handler(self, self.legacy_api, data) # check type
|
||||
val = self._tags[tag]
|
||||
if self.legacy_api and not isinstance(val, (tuple, bytes)):
|
||||
val = val,
|
||||
|
@ -404,7 +404,7 @@ class ImageFileDirectory_v2(collections.MutableMapping):
|
|||
idx, fmt, name = idx_fmt_name
|
||||
TYPES[idx] = name
|
||||
size = struct.calcsize("=" + fmt)
|
||||
_load_dispatch[idx] = size, lambda self, data: (
|
||||
_load_dispatch[idx] = size, lambda self, legacy_api, data: (
|
||||
self._unpack("{0}{1}".format(len(data) // size, fmt), data))
|
||||
_write_dispatch[idx] = lambda self, *values: (
|
||||
b"".join(self._pack(fmt, value) for value in values))
|
||||
|
@ -415,8 +415,8 @@ class ImageFileDirectory_v2(collections.MutableMapping):
|
|||
(9, "l", "signed long"), (11, "f", "float"), (12, "d", "double")]))
|
||||
|
||||
@_register_loader(1, 1) # Basic type, except for the legacy API.
|
||||
def load_byte(self, data):
|
||||
return (data if self.legacy_api else
|
||||
def load_byte(self, legacy_api, data):
|
||||
return (data if legacy_api else
|
||||
tuple(map(ord, data) if bytes is str else data))
|
||||
|
||||
@_register_writer(1) # Basic type, except for the legacy API.
|
||||
|
@ -424,7 +424,7 @@ class ImageFileDirectory_v2(collections.MutableMapping):
|
|||
return data
|
||||
|
||||
@_register_loader(2, 1)
|
||||
def load_string(self, data):
|
||||
def load_string(self, legacy_api, data):
|
||||
if data.endswith(b"\0"):
|
||||
data = data[:-1]
|
||||
return data.decode("latin-1", "replace")
|
||||
|
@ -437,9 +437,9 @@ class ImageFileDirectory_v2(collections.MutableMapping):
|
|||
return b"" + value.encode('ascii', 'replace') + b"\0"
|
||||
|
||||
@_register_loader(5, 8)
|
||||
def load_rational(self, data):
|
||||
def load_rational(self, legacy_api, data):
|
||||
vals = self._unpack("{0}L".format(len(data) // 4), data)
|
||||
combine = lambda a, b: (a, b) if self.legacy_api else a / b
|
||||
combine = lambda a, b: (a, b) if legacy_api else a / b
|
||||
return tuple(combine(num, denom)
|
||||
for num, denom in zip(vals[::2], vals[1::2]))
|
||||
|
||||
|
@ -449,7 +449,7 @@ class ImageFileDirectory_v2(collections.MutableMapping):
|
|||
for frac in values)
|
||||
|
||||
@_register_loader(7, 1)
|
||||
def load_undefined(self, data):
|
||||
def load_undefined(self, legacy_api, data):
|
||||
return data
|
||||
|
||||
@_register_writer(7)
|
||||
|
@ -457,9 +457,9 @@ class ImageFileDirectory_v2(collections.MutableMapping):
|
|||
return value
|
||||
|
||||
@_register_loader(10, 8)
|
||||
def load_signed_rational(self, data):
|
||||
def load_signed_rational(self, legacy_api, data):
|
||||
vals = self._unpack("{0}l".format(len(data) // 4), data)
|
||||
combine = lambda a, b: (a, b) if self.legacy_api else a / b
|
||||
combine = lambda a, b: (a, b) if legacy_api else a / b
|
||||
return tuple(combine(num, denom)
|
||||
for num, denom in zip(vals[::2], vals[1::2]))
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user