From c8eebc1dc72a53a68f1a027cb7f7ee53a557734d Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 19 Dec 2017 15:06:43 +0200 Subject: [PATCH 1/4] Re-roll in case of identical noise --- Tests/test_image.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index e2859d237..58ea468e7 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -347,14 +347,24 @@ class TestImage(PillowTestCase): # Arrange size = (100, 100) sigma = 128 + # To reduce chance of randomly having the same value twice + attempts = 5 # Act - im = Image.effect_noise(size, sigma) + while attempts > 0: + im = Image.effect_noise(size, sigma) - # Assert - self.assertEqual(im.size, (100, 100)) - self.assertEqual(im.mode, "L") - self.assertNotEqual(im.getpixel((0, 0)), im.getpixel((0, 1))) + # Assert + self.assertEqual(im.size, (100, 100)) + self.assertEqual(im.mode, "L") + p0 = im.getpixel((0, 0)) + p1 = im.getpixel((0, 1)) + if p0 == p1: + # Let's roll again + attempts -= 1 + else: + break + self.assertNotEqual(p0, p1) def test_effect_spread(self): # Arrange From 11a2026f12a76ab890ebb5465e091d1a948c8d52 Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 19 Dec 2017 15:12:02 +0200 Subject: [PATCH 2/4] flake8 --- Tests/test_image.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 58ea468e7..875e925a8 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -22,7 +22,7 @@ class TestImage(PillowTestCase): 'BGR;15', 'BGR;16', 'BGR;24', 'BGR;32' ]: with self.assertRaises(ValueError) as e: - Image.new(mode, (1, 1)); + Image.new(mode, (1, 1)) self.assertEqual(str(e.exception), 'unrecognized image mode') def test_sanity(self): @@ -236,9 +236,9 @@ class TestImage(PillowTestCase): self.assertEqual(img_colors, expected_colors) def test_alpha_inplace(self): - src = Image.new('RGBA', (128,128), 'blue') + src = Image.new('RGBA', (128, 128), 'blue') - over = Image.new('RGBA', (128,128), 'red') + over = Image.new('RGBA', (128, 128), 'red') mask = hopper('L') over.putalpha(mask) @@ -275,17 +275,18 @@ class TestImage(PillowTestCase): # errors self.assertRaises(ValueError, - source.alpha_composite, over, "invalid source") + source.alpha_composite, over, "invalid source") self.assertRaises(ValueError, - source.alpha_composite, over, (0, 0), "invalid destination") + source.alpha_composite, over, (0, 0), + "invalid destination") self.assertRaises(ValueError, - source.alpha_composite, over, (0)) + source.alpha_composite, over, (0)) self.assertRaises(ValueError, - source.alpha_composite, over, (0, 0), (0)) + source.alpha_composite, over, (0, 0), (0)) self.assertRaises(ValueError, - source.alpha_composite, over, (0, -1)) + source.alpha_composite, over, (0, -1)) self.assertRaises(ValueError, - source.alpha_composite, over, (0, 0), (0, -1)) + source.alpha_composite, over, (0, 0), (0, -1)) def test_registered_extensions_uninitialized(self): # Arrange @@ -500,8 +501,8 @@ class TestImage(PillowTestCase): im = hopper('RGB') im_p = hopper('P') - blank_p = Image.new('P', (10,10)) - blank_pa = Image.new('PA', (10,10)) + blank_p = Image.new('P', (10, 10)) + blank_pa = Image.new('PA', (10, 10)) blank_p.palette = None blank_pa.palette = None From 8adab0ec0dd46813335b857e4e92b08c53b65cc1 Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 19 Dec 2017 17:12:58 +0200 Subject: [PATCH 3/4] Noise effect: take five pixels, assert not all same --- Tests/helper.py | 14 ++++++++++---- Tests/test_image.py | 25 ++++++++++--------------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/Tests/helper.py b/Tests/helper.py index 117c5da54..e5e08e15d 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -100,7 +100,7 @@ class PillowTestCase(unittest.TestCase): logger.error("Url for test images: %s" %url) except: pass - + self.fail(msg or "got different content") def assert_image_similar(self, a, b, epsilon, msg=None): @@ -161,6 +161,12 @@ class PillowTestCase(unittest.TestCase): self.assertTrue(found) return result + def assert_all_same(self, items, msg=None): + self.assertTrue(items.count(items[0]) == len(items), msg) + + def assert_not_all_same(self, items, msg=None): + self.assertFalse(items.count(items[0]) == len(items), msg) + def skipKnownBadTest(self, msg=None, platform=None, travis=None, interpreter=None): # Skip if platform/travis matches, and @@ -206,7 +212,7 @@ class PillowLeakTestCase(PillowTestCase): # requires unix/osx iterations = 100 # count mem_limit = 512 # k - + def _get_mem_usage(self): """ Gets the RUSAGE memory usage, returns in K. Encapsulates the difference @@ -214,7 +220,7 @@ class PillowLeakTestCase(PillowTestCase): :returns; memory usage in kilobytes """ - + from resource import getpagesize, getrusage, RUSAGE_SELF mem = getrusage(RUSAGE_SELF).ru_maxrss if sys.platform == 'darwin': @@ -225,7 +231,7 @@ class PillowLeakTestCase(PillowTestCase): # linux # man 2 getrusage # ru_maxrss (since Linux 2.6.32) - # This is the maximum resident set size used (in kilobytes). + # This is the maximum resident set size used (in kilobytes). return mem # Kb def _test_leak(self, core): diff --git a/Tests/test_image.py b/Tests/test_image.py index 875e925a8..6f6d1983e 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -348,24 +348,19 @@ class TestImage(PillowTestCase): # Arrange size = (100, 100) sigma = 128 - # To reduce chance of randomly having the same value twice - attempts = 5 # Act - while attempts > 0: - im = Image.effect_noise(size, sigma) + im = Image.effect_noise(size, sigma) - # Assert - self.assertEqual(im.size, (100, 100)) - self.assertEqual(im.mode, "L") - p0 = im.getpixel((0, 0)) - p1 = im.getpixel((0, 1)) - if p0 == p1: - # Let's roll again - attempts -= 1 - else: - break - self.assertNotEqual(p0, p1) + # Assert + self.assertEqual(im.size, (100, 100)) + self.assertEqual(im.mode, "L") + p0 = im.getpixel((0, 0)) + p1 = im.getpixel((0, 1)) + p2 = im.getpixel((0, 2)) + p3 = im.getpixel((0, 3)) + p4 = im.getpixel((0, 4)) + self.assert_not_all_same([p0, p1, p2, p3, p4]) def test_effect_spread(self): # Arrange From 070e66fe7be1fcf0ac659c0fcbe9a3e352c6cb27 Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 19 Dec 2017 17:16:15 +0200 Subject: [PATCH 4/4] flake8 --- Tests/helper.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Tests/helper.py b/Tests/helper.py index e5e08e15d..38eb98a94 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -20,6 +20,7 @@ try: except ImportError: pass + def convert_to_comparable(a, b): new_a, new_b = a, b if a.mode == 'P': @@ -97,7 +98,7 @@ class PillowTestCase(unittest.TestCase): if HAS_UPLOADER: try: url = test_image_results.upload(a, b) - logger.error("Url for test images: %s" %url) + logger.error("Url for test images: %s" % url) except: pass @@ -130,7 +131,7 @@ class PillowTestCase(unittest.TestCase): if HAS_UPLOADER: try: url = test_image_results.upload(a, b) - logger.error("Url for test images: %s" %url) + logger.error("Url for test images: %s" % url) except: pass raise e @@ -207,11 +208,12 @@ class PillowTestCase(unittest.TestCase): return Image.open(outfile) raise IOError() + @unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS") class PillowLeakTestCase(PillowTestCase): # requires unix/osx - iterations = 100 # count - mem_limit = 512 # k + iterations = 100 # count + mem_limit = 512 # k def _get_mem_usage(self): """ @@ -221,7 +223,7 @@ class PillowLeakTestCase(PillowTestCase): :returns; memory usage in kilobytes """ - from resource import getpagesize, getrusage, RUSAGE_SELF + from resource import getrusage, RUSAGE_SELF mem = getrusage(RUSAGE_SELF).ru_maxrss if sys.platform == 'darwin': # man 2 getrusage: @@ -315,6 +317,7 @@ def imagemagick_available(): def on_appveyor(): return 'APPVEYOR' in os.environ + if sys.platform == 'win32': IMCONVERT = os.environ.get('MAGICK_HOME', '') if IMCONVERT: