diff --git a/Tests/check_imaging_leaks.py b/Tests/check_imaging_leaks.py index 2b9a9605b..a109b5cd5 100755 --- a/Tests/check_imaging_leaks.py +++ b/Tests/check_imaging_leaks.py @@ -2,17 +2,15 @@ from __future__ import division -import sys - from PIL import Image -from .helper import PillowTestCase, unittest +from .helper import PillowTestCase, is_win32, unittest min_iterations = 100 max_iterations = 10000 -@unittest.skipIf(sys.platform.startswith("win32"), "requires Unix or macOS") +@unittest.skipIf(is_win32(), "requires Unix or macOS") class TestImagingLeaks(PillowTestCase): def _get_mem_usage(self): from resource import getpagesize, getrusage, RUSAGE_SELF diff --git a/Tests/check_j2k_leaks.py b/Tests/check_j2k_leaks.py index 4614529ed..d9c6c68b7 100755 --- a/Tests/check_j2k_leaks.py +++ b/Tests/check_j2k_leaks.py @@ -1,9 +1,8 @@ -import sys from io import BytesIO from PIL import Image -from .helper import PillowTestCase, unittest +from .helper import PillowTestCase, is_win32, unittest # Limits for testing the leak mem_limit = 1024 * 1048576 @@ -13,7 +12,7 @@ codecs = dir(Image.core) test_file = "Tests/images/rgb_trns_ycbc.jp2" -@unittest.skipIf(sys.platform.startswith("win32"), "requires Unix or macOS") +@unittest.skipIf(is_win32(), "requires Unix or macOS") class TestJpegLeaks(PillowTestCase): def setUp(self): if "jpeg2k_encoder" not in codecs or "jpeg2k_decoder" not in codecs: diff --git a/Tests/check_jpeg_leaks.py b/Tests/check_jpeg_leaks.py index 2f758ba10..8cb93f2dd 100644 --- a/Tests/check_jpeg_leaks.py +++ b/Tests/check_jpeg_leaks.py @@ -1,7 +1,6 @@ -import sys from io import BytesIO -from .helper import PillowTestCase, hopper, unittest +from .helper import PillowTestCase, hopper, is_win32, unittest iterations = 5000 @@ -15,7 +14,7 @@ valgrind --tool=massif python test-installed.py -s -v Tests/check_jpeg_leaks.py """ -@unittest.skipIf(sys.platform.startswith("win32"), "requires Unix or macOS") +@unittest.skipIf(is_win32(), "requires Unix or macOS") class TestJpegLeaks(PillowTestCase): """ diff --git a/Tests/helper.py b/Tests/helper.py index c352f54b9..312c6aa4e 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -368,6 +368,14 @@ def on_ci(): ) +def is_win32(): + return sys.platform.startswith("win32") + + +def is_pypy(): + return hasattr(sys, "pypy_translation_info") + + if sys.platform == "win32": IMCONVERT = os.environ.get("MAGICK_HOME", "") if IMCONVERT: diff --git a/Tests/test_core_resources.py b/Tests/test_core_resources.py index eefb1a0ef..0aedb91c9 100644 --- a/Tests/test_core_resources.py +++ b/Tests/test_core_resources.py @@ -4,9 +4,7 @@ import sys from PIL import Image -from .helper import PillowTestCase, unittest - -is_pypy = hasattr(sys, "pypy_version_info") +from .helper import PillowTestCase, is_pypy, unittest class TestCoreStats(PillowTestCase): @@ -87,7 +85,7 @@ class TestCoreMemory(PillowTestCase): stats = Image.core.get_stats() self.assertGreaterEqual(stats["new_count"], 1) self.assertGreaterEqual(stats["allocated_blocks"], 64) - if not is_pypy: + if not is_pypy(): self.assertGreaterEqual(stats["freed_blocks"], 64) def test_get_blocks_max(self): @@ -108,7 +106,7 @@ class TestCoreMemory(PillowTestCase): if sys.maxsize < 2 ** 32: self.assertRaises(ValueError, Image.core.set_blocks_max, 2 ** 29) - @unittest.skipIf(is_pypy, "images are not collected") + @unittest.skipIf(is_pypy(), "images are not collected") def test_set_blocks_max_stats(self): Image.core.reset_stats() Image.core.set_blocks_max(128) @@ -123,7 +121,7 @@ class TestCoreMemory(PillowTestCase): self.assertEqual(stats["freed_blocks"], 0) self.assertEqual(stats["blocks_cached"], 64) - @unittest.skipIf(is_pypy, "images are not collected") + @unittest.skipIf(is_pypy(), "images are not collected") def test_clear_cache_stats(self): Image.core.reset_stats() Image.core.clear_cache() @@ -153,7 +151,7 @@ class TestCoreMemory(PillowTestCase): self.assertGreaterEqual(stats["allocated_blocks"], 16) self.assertGreaterEqual(stats["reused_blocks"], 0) self.assertEqual(stats["blocks_cached"], 0) - if not is_pypy: + if not is_pypy(): self.assertGreaterEqual(stats["freed_blocks"], 16) diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 7f9bf7c1d..5a34a3faa 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -1,10 +1,16 @@ import os -import sys from io import BytesIO from PIL import Image, ImageFile, JpegImagePlugin -from .helper import PillowTestCase, cjpeg_available, djpeg_available, hopper, unittest +from .helper import ( + PillowTestCase, + cjpeg_available, + djpeg_available, + hopper, + is_win32, + unittest, +) codecs = dir(Image.core) @@ -654,7 +660,7 @@ class TestFileJpeg(PillowTestCase): self.assertNotIn("photoshop", im.info) -@unittest.skipUnless(sys.platform.startswith("win32"), "Windows only") +@unittest.skipUnless(is_win32(), "Windows only") class TestFileCloseW32(PillowTestCase): def setUp(self): if "jpeg_encoder" not in codecs or "jpeg_decoder" not in codecs: diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 6d76a6caa..07e84ef72 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -1,11 +1,10 @@ -import sys import zlib from io import BytesIO from PIL import Image, ImageFile, PngImagePlugin from PIL._util import py3 -from .helper import PillowLeakTestCase, PillowTestCase, hopper, unittest +from .helper import PillowLeakTestCase, PillowTestCase, hopper, is_win32, unittest try: from PIL import _webp @@ -650,7 +649,7 @@ class TestFilePng(PillowTestCase): self.assert_image_similar(im, expected, 0.23) -@unittest.skipIf(sys.platform.startswith("win32"), "requires Unix or macOS") +@unittest.skipIf(is_win32(), "requires Unix or macOS") class TestTruncatedPngPLeaks(PillowLeakTestCase): mem_limit = 2 * 1024 # max increase in K iterations = 100 # Leak is 56k/iteration, this will leak 5.6megs diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index c3f55150c..c8a4c2df3 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -1,12 +1,11 @@ import logging -import sys from io import BytesIO from PIL import Image, TiffImagePlugin, features from PIL._util import py3 from PIL.TiffImagePlugin import RESOLUTION_UNIT, X_RESOLUTION, Y_RESOLUTION -from .helper import PillowTestCase, hopper, unittest +from .helper import PillowTestCase, hopper, is_win32, unittest logger = logging.getLogger(__name__) @@ -612,7 +611,7 @@ class TestFileTiff(PillowTestCase): im.load() -@unittest.skipUnless(sys.platform.startswith("win32"), "Windows only") +@unittest.skipUnless(is_win32(), "Windows only") class TestFileTiffW32(PillowTestCase): def test_fd_leak(self): tmpfile = self.tempfile("temp.tif") diff --git a/Tests/test_font_leaks.py b/Tests/test_font_leaks.py index 14b368585..9b5fe2055 100644 --- a/Tests/test_font_leaks.py +++ b/Tests/test_font_leaks.py @@ -1,13 +1,11 @@ from __future__ import division -import sys - from PIL import Image, ImageDraw, ImageFont, features -from .helper import PillowLeakTestCase, unittest +from .helper import PillowLeakTestCase, is_win32, unittest -@unittest.skipIf(sys.platform.startswith("win32"), "requires Unix or macOS") +@unittest.skipIf(is_win32(), "requires Unix or macOS") class TestTTypeFontLeak(PillowLeakTestCase): # fails at iteration 3 in master iterations = 10 diff --git a/Tests/test_image.py b/Tests/test_image.py index 493b4735a..cbf52b3b4 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -1,11 +1,10 @@ import os import shutil -import sys from PIL import Image from PIL._util import py3 -from .helper import PillowTestCase, hopper, unittest +from .helper import PillowTestCase, hopper, is_win32, unittest class TestImage(PillowTestCase): @@ -150,9 +149,7 @@ class TestImage(PillowTestCase): im.paste(0, (0, 0, 100, 100)) self.assertFalse(im.readonly) - @unittest.skipIf( - sys.platform.startswith("win32"), "Test requires opening tempfile twice" - ) + @unittest.skipIf(is_win32(), "Test requires opening tempfile twice") def test_readonly_save(self): temp_file = self.tempfile("temp.bmp") shutil.copy("Tests/images/rgb32bf-rgba.bmp", temp_file) diff --git a/Tests/test_image_access.py b/Tests/test_image_access.py index b69c2040f..577df3dff 100644 --- a/Tests/test_image_access.py +++ b/Tests/test_image_access.py @@ -3,7 +3,7 @@ import sys from PIL import Image -from .helper import PillowTestCase, hopper, on_ci, unittest +from .helper import PillowTestCase, hopper, is_win32, on_ci, unittest # CFFI imports pycparser which doesn't support PYTHONOPTIMIZE=2 # https://github.com/eliben/pycparser/pull/198#issuecomment-317001670 @@ -333,7 +333,7 @@ class TestCffi(AccessTest): class TestEmbeddable(unittest.TestCase): @unittest.skipIf( - not sys.platform.startswith("win32") or on_ci(), + not is_win32() or on_ci(), "Failing on AppVeyor / GitHub Actions when run from subprocess, not from shell", ) def test_embeddable(self): diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 6a2d572a9..6944404c1 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -9,7 +9,7 @@ from io import BytesIO from PIL import Image, ImageDraw, ImageFont, features -from .helper import PillowTestCase, unittest +from .helper import PillowTestCase, is_pypy, is_win32, unittest FONT_PATH = "Tests/fonts/FreeMono.ttf" FONT_SIZE = 20 @@ -464,10 +464,7 @@ class TestImageFont(PillowTestCase): with self.assertRaises(UnicodeEncodeError): font.getsize(u"’") - @unittest.skipIf( - sys.version.startswith("2") or hasattr(sys, "pypy_translation_info"), - "requires CPython 3.3+", - ) + @unittest.skipIf(sys.version.startswith("2") or is_pypy(), "requires CPython 3.3+") def test_unicode_extended(self): # issue #3777 text = u"A\u278A\U0001F12B" @@ -504,7 +501,7 @@ class TestImageFont(PillowTestCase): name = font.getname() self.assertEqual(("FreeMono", "Regular"), name) - @unittest.skipIf(sys.platform.startswith("win32"), "requires Unix or macOS") + @unittest.skipIf(is_win32(), "requires Unix or macOS") def test_find_linux_font(self): # A lot of mocking here - this is more for hitting code and # catching syntax like errors @@ -550,7 +547,7 @@ class TestImageFont(PillowTestCase): font_directory + "/Duplicate.ttf", "Duplicate" ) - @unittest.skipIf(sys.platform.startswith("win32"), "requires Unix or macOS") + @unittest.skipIf(is_win32(), "requires Unix or macOS") def test_find_macos_font(self): # Like the linux test, more cover hitting code rather than testing # correctness. diff --git a/Tests/test_imageshow.py b/Tests/test_imageshow.py index 1c756da9f..aac48d6c0 100644 --- a/Tests/test_imageshow.py +++ b/Tests/test_imageshow.py @@ -1,8 +1,6 @@ -import sys - from PIL import Image, ImageShow -from .helper import PillowTestCase, hopper, on_ci, on_github_actions, unittest +from .helper import PillowTestCase, hopper, is_win32, on_ci, on_github_actions, unittest class TestImageShow(PillowTestCase): @@ -37,7 +35,7 @@ class TestImageShow(PillowTestCase): ImageShow._viewers.pop(0) @unittest.skipUnless( - on_ci() and not (sys.platform == "win32" and on_github_actions()), + on_ci() and not (is_win32() and on_github_actions()), "Only run on CIs; hangs on Windows on GitHub Actions", ) def test_show(self): diff --git a/Tests/test_imagewin.py b/Tests/test_imagewin.py index 92fcdc28d..2b2034187 100644 --- a/Tests/test_imagewin.py +++ b/Tests/test_imagewin.py @@ -1,8 +1,6 @@ -import sys - from PIL import ImageWin -from .helper import PillowTestCase, hopper, unittest +from .helper import PillowTestCase, hopper, is_win32, unittest class TestImageWin(PillowTestCase): @@ -32,7 +30,7 @@ class TestImageWin(PillowTestCase): self.assertEqual(wnd2, 50) -@unittest.skipUnless(sys.platform.startswith("win32"), "Windows only") +@unittest.skipUnless(is_win32(), "Windows only") class TestImageWinDib(PillowTestCase): def test_dib_image(self): # Arrange diff --git a/Tests/test_imagewin_pointers.py b/Tests/test_imagewin_pointers.py index efa3753b9..d7c22a209 100644 --- a/Tests/test_imagewin_pointers.py +++ b/Tests/test_imagewin_pointers.py @@ -1,14 +1,13 @@ import ctypes -import sys from io import BytesIO from PIL import Image, ImageWin -from .helper import PillowTestCase, hopper +from .helper import PillowTestCase, hopper, is_win32 # see https://github.com/python-pillow/Pillow/pull/1431#issuecomment-144692652 -if sys.platform.startswith("win32"): +if is_win32(): import ctypes.wintypes class BITMAPFILEHEADER(ctypes.Structure): diff --git a/Tests/test_main.py b/Tests/test_main.py index 936a938f9..c6f9a694e 100644 --- a/Tests/test_main.py +++ b/Tests/test_main.py @@ -5,14 +5,12 @@ import subprocess import sys from unittest import TestCase -from .helper import on_github_actions, unittest +from .helper import is_pypy, is_win32, on_github_actions, unittest class TestMain(TestCase): @unittest.skipIf( - sys.platform == "win32" - and hasattr(sys, "pypy_translation_info") - and on_github_actions(), + is_win32() and is_pypy() and on_github_actions(), "Failing on Windows on GitHub Actions running PyPy", ) def test_main(self): diff --git a/Tests/test_map.py b/Tests/test_map.py index 3fc42651b..8d4e32219 100644 --- a/Tests/test_map.py +++ b/Tests/test_map.py @@ -2,7 +2,7 @@ import sys from PIL import Image -from .helper import PillowTestCase, unittest +from .helper import PillowTestCase, is_win32, unittest try: import numpy @@ -10,7 +10,7 @@ except ImportError: numpy = None -@unittest.skipIf(sys.platform.startswith("win32"), "Win32 does not call map_buffer") +@unittest.skipIf(is_win32(), "Win32 does not call map_buffer") class TestMap(PillowTestCase): def test_overflow(self): # There is the potential to overflow comparisons in map.c diff --git a/Tests/test_shell_injection.py b/Tests/test_shell_injection.py index 35a3dcfcd..97774a0a4 100644 --- a/Tests/test_shell_injection.py +++ b/Tests/test_shell_injection.py @@ -1,5 +1,4 @@ import shutil -import sys from PIL import GifImagePlugin, Image, JpegImagePlugin @@ -7,6 +6,7 @@ from .helper import ( PillowTestCase, cjpeg_available, djpeg_available, + is_win32, netpbm_available, unittest, ) @@ -17,7 +17,7 @@ TEST_GIF = "Tests/images/hopper.gif" test_filenames = ("temp_';", 'temp_";', "temp_'\"|", "temp_'\"||", "temp_'\"&&") -@unittest.skipIf(sys.platform.startswith("win32"), "requires Unix or macOS") +@unittest.skipIf(is_win32(), "requires Unix or macOS") class TestShellInjection(PillowTestCase): def assert_save_filename_check(self, src_img, save_func): for filename in test_filenames: