Do not try to close fp if fp is empty

This commit is contained in:
Andrew Murray 2020-07-29 08:13:56 +10:00
parent cb79b1fa89
commit 14af7bb5ac
2 changed files with 11 additions and 1 deletions

View File

@ -1,3 +1,4 @@
import logging
import os
import pytest
@ -23,6 +24,14 @@ def test_close():
im.getpixel((0, 0))
def test_close_after_load(caplog):
im = Image.open("Tests/images/hopper.gif")
im.load()
with caplog.at_level(logging.DEBUG):
im.close()
assert len(caplog.records) == 0
def test_contextmanager():
fn = None
with Image.open("Tests/images/hopper.gif") as im:

View File

@ -594,6 +594,7 @@ class Image:
try:
if hasattr(self, "_close__fp"):
self._close__fp()
if self.fp:
self.fp.close()
self.fp = None
except Exception as msg: