From ecc4c7fecc3068ea67e52412eabfa68aae24a956 Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 10 Apr 2018 13:40:44 +0300 Subject: [PATCH 1/4] Remove unittest regex deprecation warnings --- Tests/helper.py | 5 +++ Tests/test_color_lut.py | 59 ++++++++++++++++++------------------ Tests/test_file_jpeg.py | 2 +- Tests/test_file_jpeg2k.py | 2 +- Tests/test_file_png.py | 3 +- Tests/test_image_resample.py | 20 ++++++------ Tests/test_imagecms.py | 2 +- Tests/test_imagefont.py | 3 +- 8 files changed, 50 insertions(+), 46 deletions(-) diff --git a/Tests/helper.py b/Tests/helper.py index f04dffbc5..606dca006 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -267,6 +267,11 @@ class PillowLeakTestCase(PillowTestCase): py3 = sys.version_info.major >= 3 +if not py3: + # Remove DeprecationWarning in Python 3 + PillowTestCase.assertRaisesRegex = PillowTestCase.assertRaisesRegexp + PillowTestCase.assertRegex = PillowTestCase.assertRegexpMatches + def fromstring(data): from io import BytesIO diff --git a/Tests/test_color_lut.py b/Tests/test_color_lut.py index 8bf33be92..2c6c83af1 100644 --- a/Tests/test_color_lut.py +++ b/Tests/test_color_lut.py @@ -32,39 +32,39 @@ class TestColorLut3DCoreAPI(PillowTestCase): def test_wrong_args(self): im = Image.new('RGB', (10, 10), 0) - with self.assertRaisesRegexp(ValueError, "filter"): + with self.assertRaisesRegex(ValueError, "filter"): im.im.color_lut_3d('RGB', Image.CUBIC, *self.generate_identity_table(3, 3)) - with self.assertRaisesRegexp(ValueError, "image mode"): + with self.assertRaisesRegex(ValueError, "image mode"): im.im.color_lut_3d('wrong', Image.LINEAR, *self.generate_identity_table(3, 3)) - with self.assertRaisesRegexp(ValueError, "table_channels"): + with self.assertRaisesRegex(ValueError, "table_channels"): im.im.color_lut_3d('RGB', Image.LINEAR, *self.generate_identity_table(5, 3)) - with self.assertRaisesRegexp(ValueError, "table_channels"): + with self.assertRaisesRegex(ValueError, "table_channels"): im.im.color_lut_3d('RGB', Image.LINEAR, *self.generate_identity_table(1, 3)) - with self.assertRaisesRegexp(ValueError, "table_channels"): + with self.assertRaisesRegex(ValueError, "table_channels"): im.im.color_lut_3d('RGB', Image.LINEAR, *self.generate_identity_table(2, 3)) - with self.assertRaisesRegexp(ValueError, "Table size"): + with self.assertRaisesRegex(ValueError, "Table size"): im.im.color_lut_3d('RGB', Image.LINEAR, *self.generate_identity_table(3, (1, 3, 3))) - with self.assertRaisesRegexp(ValueError, "Table size"): + with self.assertRaisesRegex(ValueError, "Table size"): im.im.color_lut_3d('RGB', Image.LINEAR, *self.generate_identity_table(3, (66, 3, 3))) - with self.assertRaisesRegexp(ValueError, r"size1D \* size2D \* size3D"): + with self.assertRaisesRegex(ValueError, r"size1D \* size2D \* size3D"): im.im.color_lut_3d('RGB', Image.LINEAR, 3, 2, 2, 2, [0, 0, 0] * 7) - with self.assertRaisesRegexp(ValueError, r"size1D \* size2D \* size3D"): + with self.assertRaisesRegex(ValueError, r"size1D \* size2D \* size3D"): im.im.color_lut_3d('RGB', Image.LINEAR, 3, 2, 2, 2, [0, 0, 0] * 9) @@ -98,27 +98,27 @@ class TestColorLut3DCoreAPI(PillowTestCase): *self.generate_identity_table(3, (3, 3, 65))) def test_wrong_mode(self): - with self.assertRaisesRegexp(ValueError, "wrong mode"): + with self.assertRaisesRegex(ValueError, "wrong mode"): im = Image.new('L', (10, 10), 0) im.im.color_lut_3d('RGB', Image.LINEAR, *self.generate_identity_table(3, 3)) - with self.assertRaisesRegexp(ValueError, "wrong mode"): + with self.assertRaisesRegex(ValueError, "wrong mode"): im = Image.new('RGB', (10, 10), 0) im.im.color_lut_3d('L', Image.LINEAR, *self.generate_identity_table(3, 3)) - with self.assertRaisesRegexp(ValueError, "wrong mode"): + with self.assertRaisesRegex(ValueError, "wrong mode"): im = Image.new('L', (10, 10), 0) im.im.color_lut_3d('L', Image.LINEAR, *self.generate_identity_table(3, 3)) - with self.assertRaisesRegexp(ValueError, "wrong mode"): + with self.assertRaisesRegex(ValueError, "wrong mode"): im = Image.new('RGB', (10, 10), 0) im.im.color_lut_3d('RGBA', Image.LINEAR, *self.generate_identity_table(3, 3)) - with self.assertRaisesRegexp(ValueError, "wrong mode"): + with self.assertRaisesRegex(ValueError, "wrong mode"): im = Image.new('RGB', (10, 10), 0) im.im.color_lut_3d('RGB', Image.LINEAR, *self.generate_identity_table(4, 3)) @@ -238,31 +238,31 @@ class TestColorLut3DCoreAPI(PillowTestCase): class TestColorLut3DFilter(PillowTestCase): def test_wrong_args(self): - with self.assertRaisesRegexp(ValueError, "should be either an integer"): + with self.assertRaisesRegex(ValueError, "should be either an integer"): ImageFilter.Color3DLUT("small", [1]) - with self.assertRaisesRegexp(ValueError, "should be either an integer"): + with self.assertRaisesRegex(ValueError, "should be either an integer"): ImageFilter.Color3DLUT((11, 11), [1]) - with self.assertRaisesRegexp(ValueError, r"in \[2, 65\] range"): + with self.assertRaisesRegex(ValueError, r"in \[2, 65\] range"): ImageFilter.Color3DLUT((11, 11, 1), [1]) - with self.assertRaisesRegexp(ValueError, r"in \[2, 65\] range"): + with self.assertRaisesRegex(ValueError, r"in \[2, 65\] range"): ImageFilter.Color3DLUT((11, 11, 66), [1]) - with self.assertRaisesRegexp(ValueError, "table should have .+ items"): + with self.assertRaisesRegex(ValueError, "table should have .+ items"): ImageFilter.Color3DLUT((3, 3, 3), [1, 1, 1]) - with self.assertRaisesRegexp(ValueError, "table should have .+ items"): + with self.assertRaisesRegex(ValueError, "table should have .+ items"): ImageFilter.Color3DLUT((3, 3, 3), [[1, 1, 1]] * 2) - with self.assertRaisesRegexp(ValueError, "should have a length of 4"): + with self.assertRaisesRegex(ValueError, "should have a length of 4"): ImageFilter.Color3DLUT((3, 3, 3), [[1, 1, 1]] * 27, channels=4) - with self.assertRaisesRegexp(ValueError, "should have a length of 3"): + with self.assertRaisesRegex(ValueError, "should have a length of 3"): ImageFilter.Color3DLUT((2, 2, 2), [[1, 1]] * 8) - with self.assertRaisesRegexp(ValueError, "Only 3 or 4 output"): + with self.assertRaisesRegex(ValueError, "Only 3 or 4 output"): ImageFilter.Color3DLUT((2, 2, 2), [[1, 1]] * 8, channels=2) def test_convert_table(self): @@ -290,16 +290,17 @@ class TestColorLut3DFilter(PillowTestCase): self.assertEqual(repr(lut), "") + class TestGenerateColorLut3D(PillowTestCase): def test_wrong_channels_count(self): - with self.assertRaisesRegexp(ValueError, "3 or 4 output channels"): + with self.assertRaisesRegex(ValueError, "3 or 4 output channels"): ImageFilter.Color3DLUT.generate(5, channels=2, callback=lambda r, g, b: (r, g, b)) - with self.assertRaisesRegexp(ValueError, "should have either channels"): + with self.assertRaisesRegex(ValueError, "should have either channels"): ImageFilter.Color3DLUT.generate(5, lambda r, g, b: (r, g, b, r)) - with self.assertRaisesRegexp(ValueError, "should have either channels"): + with self.assertRaisesRegex(ValueError, "should have either channels"): ImageFilter.Color3DLUT.generate(5, channels=4, callback=lambda r, g, b: (r, g, b)) @@ -333,13 +334,13 @@ class TestTransformColorLut3D(PillowTestCase): def test_wrong_args(self): source = ImageFilter.Color3DLUT.generate(5, lambda r, g, b: (r, g, b)) - with self.assertRaisesRegexp(ValueError, "Only 3 or 4 output"): + with self.assertRaisesRegex(ValueError, "Only 3 or 4 output"): source.transform(lambda r, g, b: (r, g, b), channels=8) - with self.assertRaisesRegexp(ValueError, "should have either channels"): + with self.assertRaisesRegex(ValueError, "should have either channels"): source.transform(lambda r, g, b: (r, g, b), channels=4) - with self.assertRaisesRegexp(ValueError, "should have either channels"): + with self.assertRaisesRegex(ValueError, "should have either channels"): source.transform(lambda r, g, b: (r, g, b, 1)) with self.assertRaises(TypeError): diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 205a6ffdf..e7e639f05 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -41,7 +41,7 @@ class TestFileJpeg(PillowTestCase): def test_sanity(self): # internal version number - self.assertRegexpMatches(Image.core.jpeglib_version, r"\d+\.\d+$") + self.assertRegex(Image.core.jpeglib_version, r"\d+\.\d+$") im = Image.open(TEST_FILE) im.load() diff --git a/Tests/test_file_jpeg2k.py b/Tests/test_file_jpeg2k.py index 810e21a9d..71f15f7b8 100644 --- a/Tests/test_file_jpeg2k.py +++ b/Tests/test_file_jpeg2k.py @@ -31,7 +31,7 @@ class TestFileJpeg2k(PillowTestCase): def test_sanity(self): # Internal version number - self.assertRegexpMatches(Image.core.jp2klib_version, r'\d+\.\d+\.\d+$') + self.assertRegex(Image.core.jp2klib_version, r'\d+\.\d+\.\d+$') im = Image.open('Tests/images/test-card-lossless.jp2') px = im.load() diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 8cf109e70..abf3f2953 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -68,8 +68,7 @@ class TestFilePng(PillowTestCase): def test_sanity(self): # internal version number - self.assertRegexpMatches( - Image.core.zlib_version, r"\d+\.\d+\.\d+(\.\d+)?$") + self.assertRegex(Image.core.zlib_version, r"\d+\.\d+\.\d+(\.\d+)?$") test_file = self.tempfile("temp.png") diff --git a/Tests/test_image_resample.py b/Tests/test_image_resample.py index 4223db530..1ecb6cda6 100644 --- a/Tests/test_image_resample.py +++ b/Tests/test_image_resample.py @@ -383,24 +383,24 @@ class CoreResampleBoxTest(PillowTestCase): im.resize((32, 32), resample, (20, 20, 20, 100)) im.resize((32, 32), resample, (20, 20, 100, 20)) - with self.assertRaisesRegexp(TypeError, "must be sequence of length 4"): + with self.assertRaisesRegex(TypeError, "must be sequence of length 4"): im.resize((32, 32), resample, (im.width, im.height)) - with self.assertRaisesRegexp(ValueError, "can't be negative"): + with self.assertRaisesRegex(ValueError, "can't be negative"): im.resize((32, 32), resample, (-20, 20, 100, 100)) - with self.assertRaisesRegexp(ValueError, "can't be negative"): + with self.assertRaisesRegex(ValueError, "can't be negative"): im.resize((32, 32), resample, (20, -20, 100, 100)) - with self.assertRaisesRegexp(ValueError, "can't be empty"): + with self.assertRaisesRegex(ValueError, "can't be empty"): im.resize((32, 32), resample, (20.1, 20, 20, 100)) - with self.assertRaisesRegexp(ValueError, "can't be empty"): + with self.assertRaisesRegex(ValueError, "can't be empty"): im.resize((32, 32), resample, (20, 20.1, 100, 20)) - with self.assertRaisesRegexp(ValueError, "can't be empty"): + with self.assertRaisesRegex(ValueError, "can't be empty"): im.resize((32, 32), resample, (20.1, 20.1, 20, 20)) - with self.assertRaisesRegexp(ValueError, "can't exceed"): + with self.assertRaisesRegex(ValueError, "can't exceed"): im.resize((32, 32), resample, (0, 0, im.width + 1, im.height)) - with self.assertRaisesRegexp(ValueError, "can't exceed"): + with self.assertRaisesRegex(ValueError, "can't exceed"): im.resize((32, 32), resample, (0, 0, im.width, im.height + 1)) def resize_tiled(self, im, dst_size, xtiles, ytiles): @@ -447,7 +447,7 @@ class CoreResampleBoxTest(PillowTestCase): # error with box should be much smaller than without self.assert_image_similar(reference, with_box, 6) - with self.assertRaisesRegexp(AssertionError, "difference 29\."): + with self.assertRaisesRegex(AssertionError, "difference 29\."): self.assert_image_similar(reference, without_box, 5) def test_formats(self): @@ -490,7 +490,7 @@ class CoreResampleBoxTest(PillowTestCase): try: res = im.resize(size, Image.LANCZOS, box) self.assertEqual(res.size, size) - with self.assertRaisesRegexp(AssertionError, "difference \d"): + with self.assertRaisesRegex(AssertionError, "difference \d"): # check that the difference at least that much self.assert_image_similar(res, im.crop(box), 20) except AssertionError: diff --git a/Tests/test_imagecms.py b/Tests/test_imagecms.py index 31417564f..4138f455f 100644 --- a/Tests/test_imagecms.py +++ b/Tests/test_imagecms.py @@ -43,7 +43,7 @@ class TestImageCms(PillowTestCase): self.assertEqual(list(map(type, v)), [str, str, str, str]) # internal version number - self.assertRegexpMatches(ImageCms.core.littlecms_version, r"\d+\.\d+$") + self.assertRegex(ImageCms.core.littlecms_version, r"\d+\.\d+$") self.skip_missing() i = ImageCms.profileToProfile(hopper(), SRGB, SRGB) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 0ec49384e..986b0b5ef 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -75,8 +75,7 @@ class TestImageFont(PillowTestCase): layout_engine=self.LAYOUT_ENGINE) def test_sanity(self): - self.assertRegexpMatches( - ImageFont.core.freetype2_version, r"\d+\.\d+\.\d+$") + self.assertRegex(ImageFont.core.freetype2_version, r"\d+\.\d+\.\d+$") def test_font_properties(self): ttf = self.get_font() From f733482c0e22afd7a38afcfe4097c70b11c1909f Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 10 Apr 2018 15:24:58 +0300 Subject: [PATCH 2/4] Remove workaround for since-fixed scipy regression, to remove recent scipy DeprecationWarning --- Tests/test_scipy.py | 53 --------------------------------------------- src/PIL/Image.py | 2 -- 2 files changed, 55 deletions(-) delete mode 100644 Tests/test_scipy.py diff --git a/Tests/test_scipy.py b/Tests/test_scipy.py deleted file mode 100644 index 18c4403a0..000000000 --- a/Tests/test_scipy.py +++ /dev/null @@ -1,53 +0,0 @@ -from helper import unittest, PillowTestCase -from distutils.version import LooseVersion -try: - import numpy as np - from numpy.testing import assert_equal - - from scipy import misc - import scipy - HAS_SCIPY = True -except ImportError: - HAS_SCIPY = False - - -class Test_scipy_resize(PillowTestCase): - """ Tests for scipy regression in Pillow 2.6.0 - - Tests from https://github.com/scipy/scipy/blob/master/scipy/misc/pilutil.py - """ - - def setUp(self): - if not HAS_SCIPY: - self.skipTest("Scipy Required") - - def test_imresize(self): - im = np.random.random((10, 20)) - for T in np.sctypes['float'] + [float]: - # 1.1 rounds to below 1.1 for float16, 1.101 works - im1 = misc.imresize(im, T(1.101)) - self.assertEqual(im1.shape, (11, 22)) - - # this test fails prior to scipy 0.14.0b1 - # https://github.com/scipy/scipy/commit/855ff1fff805fb91840cf36b7082d18565fc8352 - @unittest.skipIf(HAS_SCIPY and - (LooseVersion(scipy.__version__) < LooseVersion('0.14.0')), - "Test fails on scipy < 0.14.0") - def test_imresize4(self): - im = np.array([[1, 2], - [3, 4]]) - res = np.array([[1., 1.25, 1.75, 2.], - [1.5, 1.75, 2.25, 2.5], - [2.5, 2.75, 3.25, 3.5], - [3., 3.25, 3.75, 4.]], dtype=np.float32) - # Check that resizing by target size, float and int are the same - im2 = misc.imresize(im, (4, 4), mode='F') # output size - im3 = misc.imresize(im, 2., mode='F') # fraction - im4 = misc.imresize(im, 200, mode='F') # percentage - assert_equal(im2, res) - assert_equal(im3, res) - assert_equal(im4, res) - - -if __name__ == '__main__': - unittest.main() diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 22ee46bfc..c4ff94b6d 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1725,8 +1725,6 @@ class Image(object): ): raise ValueError("unknown resampling filter") - size = tuple(size) - if box is None: box = (0, 0) + self.size else: From 7e8998a4523a77f51f666524f5a1e9f40425bfc2 Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 10 Apr 2018 17:08:32 +0300 Subject: [PATCH 3/4] ImageOps.box_blur is deprecated, use ImageFilter.BoxBlur instead --- Tests/test_box_blur.py | 4 ++-- Tests/test_imageops_usm.py | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Tests/test_box_blur.py b/Tests/test_box_blur.py index 622b842d0..2787dfc0d 100644 --- a/Tests/test_box_blur.py +++ b/Tests/test_box_blur.py @@ -1,6 +1,6 @@ from helper import unittest, PillowTestCase -from PIL import Image, ImageOps +from PIL import Image, ImageFilter sample = Image.new("L", (7, 5)) @@ -16,7 +16,7 @@ sample.putdata(sum([ class TestBoxBlurApi(PillowTestCase): def test_imageops_box_blur(self): - i = ImageOps.box_blur(sample, 1) + i = sample.filter(ImageFilter.BoxBlur(1)) self.assertEqual(i.mode, sample.mode) self.assertEqual(i.size, sample.size) self.assertIsInstance(i, Image.Image) diff --git a/Tests/test_imageops_usm.py b/Tests/test_imageops_usm.py index 3fb021fe7..2666d3482 100644 --- a/Tests/test_imageops_usm.py +++ b/Tests/test_imageops_usm.py @@ -17,6 +17,11 @@ class TestImageOpsUsm(PillowTestCase): self.assertEqual(i.mode, "RGB") self.assertEqual(i.size, (128, 128)) + i = self.assert_warning(DeprecationWarning, + ImageOps.box_blur, im, 1) + self.assertEqual(i.mode, "RGB") + self.assertEqual(i.size, (128, 128)) + i = self.assert_warning(DeprecationWarning, ImageOps.gblur, im, 2.0) self.assertEqual(i.mode, "RGB") From 43d9068fc3be182469abfbaf0f03df60ad437c4e Mon Sep 17 00:00:00 2001 From: Hugo Date: Sun, 15 Apr 2018 10:35:46 +0300 Subject: [PATCH 4/4] Revert conversion removal --- src/PIL/Image.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index c4ff94b6d..22ee46bfc 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1725,6 +1725,8 @@ class Image(object): ): raise ValueError("unknown resampling filter") + size = tuple(size) + if box is None: box = (0, 0) + self.size else: