Skip to content

Commit

Permalink
Added DeferredError to _fp
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Apr 17, 2022
1 parent f18688e commit e62449f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 2 deletions.
9 changes: 9 additions & 0 deletions Tests/test_file_apng.py
Expand Up @@ -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_",
Expand Down
9 changes: 9 additions & 0 deletions Tests/test_file_fli.py
Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions Tests/test_file_gif.py
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions Tests/test_file_mpo.py
Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions Tests/test_file_tiff.py
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/Image.py
Expand Up @@ -547,7 +547,7 @@ def __exit__(self, *args):
if getattr(self, "_fp", False):
if self._fp != self.fp:
self._fp.close()
self._fp = None
self._fp = DeferredError(ValueError("Operation on closed image"))
if self.fp:
self.fp.close()
self.fp = None
Expand All @@ -568,7 +568,7 @@ def close(self):
if getattr(self, "_fp", False):
if self._fp != self.fp:
self._fp.close()
self._fp = None
self._fp = DeferredError(ValueError("Operation on closed image"))
if self.fp:
self.fp.close()
self.fp = None
Expand Down

0 comments on commit e62449f

Please sign in to comment.