mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-06-04 05:03:14 +03:00
Merge pull request #7557 from RaphaelVRossi/check-image-has-fp-when-close
If absent, do not try to close fp when closing image
This commit is contained in:
commit
f8d061c88e
|
@ -1,4 +1,5 @@
|
||||||
import io
|
import io
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
|
@ -1014,6 +1015,15 @@ 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_close_graceful(self, caplog):
|
||||||
|
with Image.open("Tests/images/hopper.jpg") as im:
|
||||||
|
copy = im.copy()
|
||||||
|
with caplog.at_level(logging.DEBUG):
|
||||||
|
im.close()
|
||||||
|
copy.close()
|
||||||
|
assert len(caplog.records) == 0
|
||||||
|
assert im.fp is None
|
||||||
|
|
||||||
|
|
||||||
class MockEncoder:
|
class MockEncoder:
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -549,16 +549,17 @@ class Image:
|
||||||
:py:meth:`~PIL.Image.Image.load` method. See :ref:`file-handling` for
|
:py:meth:`~PIL.Image.Image.load` method. See :ref:`file-handling` for
|
||||||
more information.
|
more information.
|
||||||
"""
|
"""
|
||||||
try:
|
if hasattr(self, "fp"):
|
||||||
if getattr(self, "_fp", False):
|
try:
|
||||||
if self._fp != self.fp:
|
if getattr(self, "_fp", False):
|
||||||
self._fp.close()
|
if self._fp != self.fp:
|
||||||
self._fp = DeferredError(ValueError("Operation on closed image"))
|
self._fp.close()
|
||||||
if self.fp:
|
self._fp = DeferredError(ValueError("Operation on closed image"))
|
||||||
self.fp.close()
|
if self.fp:
|
||||||
self.fp = None
|
self.fp.close()
|
||||||
except Exception as msg:
|
self.fp = None
|
||||||
logger.debug("Error closing: %s", msg)
|
except Exception as msg:
|
||||||
|
logger.debug("Error closing: %s", msg)
|
||||||
|
|
||||||
if getattr(self, "map", None):
|
if getattr(self, "map", None):
|
||||||
self.map = None
|
self.map = None
|
||||||
|
|
Loading…
Reference in New Issue
Block a user