From 5431b15bd2f8177b762332ac632b0342c3b9d506 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 24 Nov 2023 15:19:19 +1100 Subject: [PATCH 1/2] Do not assign new fp attribute to image when exiting context manager --- Tests/test_image.py | 5 +++++ src/PIL/Image.py | 17 +++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index f0861bb4f81..98a82cfaa61 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -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() diff --git a/src/PIL/Image.py b/src/PIL/Image.py index d435b561720..b6ac1f0825b 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -528,14 +528,15 @@ def __enter__(self): return self 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): + 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 def close(self): """ From 5fb86c55ed52fecf61daa9c9e93108abc1831948 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 29 Nov 2023 20:05:17 +1100 Subject: [PATCH 2/2] Moved code closing fp and _fp into common method --- src/PIL/Image.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index b6ac1f0825b..7e0e14f2636 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -527,15 +527,18 @@ 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"): if 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._close_fp() self.fp = None def close(self): @@ -552,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)