mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-10-31 16:07:30 +03:00 
			
		
		
		
	Deprecated constants in favour of enums
This commit is contained in:
		
							parent
							
								
									f8e4e9c2dd
								
							
						
					
					
						commit
						ed8073e846
					
				|  | @ -635,3 +635,13 @@ def test_apng_save_blend(tmp_path): | ||||||
|     with Image.open(test_file) as im: |     with Image.open(test_file) as im: | ||||||
|         im.seek(2) |         im.seek(2) | ||||||
|         assert im.getpixel((0, 0)) == (0, 255, 0, 255) |         assert im.getpixel((0, 0)) == (0, 255, 0, 255) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_constants_deprecation(): | ||||||
|  |     for enum, prefix in { | ||||||
|  |         PngImagePlugin.Disposal: "APNG_DISPOSE_", | ||||||
|  |         PngImagePlugin.Blend: "APNG_BLEND_", | ||||||
|  |     }.items(): | ||||||
|  |         for name in enum.__members__: | ||||||
|  |             with pytest.warns(DeprecationWarning): | ||||||
|  |                 assert getattr(PngImagePlugin, prefix + name) == enum[name] | ||||||
|  |  | ||||||
|  | @ -1,6 +1,6 @@ | ||||||
| import pytest | import pytest | ||||||
| 
 | 
 | ||||||
| from PIL import Image | from PIL import BlpImagePlugin, Image | ||||||
| 
 | 
 | ||||||
| from .helper import assert_image_equal_tofile | from .helper import assert_image_equal_tofile | ||||||
| 
 | 
 | ||||||
|  | @ -37,3 +37,14 @@ def test_crashes(test_file): | ||||||
|         with Image.open(f) as im: |         with Image.open(f) as im: | ||||||
|             with pytest.raises(OSError): |             with pytest.raises(OSError): | ||||||
|                 im.load() |                 im.load() | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_constants_deprecation(): | ||||||
|  |     for enum, prefix in { | ||||||
|  |         BlpImagePlugin.Format: "BLP_FORMAT_", | ||||||
|  |         BlpImagePlugin.Encoding: "BLP_ENCODING_", | ||||||
|  |         BlpImagePlugin.AlphaEncoding: "BLP_ALPHA_ENCODING_", | ||||||
|  |     }.items(): | ||||||
|  |         for name in enum.__members__: | ||||||
|  |             with pytest.warns(DeprecationWarning): | ||||||
|  |                 assert getattr(BlpImagePlugin, prefix + name) == enum[name] | ||||||
|  |  | ||||||
|  | @ -1,4 +1,6 @@ | ||||||
| from PIL import Image | import pytest | ||||||
|  | 
 | ||||||
|  | from PIL import FtexImagePlugin, Image | ||||||
| 
 | 
 | ||||||
| from .helper import assert_image_equal_tofile, assert_image_similar | from .helper import assert_image_equal_tofile, assert_image_similar | ||||||
| 
 | 
 | ||||||
|  | @ -12,3 +14,12 @@ def test_load_dxt1(): | ||||||
|     with Image.open("Tests/images/ftex_dxt1.ftc") as im: |     with Image.open("Tests/images/ftex_dxt1.ftc") as im: | ||||||
|         with Image.open("Tests/images/ftex_dxt1.png") as target: |         with Image.open("Tests/images/ftex_dxt1.png") as target: | ||||||
|             assert_image_similar(im, target.convert("RGBA"), 15) |             assert_image_similar(im, target.convert("RGBA"), 15) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_constants_deprecation(): | ||||||
|  |     for enum, prefix in { | ||||||
|  |         FtexImagePlugin.Format: "FORMAT_", | ||||||
|  |     }.items(): | ||||||
|  |         for name in enum.__members__: | ||||||
|  |             with pytest.warns(DeprecationWarning): | ||||||
|  |                 assert getattr(FtexImagePlugin, prefix + name) == enum[name] | ||||||
|  |  | ||||||
|  | @ -802,6 +802,31 @@ class TestImage: | ||||||
|         with pytest.warns(DeprecationWarning): |         with pytest.warns(DeprecationWarning): | ||||||
|             assert Image.CONTAINER == 2 |             assert Image.CONTAINER == 2 | ||||||
| 
 | 
 | ||||||
