From 83426ef132f3e5347e68ab1d0934d915c7a7a32e Mon Sep 17 00:00:00 2001 From: hugovk Date: Tue, 30 Dec 2014 16:20:42 +0200 Subject: [PATCH] Failing test for ResourceWarning on Python 3 #835 --- Tests/helper.py | 19 ++++++++++++------- Tests/test_image.py | 10 ++++++++++ Tests/test_numpy.py | 11 ++++++++++- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/Tests/helper.py b/Tests/helper.py index abb2fbd6d..c8b06f9af 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -111,13 +111,18 @@ 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, + "Expected no warnings, got %s" % + list(v.category for v in w)) + 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 a9c168fa7..5355b488f 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -237,6 +237,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() diff --git a/Tests/test_numpy.py b/Tests/test_numpy.py index 4f2a5afb1..f8e324e27 100644 --- a/Tests/test_numpy.py +++ b/Tests/test_numpy.py @@ -136,10 +136,19 @@ class TestNumpy(PillowTestCase): im = Image.new('F', (150, 100)) arr = numpy.zeros((15000,), numpy.float32) 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()