Merge pull request #4823 from radarhere/close

Do not try to close file pointer if file pointer is empty
This commit is contained in:
Hugo van Kemenade 2020-08-23 14:26:11 +03:00 committed by GitHub
commit 27d34d2f13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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,7 +594,8 @@ class Image:
try:
if hasattr(self, "_close__fp"):
self._close__fp()
self.fp.close()
if self.fp:
self.fp.close()
self.fp = None
except Exception as msg:
logger.debug("Error closing: %s", msg)