Skip to content

Commit 248b40a

Browse files
committed
find u32
1 parent 5c5fdce commit 248b40a

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

test/test_socketcan.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,15 @@ def side_effect_ctypes_alignment(value):
238238
self.assertEqual(expected_fields, BcmMsgHead._fields_)
239239

240240
def test_build_bcm_header(self):
241-
def _standard_size_little_endian_to_native(data: bytes, fmt: str) -> bytes:
242-
aligned_data = struct.pack("@" + fmt[1:], *struct.unpack(fmt, data))
241+
def _find_u32_fmt_char() -> str:
242+
for _fmt in ("H", "I", "L", "Q"):
243+
if struct.calcsize(_fmt) == 4:
244+
return _fmt
245+
246+
def _standard_size_little_endian_to_native(data: bytes) -> bytes:
247+
std_le_fmt = "<IIIllllII"
248+
native_fmt = "@" + std_le_fmt[1:].replace("I", _find_u32_fmt_char())
249+
aligned_data = struct.pack(native_fmt, *struct.unpack(std_le_fmt, data))
243250
padded_data = aligned_data + b"\x00" * ((8 - len(aligned_data) % 8) % 8)
244251
return padded_data
245252

@@ -248,8 +255,7 @@ def _standard_size_little_endian_to_native(data: bytes, fmt: str) -> bytes:
248255
b"\x00\x00\x00\x00\x00\x00\x00\x00"
249256
b"\x00\x00\x00\x00\x00\x00\x00\x00"
250257
b"\x00\x00\x00\x00\x01\x04\x00\x00"
251-
b"\x01\x00\x00\x00",
252-
fmt="<IIIllllII",
258+
b"\x01\x00\x00\x00"
253259
)
254260

255261
self.assertEqual(

0 commit comments

Comments
 (0)