mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-13 01:32:18 +03:00
Merge pull request #1 from radarhere/check-image-has-fp-when-close
Do not assign new fp attribute to image when closing
This commit is contained in:
commit
8186c63d40
|
@ -1,4 +1,5 @@
|
||||||
import io
|
import io
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
|
@ -1014,20 +1015,14 @@ 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"
|
||||||
|
|
||||||
@pytest.fixture(scope="function")
|
def test_close_graceful(self, caplog):
|
||||||
def inject_caplog(self, caplog):
|
|
||||||
self._caplog = caplog
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("inject_caplog")
|
|
||||||
def test_close_graceful(self):
|
|
||||||
with Image.open("Tests/images/hopper.jpg") as im:
|
with Image.open("Tests/images/hopper.jpg") as im:
|
||||||
copy = im.copy()
|
copy = im.copy()
|
||||||
|
with caplog.at_level(logging.DEBUG):
|
||||||
im.close()
|
im.close()
|
||||||
copy.close()
|
copy.close()
|
||||||
|
assert len(caplog.records) == 0
|
||||||
assert len(self._caplog.records) == 0
|
|
||||||
assert im.fp is None
|
assert im.fp is None
|
||||||
assert copy.fp is None
|
|
||||||
|
|
||||||
|
|
||||||
class MockEncoder:
|
class MockEncoder:
|
||||||
|
|
|
@ -549,8 +549,8 @@ 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 hasattr(self, "fp"):
|
||||||
|
try:
|
||||||
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()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user