From aba2b93ccbadd05d1552feb30bcf7fb2ac5c54c3 Mon Sep 17 00:00:00 2001 From: Yay295 Date: Sat, 22 Jun 2024 10:06:43 -0500 Subject: [PATCH] remove _size and _mode getters --- src/PIL/GifImagePlugin.py | 2 +- src/PIL/Image.py | 22 ++++++++++------------ src/PIL/ImageOps.py | 2 +- src/PIL/PcxImagePlugin.py | 2 +- src/PIL/QoiImagePlugin.py | 2 +- 5 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index 284128c773f..2cc7b9a3af2 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -276,7 +276,7 @@ def _seek(self, frame: int, update_image: bool = True) -> None: x1, y1 = x0 + i16(s, 4), y0 + i16(s, 6) if (x1 > self.size[0] or y1 > self.size[1]) and update_image: self._size = max(x1, self.size[0]), max(y1, self.size[1]) - Image._decompression_bomb_check(self._size) + Image._decompression_bomb_check(self.size) frame_dispose_extent = x0, y0, x1, y1 flags = s[8] diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 0c39626ec10..851f2e598e2 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -570,38 +570,36 @@ def height(self) -> int: @property def size(self) -> tuple[int, int]: - return self._size - - @property - def _size(self) -> tuple[int, int]: if self._use_im_values(): return self.im.size return self.__size - @_size.setter - def _size(self, value: tuple[int, int]) -> None: + def _set_size(self, value: tuple[int, int]) -> None: # set im.size first in case it raises an exception if self._use_im_values(): self.im.size = value self.__size = value - @property - def mode(self) -> str: - return self._mode + # MyPy doesn't support "x = property(...)" + # https://github.com/python/mypy/issues/8083 + _size: tuple[int, int] = cast(Tuple[int, int], property(fset=_set_size)) @property - def _mode(self) -> str: + def mode(self) -> str: if self._use_im_values(): return self.im.mode return self.__mode - @_mode.setter - def _mode(self, value: str) -> None: + def _set_mode(self, value: str) -> None: # set im.mode first in case it raises an exception if self._use_im_values(): self.im.mode = value self.__mode = value + # MyPy doesn't support "x = property(...)" + # https://github.com/python/mypy/issues/8083 + _mode: str = cast(str, property(fset=_set_mode)) + def _new(self, im: core.ImagingCore) -> Image: new = Image() new.im = im diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py index a84c0834561..4649bb60ac9 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -699,7 +699,7 @@ def exif_transpose(image: Image.Image, *, in_place: bool = False) -> Image.Image if in_place: image.im = transposed_image.im image.pyaccess = None - image._size = transposed_image._size + image._size = transposed_image.size exif_image = image if in_place else transposed_image exif = exif_image.getexif() diff --git a/src/PIL/PcxImagePlugin.py b/src/PIL/PcxImagePlugin.py index dd42003b5a3..28e136c246a 100644 --- a/src/PIL/PcxImagePlugin.py +++ b/src/PIL/PcxImagePlugin.py @@ -118,7 +118,7 @@ def _open(self) -> None: # Don't trust the passed in stride. # Calculate the approximate position for ourselves. # CVE-2020-35653 - stride = (self._size[0] * bits + 7) // 8 + stride = (self.size[0] * bits + 7) // 8 # While the specification states that this must be even, # not all images follow this diff --git a/src/PIL/QoiImagePlugin.py b/src/PIL/QoiImagePlugin.py index d20d0ab2d51..16ab109c3ab 100644 --- a/src/PIL/QoiImagePlugin.py +++ b/src/PIL/QoiImagePlugin.py @@ -32,7 +32,7 @@ def _open(self) -> None: self._mode = "RGB" if channels == 3 else "RGBA" self.fp.seek(1, os.SEEK_CUR) # colorspace - self.tile = [("qoi", (0, 0) + self._size, self.fp.tell(), None)] + self.tile = [("qoi", (0, 0) + self.size, self.fp.tell(), None)] class QoiDecoder(ImageFile.PyDecoder):