Close exclusive fp on open exception

This commit is contained in:
Andrew Murray 2018-11-09 11:35:08 +11:00
parent 3d76a686c4
commit 747eccbfc2
2 changed files with 13 additions and 5 deletions

View File

@ -17,11 +17,15 @@ class TestBmpReference(PillowTestCase):
""" These shouldn't crash/dos, but they shouldn't return anything """ These shouldn't crash/dos, but they shouldn't return anything
either """ either """
for f in self.get_files('b'): for f in self.get_files('b'):
try: def open(f):
im = Image.open(f) try:
im.load() im = Image.open(f)
except Exception: # as msg: im.load()
pass except Exception: # as msg:
pass
# Assert that there is no unclosed file warning
self.assert_warning(None, open, f)
def test_questionable(self): def test_questionable(self):
""" These shouldn't crash/dos, but it's not well defined that these """ These shouldn't crash/dos, but it's not well defined that these

View File

@ -2641,6 +2641,10 @@ def open(fp, mode="r"):
# opening failures that are entirely expected. # opening failures that are entirely expected.
# logger.debug("", exc_info=True) # logger.debug("", exc_info=True)
continue continue
except Exception:
if exclusive_fp:
fp.close()
raise
return None return None
im = _open_core(fp, filename, prefix) im = _open_core(fp, filename, prefix)