mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-15 20:06:28 +03:00
Merge pull request #2793 from radarhere/contextmanager
Added context manager support
This commit is contained in:
commit
7d8c0d9e39
|
@ -420,6 +420,12 @@ class Parser(object):
|
||||||
|
|
||||||
self.image = im
|
self.image = im
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *args):
|
||||||
|
self.close()
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
"""
|
"""
|
||||||
(Consumer) Close the stream.
|
(Consumer) Close the stream.
|
||||||
|
|
|
@ -118,6 +118,12 @@ class ChunkStream(object):
|
||||||
|
|
||||||
return cid, pos, length
|
return cid, pos, length
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *args):
|
||||||
|
self.close()
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
self.queue = self.crc = self.fp = None
|
self.queue = self.crc = self.fp = None
|
||||||
|
|
||||||
|
|
|
@ -504,7 +504,7 @@ class TestFilePng(PillowTestCase):
|
||||||
chunks = []
|
chunks = []
|
||||||
with open(test_file, "rb") as fp:
|
with open(test_file, "rb") as fp:
|
||||||
fp.read(8)
|
fp.read(8)
|
||||||
png = PngImagePlugin.PngStream(fp)
|
with PngImagePlugin.PngStream(fp) as png:
|
||||||
while True:
|
while True:
|
||||||
cid, pos, length = png.read()
|
cid, pos, length = png.read()
|
||||||
chunks.append(cid)
|
chunks.append(cid)
|
||||||
|
|
|
@ -74,7 +74,7 @@ class TestImageFile(PillowTestCase):
|
||||||
def test_ico(self):
|
def test_ico(self):
|
||||||
with open('Tests/images/python.ico', 'rb') as f:
|
with open('Tests/images/python.ico', 'rb') as f:
|
||||||
data = f.read()
|
data = f.read()
|
||||||
p = ImageFile.Parser()
|
with ImageFile.Parser() as p:
|
||||||
p.feed(data)
|
p.feed(data)
|
||||||
self.assertEqual((48, 48), p.image.size)
|
self.assertEqual((48, 48), p.image.size)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user