Do not assign new fp attribute to image when exiting context manager

This commit is contained in:
Andrew Murray 2023-11-24 15:19:19 +11:00
parent 04a4d54275
commit 5431b15bd2
2 changed files with 14 additions and 8 deletions

View File

@ -1015,6 +1015,11 @@ class TestImage:
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()

View File

@ -528,7 +528,8 @@ class Image:
return self
def __exit__(self, *args):
if hasattr(self, "fp") and getattr(self, "_exclusive_fp", False):
if hasattr(self, "fp"):
if getattr(self, "_exclusive_fp", False):
if getattr(self, "_fp", False):
if self._fp != self.fp:
self._fp.close()