Skip to content

Commit

Permalink
Merge pull request #2307 from radarhere/contextmanagers
Browse files Browse the repository at this point in the history
Added context managers
  • Loading branch information
wiredfool committed Jan 17, 2017
2 parents 8aec14c + a06dd59 commit c1b510c
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 51 deletions.
22 changes: 10 additions & 12 deletions PIL/GifImagePlugin.py
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions PIL/IcnsImagePlugin.py
Expand Up @@ -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()

Expand Down
5 changes: 2 additions & 3 deletions PIL/SpiderImagePlugin.py
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion Tests/helper.py
Expand Up @@ -151,7 +151,7 @@ def skipKnownBadTest(self, msg=None, platform=None,

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)
Expand Down
26 changes: 12 additions & 14 deletions Tests/test_file_gif.py
Expand Up @@ -56,7 +56,7 @@ def test_optimize_correctness(self):
# 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),
Expand All @@ -70,7 +70,7 @@ def check(colors, size, expected_palette_length):
# 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'))


Expand Down Expand Up @@ -271,12 +271,11 @@ def test_duration(self):
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)
Expand Down Expand Up @@ -329,12 +328,11 @@ def test_number_of_loops(self):
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)
Expand Down
9 changes: 4 additions & 5 deletions Tests/test_file_ppm.py
Expand Up @@ -36,9 +36,8 @@ def test_16bit_pgm_write(self):

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))

Expand All @@ -47,8 +46,8 @@ def test_neg_ppm(self):
# 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')

Expand Down
10 changes: 5 additions & 5 deletions Tests/test_image.py
Expand Up @@ -80,11 +80,11 @@ def test_tempfile(self):
# 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):

Expand Down
10 changes: 4 additions & 6 deletions Tests/test_psdraw.py
Expand Up @@ -35,12 +35,10 @@ def test_draw_postscript(self):

# 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
Expand Down
5 changes: 2 additions & 3 deletions setup.py
Expand Up @@ -728,9 +728,8 @@ def add_multiarch_paths(self):
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,
Expand Down

0 comments on commit c1b510c

Please sign in to comment.