Skip to content

Commit 419b653

Browse files
committed
Support bytearray for uX
1 parent c72e517 commit 419b653

5 files changed

Lines changed: 67 additions & 26 deletions

File tree

ptrlib/binary/encoding/byteconv.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def bytes2str(data: Union[str, bytes]) -> str:
1515
Returns:
1616
str: The converted data.
1717
"""
18-
if isinstance(data, bytes):
18+
if isinstance(data, (bytes, bytearray)):
1919
return ''.join(list(map(chr, data)))
2020

2121
if isinstance(data, str):
@@ -38,8 +38,8 @@ def str2bytes(data: Union[str, bytes]) -> bytes:
3838
except ValueError:
3939
return data.encode('utf-8')
4040

41-
if isinstance(data, bytes):
42-
return data
41+
if isinstance(data, (bytes, bytearray)):
42+
return bytes(data)
4343

4444
raise TypeError(f"{type(data)} given ('str' expected)")
4545

ptrlib/binary/operation/xor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def xor(data: Union[str, bytes, List[int]], key: Union[int, str, bytes, List[int
2626
xor([1,2,3,4,5], [0xaa,0x55])
2727
```
2828
"""
29-
assert isinstance(data, (str, bytes, list))
30-
assert isinstance(key, (str, bytes, int, list))
29+
assert isinstance(data, (str, bytes, bytearray, list))
30+
assert isinstance(key, (str, bytes, bytearray, int, list))
3131

