From a9396ab4120f68759a1eda00f078d5d5ba00ac4f Mon Sep 17 00:00:00 2001 From: wiredfool Date: Sun, 13 Sep 2015 11:07:43 +0100 Subject: [PATCH] convert load_* functions to pure functions with no state --- PIL/TiffImagePlugin.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index f8fbae224..248fab15f 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -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]))