From 51d84d68e3954be9e0ced6353ee6d6ff54b56721 Mon Sep 17 00:00:00 2001 From: hugovk Date: Tue, 30 Dec 2014 16:20:42 +0200 Subject: [PATCH 1/4] Failing test for ResourceWarning on Python 3 --- Tests/helper.py | 17 ++++++++++------- Tests/test_image.py | 10 ++++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Tests/helper.py b/Tests/helper.py index 563f42060..79d2d5b81 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -116,13 +116,16 @@ class PillowTestCase(unittest.TestCase): result = func() # Verify some things. - self.assertGreaterEqual(len(w), 1) - found = False - for v in w: - if issubclass(v.category, warn_class): - found = True - break - self.assertTrue(found) + if warn_class is None: + self.assertEqual(len(w), 0) + else: + self.assertGreaterEqual(len(w), 1) + found = False + for v in w: + if issubclass(v.category, warn_class): + found = True + break + self.assertTrue(found) return result def skipKnownBadTest(self, msg=None, platform=None, diff --git a/Tests/test_image.py b/Tests/test_image.py index 26e45b10a..156164f0c 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -196,6 +196,16 @@ class TestImage(PillowTestCase): im3 = Image.open('Tests/images/effect_spread.png') self.assert_image_similar(im2, im3, 110) + def test_no_resource_warning_on_save(self): + # https://github.com/python-pillow/Pillow/issues/835 + # Arrange + test_file = 'Tests/images/hopper.png' + + # Act/Assert + with Image.open(test_file) as im: + self.assert_warning(None, lambda: im.save('test_img.jpg')) + + if __name__ == '__main__': unittest.main() From e96ea667292afbe881ec775ae3c4ab6070fef3e4 Mon Sep 17 00:00:00 2001 From: hugovk Date: Tue, 30 Dec 2014 16:51:19 +0200 Subject: [PATCH 2/4] Improve assert_warning() error message for warn_class=None --- Tests/helper.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Tests/helper.py b/Tests/helper.py index 79d2d5b81..59dfaedd7 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -117,7 +117,9 @@ class PillowTestCase(unittest.TestCase): # Verify some things. if warn_class is None: - self.assertEqual(len(w), 0) + self.assertEqual(len(w), 0, + "Expected no warnings, got %s" % + list(v.category for v in w)) else: self.assertGreaterEqual(len(w), 1) found = False From 739cb150ac1f12535af31b009c40f19ca1973ab1 Mon Sep 17 00:00:00 2001 From: hugovk Date: Tue, 30 Dec 2014 17:02:59 +0200 Subject: [PATCH 3/4] A numpy failing test for ResourceWarning on Python 3 --- Tests/test_image.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Tests/test_image.py b/Tests/test_image.py index 156164f0c..ff16b7307 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -205,6 +205,16 @@ class TestImage(PillowTestCase): with Image.open(test_file) as im: self.assert_warning(None, lambda: im.save('test_img.jpg')) + def test_no_resource_warning_for_numpy_array(self): + # https://github.com/python-pillow/Pillow/issues/835 + # Arrange + from numpy import array + test_file = 'Tests/images/hopper.png' + im = Image.open(test_file) + + # Act/Assert + self.assert_warning(None, lambda: array(im)) + if __name__ == '__main__': unittest.main() From 08f285d79bb3f9c7f39cae591644ee66faa0cc32 Mon Sep 17 00:00:00 2001 From: hugovk Date: Thu, 1 Jan 2015 12:57:43 +0200 Subject: [PATCH 4/4] Move test_no_resource_warning_for_numpy_array to test_numpy so it can be skipped easily --- Tests/test_image.py | 10 ---------- Tests/test_numpy.py | 11 ++++++++++- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index ff16b7307..156164f0c 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -205,16 +205,6 @@ class TestImage(PillowTestCase): with Image.open(test_file) as im: self.assert_warning(None, lambda: im.save('test_img.jpg')) - def test_no_resource_warning_for_numpy_array(self): - # https://github.com/python-pillow/Pillow/issues/835 - # Arrange - from numpy import array - test_file = 'Tests/images/hopper.png' - im = Image.open(test_file) - - # Act/Assert - self.assert_warning(None, lambda: array(im)) - if __name__ == '__main__': unittest.main() diff --git a/Tests/test_numpy.py b/Tests/test_numpy.py index 7b6f22b2b..dbdcf1758 100644 --- a/Tests/test_numpy.py +++ b/Tests/test_numpy.py @@ -130,7 +130,16 @@ class TestNumpy(PillowTestCase): im.putdata(arr) self.assertEqual(len(im.getdata()),len(arr)) - + + def test_no_resource_warning_for_numpy_array(self): + # https://github.com/python-pillow/Pillow/issues/835 + # Arrange + from numpy import array + test_file = 'Tests/images/hopper.png' + im = Image.open(test_file) + + # Act/Assert + self.assert_warning(None, lambda: array(im)) if __name__ == '__main__': unittest.main()