mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-10 16:22:22 +03:00
Merge pull request #3683 from radarhere/exclusive
Only close original fp in __del__ and __exit__ if original fp is exclusive
This commit is contained in:
commit
b8ea8814f0
|
@ -532,6 +532,18 @@ class TestImage(PillowTestCase):
|
||||||
with Image.open(test_file) as im:
|
with Image.open(test_file) as im:
|
||||||
self.assert_warning(None, im.save, temp_file)
|
self.assert_warning(None, im.save, temp_file)
|
||||||
|
|
||||||
|
def test_load_on_nonexclusive_multiframe(self):
|
||||||
|
with open("Tests/images/frozenpond.mpo", "rb") as fp:
|
||||||
|
def act(fp):
|
||||||
|
im = Image.open(fp)
|
||||||
|
im.load()
|
||||||
|
act(fp)
|
||||||
|
|
||||||
|
with Image.open(fp) as im:
|
||||||
|
im.load()
|
||||||
|
|
||||||
|
self.assertFalse(fp.closed)
|
||||||
|
|
||||||
|
|
||||||
class MockEncoder(object):
|
class MockEncoder(object):
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -578,11 +578,11 @@ class Image(object):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __exit__(self, *args):
|
def __exit__(self, *args):
|
||||||
if hasattr(self, "_close__fp"):
|
if hasattr(self, 'fp') and getattr(self, '_exclusive_fp', False):
|
||||||
self._close__fp()
|
if hasattr(self, "_close__fp"):
|
||||||
if (hasattr(self, 'fp') and hasattr(self, '_exclusive_fp')
|
self._close__fp()
|
||||||
and self.fp and self._exclusive_fp):
|
if self.fp:
|
||||||
self.fp.close()
|
self.fp.close()
|
||||||
self.fp = None
|
self.fp = None
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
|
|
@ -186,7 +186,7 @@ def _save_all(im, fp, filename):
|
||||||
# will preserve non-alpha modes
|
# will preserve non-alpha modes
|
||||||
total = 0
|
total = 0
|
||||||
for ims in [im]+append_images:
|
for ims in [im]+append_images:
|
||||||
total += 1 if not hasattr(ims, "n_frames") else ims.n_frames
|
total += getattr(ims, "n_frames", 1)
|
||||||
if total == 1:
|
if total == 1:
|
||||||
_save(im, fp, filename)
|
_save(im, fp, filename)
|
||||||
return
|
return
|
||||||
|
@ -254,10 +254,7 @@ def _save_all(im, fp, filename):
|
||||||
try:
|
try:
|
||||||
for ims in [im]+append_images:
|
for ims in [im]+append_images:
|
||||||
# Get # of frames in this image
|
# Get # of frames in this image
|
||||||
if not hasattr(ims, "n_frames"):
|
nfr = getattr(ims, "n_frames", 1)
|
||||||
nfr = 1
|
|
||||||
else:
|
|
||||||
nfr = ims.n_frames
|
|
||||||
|
|
||||||
for idx in range(nfr):
|
for idx in range(nfr):
|
||||||
ims.seek(idx)
|
ims.seek(idx)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user