Skip to content

Commit 85e31d6

Browse files
committed
separate
1 parent 7d4a757 commit 85e31d6

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/PIL/Image.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -590,16 +590,11 @@ def _new(self, im: core.ImagingCore) -> Image:
590590
return new
591591

592592
# Context manager support
593-
def __enter__(self):
593+
def __enter__(self) -> Image:
594594
return self
595595

596-
def __exit__(self, *args):
597-
from . import ImageFile
598-
599-
if isinstance(self, ImageFile.ImageFile):
600-
if getattr(self, "_exclusive_fp", False):
601-
self._close_fp()
602-
self.fp = None
596+
def __exit__(self, *args: object) -> None:
597+
pass
603598

604599
def close(self) -> None:
605600
"""

src/PIL/ImageFile.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,23 @@ def __init__(
167167
def _open(self) -> None:
168168
pass
169169

170-
def _close_fp(self):
170+
# Context manager support
171+
def __enter__(self) -> ImageFile:
172+
return self
173+
174+
def _close_fp(self) -> None:
171175
if getattr(self, "_fp", False) and not isinstance(self._fp, DeferredError):
172176
if self._fp != self.fp:
173177
self._fp.close()
174178
self._fp = DeferredError(ValueError("Operation on closed image"))
175179
if self.fp:
176180
self.fp.close()
177181

182+
def __exit__(self, *args: object) -> None:
183+
if getattr(self, "_exclusive_fp", False):
184+
self._close_fp()
185+
self.fp = None
186+
178187
def close(self) -> None:
179188
"""
180189
Closes the file pointer, if possible.

0 commit comments

Comments
 (0)