From 77f0608c4ede26917f87c088737a98b05c6ba187 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 | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/Tests/helper.py b/Tests/helper.py index 989215ca4ae..2db550b0232 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -121,13 +121,16 @@ def assert_warning(self, warn_class, func, *args, **kwargs): result = func(*args, **kwargs) # 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 c960e84de04..92a8732b73c 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -478,7 +478,6 @@ def test_remap_palette(self): im = hopper() self.assertRaises(ValueError, im.remap_palette, None) - def test__new(self): from PIL import ImagePalette @@ -496,7 +495,8 @@ def _make_new(base_image, im, palette_result=None): self.assertEqual(new_im.size, im.size) self.assertEqual(new_im.info, base_image.info) if palette_result is not None: - self.assertEqual(new_im.palette.tobytes(), palette_result.tobytes()) + self.assertEqual(new_im.palette.tobytes(), + palette_result.tobytes()) else: self.assertEqual(new_im.palette, None) @@ -505,6 +505,15 @@ def _make_new(base_image, im, palette_result=None): _make_new(im, blank_p, ImagePalette.ImagePalette()) _make_new(im, blank_pa, ImagePalette.ImagePalette()) + 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')) + class MockEncoder(object): pass @@ -534,5 +543,6 @@ def test_encode_registry_fail(self): ('args',), extra=('extra',)) + if __name__ == '__main__': unittest.main() From cde8c1b56064d72b63e5d01f61fc90d416569215 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 2db550b0232..1375eb7e5dd 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -122,7 +122,9 @@ def assert_warning(self, warn_class, func, *args, **kwargs): # 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 0b6691ecc9e527e20aef2091b3ab82c12284f67e 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 92a8732b73c..d62457fe336 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -514,6 +514,16 @@ def test_no_resource_warning_on_save(self): 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)) + class MockEncoder(object): pass From 9344bd20dd848f28883fd2e7d23f45e66f4164ab 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 | 12 +++++++++++- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index d62457fe336..92a8732b73c 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -514,16 +514,6 @@ def test_no_resource_warning_on_save(self): 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)) - class MockEncoder(object): pass diff --git a/Tests/test_numpy.py b/Tests/test_numpy.py index 0529cce4da2..7eeee3a83f0 100644 --- a/Tests/test_numpy.py +++ b/Tests/test_numpy.py @@ -212,7 +212,6 @@ def test_zero_size(self): self.assertEqual(im.size, (0, 0)) - def test_bool(self): # https://github.com/python-pillow/Pillow/issues/2044 a = numpy.zeros((10,2), dtype=numpy.bool) @@ -221,5 +220,16 @@ def test_bool(self): im2 = Image.fromarray(a) self.assertEqual(im2.getdata()[0], 255) + 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()