From c27519960f4d9bbcb089cc6c2903ce518a500276 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 3 Mar 2022 22:10:19 +1100 Subject: [PATCH] Check if self.im is not None --- Tests/test_image_access.py | 15 +++++++++------ src/PIL/GifImagePlugin.py | 2 +- src/PIL/IcnsImagePlugin.py | 2 +- src/PIL/IcoImagePlugin.py | 2 +- src/PIL/Image.py | 4 ++-- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Tests/test_image_access.py b/Tests/test_image_access.py index 7b3036979..bdbfdd0e2 100644 --- a/Tests/test_image_access.py +++ b/Tests/test_image_access.py @@ -154,14 +154,17 @@ class TestImageGetPixel(AccessTest): # Check 0 im = Image.new(mode, (0, 0), None) - with pytest.raises(IndexError): + assert im.load() is not None + + error = ValueError if self._need_cffi_access else IndexError + with pytest.raises(error): im.putpixel((0, 0), c) - with pytest.raises(IndexError): + with pytest.raises(error): im.getpixel((0, 0)) # Check 0 negative index - with pytest.raises(IndexError): + with pytest.raises(error): im.putpixel((-1, -1), c) - with pytest.raises(IndexError): + with pytest.raises(error): im.getpixel((-1, -1)) # check initial color @@ -176,10 +179,10 @@ class TestImageGetPixel(AccessTest): # Check 0 im = Image.new(mode, (0, 0), c) - with pytest.raises(IndexError): + with pytest.raises(error): im.getpixel((0, 0)) # Check 0 negative index - with pytest.raises(IndexError): + with pytest.raises(error): im.getpixel((-1, -1)) def test_basic(self): diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index 4f8ea209d..f3608cdef 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -298,7 +298,7 @@ class GifImageFile(ImageFile.ImageFile): self.dispose = Image.core.fill(dispose_mode, dispose_size, color) else: # replace with previous contents - if self.im: + if self.im is not None: # only dispose the extent in this frame self.dispose = self._crop(self.im, self.dispose_extent) elif frame_transparency is not None: diff --git a/src/PIL/IcnsImagePlugin.py b/src/PIL/IcnsImagePlugin.py index 069aff96b..57e3ea12d 100644 --- a/src/PIL/IcnsImagePlugin.py +++ b/src/PIL/IcnsImagePlugin.py @@ -287,7 +287,7 @@ class IcnsImageFile(ImageFile.ImageFile): ) px = Image.Image.load(self) - if self.im and self.im.size == self.size: + if self.im is not None and self.im.size == self.size: # Already loaded return px self.load_prepare() diff --git a/src/PIL/IcoImagePlugin.py b/src/PIL/IcoImagePlugin.py index b4f84ee20..915e4c928 100644 --- a/src/PIL/IcoImagePlugin.py +++ b/src/PIL/IcoImagePlugin.py @@ -304,7 +304,7 @@ class IcoImageFile(ImageFile.ImageFile): self._size = value def load(self): - if self.im and self.im.size == self.size: + if self.im is not None and self.im.size == self.size: # Already loaded return Image.Image.load(self) im = self.ico.getimage(self.size) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index c9265b5ab..5efecf0bf 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -847,7 +847,7 @@ class Image: :returns: An image access object. :rtype: :ref:`PixelAccess` or :py:class:`PIL.PyAccess` """ - if self.im and self.palette and self.palette.dirty: + if self.im is not None and self.palette and self.palette.dirty: # realize palette mode, arr = self.palette.getdata() self.im.putpalette(mode, arr) @@ -864,7 +864,7 @@ class Image: self.palette.mode = palette_mode self.palette.palette = self.im.getpalette(palette_mode, palette_mode) - if self.im: + if self.im is not None: if cffi and USE_CFFI_ACCESS: if self.pyaccess: return self.pyaccess