From 2ac183b73363b426cc5a4ec25aa76adbfe2cfbb5 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 19 Feb 2022 14:32:13 +1100 Subject: [PATCH] Deprecated FitsStubImagePlugin --- Tests/test_file_fits.py | 37 +++++++++++++++- docs/deprecations.rst | 9 ++++ docs/releasenotes/9.1.0.rst | 9 ++++ src/PIL/FitsStubImagePlugin.py | 77 ++++++++++++++++++++++++++++++++++ src/PIL/__init__.py | 1 + 5 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 src/PIL/FitsStubImagePlugin.py diff --git a/Tests/test_file_fits.py b/Tests/test_file_fits.py index 68d708fb173..447888acd8d 100644 --- a/Tests/test_file_fits.py +++ b/Tests/test_file_fits.py @@ -2,7 +2,7 @@ import pytest -from PIL import FitsImagePlugin, Image +from PIL import FitsImagePlugin, FitsStubImagePlugin, Image from .helper import assert_image_equal, hopper @@ -43,3 +43,38 @@ def test_naxis_zero(): with pytest.raises(ValueError): with Image.open("Tests/images/hopper_naxis_zero.fits"): pass + + +def test_stub_deprecated(): + class Handler: + opened = False + loaded = False + + def open(self, im): + self.opened = True + + def load(self, im): + self.loaded = True + return Image.new("RGB", (1, 1)) + + handler = Handler() + with pytest.warns(DeprecationWarning): + FitsStubImagePlugin.register_handler(handler) + + with Image.open(TEST_FILE) as im: + assert im.format == "FITS" + assert im.size == (128, 128) + assert im.mode == "L" + + assert handler.opened + assert not handler.loaded + + im.load() + assert handler.loaded + + FitsStubImagePlugin._handler = None + Image.register_open( + FitsImagePlugin.FitsImageFile.format, + FitsImagePlugin.FitsImageFile, + FitsImagePlugin._accept, + ) diff --git a/docs/deprecations.rst b/docs/deprecations.rst index a3abe81fa34..b3021a8714d 100644 --- a/docs/deprecations.rst +++ b/docs/deprecations.rst @@ -66,6 +66,15 @@ In effect, ``viewer.show_file("test.jpg")`` will continue to work unchanged. ``viewer.show_file(file="test.jpg")`` will raise a deprecation warning, and suggest ``viewer.show_file(path="test.jpg")`` instead. +FitsStubImagePlugin +~~~~~~~~~~~~~~~~~~~ + +.. deprecated:: 9.1.0 + +The stub image plugin FitsStubImagePlugin has been deprecated and will be removed in +Pillow 10.0.0 (2023-07-01). FITS images can be read without a handler through +FitsImagePlugin instead. + Removed features ---------------- diff --git a/docs/releasenotes/9.1.0.rst b/docs/releasenotes/9.1.0.rst index cbf9fe6e1bd..8120af204dd 100644 --- a/docs/releasenotes/9.1.0.rst +++ b/docs/releasenotes/9.1.0.rst @@ -31,6 +31,15 @@ In effect, ``viewer.show_file("test.jpg")`` will continue to work unchanged. ``viewer.show_file(file="test.jpg")`` will raise a deprecation warning, and suggest ``viewer.show_file(path="test.jpg")`` instead. +FitsStubImagePlugin +~~~~~~~~~~~~~~~~~~~ + +.. deprecated:: 9.1.0 + +The stub image plugin FitsStubImagePlugin has been deprecated and will be removed in +Pillow 10.0.0 (2023-07-01). FITS images can be read without a handler through +FitsImagePlugin instead. + API Additions ============= diff --git a/src/PIL/FitsStubImagePlugin.py b/src/PIL/FitsStubImagePlugin.py new file mode 100644 index 00000000000..9eed02999e2 --- /dev/null +++ b/src/PIL/FitsStubImagePlugin.py @@ -0,0 +1,77 @@ +# +# The Python Imaging Library +# $Id$ +# +# FITS stub adapter +# +# Copyright (c) 1998-2003 by Fredrik Lundh +# +# See the README file for information on usage and redistribution. +# + +import warnings + +from . import FitsImagePlugin, Image, ImageFile + +_handler = None + + +def register_handler(handler): + """ + Install application-specific FITS image handler. + + :param handler: Handler object. + """ + global _handler + _handler = handler + + warnings.warn( + "FitsStubImagePlugin is deprecated and will be removed in Pillow " + "10 (2023-07-01). FITS images can now be read without a handler through " + "FitsImagePlugin instead.", + DeprecationWarning, + ) + + # Override FitsImagePlugin with this handler + # for backwards compatibility + try: + Image.ID.remove(FITSStubImageFile.format) + except ValueError: + pass + + Image.register_open( + FITSStubImageFile.format, FITSStubImageFile, FitsImagePlugin._accept + ) + + +class FITSStubImageFile(ImageFile.StubImageFile): + + format = FitsImagePlugin.FitsImageFile.format + format_description = FitsImagePlugin.FitsImageFile.format_description + + def _open(self): + offset = self.fp.tell() + + im = FitsImagePlugin.FitsImageFile(self.fp) + self._size = im.size + self.mode = im.mode + self.tile = [] + + self.fp.seek(offset) + + loader = self._load() + if loader: + loader.open(self) + + def _load(self): + return _handler + + +def _save(im, fp, filename): + raise OSError("FITS save handler not installed") + + +# -------------------------------------------------------------------- +# Registry + +Image.register_save(FITSStubImageFile.format, _save) diff --git a/src/PIL/__init__.py b/src/PIL/__init__.py index 6352e088fe6..c90d99881dd 100644 --- a/src/PIL/__init__.py +++ b/src/PIL/__init__.py @@ -30,6 +30,7 @@ "DcxImagePlugin", "DdsImagePlugin", "EpsImagePlugin", + "FitsStubImagePlugin", "FitsImagePlugin", "FliImagePlugin", "FpxImagePlugin",