mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-08-08 14:24:46 +03:00
use context block
This commit is contained in:
parent
8c75b955c1
commit
b7c30a6f92
14
PIL/Image.py
14
PIL/Image.py
|
@ -2239,21 +2239,20 @@ def open(fp, mode="r"):
|
||||||
opened and identified.
|
opened and identified.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
should_close = False
|
|
||||||
if mode != "r":
|
if mode != "r":
|
||||||
raise ValueError("bad mode %r" % mode)
|
raise ValueError("bad mode %r" % mode)
|
||||||
|
|
||||||
if isPath(fp):
|
if isPath(fp):
|
||||||
filename = fp
|
filename = fp
|
||||||
fp = builtins.open(fp, "rb")
|
with builtins.open(fp, "rb") as fp:
|
||||||
should_close = True
|
return _open(fp, filename, mode)
|
||||||
else:
|
else:
|
||||||
filename = ""
|
filename = ""
|
||||||
|
return _open(fp, filename, mode)
|
||||||
|
|
||||||
def finalize():
|
|
||||||
if should_close:
|
|
||||||
fp.close()
|
|
||||||
|
|
||||||
|
|
||||||
|
def _open(fp, filename, mode):
|
||||||
prefix = fp.read(16)
|
prefix = fp.read(16)
|
||||||
|
|
||||||
preinit()
|
preinit()
|
||||||
|
@ -2265,7 +2264,6 @@ def open(fp, mode="r"):
|
||||||
fp.seek(0)
|
fp.seek(0)
|
||||||
im = factory(fp, filename)
|
im = factory(fp, filename)
|
||||||
_decompression_bomb_check(im.size)
|
_decompression_bomb_check(im.size)
|
||||||
finalize()
|
|
||||||
return im
|
return im
|
||||||
except (SyntaxError, IndexError, TypeError):
|
except (SyntaxError, IndexError, TypeError):
|
||||||
# import traceback
|
# import traceback
|
||||||
|
@ -2281,14 +2279,12 @@ def open(fp, mode="r"):
|
||||||
fp.seek(0)
|
fp.seek(0)
|
||||||
im = factory(fp, filename)
|
im = factory(fp, filename)
|
||||||
_decompression_bomb_check(im.size)
|
_decompression_bomb_check(im.size)
|
||||||
finalize()
|
|
||||||
return im
|
return im
|
||||||
except (SyntaxError, IndexError, TypeError):
|
except (SyntaxError, IndexError, TypeError):
|
||||||
# import traceback
|
# import traceback
|
||||||
# traceback.print_exc()
|
# traceback.print_exc()
|
||||||
pass
|
pass
|
||||||
|
|
||||||
finalize()
|
|
||||||
raise IOError("cannot identify image file %r"
|
raise IOError("cannot identify image file %r"
|
||||||
% (filename if filename else fp))
|
% (filename if filename else fp))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user