mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-14 03:07:35 +03:00
close file in case of failures
This commit is contained in:
parent
8b649d6e79
commit
88ec0bb52f
|
@ -2391,6 +2391,7 @@ def open(fp, mode="r"):
|
|||
if mode != "r":
|
||||
raise ValueError("bad mode %r" % mode)
|
||||
|
||||
exclusive_fp = False
|
||||
filename = ""
|
||||
if isPath(fp):
|
||||
filename = fp
|
||||
|
@ -2404,11 +2405,13 @@ def open(fp, mode="r"):
|
|||
|
||||
if filename:
|
||||
fp = builtins.open(filename, "rb")
|
||||
exclusive_fp = True
|
||||
|
||||
try:
|
||||
fp.seek(0)
|
||||
except (AttributeError, io.UnsupportedOperation):
|
||||
fp = io.BytesIO(fp.read())
|
||||
exclusive_fp = True
|
||||
|
||||
prefix = fp.read(16)
|
||||
|
||||
|
@ -2439,6 +2442,8 @@ def open(fp, mode="r"):
|
|||
if im:
|
||||
return im
|
||||
|
||||
if exclusive_fp:
|
||||
fp.close()
|
||||
raise IOError("cannot identify image file %r"
|
||||
% (filename if filename else fp))
|
||||
|
||||
|
|
|
@ -88,10 +88,12 @@ class ImageFile(Image.Image):
|
|||
# filename
|
||||
self.fp = open(fp, "rb")
|
||||
self.filename = fp
|
||||
self._exclusive_fp = True
|
||||
else:
|
||||
# stream
|
||||
self.fp = fp
|
||||
self.filename = filename
|
||||
self._exclusive_fp = False
|
||||
|
||||
try:
|
||||
self._open()
|
||||
|
@ -100,6 +102,9 @@ class ImageFile(Image.Image):
|
|||
KeyError, # unsupported mode
|
||||
EOFError, # got header but not the first frame
|
||||
struct.error) as v:
|
||||
# close the file only if we have opened it this constructor
|
||||
if self._exclusive_fp:
|
||||
self.fp.close()
|
||||
raise SyntaxError(v)
|
||||
|
||||
if not self.mode or self.size[0] <= 0:
|
||||
|
|
Loading…
Reference in New Issue
Block a user