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

Do not assign new fp attribute when exiting context manager #7566

Merged
merged 2 commits into from Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions Tests/test_image.py
Expand Up @@ -1015,6 +1015,11 @@ def test_fli_overrun2(self):
except OSError as e:
assert str(e) == "buffer overrun when reading image file"

def test_exit_fp(self):
with Image.new("L", (1, 1)) as im:
pass
assert not hasattr(im, "fp")

def test_close_graceful(self, caplog):
with Image.open("Tests/images/hopper.jpg") as im:
copy = im.copy()
Expand Down
27 changes: 13 additions & 14 deletions src/PIL/Image.py
Expand Up @@ -527,15 +527,19 @@ def _new(self, im):
def __enter__(self):
return self

def _close_fp(self):
if getattr(self, "_fp", False):
if self._fp != self.fp:
self._fp.close()
self._fp = DeferredError(ValueError("Operation on closed image"))
if self.fp:
self.fp.close()

def __exit__(self, *args):
if hasattr(self, "fp") and getattr(self, "_exclusive_fp", False):
if getattr(self, "_fp", False):
if self._fp != self.fp:
self._fp.close()
self._fp = DeferredError(ValueError("Operation on closed image"))
if self.fp:
self.fp.close()
self.fp = None
if hasattr(self, "fp"):
if getattr(self, "_exclusive_fp", False):
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of curiosity: does exclusive_fp mean, like I think it does, "we own this fp because we opened it"? If so, does it make sense for .close() to also close FPs that aren't "owned" by this instance?

Copy link
Member Author

Choose a reason for hiding this comment

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

Out of curiosity: does exclusive_fp mean, like I think it does, "we own this fp because we opened it"?

Yes

Pillow/src/PIL/Image.py

Lines 3250 to 3258 in 697c24b

if filename:
fp = builtins.open(filename, "rb")
exclusive_fp = True
try:
fp.seek(0)
except (AttributeError, io.UnsupportedOperation):
fp = io.BytesIO(fp.read())
exclusive_fp = True

If so, does it make sense for .close() to also close FPs that aren't "owned" by this instance?

See #5309

self._close_fp()
self.fp = None

def close(self):
"""
Expand All @@ -551,12 +555,7 @@ def close(self):
"""
if hasattr(self, "fp"):
try:
if getattr(self, "_fp", False):
if self._fp != self.fp:
self._fp.close()
self._fp = DeferredError(ValueError("Operation on closed image"))
if self.fp:
self.fp.close()
self._close_fp()
self.fp = None
except Exception as msg:
logger.debug("Error closing: %s", msg)
Expand Down