From 2b84221edf38fb60b3411b7698d1edc18483786a Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 26 Jan 2020 15:59:20 -0800 Subject: [PATCH] Fix ResourceWarning emitted during tests Appeared in the form: ResourceWarning: unclosed file <_io.BufferedReader name='Tests/images/invalid-exif-without-x-resolution.jpg'> Enable all warnings to always display during tests to help catch these warnings earlier. --- Tests/test_file_jpeg.py | 8 ++++---- Tests/test_file_tiff_metadata.py | 34 ++++++++++++++++---------------- tox.ini | 2 +- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index af95139ac..aa118016d 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -640,11 +640,11 @@ class TestFileJpeg(PillowTestCase): def test_invalid_exif_x_resolution(self): # When no x or y resolution is defined in EXIF - im = Image.open("Tests/images/invalid-exif-without-x-resolution.jpg") + with Image.open("Tests/images/invalid-exif-without-x-resolution.jpg") as im: - # This should return the default, and not a ValueError or - # OSError for an unidentified image. - self.assertEqual(im.info.get("dpi"), (72, 72)) + # This should return the default, and not a ValueError or + # OSError for an unidentified image. + self.assertEqual(im.info.get("dpi"), (72, 72)) def test_ifd_offset_exif(self): # Arrange diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py index f5407d49d..7188fb724 100644 --- a/Tests/test_file_tiff_metadata.py +++ b/Tests/test_file_tiff_metadata.py @@ -231,9 +231,9 @@ class TestFileTiffMetadata(PillowTestCase): out = self.tempfile("temp.tiff") im.save(out, tiffinfo=info, compression="raw") - reloaded = Image.open(out) - self.assertEqual(max_long, reloaded.tag_v2[41493].numerator) - self.assertEqual(1, reloaded.tag_v2[41493].denominator) + with Image.open(out) as reloaded: + self.assertEqual(max_long, reloaded.tag_v2[41493].numerator) + self.assertEqual(1, reloaded.tag_v2[41493].denominator) # out of bounds of 4 byte unsigned long numerator = max_long + 1 @@ -243,9 +243,9 @@ class TestFileTiffMetadata(PillowTestCase): out = self.tempfile("temp.tiff") im.save(out, tiffinfo=info, compression="raw") - reloaded = Image.open(out) - self.assertEqual(max_long, reloaded.tag_v2[41493].numerator) - self.assertEqual(1, reloaded.tag_v2[41493].denominator) + with Image.open(out) as reloaded: + self.assertEqual(max_long, reloaded.tag_v2[41493].numerator) + self.assertEqual(1, reloaded.tag_v2[41493].denominator) def test_ifd_signed_rational(self): im = hopper() @@ -260,9 +260,9 @@ class TestFileTiffMetadata(PillowTestCase): out = self.tempfile("temp.tiff") im.save(out, tiffinfo=info, compression="raw") - reloaded = Image.open(out) - self.assertEqual(numerator, reloaded.tag_v2[37380].numerator) - self.assertEqual(denominator, reloaded.tag_v2[37380].denominator) + with Image.open(out) as reloaded: + self.assertEqual(numerator, reloaded.tag_v2[37380].numerator) + self.assertEqual(denominator, reloaded.tag_v2[37380].denominator) numerator = -(2 ** 31) denominator = 2 ** 31 - 1 @@ -272,9 +272,9 @@ class TestFileTiffMetadata(PillowTestCase): out = self.tempfile("temp.tiff") im.save(out, tiffinfo=info, compression="raw") - reloaded = Image.open(out) - self.assertEqual(numerator, reloaded.tag_v2[37380].numerator) - self.assertEqual(denominator, reloaded.tag_v2[37380].denominator) + with Image.open(out) as reloaded: + self.assertEqual(numerator, reloaded.tag_v2[37380].numerator) + self.assertEqual(denominator, reloaded.tag_v2[37380].denominator) # out of bounds of 4 byte signed long numerator = -(2 ** 31) - 1 @@ -285,9 +285,9 @@ class TestFileTiffMetadata(PillowTestCase): out = self.tempfile("temp.tiff") im.save(out, tiffinfo=info, compression="raw") - reloaded = Image.open(out) - self.assertEqual(2 ** 31 - 1, reloaded.tag_v2[37380].numerator) - self.assertEqual(-1, reloaded.tag_v2[37380].denominator) + with Image.open(out) as reloaded: + self.assertEqual(2 ** 31 - 1, reloaded.tag_v2[37380].numerator) + self.assertEqual(-1, reloaded.tag_v2[37380].denominator) def test_ifd_signed_long(self): im = hopper() @@ -298,8 +298,8 @@ class TestFileTiffMetadata(PillowTestCase): out = self.tempfile("temp.tiff") im.save(out, tiffinfo=info, compression="raw") - reloaded = Image.open(out) - self.assertEqual(reloaded.tag_v2[37000], -60000) + with Image.open(out) as reloaded: + self.assertEqual(reloaded.tag_v2[37000], -60000) def test_empty_values(self): data = io.BytesIO( diff --git a/tox.ini b/tox.ini index 07d75be64..44ec8d51c 100644 --- a/tox.ini +++ b/tox.ini @@ -14,7 +14,7 @@ commands = {envpython} setup.py clean {envpython} setup.py build_ext --inplace {envpython} selftest.py - {envpython} -m pytest {posargs} + {envpython} -W always -m pytest {posargs} deps = cffi numpy