|  |     def test_constants_deprecation(self): | ||||||
|  |         with pytest.warns(DeprecationWarning): | ||||||
|  |             assert Image.NEAREST == 0 | ||||||
|  |         with pytest.warns(DeprecationWarning): | ||||||
|  |             assert Image.NONE == 0 | ||||||
|  | 
 | ||||||
|  |         with pytest.warns(DeprecationWarning): | ||||||
|  |             assert Image.LINEAR == Image.Resampling.BILINEAR | ||||||
|  |         with pytest.warns(DeprecationWarning): | ||||||
|  |             assert Image.CUBIC == Image.Resampling.BICUBIC | ||||||
|  |         with pytest.warns(DeprecationWarning): | ||||||
|  |             assert Image.ANTIALIAS == Image.Resampling.LANCZOS | ||||||
|  | 
 | ||||||
|  |         for enum in ( | ||||||
|  |             Image.Transpose, | ||||||
|  |             Image.Transform, | ||||||
|  |             Image.Resampling, | ||||||
|  |             Image.Dither, | ||||||
|  |             Image.Palette, | ||||||
|  |             Image.Quantize, | ||||||
|  |         ): | ||||||
|  |             for name in enum.__members__: | ||||||
|  |                 with pytest.warns(DeprecationWarning): | ||||||
|  |                     assert getattr(Image, name) == enum[name] | ||||||
|  | 
 | ||||||
|     @pytest.mark.parametrize( |     @pytest.mark.parametrize( | ||||||
|         "path", |         "path", | ||||||
|         [ |         [ | ||||||
|  |  | ||||||
|  | @ -593,3 +593,13 @@ def test_auxiliary_channels_isolated(): | ||||||
|                 ) |                 ) | ||||||
| 
 | 
 | ||||||
|                 assert_image_equal(test_image.convert(dst_format[2]), reference_image) |                 assert_image_equal(test_image.convert(dst_format[2]), reference_image) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_constants_deprecation(): | ||||||
|  |     for enum, prefix in { | ||||||
|  |         ImageCms.Intent: "INTENT_", | ||||||
|  |         ImageCms.Direction: "DIRECTION_", | ||||||
|  |     }.items(): | ||||||
|  |         for name in enum.__members__: | ||||||
|  |             with pytest.warns(DeprecationWarning): | ||||||
|  |                 assert getattr(ImageCms, prefix + name) == enum[name] | ||||||
|  |  | ||||||
|  | @ -1022,3 +1022,12 @@ def test_oom(test_file): | ||||||
|         font = ImageFont.truetype(BytesIO(f.read())) |         font = ImageFont.truetype(BytesIO(f.read())) | ||||||
|         with pytest.raises(Image.DecompressionBombError): |         with pytest.raises(Image.DecompressionBombError): | ||||||
|             font.getmask("Test Text") |             font.getmask("Test Text") | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_constants_deprecation(): | ||||||
|  |     for enum, prefix in { | ||||||
|  |         ImageFont.Layout: "LAYOUT_", | ||||||
|  |     }.items(): | ||||||
|  |         for name in enum.__members__: | ||||||
|  |             with pytest.warns(DeprecationWarning): | ||||||
|  |                 assert getattr(ImageFont, prefix + name) == enum[name] | ||||||
|  |  | ||||||
|  | @ -30,6 +30,7 @@ BLP files come in many different flavours: | ||||||
| """ | """ | ||||||
| 
 | 
 | ||||||
| import struct | import struct | ||||||
|  | import warnings | ||||||
| from enum import IntEnum | from enum import IntEnum | ||||||
| from io import BytesIO | from io import BytesIO | ||||||
| 
 | 
 | ||||||
|  | @ -52,11 +53,31 @@ class AlphaEncoding(IntEnum): | ||||||
|     DXT5 = 7 |     DXT5 = 7 | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| globals().update({"BLP_FORMAT_" + k: v for k, v in Format.__members__.items()}) | def __getattr__(name): | ||||||
| globals().update({"BLP_ENCODING_" + k: v for k, v in Encoding.__members__.items()}) |     deprecated = "deprecated and will be removed in Pillow 10 (2023-07-01). " | ||||||
| globals().update( |     for enum, prefix in { | ||||||
|     {"BLP_ALPHA_ENCODING_" + k: v for k, v in AlphaEncoding.__members__.items()} |         Format: "BLP_FORMAT_", | ||||||
| ) |         Encoding: "BLP_ENCODING_", | ||||||
|  |         AlphaEncoding: "BLP_ALPHA_ENCODING_", | ||||||
|  |     }.items(): | ||||||
|  |         if name.startswith(prefix): | ||||||
|  |             name = name[len(prefix) :] | ||||||
|  |             if name in enum.__members__: | ||||||
|  |                 warnings.warn( | ||||||
|  |                     prefix | ||||||
|  |                     + name | ||||||
|  |                     + " is " | ||||||
|  |                     + deprecated | ||||||
|  |                     + "Use " | ||||||
|  |                     + enum.__name__ | ||||||
|  |                     + "." | ||||||
|  |                     + name | ||||||
|  |                     + " instead.", | ||||||
|  |                     DeprecationWarning, | ||||||
|  |                     stacklevel=2, | ||||||
|  |                 ) | ||||||
|  |                 return enum[name] | ||||||
|  |     raise AttributeError(f"module '{__name__}' has no attribute '{name}'") | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def unpack_565(i): | def unpack_565(i): | ||||||
|  |  | ||||||
|  | @ -52,6 +52,7 @@ Note: All data is stored in little-Endian (Intel) byte order. | ||||||
| """ | """ | ||||||
| 
 | 
 | ||||||
| import struct | import struct | ||||||
|  | import warnings | ||||||
| from enum import IntEnum | from enum import IntEnum | ||||||
| from io import BytesIO | from io import BytesIO | ||||||
| 
 | 
 | ||||||
|  | @ -65,7 +66,27 @@ class Format(IntEnum): | ||||||
|     UNCOMPRESSED = 1 |     UNCOMPRESSED = 1 | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| globals().update({"FORMAT_" + k: v for k, v in Format.__members__.items()}) | def __getattr__(name): | ||||||
|  |     deprecated = "deprecated and will be removed in Pillow 10 (2023-07-01). " | ||||||
|  |     for enum, prefix in {Format: "FORMAT_"}.items(): | ||||||
|  |         if name.startswith(prefix): | ||||||
|  |             name = name[len(prefix) :] | ||||||
|  |             if name in enum.__members__: | ||||||
|  |                 warnings.warn( | ||||||
|  |                     prefix | ||||||
|  |                     + name | ||||||
|  |                     + " is " | ||||||
|  |                     + deprecated | ||||||
|  |                     + "Use " | ||||||
|  |                     + enum.__name__ | ||||||
|  |                     + "." | ||||||
|  |                     + name | ||||||
|  |                     + " instead.", | ||||||
|  |                     DeprecationWarning, | ||||||
|  |                     stacklevel=2, | ||||||
|  |                 ) | ||||||
|  |                 return enum[name] | ||||||
|  |     raise AttributeError(f"module '{__name__}' has no attribute '{name}'") | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class FtexImageFile(ImageFile.ImageFile): | class FtexImageFile(ImageFile.ImageFile): | ||||||
|  |  | ||||||
|  | @ -54,15 +54,57 @@ from ._util import deferred_error, isPath | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def __getattr__(name): | def __getattr__(name): | ||||||
|  |     deprecated = "deprecated and will be removed in Pillow 10 (2023-07-01). " | ||||||
|     categories = {"NORMAL": 0, "SEQUENCE": 1, "CONTAINER": 2} |     categories = {"NORMAL": 0, "SEQUENCE": 1, "CONTAINER": 2} | ||||||
|     if name in categories: |     if name in categories: | ||||||
|         warnings.warn( |         warnings.warn( | ||||||
|             "Image categories are deprecated and will be removed in Pillow 10 " |             "Image categories are " + deprecated + "Use is_animated instead.", | ||||||
|             "(2023-07-01). Use is_animated instead.", |  | ||||||
|             DeprecationWarning, |             DeprecationWarning, | ||||||
|             stacklevel=2, |             stacklevel=2, | ||||||
|         ) |         ) | ||||||
|         return categories[name] |         return categories[name] | ||||||
|  |     elif name in ("NEAREST", "NONE"): | ||||||
|  |         warnings.warn( | ||||||
|  |             name | ||||||
|  |             + " is " | ||||||
|  |             + deprecated | ||||||
|  |             + "Use Resampling.NEAREST or Dither.NONE instead.", | ||||||
|  |             DeprecationWarning, | ||||||
|  |             stacklevel=2, | ||||||
|  |         ) | ||||||
|  |         return 0 | ||||||
|  |     old_resampling = { | ||||||
|  |         "LINEAR": "BILINEAR", | ||||||
|  |         "CUBIC": "BICUBIC", | ||||||
|  |         "ANTIALIAS": "LANCZOS", | ||||||
|  |     } | ||||||
|  |     if name in old_resampling: | ||||||
|  |         warnings.warn( | ||||||
|  |             name | ||||||
|  |             + " is " | ||||||
|  |             + deprecated | ||||||
|  |             + "Use Resampling." | ||||||
|  |             + old_resampling[name] | ||||||
|  |             + " instead.", | ||||||
|  |             DeprecationWarning, | ||||||
|  |             stacklevel=2, | ||||||
|  |         ) | ||||||
|  |         return Resampling[old_resampling[name]] | ||||||
|  |     for enum in (Transpose, Transform, Resampling, Dither, Palette, Quantize): | ||||||
|  |         if name in enum.__members__: | ||||||
|  |             warnings.warn( | ||||||
|  |                 name | ||||||
|  |                 + " is " | ||||||
|  |                 + deprecated | ||||||
|  |                 + "Use " | ||||||
|  |                 + enum.__name__ | ||||||
|  |                 + "." | ||||||
|  |                 + name | ||||||
|  |                 + " instead.", | ||||||
|  |                 DeprecationWarning, | ||||||
|  |                 stacklevel=2, | ||||||
|  |             ) | ||||||
|  |             return enum[name] | ||||||
|     raise AttributeError(f"module '{__name__}' has no attribute '{name}'") |     raise AttributeError(f"module '{__name__}' has no attribute '{name}'") | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -199,10 +241,6 @@ class Quantize(IntEnum): | ||||||
|     LIBIMAGEQUANT = 3 |     LIBIMAGEQUANT = 3 | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| for enum in (Transpose, Transform, Resampling, Dither, Palette, Quantize): |  | ||||||
|     globals().update(enum.__members__) |  | ||||||
| NEAREST = NONE = 0 |  | ||||||
| 
 |  | ||||||
| if hasattr(core, "DEFAULT_STRATEGY"): | if hasattr(core, "DEFAULT_STRATEGY"): | ||||||
|     DEFAULT_STRATEGY = core.DEFAULT_STRATEGY |     DEFAULT_STRATEGY = core.DEFAULT_STRATEGY | ||||||
|     FILTERED = core.FILTERED |     FILTERED = core.FILTERED | ||||||
|  |  | ||||||
|  | @ -16,6 +16,7 @@ | ||||||
| # below for the original description. | # below for the original description. | ||||||
| 
 | 
 | ||||||
| import sys | import sys | ||||||
|  | import warnings | ||||||
| from enum import IntEnum | from enum import IntEnum | ||||||
| 
 | 
 | ||||||
| from PIL import Image | from PIL import Image | ||||||
|  | @ -115,8 +116,28 @@ class Direction(IntEnum): | ||||||
|     PROOF = 2 |     PROOF = 2 | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| globals().update({"INTENT_" + k: v for k, v in Intent.__members__.items()}) | def __getattr__(name): | ||||||
| globals().update({"DIRECTION_" + k: v for k, v in Direction.__members__.items()}) |     deprecated = "deprecated and will be removed in Pillow 10 (2023-07-01). " | ||||||
|  |     for enum, prefix in {Intent: "INTENT_", Direction: "DIRECTION_"}.items(): | ||||||
|  |         if name.startswith(prefix): | ||||||
|  |             name = name[len(prefix) :] | ||||||
|  |             if name in enum.__members__: | ||||||
|  |                 warnings.warn( | ||||||
|  |                     prefix | ||||||
|  |                     + name | ||||||
|  |                     + " is " | ||||||
|  |                     + deprecated | ||||||
|  |                     + "Use " | ||||||
|  |                     + enum.__name__ | ||||||
|  |                     + "." | ||||||
|  |                     + name | ||||||
|  |                     + " instead.", | ||||||
|  |                     DeprecationWarning, | ||||||
|  |                     stacklevel=2, | ||||||
|  |                 ) | ||||||
|  |                 return enum[name] | ||||||
|  |     raise AttributeError(f"module '{__name__}' has no attribute '{name}'") | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| # | # | ||||||
| # flags | # flags | ||||||
|  |  | ||||||
|  | @ -28,6 +28,7 @@ | ||||||
| import base64 | import base64 | ||||||
| import os | import os | ||||||
| import sys | import sys | ||||||
|  | import warnings | ||||||
| from enum import IntEnum | from enum import IntEnum | ||||||
| from io import BytesIO | from io import BytesIO | ||||||
| 
 | 
 | ||||||
|  | @ -40,7 +41,27 @@ class Layout(IntEnum): | ||||||
|     RAQM = 1 |     RAQM = 1 | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| globals().update({"LAYOUT_" + k: v for k, v in Layout.__members__.items()}) | def __getattr__(name): | ||||||
|  |     deprecated = "deprecated and will be removed in Pillow 10 (2023-07-01). " | ||||||
|  |     for enum, prefix in {Layout: "LAYOUT_"}.items(): | ||||||
|  |         if name.startswith(prefix): | ||||||
|  |             name = name[len(prefix) :] | ||||||
|  |             if name in enum.__members__: | ||||||
|  |                 warnings.warn( | ||||||
|  |                     prefix | ||||||
|  |                     + name | ||||||
|  |                     + " is " | ||||||
|  |                     + deprecated | ||||||
|  |                     + "Use " | ||||||
|  |                     + enum.__name__ | ||||||
|  |                     + "." | ||||||
|  |                     + name | ||||||
|  |                     + " instead.", | ||||||
|  |                     DeprecationWarning, | ||||||
|  |                     stacklevel=2, | ||||||
|  |                 ) | ||||||
|  |                 return enum[name] | ||||||
|  |     raise AttributeError(f"module '{__name__}' has no attribute '{name}'") | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class _imagingft_not_installed: | class _imagingft_not_installed: | ||||||
|  |  | ||||||
|  | @ -130,8 +130,27 @@ class Blend(IntEnum): | ||||||
|     """ |     """ | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| globals().update({"APNG_DISPOSE_" + k: v for k, v in Disposal.__members__.items()}) | def __getattr__(name): | ||||||
| globals().update(Blend.__members__) |     deprecated = "deprecated and will be removed in Pillow 10 (2023-07-01). " | ||||||
|  |     for enum, prefix in {Disposal: "APNG_DISPOSE_", Blend: "APNG_BLEND_"}.items(): | ||||||
|  |         if name.startswith(prefix): | ||||||
|  |             name = name[len(prefix) :] | ||||||
|  |             if name in enum.__members__: | ||||||
|  |                 warnings.warn( | ||||||
|  |                     prefix | ||||||
|  |                     + name | ||||||
|  |                     + " is " | ||||||
|  |                     + deprecated | ||||||
|  |                     + "Use " | ||||||
|  |                     + enum.__name__ | ||||||
|  |                     + "." | ||||||
|  |                     + name | ||||||
|  |                     + " instead.", | ||||||
|  |                     DeprecationWarning, | ||||||
|  |                     stacklevel=2, | ||||||
|  |                 ) | ||||||
|  |                 return enum[name] | ||||||
|  |     raise AttributeError(f"module '{__name__}' has no attribute '{name}'") | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def _safe_zlib_decompress(s): | def _safe_zlib_decompress(s): | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user