3232
if isinstance(data, str):
3333
data = str2bytes(data)
@@ -40,7 +40,7 @@ def xor(data: Union[str, bytes, List[int]], key: Union[int, str, bytes, List[int
4040
key = bytes(key)
4141
elif isinstance(key, int):
4242
if key < 0 or key > 0xff:
43-
logger.warning("key (int) should be in [0, 0x100) (%x given)", key)
43+
logger.warning("key (int) should be in [0x00, 0xff] (0x%x given)", key)
4444
key = bytes([key & 0xff])
4545

4646
result = b''

ptrlib/binary/packing/flat.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
from typing import Any, Callable, List
44

55

6-
def flat(chunks: List[Any],
7-
map: Callable[[Any], bytes]) -> bytes:
6+
def flat(chunks: List[Any], map: Callable[[Any], bytes]) -> bytes:
87
"""Concatnate chunks into bytes.
98
109
Args:

ptrlib/binary/packing/unpack.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ def u8(data: Union[str, bytes], signed: bool=False) -> int:
2121
if isinstance(data, str):
2222
data = str2bytes(data)
2323

24-
if not isinstance(data, bytes):
25-
raise ValueError(f"u8: {type(data)} given ('bytes' expected)")
26-
2724
return int.from_bytes(data, 'big', signed=signed)
2825

2926
def u16(data: Union[str, bytes], byteorder: PtrlibEndiannessT="little", signed: bool=False) -> int:
@@ -38,9 +35,6 @@ def u16(data: Union[str, bytes], byteorder: PtrlibEndiannessT="little", signed:
3835
if isinstance(data, str):
3936
data = str2bytes(data)
4037

41-
if not isinstance(data, bytes):
42-
raise ValueError(f"u16: {type(data)} given ('bytes' expected)")
43-
4438
return int.from_bytes(data, byteorder=byteorder, signed=signed)
4539

4640
def u32(data: Union[str, bytes], byteorder: PtrlibEndiannessT="little", signed: bool=False) -> int:
@@ -55,9 +49,6 @@ def u32(data: Union[str, bytes], byteorder: PtrlibEndiannessT="little", signed:
5549
if isinstance(data, str):
5650
data = str2bytes(data)
5751

58-
if not isinstance(data, bytes):
59-
raise ValueError(f"u32: {type(data)} given ('bytes' expected)")
60-
6152
return int.from_bytes(data, byteorder=byteorder, signed=signed)
6253

6354
def u32f(data: Union[str, bytes], byteorder: PtrlibEndiannessT="little") -> float:
@@ -72,9 +63,6 @@ def u32f(data: Union[str, bytes], byteorder: PtrlibEndiannessT="little") -> floa
7263
if isinstance(data, str):
7364
data = str2bytes(data)
7465

75-
if not isinstance(data, bytes):
76-
raise ValueError(f"u32f: {type(data)} given ('bytes' expected)")
77-
7866
return struct.unpack('<f' if byteorder == 'little' else '>f', data)[0]
7967

8068
def u64(data: Union[str, bytes], byteorder: PtrlibEndiannessT='little', signed: bool=False) -> int:
@@ -89,9 +77,6 @@ def u64(data: Union[str, bytes], byteorder: PtrlibEndiannessT='little', signed:
8977
if isinstance(data, str):
9078
data = str2bytes(data)
9179

92-
if not isinstance(data, bytes):
93-
raise ValueError(f"u64: {type(data)} given ('bytes' expected)")
94-
9580
return int.from_bytes(data, byteorder=byteorder, signed=signed)
9681

9782
def u64f(data: Union[str, bytes], byteorder: PtrlibEndiannessT="little") -> float:
@@ -106,9 +91,6 @@ def u64f(data: Union[str, bytes], byteorder: PtrlibEndiannessT="little") -> floa
10691
if isinstance(data, str):
10792
data = str2bytes(data)
10893

109-
if not isinstance(data, bytes):
110-
raise ValueError(f"u64f: {type(data)} given ('bytes' expected)")
111-
11294
return struct.unpack('<d' if byteorder == 'little' else '>d', data)[0]
11395

11496

tests/binary/packing/test_unpack.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,44 @@ def test_p8(self):
1111
v = '\xff'
1212
self.assertEqual(u8(v), 0xff)
1313
self.assertEqual(u8(v, signed=True), -1)
14+
v = b'\xff'
15+
self.assertEqual(u8(v), 0xff)
16+
self.assertEqual(u8(v, signed=True), -1)
17+
v = bytearray(b'\xff')
18+
self.assertEqual(u8(v), 0xff)
19+
self.assertEqual(u8(v, signed=True), -1)
1420

1521
def test_u16(self):
22+
v = '\xcc\xed'
23+
self.assertEqual(u16(v), 0xedcc)
24+
self.assertEqual(u16(v, signed=True), -0x1234)
25+
v = '\xed\xcc'
26+
self.assertEqual(u16(v, byteorder='big'), 0xedcc)
27+
self.assertEqual(u16(v, byteorder='big', signed=True), -0x1234)
1628
v = b'\xcc\xed'
1729
self.assertEqual(u16(v), 0xedcc)
1830
self.assertEqual(u16(v, signed=True), -0x1234)
1931
v = b'\xed\xcc'
2032
self.assertEqual(u16(v, byteorder='big'), 0xedcc)
2133
self.assertEqual(u16(v, byteorder='big', signed=True), -0x1234)
34+
v = bytearray(b'\xcc\xed')
35+
self.assertEqual(u16(v), 0xedcc)
36+
self.assertEqual(u16(v, signed=True), -0x1234)
37+
v = bytearray(b'\xed\xcc')
38+
self.assertEqual(u16(v, byteorder='big'), 0xedcc)
39+
self.assertEqual(u16(v, byteorder='big', signed=True), -0x1234)
2240

2341
def test_u32(self):
42+
v = '\x88\xa9\xcb\xed'
43+
self.assertEqual(u32(v), 0xedcba988)
44+
self.assertEqual(u32(v, signed=True), -0x12345678)
45+
v = '\xed\xcb\xa9\x88'
46+
self.assertEqual(u32(v, byteorder='big'), 0xedcba988)
47+
self.assertEqual(u32(v, byteorder='big', signed=True), -0x12345678)
48+
v = '\x00\x00\x40\x40'
49+
self.assertEqual(u32f(v), 3.0)
50+
self.assertEqual(u32f(v[::-1], byteorder='big'), 3.0)
51+
2452
v = b'\x88\xa9\xcb\xed'
2553
self.assertEqual(u32(v), 0xedcba988)
2654
self.assertEqual(u32(v, signed=True), -0x12345678)
@@ -31,7 +59,28 @@ def test_u32(self):
3159
self.assertEqual(u32f(v), 3.0)
3260
self.assertEqual(u32f(v[::-1], byteorder='big'), 3.0)
3361

62+
v = bytearray(b'\x88\xa9\xcb\xed')
63+
self.assertEqual(u32(v), 0xedcba988)
64+
self.assertEqual(u32(v, signed=True), -0x12345678)
65+
v = bytearray(b'\xed\xcb\xa9\x88')
66+
self.assertEqual(u32(v, byteorder='big'), 0xedcba988)
67+
self.assertEqual(u32(v, byteorder='big', signed=True), -0x12345678)
68+
v = bytearray(b'\x00\x00\x40\x40')
69+
self.assertEqual(u32f(v), 3.0)
70+
self.assertEqual(u32f(v[::-1], byteorder='big'), 3.0)
71+
3472
def test_u64(self):
73+
v = '\x11\x32\x54\x6f\x87\xa9\xcb\xed'
74+
self.assertEqual(u64(v), 0xedcba9876f543211)
75+
self.assertEqual(u64(v, signed=True), -0x1234567890abcdef)
76+
v = '\xed\xcb\xa9\x87\x6f\x54\x32\x11'
77+
self.assertEqual(u64(v, byteorder='big'), 0xedcba9876f543211)
78+
self.assertEqual(u64(v, byteorder='big', signed=True),
79+
-0x1234567890abcdef)
80+
v = '\xf1\xd4\xc8\x53\xfb\x21\x09\x40'
81+
self.assertEqual(u64f(v), 3.14159265)
82+
self.assertEqual(u64f(v[::-1], byteorder='big'), 3.14159265)
83+
3584
v = b'\x11\x32\x54\x6f\x87\xa9\xcb\xed'
3685
self.assertEqual(u64(v), 0xedcba9876f543211)
3786
self.assertEqual(u64(v, signed=True), -0x1234567890abcdef)
@@ -42,3 +91,14 @@ def test_u64(self):
4291
v = b'\xf1\xd4\xc8\x53\xfb\x21\x09\x40'
4392
self.assertEqual(u64f(v), 3.14159265)
4493
self.assertEqual(u64f(v[::-1], byteorder='big'), 3.14159265)
94+
95+
v = bytearray(b'\x11\x32\x54\x6f\x87\xa9\xcb\xed')
96+
self.assertEqual(u64(v), 0xedcba9876f543211)
97+
self.assertEqual(u64(v, signed=True), -0x1234567890abcdef)
98+
v = bytearray(b'\xed\xcb\xa9\x87\x6f\x54\x32\x11')
99+
self.assertEqual(u64(v, byteorder='big'), 0xedcba9876f543211)
100+
self.assertEqual(u64(v, byteorder='big', signed=True),
101+
-0x1234567890abcdef)
102+
v = bytearray(b'\xf1\xd4\xc8\x53\xfb\x21\x09\x40')
103+
self.assertEqual(u64f(v), 3.14159265)
104+
self.assertEqual(u64f(v[::-1], byteorder='big'), 3.14159265)

0 commit comments

Comments
 (0)