Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type hints to _binary #7659

Merged
merged 2 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/PIL/IptcImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,29 @@
import tempfile

from . import Image, ImageFile
from ._binary import i8, o8
from ._binary import i16be as i16
from ._binary import i32be as i32

COMPRESSION = {1: "raw", 5: "jpeg"}

PAD = o8(0) * 4
PAD = b"\0\0\0\0"


#
# Helpers


def _i8(c: int | bytes) -> int:
return c if isinstance(c, int) else c[0]


def i(c):
return i32((PAD + c)[-4:])


def dump(c):
for i in c:
print("%02x" % i8(i), end=" ")
print("%02x" % _i8(i), end=" ")
print()


Expand Down Expand Up @@ -103,10 +106,10 @@
self.info[tag] = tagdata

# mode
layers = i8(self.info[(3, 60)][0])
component = i8(self.info[(3, 60)][1])
layers = self.info[(3, 60)][0]
component = self.info[(3, 60)][1]

Check warning on line 110 in src/PIL/IptcImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/IptcImagePlugin.py#L110

Added line #L110 was not covered by tests
if (3, 65) in self.info:
id = i8(self.info[(3, 65)][0]) - 1
id = self.info[(3, 65)][0] - 1

Check warning on line 112 in src/PIL/IptcImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/IptcImagePlugin.py#L112

Added line #L112 was not covered by tests
else:
id = 0
if layers == 1 and not component:
Expand Down
28 changes: 14 additions & 14 deletions src/PIL/_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
from struct import pack, unpack_from


def i8(c) -> int:
return c if c.__class__ is int else c[0]
def i8(c: bytes) -> int:
return c[0]


def o8(i):
def o8(i: int) -> bytes:
return bytes((i & 255,))


# Input, le = little endian, be = big endian
def i16le(c, o=0):
def i16le(c: bytes, o: int = 0) -> int:
"""
Converts a 2-bytes (16 bits) string to an unsigned integer.
Expand All @@ -37,7 +37,7 @@ def i16le(c, o=0):
return unpack_from("<H", c, o)[0]


def si16le(c, o=0):
def si16le(c: bytes, o: int = 0) -> int:
"""
Converts a 2-bytes (16 bits) string to a signed integer.
Expand All @@ -47,7 +47,7 @@ def si16le(c, o=0):
return unpack_from("<h", c, o)[0]


def si16be(c, o=0):
def si16be(c: bytes, o: int = 0) -> int:
"""
Converts a 2-bytes (16 bits) string to a signed integer, big endian.
Expand All @@ -57,7 +57,7 @@ def si16be(c, o=0):
return unpack_from(">h", c, o)[0]


def i32le(c, o=0) -> int:
def i32le(c: bytes, o: int = 0) -> int:
"""
Converts a 4-bytes (32 bits) string to an unsigned integer.
Expand All @@ -67,7 +67,7 @@ def i32le(c, o=0) -> int:
return unpack_from("<I", c, o)[0]


def si32le(c, o=0):
def si32le(c: bytes, o: int = 0) -> int:
"""
Converts a 4-bytes (32 bits) string to a signed integer.
Expand All @@ -77,26 +77,26 @@ def si32le(c, o=0):
return unpack_from("<i", c, o)[0]


def i16be(c, o=0):
def i16be(c: bytes, o: int = 0) -> int:
return unpack_from(">H", c, o)[0]


def i32be(c, o=0):
def i32be(c: bytes, o: int = 0) -> int:
return unpack_from(">I", c, o)[0]


# Output, le = little endian, be = big endian
def o16le(i):
def o16le(i: int) -> bytes:
return pack("<H", i)


def o32le(i):
def o32le(i: int) -> bytes:
return pack("<I", i)


def o16be(i) -> bytes:
def o16be(i: int) -> bytes:
return pack(">H", i)


def o32be(i):
def o32be(i: int) -> bytes:
return pack(">I", i)
Loading