Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if self.im is not None #6108

Merged
merged 1 commit into from Mar 10, 2022
Merged

Check if self.im is not None #6108

merged 1 commit into from Mar 10, 2022

Conversation

radarhere
Copy link
Member

For a (0, 0) image, im.load() currently returns None.

>>> from PIL import Image
>>> im = Image.new("RGB", (1, 1))
>>> im.load()
<PixelAccess object at 0x103b865f0>
>>> im = Image.new("RGB", (0, 0))
>>> im.load()
>>> 

This is because load() tests if self.im is truthy.

Pillow/src/PIL/Image.py

Lines 867 to 876 in 92c26a7

if self.im:
if cffi and USE_CFFI_ACCESS:
if self.pyaccess:
return self.pyaccess
from . import PyAccess
self.pyaccess = PyAccess.new(self, self.readonly)
if self.pyaccess:
return self.pyaccess
return self.im.pixel_access(self.readonly)

And the image size is what is used to determine the boolean value.

>>> from PIL import Image
>>> im = Image.new("RGB", (10, 10))
>>> len(im.im)
100
>>> bool(im.im)
True
>>> im = Image.new("RGB", (0, 0))
>>> len(im.im)
0
>>> bool(im.im)
False

So this PR instead changes the Python code to check self.im is not None, there and in other locations. I've added a check to assert im.load() is not None for a (0, 0) image.

This then required existing checks in Tests/test_image_access.py on (0, 0) images to be updated. Why? Because once the (0, 0) image started returning an access object from load(), it is revealed that PixelAccess and PyAccess return different errors when the index is out of range.

PyErr_SetString(PyExc_IndexError, outside_image);

raise ValueError("pixel location out of range")

@hugovk hugovk merged commit 29960c6 into python-pillow:main Mar 10, 2022
@radarhere radarhere deleted the none branch March 10, 2022 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants