Skip to content

Commit c78d23d

Browse files
committed
Moved _close_fp into ImageFile
1 parent 4ff18e0 commit c78d23d

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/PIL/Image.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -603,16 +603,10 @@ def _new(self, im: core.ImagingCore) -> Image:
603603
def __enter__(self):
604604
return self
605605

606-
def _close_fp(self):
607-
if getattr(self, "_fp", False):
608-
if self._fp != self.fp:
609-
self._fp.close()
610-
self._fp = DeferredError(ValueError("Operation on closed image"))
611-
if self.fp:
612-
self.fp.close()
613-
614606
def __exit__(self, *args):
615-
if hasattr(self, "fp"):
607+
from . import ImageFile
608+
609+
if isinstance(self, ImageFile.ImageFile):
616610
if getattr(self, "_exclusive_fp", False):
617611
self._close_fp()
618612
self.fp = None

src/PIL/ImageFile.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
from . import ExifTags, Image
4141
from ._deprecate import deprecate
42-
from ._util import is_path
42+
from ._util import DeferredError, is_path
4343

4444
if TYPE_CHECKING:
4545
from ._typing import StrOrBytesPath
@@ -166,6 +166,14 @@ def __init__(
166166
def _open(self) -> None:
167167
pass
168168

169+
def _close_fp(self):
170+
if getattr(self, "_fp", False):
171+
if self._fp != self.fp:
172+
self._fp.close()
173+
self._fp = DeferredError(ValueError("Operation on closed image"))
174+
if self.fp:
175+
self.fp.close()
176+
169177
def close(self) -> None:
170178
"""
171179
Closes the file pointer, if possible.

0 commit comments

Comments
 (0)