From 8c75b955c16ea43c6a96eb6a204d1819bd3cdcc3 Mon Sep 17 00:00:00 2001 From: Atsushi Odagiri Date: Fri, 20 Mar 2015 15:01:21 +0900 Subject: [PATCH] added explicit closing file opened in ``Image.open`` --- PIL/Image.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/PIL/Image.py b/PIL/Image.py index 2f304a9a8..09ea77f13 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -2239,15 +2239,21 @@ def open(fp, mode="r"): opened and identified. """ + should_close = False if mode != "r": raise ValueError("bad mode %r" % mode) if isPath(fp): filename = fp fp = builtins.open(fp, "rb") + should_close = True else: filename = "" + def finalize(): + if should_close: + fp.close() + prefix = fp.read(16) preinit() @@ -2259,6 +2265,7 @@ def open(fp, mode="r"): fp.seek(0) im = factory(fp, filename) _decompression_bomb_check(im.size) + finalize() return im except (SyntaxError, IndexError, TypeError): # import traceback @@ -2274,12 +2281,14 @@ def open(fp, mode="r"): fp.seek(0) im = factory(fp, filename) _decompression_bomb_check(im.size) + finalize() return im except (SyntaxError, IndexError, TypeError): # import traceback # traceback.print_exc() pass + finalize() raise IOError("cannot identify image file %r" % (filename if filename else fp))