From c10ad19a2b1a25d00c845c71f0b27c6738be4bdb Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 26 Aug 2017 10:20:30 +1000 Subject: [PATCH] Changed file opening in tests to use with --- Tests/test_file_png.py | 22 +++++++++++----------- Tests/test_file_tiff.py | 12 ++++++------ Tests/test_font_bdf.py | 4 ++-- Tests/test_font_pcf.py | 4 ++-- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 7ee2c9fb7..d4970d2df 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -505,17 +505,17 @@ class TestFilePng(PillowTestCase): im.convert("P").save(test_file, dpi=(100, 100)) chunks = [] - fp = open(test_file, "rb") - fp.read(8) - png = PngImagePlugin.PngStream(fp) - while True: - cid, pos, length = png.read() - chunks.append(cid) - try: - s = png.call(cid, pos, length) - except EOFError: - break - png.crc(cid, s) + with open(test_file, "rb") as fp: + fp.read(8) + png = PngImagePlugin.PngStream(fp) + while True: + cid, pos, length = png.read() + chunks.append(cid) + try: + s = png.call(cid, pos, length) + except EOFError: + break + png.crc(cid, s) # https://www.w3.org/TR/PNG/#5ChunkOrdering # IHDR - shall be first diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index bfc3bf120..8dcd518b7 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -495,12 +495,12 @@ class TestFileTiff(PillowTestCase): with Image.open("Tests/images/uint16_1_4660.tif") as im: im.save(tmpfile) - f = open(tmpfile, 'rb') - im = Image.open(f) - fp = im.fp - self.assertFalse(fp.closed) - im.load() - self.assertFalse(fp.closed) + with open(tmpfile, 'rb') as f: + im = Image.open(f) + fp = im.fp + self.assertFalse(fp.closed) + im.load() + self.assertFalse(fp.closed) @unittest.skipUnless(sys.platform.startswith('win32'), "Windows only") class TestFileTiffW32(PillowTestCase): diff --git a/Tests/test_font_bdf.py b/Tests/test_font_bdf.py index e3b7fbbd3..20007215b 100644 --- a/Tests/test_font_bdf.py +++ b/Tests/test_font_bdf.py @@ -9,8 +9,8 @@ class TestFontBdf(PillowTestCase): def test_sanity(self): - test_file = open(filename, "rb") - font = BdfFontFile.BdfFontFile(test_file) + with open(filename, "rb") as test_file: + font = BdfFontFile.BdfFontFile(test_file) self.assertIsInstance(font, FontFile.FontFile) self.assertEqual(len([_f for _f in font.glyph if _f]), 190) diff --git a/Tests/test_font_pcf.py b/Tests/test_font_pcf.py index 25c5d7001..4c0333050 100644 --- a/Tests/test_font_pcf.py +++ b/Tests/test_font_pcf.py @@ -17,8 +17,8 @@ class TestFontPcf(PillowTestCase): self.skipTest("zlib support not available") def save_font(self): - test_file = open(fontname, "rb") - font = PcfFontFile.PcfFontFile(test_file) + with open(fontname, "rb") as test_file: + font = PcfFontFile.PcfFontFile(test_file) self.assertIsInstance(font, FontFile.FontFile) self.assertEqual(len([_f for _f in font.glyph if _f]), 192)