From c6155351d2367eeb98bde95da215e1626ec37f44 Mon Sep 17 00:00:00 2001 From: Nulano Date: Sat, 30 Dec 2023 00:02:34 +0100 Subject: [PATCH] deprecate IptcImagePlugin.{dump,i,PAD} --- Tests/test_file_iptc.py | 20 ++++++++++++-------- src/PIL/IptcImagePlugin.py | 21 +++++++++++++++------ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/Tests/test_file_iptc.py b/Tests/test_file_iptc.py index d0ecde393a4..79dc1d3d89c 100644 --- a/Tests/test_file_iptc.py +++ b/Tests/test_file_iptc.py @@ -78,24 +78,28 @@ def test_i(): c = b"a" # Act - ret = IptcImagePlugin.i(c) + with pytest.warns(DeprecationWarning): + ret = IptcImagePlugin.i(c) # Assert assert ret == 97 -def test_dump(): +def test_dump(monkeypatch): # Arrange c = b"abc" # Temporarily redirect stdout - old_stdout = sys.stdout - sys.stdout = mystdout = StringIO() + mystdout = StringIO() + monkeypatch.setattr(sys, "stdout", mystdout) # Act - IptcImagePlugin.dump(c) - - # Reset stdout - sys.stdout = old_stdout + with pytest.warns(DeprecationWarning): + IptcImagePlugin.dump(c) # Assert assert mystdout.getvalue() == "61 62 63 \n" + + +def test_pad_deprecation(): + with pytest.warns(DeprecationWarning): + assert IptcImagePlugin.PAD == b"\0\0\0\0" diff --git a/src/PIL/IptcImagePlugin.py b/src/PIL/IptcImagePlugin.py index 8d1a22fa220..a31be2c10d8 100644 --- a/src/PIL/IptcImagePlugin.py +++ b/src/PIL/IptcImagePlugin.py @@ -20,13 +20,19 @@ import tempfile from . import Image, ImageFile -from ._binary import o8 from ._binary import i16be as i16 from ._binary import i32be as i32 +from ._deprecate import deprecate COMPRESSION = {1: "raw", 5: "jpeg"} -PAD = o8(0) * 4 + +def __getattr__(name): + if name == "PAD": + deprecate("IptcImagePlugin.PAD", 12) + return b"\0\0\0\0" + msg = f"module '{__name__}' has no attribute '{name}'" + raise AttributeError(msg) # @@ -37,11 +43,14 @@ def _i8(c: int | bytes) -> int: return c if isinstance(c, int) else c[0] -def i(c): - return i32((PAD + c)[-4:]) +def i(c, *, _internal=False): + if not _internal: + deprecate("IptcImagePlugin.i", 12) + return i32((b"\0\0\0\0" + c)[-4:]) def dump(c): + deprecate("IptcImagePlugin.dump", 12) for i in c: print("%02x" % _i8(i), end=" ") print() @@ -57,7 +66,7 @@ class IptcImageFile(ImageFile.ImageFile): format_description = "IPTC/NAA" def getint(self, key): - return i(self.info[key]) + return i(self.info[key], _internal=True) def field(self): # @@ -81,7 +90,7 @@ def field(self): elif size == 128: size = 0 elif size > 128: - size = i(self.fp.read(size - 128)) + size = i(self.fp.read(size - 128), _internal=True) else: size = i16(s, 3)