Added DeferredError to _fp

This commit is contained in:
Andrew Murray 2022-04-17 12:14:53 +10:00
parent f18688e84e
commit e62449f94c
6 changed files with 50 additions and 2 deletions

View File

@ -637,6 +637,15 @@ def test_apng_save_blend(tmp_path):
assert im.getpixel((0, 0)) == (0, 255, 0, 255) assert im.getpixel((0, 0)) == (0, 255, 0, 255)
def test_seek_after_close():
im = Image.open("Tests/images/apng/delay.png")
im.seek(1)
im.close()
with pytest.raises(ValueError):
im.seek(0)
def test_constants_deprecation(): def test_constants_deprecation():
for enum, prefix in { for enum, prefix in {
PngImagePlugin.Disposal: "APNG_DISPOSE_", PngImagePlugin.Disposal: "APNG_DISPOSE_",

View File

@ -46,6 +46,15 @@ def test_closed_file():
im.close() im.close()
def test_seek_after_close():
im = Image.open(animated_test_file)
im.seek(1)
im.close()
with pytest.raises(ValueError):
im.seek(0)
def test_context_manager(): def test_context_manager():
with warnings.catch_warnings(): with warnings.catch_warnings():
with Image.open(static_test_file) as im: with Image.open(static_test_file) as im:

View File

@ -46,6 +46,19 @@ def test_closed_file():
im.close() im.close()
def test_seek_after_close():
im = Image.open("Tests/images/iss634.gif")
im.load()
im.close()
with pytest.raises(ValueError):
im.is_animated
with pytest.raises(ValueError):
im.n_frames
with pytest.raises(ValueError):
im.seek(1)
def test_context_manager(): def test_context_manager():
with warnings.catch_warnings(): with warnings.catch_warnings():
with Image.open(TEST_GIF) as im: with Image.open(TEST_GIF) as im:

View File

@ -48,6 +48,14 @@ def test_closed_file():
im.close() im.close()
def test_seek_after_close():
im = Image.open(test_files[0])
im.close()
with pytest.raises(ValueError):
im.seek(1)
def test_context_manager(): def test_context_manager():
with warnings.catch_warnings(): with warnings.catch_warnings():
with Image.open(test_files[0]) as im: with Image.open(test_files[0]) as im:

View File

@ -70,6 +70,15 @@ class TestFileTiff:
im.load() im.load()
im.close() im.close()
def test_seek_after_close(self):
im = Image.open("Tests/images/multipage.tiff")
im.close()
with pytest.raises(ValueError):
im.n_frames
with pytest.raises(ValueError):
im.seek(1)
def test_context_manager(self): def test_context_manager(self):
with warnings.catch_warnings(): with warnings.catch_warnings():
with Image.open("Tests/images/multipage.tiff") as im: with Image.open("Tests/images/multipage.tiff") as im:

View File

@ -547,7 +547,7 @@ class Image:
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 = None self._fp = DeferredError(ValueError("Operation on closed image"))
if self.fp: if self.fp:
self.fp.close() self.fp.close()
self.fp = None self.fp = None
@ -568,7 +568,7 @@ class Image:
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 = None self._fp = DeferredError(ValueError("Operation on closed image"))
if self.fp: if self.fp:
self.fp.close() self.fp.close()
self.fp = None self.fp = None