From 67e12532028649cc68f12ff6e6a06480fbe147f1 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 17 Nov 2023 08:32:25 +1100 Subject: [PATCH] Do not assign new fp attribute to image when closing --- Tests/test_image.py | 1 - src/PIL/Image.py | 10 +++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 0c6843346..243e71707 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -1027,7 +1027,6 @@ class TestImage: assert len(self._caplog.records) == 0 assert im.fp is None - assert copy.fp is None class MockEncoder: diff --git a/src/PIL/Image.py b/src/PIL/Image.py index c873a93e7..d435b5617 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -549,17 +549,17 @@ class Image: :py:meth:`~PIL.Image.Image.load` method. See :ref:`file-handling` for more information. """ - try: - if hasattr(self, "fp"): + 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.fp = None - except Exception as msg: - logger.debug("Error closing: %s", msg) + self.fp = None + except Exception as msg: + logger.debug("Error closing: %s", msg) if getattr(self, "map", None): self.map = None