Skip to content

Commit 6282caf

Browse files
authored
Merge pull request #7659 from nulano/types-binary
2 parents 42c574a + 3396ce1 commit 6282caf

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

src/PIL/IptcImagePlugin.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,29 @@
2020
import tempfile
2121

2222
from . import Image, ImageFile
23-
from ._binary import i8, o8
2423
from ._binary import i16be as i16
2524
from ._binary import i32be as i32
2625

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

29-
PAD = o8(0) * 4
28+
PAD = b"\0\0\0\0"
3029

3130

3231
#
3332
# Helpers
3433

3534

35+
def _i8(c: int | bytes) -> int:
36+
return c if isinstance(c, int) else c[0]
37+
38+
3639
def i(c):
3740
return i32((PAD + c)[-4:])
3841

3942

4043
def dump(c):
4144
for i in c:
42-
print("%02x" % i8(i), end=" ")
45+
print("%02x" % _i8(i), end=" ")
4346
print()
4447

4548

@@ -103,10 +106,10 @@ def _open(self):
103106
self.info[tag] = tagdata
104107

105108
# mode
106-
layers = i8(self.info[(3, 60)][0])
107-
component = i8(self.info[(3, 60)][1])
109+
layers = self.info[(3, 60)][0]
110+
component = self.info[(3, 60)][1]
108111
if (3, 65) in self.info:
109-
id = i8(self.info[(3, 65)][0]) - 1
112+
id = self.info[(3, 65)][0] - 1
110113
else:
111114
id = 0
112115
if layers == 1 and not component:

src/PIL/_binary.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
from struct import pack, unpack_from
1919

2020

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

2424

25-
def o8(i):
25+
def o8(i: int) -> bytes:
2626
return bytes((i & 255,))
2727

2828

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

3939

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

4949

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

5959

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

6969

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

7979

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

8383

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

8787

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

9292

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

9696

97-
def o16be(i) -> bytes:
97+
def o16be(i: int) -> bytes:
9898
return pack(">H", i)
9999

100100

101-
def o32be(i):
101+
def o32be(i: int) -> bytes:
102102
return pack(">I", i)

0 commit comments

Comments
 (0)