mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 09:56:17 +03:00
Merge pull request #7566 from radarhere/exit
This commit is contained in:
commit
d71cf168ee
|
@ -1016,6 +1016,11 @@ class TestImage:
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
assert str(e) == "buffer overrun when reading image file"
|
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):
|
def test_close_graceful(self, caplog):
|
||||||
with Image.open("Tests/images/hopper.jpg") as im:
|
with Image.open("Tests/images/hopper.jpg") as im:
|
||||||
copy = im.copy()
|
copy = im.copy()
|
||||||
|
|
|
@ -530,14 +530,18 @@ class Image:
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __exit__(self, *args):
|
def _close_fp(self):
|
||||||
if hasattr(self, "fp") and getattr(self, "_exclusive_fp", False):
|
|
||||||
if getattr(self, "_fp", False):
|
if getattr(self, "_fp", False):
|
||||||
if self._fp != self.fp:
|
if self._fp != self.fp:
|
||||||
self._fp.close()
|
self._fp.close()
|
||||||
self._fp = DeferredError(ValueError("Operation on closed image"))
|
self._fp = DeferredError(ValueError("Operation on closed image"))
|
||||||
if self.fp:
|
if self.fp:
|
||||||
self.fp.close()
|
self.fp.close()
|
||||||
|
|
||||||
|
def __exit__(self, *args):
|
||||||
|
if hasattr(self, "fp"):
|
||||||
|
if getattr(self, "_exclusive_fp", False):
|
||||||
|
self._close_fp()
|
||||||
self.fp = None
|
self.fp = None
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
@ -554,12 +558,7 @@ class Image:
|
||||||
"""
|
"""
|
||||||
if hasattr(self, "fp"):
|
if hasattr(self, "fp"):
|
||||||
try:
|
try:
|
||||||
if getattr(self, "_fp", False):
|
self._close_fp()
|
||||||
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
|
self.fp = None
|
||||||
except Exception as msg:
|
except Exception as msg:
|
||||||
logger.debug("Error closing: %s", msg)
|
logger.debug("Error closing: %s", msg)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user