Skip to content

Conversation

radarhere
Copy link
Member

This is part of #8362 - I'm hoping to break down that PR into easier-to-review chunks.

ImageFile's fp may be None.

>>> from PIL import Image
>>> im = Image.open("Tests/images/hopper.png")
>>> im.fp
<_io.BufferedReader name='Tests/images/hopper.png'>
>>> im.close()
>>> im.fp is None
True

So before using it, let's assert that it isn't None.


def test_fp_leak(self) -> None:
im: Image.Image | None = Image.open("Tests/images/hopper_g4_500.tif")
im: ImageFile.ImageFile | None = Image.open("Tests/images/hopper_g4_500.tif")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image.open returns an ImageFile instance.

Pillow/src/PIL/Image.py

Lines 3404 to 3408 in 0e3f51d

def open(
fp: StrOrBytesPath | IO[bytes],
mode: Literal["r"] = "r",
formats: list[str] | tuple[str, ...] | None = None,
) -> ImageFile.ImageFile:

@radarhere radarhere mentioned this pull request Dec 23, 2024
@radarhere radarhere force-pushed the fp branch 2 times, most recently from 82b266a to 751029e Compare April 3, 2025 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant