Skip to content

Commit

Permalink
Correct size of enums in Python.
Browse files Browse the repository at this point in the history
  • Loading branch information
agoessling committed Aug 28, 2023
1 parent 326454e commit 747de1e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/c_stuff_sack.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ def static_assert(all_types, include_enums=True):
if isinstance(t, (ss.Primitive, ss.Bitfield)):
s += f'static_assert(sizeof({c_type_name(t)}) == {t.bytes}, "{c_type_name(t)} size mismatch.");\n'
if include_enums and isinstance(t, ss.Enum):
s += f'static_assert(sizeof({t.name}) == sizeof(int), "Size of {t.name} not equal to int.");\n'
s += f'static_assert(kNum{t.name} < {" * ".join(["256"] * t.bytes)} / 2, "{t.name} size mismatch.");\n'
if isinstance(t, ss.Struct):
for f in t.fields:
Expand Down
9 changes: 1 addition & 8 deletions src/py_stuff_sack.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,6 @@ def to_ctypes_name(name, default=None):


def get_globals(library, message_spec):
ENUM_MAP = {
1: ctypes.c_int8,
2: ctypes.c_int16,
4: ctypes.c_int32,
8: ctypes.c_int64,
}

BITFIELD_MAP = {
1: ctypes.c_uint8,
2: ctypes.c_uint16,
Expand Down Expand Up @@ -193,7 +186,7 @@ def get_globals(library, message_spec):
'description': t.description,
'value_descriptions':value_descr,
}
e = _EnumType(t.name, (EnumMixin, ENUM_MAP[t.bytes]), attrs)
e = _EnumType(t.name, (EnumMixin, ctypes.c_int), attrs)
global_vars[t.name] = e
ctypes_map[t.name] = global_vars[t.name]

Expand Down
3 changes: 3 additions & 0 deletions test/test_c_stuff_sack.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ static void TestEnum(void) {
TEST_ASSERT_EQUAL_INT(128, kNumEnum2Bytes);
TEST_ASSERT_EQUAL_INT(8, SS_ENUM2_BYTES_TEST_PACKED_SIZE);

TEST_ASSERT_EQUAL_INT(sizeof(int), sizeof(Enum1Bytes));
TEST_ASSERT_EQUAL_INT(sizeof(int), sizeof(Enum2Bytes));

Enum2BytesTest enum_test = {
.enumeration = kNumEnum2Bytes,
};
Expand Down
6 changes: 6 additions & 0 deletions test/test_py_stuff_sack.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ctypes
import os
import unittest

Expand Down Expand Up @@ -60,6 +61,11 @@ def test_size(self):
self.assertEqual(128, len(msg_def.Enum2Bytes))
self.assertEqual(8, msg_def.Enum2BytesTest.packed_size)

self.assertEqual(ctypes.sizeof(ctypes.c_int),
ctypes.sizeof(msg_def.Enum1BytesTest().enumeration))
self.assertEqual(ctypes.sizeof(ctypes.c_int),
ctypes.sizeof(msg_def.Enum2BytesTest().enumeration))

def test_pack_unpack(self):
msg, buf = self.get_test_message()

Expand Down

0 comments on commit 747de1e

Please sign in to comment.