From 970ea7d3c42cce231494e5a5fe35e370f38b1601 Mon Sep 17 00:00:00 2001 From: Hugo Date: Wed, 4 Jul 2018 16:23:38 +0300 Subject: [PATCH 1/8] Test two arbitrarily chosen jobs with PYTHONOPTIMIZE --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 23225dbbb..b5adc2da7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,8 +30,10 @@ matrix: - python: '3.5' - python: '3.5' dist: trusty + env: PYTHONOPTIMIZE=1 - python: '3.4' dist: trusty + env: PYTHONOPTIMIZE=2 - env: DOCKER="alpine" DOCKER_TAG="pytest" - env: DOCKER="arch" DOCKER_TAG="pytest" # contains PyQt5 - env: DOCKER="ubuntu-trusty-x86" DOCKER_TAG="pytest" From fc8717fb040a135b91be58af3c4faf86d9765516 Mon Sep 17 00:00:00 2001 From: Hugo Date: Wed, 4 Jul 2018 16:28:02 +0300 Subject: [PATCH 2/8] Remove docstring formatting for when PYTHONOPTIMIZE=2 --- src/PIL/__init__.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/PIL/__init__.py b/src/PIL/__init__.py index a07280e31..bc8cfed8c 100644 --- a/src/PIL/__init__.py +++ b/src/PIL/__init__.py @@ -1,4 +1,4 @@ -"""Pillow {} (Fork of the Python Imaging Library) +"""Pillow (Fork of the Python Imaging Library) Pillow is the friendly PIL fork by Alex Clark and Contributors. https://github.com/python-pillow/Pillow/ @@ -24,8 +24,6 @@ PILLOW_VERSION = __version__ = _version.__version__ del _version -__doc__ = __doc__.format(__version__) # include version in docstring - _plugins = ['BlpImagePlugin', 'BmpImagePlugin', From d7e4d3db3fd73995639b72dfd55c4fcce1938c90 Mon Sep 17 00:00:00 2001 From: Hugo Date: Wed, 4 Jul 2018 16:47:48 +0300 Subject: [PATCH 3/8] Convert assert into exception --- Tests/test_imagefont.py | 2 +- src/PIL/ImageDraw.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index f2116bdc4..82108638c 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -215,7 +215,7 @@ class TestImageFont(PillowTestCase): # Act/Assert self.assertRaises( - AssertionError, + ValueError, draw.multiline_text, (0, 0), TEST_TEXT, font=ttf, align="unknown") def test_draw_align(self): diff --git a/src/PIL/ImageDraw.py b/src/PIL/ImageDraw.py index ca8c1d17b..a427e327d 100644 --- a/src/PIL/ImageDraw.py +++ b/src/PIL/ImageDraw.py @@ -246,7 +246,7 @@ class ImageDraw(object): elif align == "right": left += (max_width - widths[idx]) else: - assert False, 'align must be "left", "center" or "right"' + raise ValueError('align must be "left", "center" or "right"') self.text((left, top), line, fill, font, anchor, direction=direction, features=features) top += line_spacing From 4218a769d79c0f07a9ef1b67d7ace52bad88a835 Mon Sep 17 00:00:00 2001 From: Hugo Date: Wed, 4 Jul 2018 18:01:24 +0300 Subject: [PATCH 4/8] Refactor cffi import and skipping --- Tests/test_image_access.py | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/Tests/test_image_access.py b/Tests/test_image_access.py index 7a9378bbd..cdce3fd49 100644 --- a/Tests/test_image_access.py +++ b/Tests/test_image_access.py @@ -2,8 +2,9 @@ from helper import unittest, PillowTestCase, hopper, on_appveyor try: from PIL import PyAccess + import cffi except ImportError: - # Skip in setUp() + cffi = None pass from PIL import Image @@ -113,38 +114,20 @@ class TestImageGetPixel(AccessTest): self.check(mode, 2**16-1) +@unittest.skipIf(cffi is None, "No cffi") class TestCffiPutPixel(TestImagePutPixel): _need_cffi_access = True - def setUp(self): - try: - import cffi - assert cffi # silence warning - except ImportError: - self.skipTest("No cffi") - +@unittest.skipIf(cffi is None, "No cffi") class TestCffiGetPixel(TestImageGetPixel): _need_cffi_access = True - def setUp(self): - try: - import cffi - assert cffi # silence warning - except ImportError: - self.skipTest("No cffi") - +@unittest.skipIf(cffi is None, "No cffi") class TestCffi(AccessTest): _need_cffi_access = True - def setUp(self): - try: - import cffi - assert cffi # silence warning - except ImportError: - self.skipTest("No cffi") - def _test_get_access(self, im): """Do we get the same thing as the old pixel access From 663b06e223bfa445d0c81b2d3e5923a66887d1a9 Mon Sep 17 00:00:00 2001 From: Hugo Date: Wed, 4 Jul 2018 18:26:09 +0300 Subject: [PATCH 5/8] Skip: CFFI imports pycparser which doesn't support PYTHONOPTIMIZE=2 --- Tests/test_image_access.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/Tests/test_image_access.py b/Tests/test_image_access.py index cdce3fd49..f87e75071 100644 --- a/Tests/test_image_access.py +++ b/Tests/test_image_access.py @@ -1,16 +1,21 @@ from helper import unittest, PillowTestCase, hopper, on_appveyor -try: - from PIL import PyAccess - import cffi -except ImportError: - cffi = None - pass - from PIL import Image import sys import os +# CFFI imports pycparser which doesn't support PYTHONOPTIMIZE=2 +# https://github.com/eliben/pycparser/pull/198#issuecomment-317001670 +if os.environ.get("PYTHONOPTIMIZE") == "2": + cffi = None +else: + try: + from PIL import PyAccess + import cffi + except ImportError: + cffi = None + pass + class AccessTest(PillowTestCase): # initial value From 29b2c6e23dc98cd941ae7cca9f65041855143b09 Mon Sep 17 00:00:00 2001 From: Hugo Date: Wed, 4 Jul 2018 18:43:39 +0300 Subject: [PATCH 6/8] Only import cffi where needed, to avoid problems with PYTHONOPTIMIZE=2 --- src/PIL/ImageTk.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/PIL/ImageTk.py b/src/PIL/ImageTk.py index 17bf32f62..d58ee8528 100644 --- a/src/PIL/ImageTk.py +++ b/src/PIL/ImageTk.py @@ -32,13 +32,6 @@ if sys.version_info.major > 2: else: import Tkinter as tkinter -# required for pypy, which always has cffi installed -try: - from cffi import FFI - ffi = FFI() -except ImportError: - pass - from . import Image from io import BytesIO @@ -192,7 +185,11 @@ class PhotoImage(object): from . import _imagingtk try: if hasattr(tk, 'interp'): - # Pypy is using a ffi cdata element + # Required for PyPy, which always has CFFI installed + from cffi import FFI + ffi = FFI() + + # Pypy is using an FFI cdata element # (Pdb) self.tk.interp # _imagingtk.tkinit( From 847581f7dd752231e8ca9e72189fa4506d2041ac Mon Sep 17 00:00:00 2001 From: Hugo Date: Sat, 14 Jul 2018 09:35:42 +0300 Subject: [PATCH 7/8] Remove redundant 'pass' --- Tests/test_image_access.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Tests/test_image_access.py b/Tests/test_image_access.py index f87e75071..a7e39a499 100644 --- a/Tests/test_image_access.py +++ b/Tests/test_image_access.py @@ -14,7 +14,6 @@ else: import cffi except ImportError: cffi = None - pass class AccessTest(PillowTestCase): From 2d6f0f77dafacba1f20b2a309476c3d5c11fe22e Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 14 Aug 2018 12:34:40 +0300 Subject: [PATCH 8/8] Fix typo --- src/PIL/ImageTk.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PIL/ImageTk.py b/src/PIL/ImageTk.py index d58ee8528..c56f5560a 100644 --- a/src/PIL/ImageTk.py +++ b/src/PIL/ImageTk.py @@ -189,7 +189,7 @@ class PhotoImage(object): from cffi import FFI ffi = FFI() - # Pypy is using an FFI cdata element + # PyPy is using an FFI CDATA element # (Pdb) self.tk.interp # _imagingtk.tkinit(