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