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

Assert fp is not None #8617

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Assert fp is not None #8617

wants to merge 1 commit into from

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.

@@ -556,8 +564,9 @@ def test_bw_compression_w_rgb(self, compression: str, tmp_path: Path) -> None:
im.save(out, compression=compression)

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
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