From d606d13a6cc28e7be8bdb57128396cbb57a10672 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 15 Apr 2022 20:29:07 +1000 Subject: [PATCH] Added deferred_error to _fp --- Tests/test_file_apng.py | 9 +++++++++ Tests/test_file_fli.py | 9 +++++++++ Tests/test_file_gif.py | 13 +++++++++++++ Tests/test_file_mpo.py | 8 ++++++++ Tests/test_file_tiff.py | 9 +++++++++ src/PIL/Image.py | 4 ++-- 6 files changed, 50 insertions(+), 2 deletions(-) diff --git a/Tests/test_file_apng.py b/Tests/test_file_apng.py index d1d5c85c1ae..ad61a07ccc5 100644 --- a/Tests/test_file_apng.py +++ b/Tests/test_file_apng.py @@ -637,6 +637,15 @@ def test_apng_save_blend(tmp_path): 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(): for enum, prefix in { PngImagePlugin.Disposal: "APNG_DISPOSE_", diff --git a/Tests/test_file_fli.py b/Tests/test_file_fli.py index c1ad4a7f0f9..a7d43d2e922 100644 --- a/Tests/test_file_fli.py +++ b/Tests/test_file_fli.py @@ -46,6 +46,15 @@ def test_closed_file(): 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(): with warnings.catch_warnings(): with Image.open(static_test_file) as im: diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index dffd1006f2f..a3f6f77fc70 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -46,6 +46,19 @@ def test_closed_file(): 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(): with warnings.catch_warnings(): with Image.open(TEST_GIF) as im: diff --git a/Tests/test_file_mpo.py b/Tests/test_file_mpo.py index 0fa3b6382ec..39a5eb70604 100644 --- a/Tests/test_file_mpo.py +++ b/Tests/test_file_mpo.py @@ -48,6 +48,14 @@ def test_closed_file(): 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(): with warnings.catch_warnings(): with Image.open(test_files[0]) as im: diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index c53bb87e80f..c3baea7e1ad 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -70,6 +70,15 @@ def test_closed_file(self): im.load() 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): with warnings.catch_warnings(): with Image.open("Tests/images/multipage.tiff") as im: diff --git a/src/PIL/Image.py b/src/PIL/Image.py index a1a31e605ff..df820a4898a 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -583,7 +583,7 @@ def __exit__(self, *args): if getattr(self, "_fp", False): if self._fp != self.fp: self._fp.close() - self._fp = None + self._fp = deferred_error(ValueError("Operation on closed image")) if self.fp: self.fp.close() self.fp = None @@ -604,7 +604,7 @@ def close(self): if getattr(self, "_fp", False): if self._fp != self.fp: self._fp.close() - self._fp = None + self._fp = deferred_error(ValueError("Operation on closed image")) if self.fp: self.fp.close() self.fp = None