From 747eccbfc2e86aa4cfb87453220959b8006cd912 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 9 Nov 2018 11:35:08 +1100 Subject: [PATCH] Close exclusive fp on open exception --- Tests/test_bmp_reference.py | 14 +++++++++----- src/PIL/Image.py | 4 ++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Tests/test_bmp_reference.py b/Tests/test_bmp_reference.py index af25f7162..b3121e305 100644 --- a/Tests/test_bmp_reference.py +++ b/Tests/test_bmp_reference.py @@ -17,11 +17,15 @@ class TestBmpReference(PillowTestCase): """ These shouldn't crash/dos, but they shouldn't return anything either """ for f in self.get_files('b'): - try: - im = Image.open(f) - im.load() - except Exception: # as msg: - pass + def open(f): + try: + im = Image.open(f) + im.load() + except Exception: # as msg: + pass + + # Assert that there is no unclosed file warning + self.assert_warning(None, open, f) def test_questionable(self): """ These shouldn't crash/dos, but it's not well defined that these diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 0ce3f7f49..be94f0787 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -2641,6 +2641,10 @@ def open(fp, mode="r"): # opening failures that are entirely expected. # logger.debug("", exc_info=True) continue + except Exception: + if exclusive_fp: + fp.close() + raise return None im = _open_core(fp, filename, prefix)