mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-06-29 17:33:08 +03:00
Only close exclusive fp on Image __exit__
This commit is contained in:
parent
8194677516
commit
b1cc7df7ba
|
@ -28,3 +28,10 @@ class TestImageLoad(PillowTestCase):
|
||||||
os.fstat(fn)
|
os.fstat(fn)
|
||||||
|
|
||||||
self.assertRaises(OSError, os.fstat, fn)
|
self.assertRaises(OSError, os.fstat, fn)
|
||||||
|
|
||||||
|
def test_contextmanager_non_exclusive_fp(self):
|
||||||
|
with open("Tests/images/hopper.gif", "rb") as fp:
|
||||||
|
with Image.open(fp):
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.assertFalse(fp.closed)
|
||||||
|
|
|
@ -578,7 +578,12 @@ class Image(object):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __exit__(self, *args):
|
def __exit__(self, *args):
|
||||||
self.close()
|
if hasattr(self, "_close__fp"):
|
||||||
|
self._close__fp()
|
||||||
|
if (hasattr(self, 'fp') and hasattr(self, '_exclusive_fp')
|
||||||
|
and self.fp and self._exclusive_fp):
|
||||||
|
self.fp.close()
|
||||||
|
self.fp = None
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
"""
|
"""
|
||||||
|
@ -610,12 +615,7 @@ class Image(object):
|
||||||
|
|
||||||
if sys.version_info.major >= 3:
|
if sys.version_info.major >= 3:
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
if hasattr(self, "_close__fp"):
|
self.__exit__()
|
||||||
self._close__fp()
|
|
||||||
if (hasattr(self, 'fp') and hasattr(self, '_exclusive_fp')
|
|
||||||
and self.fp and self._exclusive_fp):
|
|
||||||
self.fp.close()
|
|
||||||
self.fp = None
|
|
||||||
|
|
||||||
def _copy(self):
|
def _copy(self):
|
||||||
self.load()
|
self.load()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user