mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 09:56:17 +03:00
Merge pull request #2307 from radarhere/contextmanagers
Added context managers
This commit is contained in:
commit
c1b510c72c
|
@ -521,22 +521,20 @@ def _save_netpbm(im, fp, filename):
|
||||||
import tempfile
|
import tempfile
|
||||||
file = im._dump()
|
file = im._dump()
|
||||||
|
|
||||||
if im.mode != "RGB":
|
with open(filename, 'wb') as f:
|
||||||
with open(filename, 'wb') as f:
|
if im.mode != "RGB":
|
||||||
stderr = tempfile.TemporaryFile()
|
with tempfile.TemporaryFile() as stderr:
|
||||||
check_call(["ppmtogif", file], stdout=f, stderr=stderr)
|
check_call(["ppmtogif", file], stdout=f, stderr=stderr)
|
||||||
else:
|
else:
|
||||||
with open(filename, 'wb') as f:
|
|
||||||
|
|
||||||
# Pipe ppmquant output into ppmtogif
|
# Pipe ppmquant output into ppmtogif
|
||||||
# "ppmquant 256 %s | ppmtogif > %s" % (file, filename)
|
# "ppmquant 256 %s | ppmtogif > %s" % (file, filename)
|
||||||
quant_cmd = ["ppmquant", "256", file]
|
quant_cmd = ["ppmquant", "256", file]
|
||||||
togif_cmd = ["ppmtogif"]
|
togif_cmd = ["ppmtogif"]
|
||||||
stderr = tempfile.TemporaryFile()
|
with tempfile.TemporaryFile() as stderr:
|
||||||
quant_proc = Popen(quant_cmd, stdout=PIPE, stderr=stderr)
|
quant_proc = Popen(quant_cmd, stdout=PIPE, stderr=stderr)
|
||||||
stderr = tempfile.TemporaryFile()
|
with tempfile.TemporaryFile() as stderr:
|
||||||
togif_proc = Popen(togif_cmd, stdin=quant_proc.stdout, stdout=f,
|
togif_proc = Popen(togif_cmd, stdin=quant_proc.stdout,
|
||||||
stderr=stderr)
|
stdout=f, stderr=stderr)
|
||||||
|
|
||||||
# Allow ppmquant to receive SIGPIPE if ppmtogif exits
|
# Allow ppmquant to receive SIGPIPE if ppmtogif exits
|
||||||
quant_proc.stdout.close()
|
quant_proc.stdout.close()
|
||||||
|
|
|
@ -330,8 +330,8 @@ def _save(im, fp, filename):
|
||||||
from subprocess import Popen, PIPE, CalledProcessError
|
from subprocess import Popen, PIPE, CalledProcessError
|
||||||
|
|
||||||
convert_cmd = ["iconutil", "-c", "icns", "-o", filename, iconset]
|
convert_cmd = ["iconutil", "-c", "icns", "-o", filename, iconset]
|
||||||
stderr = tempfile.TemporaryFile()
|
with tempfile.TemporaryFile() as stderr:
|
||||||
convert_proc = Popen(convert_cmd, stdout=PIPE, stderr=stderr)
|
convert_proc = Popen(convert_cmd, stdout=PIPE, stderr=stderr)
|
||||||
|
|
||||||
convert_proc.stdout.close()
|
convert_proc.stdout.close()
|
||||||
|
|
||||||
|
|
|
@ -83,9 +83,8 @@ def isSpiderHeader(t):
|
||||||
|
|
||||||
|
|
||||||
def isSpiderImage(filename):
|
def isSpiderImage(filename):
|
||||||
fp = open(filename, 'rb')
|
with open(filename, 'rb') as fp:
|
||||||
f = fp.read(92) # read 23 * 4 bytes
|
f = fp.read(92) # read 23 * 4 bytes
|
||||||
fp.close()
|
|
||||||
t = struct.unpack('>23f', f) # try big-endian first
|
t = struct.unpack('>23f', f) # try big-endian first
|
||||||
hdrlen = isSpiderHeader(t)
|
hdrlen = isSpiderHeader(t)
|
||||||
if hdrlen == 0:
|
if hdrlen == 0:
|
||||||
|
|
|
@ -151,7 +151,7 @@ class PillowTestCase(unittest.TestCase):
|
||||||
|
|
||||||
def tempfile(self, template):
|
def tempfile(self, template):
|
||||||
assert template[:5] in ("temp.", "temp_")
|
assert template[:5] in ("temp.", "temp_")
|
||||||
(fd, path) = tempfile.mkstemp(template[4:], template[:4])
|
fd, path = tempfile.mkstemp(template[4:], template[:4])
|
||||||
os.close(fd)
|
os.close(fd)
|
||||||
|
|
||||||
self.addCleanup(self.delete_tempfile, path)
|
self.addCleanup(self.delete_tempfile, path)
|
||||||
|
|
|
@ -56,7 +56,7 @@ class TestFileGif(PillowTestCase):
|
||||||
# 256 color Palette image, posterize to > 128 and < 128 levels
|
# 256 color Palette image, posterize to > 128 and < 128 levels
|
||||||
# Size bigger and smaller than 512x512
|
# Size bigger and smaller than 512x512
|
||||||
# Check the palette for number of colors allocated.
|
# Check the palette for number of colors allocated.
|
||||||
# Check for correctness after conversion back to RGB
|
# Check for correctness after conversion back to RGB
|
||||||
def check(colors, size, expected_palette_length):
|
def check(colors, size, expected_palette_length):
|
||||||
# make an image with empty colors in the start of the palette range
|
# make an image with empty colors in the start of the palette range
|
||||||
im = Image.frombytes('P', (colors,colors),
|
im = Image.frombytes('P', (colors,colors),
|
||||||
|
@ -70,7 +70,7 @@ class TestFileGif(PillowTestCase):
|
||||||
# check palette length
|
# check palette length
|
||||||
palette_length = max(i+1 for i,v in enumerate(reloaded.histogram()) if v)
|
palette_length = max(i+1 for i,v in enumerate(reloaded.histogram()) if v)
|
||||||
self.assertEqual(expected_palette_length, palette_length)
|
self.assertEqual(expected_palette_length, palette_length)
|
||||||
|
|
||||||
self.assert_image_equal(im.convert('RGB'), reloaded.convert('RGB'))
|
self.assert_image_equal(im.convert('RGB'), reloaded.convert('RGB'))
|
||||||
|
|
||||||
|
|
||||||
|
@ -271,12 +271,11 @@ class TestFileGif(PillowTestCase):
|
||||||
duration = 1000
|
duration = 1000
|
||||||
|
|
||||||
out = self.tempfile('temp.gif')
|
out = self.tempfile('temp.gif')
|
||||||
fp = open(out, "wb")
|
with open(out, "wb") as fp:
|
||||||
im = Image.new('L', (100, 100), '#000')
|
im = Image.new('L', (100, 100), '#000')
|
||||||
for s in GifImagePlugin.getheader(im)[0] + GifImagePlugin.getdata(im, duration=duration):
|
for s in GifImagePlugin.getheader(im)[0] + GifImagePlugin.getdata(im, duration=duration):
|
||||||
fp.write(s)
|
fp.write(s)
|
||||||
fp.write(b";")
|
fp.write(b";")
|
||||||
fp.close()
|
|
||||||
reread = Image.open(out)
|
reread = Image.open(out)
|
||||||
|
|
||||||
self.assertEqual(reread.info['duration'], duration)
|
self.assertEqual(reread.info['duration'], duration)
|
||||||
|
@ -329,12 +328,11 @@ class TestFileGif(PillowTestCase):
|
||||||
number_of_loops = 2
|
number_of_loops = 2
|
||||||
|
|
||||||
out = self.tempfile('temp.gif')
|
out = self.tempfile('temp.gif')
|
||||||
fp = open(out, "wb")
|
with open(out, "wb") as fp:
|
||||||
im = Image.new('L', (100, 100), '#000')
|
im = Image.new('L', (100, 100), '#000')
|
||||||
for s in GifImagePlugin.getheader(im)[0] + GifImagePlugin.getdata(im, loop=number_of_loops):
|
for s in GifImagePlugin.getheader(im)[0] + GifImagePlugin.getdata(im, loop=number_of_loops):
|
||||||
fp.write(s)
|
fp.write(s)
|
||||||
fp.write(b";")
|
fp.write(b";")
|
||||||
fp.close()
|
|
||||||
reread = Image.open(out)
|
reread = Image.open(out)
|
||||||
|
|
||||||
self.assertEqual(reread.info['loop'], number_of_loops)
|
self.assertEqual(reread.info['loop'], number_of_loops)
|
||||||
|
|
|
@ -36,9 +36,8 @@ class TestFilePpm(PillowTestCase):
|
||||||
|
|
||||||
def test_truncated_file(self):
|
def test_truncated_file(self):
|
||||||
path = self.tempfile('temp.pgm')
|
path = self.tempfile('temp.pgm')
|
||||||
f = open(path, 'w')
|
with open(path, 'w') as f:
|
||||||
f.write('P6')
|
f.write('P6')
|
||||||
f.close()
|
|
||||||
|
|
||||||
self.assertRaises(ValueError, lambda: Image.open(path))
|
self.assertRaises(ValueError, lambda: Image.open(path))
|
||||||
|
|
||||||
|
@ -47,8 +46,8 @@ class TestFilePpm(PillowTestCase):
|
||||||
# Storage.c accepted negative values for xsize, ysize. the
|
# Storage.c accepted negative values for xsize, ysize. the
|
||||||
# internal open_ppm function didn't check for sanity but it
|
# internal open_ppm function didn't check for sanity but it
|
||||||
# has been removed. The default opener doesn't accept negative
|
# has been removed. The default opener doesn't accept negative
|
||||||
# sizes.
|
# sizes.
|
||||||
|
|
||||||
with self.assertRaises(IOError):
|
with self.assertRaises(IOError):
|
||||||
Image.open('Tests/images/negative_size.ppm')
|
Image.open('Tests/images/negative_size.ppm')
|
||||||
|
|
||||||
|
|
|
@ -80,11 +80,11 @@ class TestImage(PillowTestCase):
|
||||||
# Will error out on save on 3.0.0
|
# Will error out on save on 3.0.0
|
||||||
import tempfile
|
import tempfile
|
||||||
im = hopper()
|
im = hopper()
|
||||||
fp = tempfile.TemporaryFile()
|
with tempfile.TemporaryFile() as fp:
|
||||||
im.save(fp, 'JPEG')
|
im.save(fp, 'JPEG')
|
||||||
fp.seek(0)
|
fp.seek(0)
|
||||||
reloaded = Image.open(fp)
|
reloaded = Image.open(fp)
|
||||||
self.assert_image_similar(im, reloaded, 20)
|
self.assert_image_similar(im, reloaded, 20)
|
||||||
|
|
||||||
def test_internals(self):
|
def test_internals(self):
|
||||||
|
|
||||||
|
|
|
@ -35,12 +35,10 @@ class TestPsDraw(PillowTestCase):
|
||||||
|
|
||||||
# Arrange
|
# Arrange
|
||||||
tempfile = self.tempfile('temp.ps')
|
tempfile = self.tempfile('temp.ps')
|
||||||
fp = open(tempfile, "wb")
|
with open(tempfile, "wb") as fp:
|
||||||
|
# Act
|
||||||
# Act
|
ps = PSDraw.PSDraw(fp)
|
||||||
ps = PSDraw.PSDraw(fp)
|
self._create_document(ps)
|
||||||
self._create_document(ps)
|
|
||||||
fp.close()
|
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
# Check non-zero file was created
|
# Check non-zero file was created
|
||||||
|
|
5
setup.py
5
setup.py
|
@ -728,9 +728,8 @@ class pil_build_ext(build_ext):
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
if ret >> 8 == 0:
|
if ret >> 8 == 0:
|
||||||
fp = open(tmpfile, 'r')
|
with open(tmpfile, 'r') as fp:
|
||||||
multiarch_path_component = fp.readline().strip()
|
multiarch_path_component = fp.readline().strip()
|
||||||
fp.close()
|
|
||||||
_add_directory(self.compiler.library_dirs,
|
_add_directory(self.compiler.library_dirs,
|
||||||
'/usr/lib/' + multiarch_path_component)
|
'/usr/lib/' + multiarch_path_component)
|
||||||
_add_directory(self.compiler.include_dirs,
|
_add_directory(self.compiler.include_dirs,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user