Skip to content

Commit

Permalink
deprecate IptcImagePlugin.{dump,i,PAD}
Browse files Browse the repository at this point in the history
  • Loading branch information
nulano committed Dec 31, 2023
1 parent b31f236 commit c615535
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
20 changes: 12 additions & 8 deletions Tests/test_file_iptc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
21 changes: 15 additions & 6 deletions src/PIL/IptcImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


#
Expand All @@ -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()
Expand All @@ -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):
#
Expand All @@ -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)

Expand Down

0 comments on commit c615535

Please sign in to comment.