Added context managers

This commit is contained in:
Andrew Murray 2016-12-28 09:54:10 +11:00
parent 2aef31be67
commit a06dd59df7
9 changed files with 42 additions and 51 deletions

View File

@ -521,22 +521,20 @@ def _save_netpbm(im, fp, filename):
import tempfile
file = im._dump()
if im.mode != "RGB":
with open(filename, 'wb') as f:
stderr = tempfile.TemporaryFile()
check_call(["ppmtogif", file], stdout=f, stderr=stderr)
else:
with open(filename, 'wb') as f:
with open(filename, 'wb') as f:
if im.mode != "RGB":
with tempfile.TemporaryFile() as stderr:
check_call(["ppmtogif", file], stdout=f, stderr=stderr)
else:
# Pipe ppmquant output into ppmtogif
# "ppmquant 256 %s | ppmtogif > %s" % (file, filename)
quant_cmd = ["ppmquant", "256", file]
togif_cmd = ["ppmtogif"]
stderr = tempfile.TemporaryFile()
quant_proc = Popen(quant_cmd, stdout=PIPE, stderr=stderr)
stderr = tempfile.TemporaryFile()
togif_proc = Popen(togif_cmd, stdin=quant_proc.stdout, stdout=f,
stderr=stderr)
with tempfile.TemporaryFile() as stderr:
quant_proc = Popen(quant_cmd, stdout=PIPE, stderr=stderr)
with tempfile.TemporaryFile() as stderr:
togif_proc = Popen(togif_cmd, stdin=quant_proc.stdout,
stdout=f, stderr=stderr)
# Allow ppmquant to receive SIGPIPE if ppmtogif exits
quant_proc.stdout.close()

View File

@ -330,8 +330,8 @@ def _save(im, fp, filename):
from subprocess import Popen, PIPE, CalledProcessError
convert_cmd = ["iconutil", "-c", "icns", "-o", filename, iconset]
stderr = tempfile.TemporaryFile()
convert_proc = Popen(convert_cmd, stdout=PIPE, stderr=stderr)
with tempfile.TemporaryFile() as stderr:
convert_proc = Popen(convert_cmd, stdout=PIPE, stderr=stderr)
convert_proc.stdout.close()

View File

@ -83,9 +83,8 @@ def isSpiderHeader(t):
def isSpiderImage(filename):
fp = open(filename, 'rb')
f = fp.read(92) # read 23 * 4 bytes
fp.close()
with open(filename, 'rb') as fp:
f = fp.read(92) # read 23 * 4 bytes
t = struct.unpack('>23f', f) # try big-endian first
hdrlen = isSpiderHeader(t)
if hdrlen == 0:

View File

@ -151,7 +151,7 @@ class PillowTestCase(unittest.TestCase):
def tempfile(self, template):
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)
self.addCleanup(self.delete_tempfile, path)

View File

@ -56,7 +56,7 @@ class TestFileGif(PillowTestCase):
# 256 color Palette image, posterize to > 128 and < 128 levels
# Size bigger and smaller than 512x512
# 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):
# make an image with empty colors in the start of the palette range
im = Image.frombytes('P', (colors,colors),
@ -70,7 +70,7 @@ class TestFileGif(PillowTestCase):
# check palette length
palette_length = max(i+1 for i,v in enumerate(reloaded.histogram()) if v)
self.assertEqual(expected_palette_length, palette_length)
self.assert_image_equal(im.convert('RGB'), reloaded.convert('RGB'))
@ -271,12 +271,11 @@ class TestFileGif(PillowTestCase):
duration = 1000
out = self.tempfile('temp.gif')
fp = open(out, "wb")
im = Image.new('L', (100, 100), '#000')
for s in GifImagePlugin.getheader(im)[0] + GifImagePlugin.getdata(im, duration=duration):
fp.write(s)
fp.write(b";")
fp.close()
with open(out, "wb") as fp:
im = Image.new('L', (100, 100), '#000')
for s in GifImagePlugin.getheader(im)[0] + GifImagePlugin.getdata(im, duration=duration):
fp.write(s)
fp.write(b";")
reread = Image.open(out)
self.assertEqual(reread.info['duration'], duration)
@ -329,12 +328,11 @@ class TestFileGif(PillowTestCase):
number_of_loops = 2
out = self.tempfile('temp.gif')
fp = open(out, "wb")
im = Image.new('L', (100, 100), '#000')
for s in GifImagePlugin.getheader(im)[0] + GifImagePlugin.getdata(im, loop=number_of_loops):
fp.write(s)
fp.write(b";")
fp.close()
with open(out, "wb") as fp:
im = Image.new('L', (100, 100), '#000')
for s in GifImagePlugin.getheader(im)[0] + GifImagePlugin.getdata(im, loop=number_of_loops):
fp.write(s)
fp.write(b";")
reread = Image.open(out)
self.assertEqual(reread.info['loop'], number_of_loops)

View File

@ -36,9 +36,8 @@ class TestFilePpm(PillowTestCase):
def test_truncated_file(self):
path = self.tempfile('temp.pgm')
f = open(path, 'w')
f.write('P6')
f.close()
with open(path, 'w') as f:
f.write('P6')
self.assertRaises(ValueError, lambda: Image.open(path))
@ -47,8 +46,8 @@ class TestFilePpm(PillowTestCase):
# Storage.c accepted negative values for xsize, ysize. the
# internal open_ppm function didn't check for sanity but it
# has been removed. The default opener doesn't accept negative
# sizes.
# sizes.
with self.assertRaises(IOError):
Image.open('Tests/images/negative_size.ppm')

View File

@ -80,11 +80,11 @@ class TestImage(PillowTestCase):
# Will error out on save on 3.0.0
import tempfile
im = hopper()
fp = tempfile.TemporaryFile()
im.save(fp, 'JPEG')
fp.seek(0)
reloaded = Image.open(fp)
self.assert_image_similar(im, reloaded, 20)
with tempfile.TemporaryFile() as fp:
im.save(fp, 'JPEG')
fp.seek(0)
reloaded = Image.open(fp)
self.assert_image_similar(im, reloaded, 20)
def test_internals(self):

View File

@ -35,12 +35,10 @@ class TestPsDraw(PillowTestCase):
# Arrange
tempfile = self.tempfile('temp.ps')
fp = open(tempfile, "wb")
# Act
ps = PSDraw.PSDraw(fp)
self._create_document(ps)
fp.close()
with open(tempfile, "wb") as fp:
# Act
ps = PSDraw.PSDraw(fp)
self._create_document(ps)
# Assert
# Check non-zero file was created

View File

@ -728,9 +728,8 @@ class pil_build_ext(build_ext):
return
try:
if ret >> 8 == 0:
fp = open(tmpfile, 'r')
multiarch_path_component = fp.readline().strip()
fp.close()
with open(tmpfile, 'r') as fp:
multiarch_path_component = fp.readline().strip()
_add_directory(self.compiler.library_dirs,
'/usr/lib/' + multiarch_path_component)
_add_directory(self.compiler.include_dirs,