Merge pull request #2756 from hugovk/ResourceWarning-cherrypicked

ResourceWarning tests
This commit is contained in:
wiredfool 2017-09-29 15:36:16 +01:00 committed by GitHub
commit efe98726cf
3 changed files with 35 additions and 10 deletions

View File

@ -121,13 +121,18 @@ class PillowTestCase(unittest.TestCase):
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,
"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,

View File

@ -478,7 +478,6 @@ class TestImage(PillowTestCase):
im = hopper()
self.assertRaises(ValueError, im.remap_palette, None)
def test__new(self):
from PIL import ImagePalette
@ -496,7 +495,8 @@ class TestImage(PillowTestCase):
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 @@ class TestImage(PillowTestCase):
_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 @@ class TestRegistry(PillowTestCase):
('args',),
extra=('extra',))
if __name__ == '__main__':
unittest.main()

View File

@ -212,7 +212,6 @@ class TestNumpy(PillowTestCase):
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 @@ class TestNumpy(PillowTestCase):
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()