From afa758eb33bd9ca02e5c680fc0ee48590c47280b Mon Sep 17 00:00:00 2001 From: Hugo Date: Sun, 29 Mar 2020 18:36:37 +0300 Subject: [PATCH 1/4] Re-add deprecated PILLOW_VERSION to give projects more time to upgrade --- docs/deprecations.rst | 18 +++++++++++------- docs/releasenotes/7.1.0.rst | 9 +++++++++ src/PIL/Image.py | 14 ++++++++++++-- src/PIL/__init__.py | 4 ++-- 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/docs/deprecations.rst b/docs/deprecations.rst index 1c4fcdc45da..227a5bc823e 100644 --- a/docs/deprecations.rst +++ b/docs/deprecations.rst @@ -12,6 +12,17 @@ Deprecated features Below are features which are considered deprecated. Where appropriate, a ``DeprecationWarning`` is issued. +PILLOW_VERSION constant +~~~~~~~~~~~~~~~~~~~~~~~ + +.. deprecated:: 5.2.0 + +``PILLOW_VERSION`` has been deprecated and will be removed in a future release. Use +``__version__`` instead. + +It was initially removed in Pillow 7.0.0, but brought back in 7.1.0 to give projects +more time to upgrade. + ImageCms.CmsProfile attributes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -69,13 +80,6 @@ Use instead: with Image.open("hopper.png") as im: im.save("out.jpg") -PILLOW_VERSION constant -~~~~~~~~~~~~~~~~~~~~~~~ - -*Removed in version 7.0.0.* - -``PILLOW_VERSION`` has been removed. Use ``__version__`` instead. - PIL.*ImagePlugin.__version__ attributes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/docs/releasenotes/7.1.0.rst b/docs/releasenotes/7.1.0.rst index e3bc107ddff..adc0fefcffa 100644 --- a/docs/releasenotes/7.1.0.rst +++ b/docs/releasenotes/7.1.0.rst @@ -27,6 +27,15 @@ Reading JPEG comments When opening a JPEG image, the comment may now be read into :py:attr:`~PIL.Image.Image.info`. +PILLOW_VERSION constant +^^^^^^^^^^^^^^^^^^^^^^^ + +``PILLOW_VERSION`` has been re-added but is deprecated and will be removed in a future +release. Use ``__version__`` instead. + +It was initially removed in Pillow 7.0.0, but brought back in 7.1.0 to give projects +more time to upgrade. + Other Changes ============= diff --git a/src/PIL/Image.py b/src/PIL/Image.py index b1e8ad3ea06..9328d44a073 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -39,12 +39,22 @@ from pathlib import Path # VERSION was removed in Pillow 6.0.0. -# PILLOW_VERSION was removed in Pillow 7.0.0. +# PILLOW_VERSION is deprecated and will be removed in a future release. # Use __version__ instead. -from . import ImageMode, TiffTags, UnidentifiedImageError, __version__, _plugins +from . import ( + PILLOW_VERSION, + ImageMode, + TiffTags, + UnidentifiedImageError, + __version__, + _plugins, +) from ._binary import i8, i32le from ._util import deferred_error, isPath +# Silence warning +assert PILLOW_VERSION + logger = logging.getLogger(__name__) diff --git a/src/PIL/__init__.py b/src/PIL/__init__.py index e7f26488d6b..d459fa45df7 100644 --- a/src/PIL/__init__.py +++ b/src/PIL/__init__.py @@ -16,9 +16,9 @@ from . import _version # VERSION was removed in Pillow 6.0.0. -# PILLOW_VERSION was removed in Pillow 7.0.0. +# PILLOW_VERSION is deprecated and will be removed in a future release. # Use __version__ instead. -__version__ = _version.__version__ +PILLOW_VERSION = __version__ = _version.__version__ del _version From 027d180eda4e7ae2c21eea051feb8da09e9ad8c2 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 30 Mar 2020 21:53:02 +1100 Subject: [PATCH 2/4] Raise a DeprecationWarning when comparing PILLOW_VERSION --- Tests/test_image.py | 8 ++++++++ src/PIL/__init__.py | 25 ++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 55e70a32641..520364c7d7c 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -3,6 +3,7 @@ import shutil import tempfile +import PIL import pytest from PIL import Image, ImageDraw, ImagePalette, UnidentifiedImageError @@ -608,6 +609,13 @@ def act(fp): assert not fp.closed + def test_pillow_version(self): + with pytest.warns(DeprecationWarning): + assert PIL.__version__ == PIL.PILLOW_VERSION + + with pytest.warns(DeprecationWarning): + assert int(PIL.PILLOW_VERSION[0]) >= 7 + def test_overrun(self): for file in [ "fli_overrun.bin", diff --git a/src/PIL/__init__.py b/src/PIL/__init__.py index d459fa45df7..3de278ded90 100644 --- a/src/PIL/__init__.py +++ b/src/PIL/__init__.py @@ -13,12 +13,35 @@ ;-) """ +import warnings + from . import _version # VERSION was removed in Pillow 6.0.0. +__version__ = _version.__version__ + + +class _Deprecated_Version(str): + def _raise_warning(self): + warnings.warn( + "PILLOW_VERSION is deprecated and will be removed in a future release. " + "Use __version__ instead.", + DeprecationWarning, + stacklevel=3, + ) + + def __getitem__(self, key): + self._raise_warning() + return super().__getitem__(key) + + def __eq__(self, other): + self._raise_warning() + return super().__eq__(other) + + # PILLOW_VERSION is deprecated and will be removed in a future release. # Use __version__ instead. -PILLOW_VERSION = __version__ = _version.__version__ +PILLOW_VERSION = _Deprecated_Version(__version__) del _version From 7597a9fbfdfe5e347b961ad6de6019ab6f39afd5 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 31 Mar 2020 17:41:47 +1100 Subject: [PATCH 3/4] Raise warning for more operations --- Tests/test_image.py | 20 ++++++++++++- src/PIL/Image.py | 20 +++++++++++-- src/PIL/__init__.py | 70 ++++++++++++++++++++++++++++++++++----------- 3 files changed, 89 insertions(+), 21 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 520364c7d7c..9fb18b195b6 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -611,11 +611,29 @@ def act(fp): def test_pillow_version(self): with pytest.warns(DeprecationWarning): - assert PIL.__version__ == PIL.PILLOW_VERSION + assert PIL.PILLOW_VERSION == PIL.__version__ + + with pytest.warns(DeprecationWarning): + str(PIL.PILLOW_VERSION) with pytest.warns(DeprecationWarning): assert int(PIL.PILLOW_VERSION[0]) >= 7 + with pytest.warns(DeprecationWarning): + assert PIL.PILLOW_VERSION < "9.9.0" + + with pytest.warns(DeprecationWarning): + assert PIL.PILLOW_VERSION <= "9.9.0" + + with pytest.warns(DeprecationWarning): + assert PIL.PILLOW_VERSION != "7.0.0" + + with pytest.warns(DeprecationWarning): + assert PIL.PILLOW_VERSION >= "7.0.0" + + with pytest.warns(DeprecationWarning): + assert PIL.PILLOW_VERSION > "7.0.0" + def test_overrun(self): for file in [ "fli_overrun.bin", diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 9328d44a073..7b96b14f487 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -42,18 +42,32 @@ # PILLOW_VERSION is deprecated and will be removed in a future release. # Use __version__ instead. from . import ( - PILLOW_VERSION, ImageMode, TiffTags, UnidentifiedImageError, __version__, _plugins, + _raise_version_warning, ) from ._binary import i8, i32le from ._util import deferred_error, isPath -# Silence warning -assert PILLOW_VERSION +if sys.version_info >= (3, 7): + + def __getattr__(name): + if name == "PILLOW_VERSION": + _raise_version_warning() + return __version__ + raise AttributeError("module '{}' has no attribute '{}'".format(__name__, name)) + + +else: + + from . import PILLOW_VERSION + + # Silence warning + assert PILLOW_VERSION + logger = logging.getLogger(__name__) diff --git a/src/PIL/__init__.py b/src/PIL/__init__.py index 3de278ded90..f9cb15772dd 100644 --- a/src/PIL/__init__.py +++ b/src/PIL/__init__.py @@ -13,6 +13,7 @@ ;-) """ +import sys import warnings from . import _version @@ -21,27 +22,62 @@ __version__ = _version.__version__ -class _Deprecated_Version(str): - def _raise_warning(self): - warnings.warn( - "PILLOW_VERSION is deprecated and will be removed in a future release. " - "Use __version__ instead.", - DeprecationWarning, - stacklevel=3, - ) +# PILLOW_VERSION is deprecated and will be removed in a future release. +# Use __version__ instead. +def _raise_version_warning(): + warnings.warn( + "PILLOW_VERSION is deprecated and will be removed in a future release. " + "Use __version__ instead.", + DeprecationWarning, + stacklevel=3, + ) - def __getitem__(self, key): - self._raise_warning() - return super().__getitem__(key) - def __eq__(self, other): - self._raise_warning() - return super().__eq__(other) +if sys.version_info >= (3, 7): + def __getattr__(name): + if name == "PILLOW_VERSION": + _raise_version_warning() + return __version__ + raise AttributeError("module '{}' has no attribute '{}'".format(__name__, name)) -# PILLOW_VERSION is deprecated and will be removed in a future release. -# Use __version__ instead. -PILLOW_VERSION = _Deprecated_Version(__version__) + +else: + + class _Deprecated_Version(str): + def __str__(self): + _raise_version_warning() + return super().__str__() + + def __getitem__(self, key): + _raise_version_warning() + return super().__getitem__(key) + + def __eq__(self, other): + _raise_version_warning() + return super().__eq__(other) + + def __ne__(self, other): + _raise_version_warning() + return super().__ne__(other) + + def __gt__(self, other): + _raise_version_warning() + return super().__gt__(other) + + def __lt__(self, other): + _raise_version_warning() + return super().__lt__(other) + + def __ge__(self, other): + _raise_version_warning() + return super().__gt__(other) + + def __le__(self, other): + _raise_version_warning() + return super().__lt__(other) + + PILLOW_VERSION = _Deprecated_Version(__version__) del _version From 750bbc72569cd36ca4219a369e84c761d6e9eec8 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 31 Mar 2020 17:25:26 +1100 Subject: [PATCH 4/4] Parametrized test --- Tests/test_image.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 9fb18b195b6..b0fd7c5403c 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -609,30 +609,33 @@ def act(fp): assert not fp.closed - def test_pillow_version(self): + @pytest.mark.parametrize( + "test_module", [PIL, Image], + ) + def test_pillow_version(self, test_module): with pytest.warns(DeprecationWarning): - assert PIL.PILLOW_VERSION == PIL.__version__ + assert test_module.PILLOW_VERSION == PIL.__version__ with pytest.warns(DeprecationWarning): - str(PIL.PILLOW_VERSION) + str(test_module.PILLOW_VERSION) with pytest.warns(DeprecationWarning): - assert int(PIL.PILLOW_VERSION[0]) >= 7 + assert int(test_module.PILLOW_VERSION[0]) >= 7 with pytest.warns(DeprecationWarning): - assert PIL.PILLOW_VERSION < "9.9.0" + assert test_module.PILLOW_VERSION < "9.9.0" with pytest.warns(DeprecationWarning): - assert PIL.PILLOW_VERSION <= "9.9.0" + assert test_module.PILLOW_VERSION <= "9.9.0" with pytest.warns(DeprecationWarning): - assert PIL.PILLOW_VERSION != "7.0.0" + assert test_module.PILLOW_VERSION != "7.0.0" with pytest.warns(DeprecationWarning): - assert PIL.PILLOW_VERSION >= "7.0.0" + assert test_module.PILLOW_VERSION >= "7.0.0" with pytest.warns(DeprecationWarning): - assert PIL.PILLOW_VERSION > "7.0.0" + assert test_module.PILLOW_VERSION > "7.0.0" def test_overrun(self): for file in [