Skip to content

Commit 2c84121

Browse files
authored
Merge pull request #1334 from ekxide/iox2-1333-fix-type-translation-in-python
[#1333] fix type translation in python
2 parents a92bea3 + fd06db0 commit 2c84121

File tree

4 files changed

+133
-36
lines changed

4 files changed

+133
-36
lines changed

.github/workflows/build-test.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,19 @@ jobs:
183183
python-bindings:
184184
needs: [preflight-check, static-code-analysis, cargo-nextest]
185185
if: ${{ needs.changes.outputs.source-code == 'true' }}
186-
runs-on: ubuntu-latest
186+
strategy:
187+
matrix:
188+
os: [ubuntu-latest, macos-latest,windows-latest]
189+
runs-on: ${{ matrix.os }}
187190
steps:
188191
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # version v6.0.1
189192

193+
- name: Prepare Windows
194+
if: ${{ runner.os == 'Windows' }}
195+
run: internal\scripts\ci_prepare_windows.ps1
196+
190197
- name: Prepare Linux
198+
if: ${{ runner.os == 'Linux' }}
191199
run: |
192200
internal/scripts/ci_prepare_ubuntu.sh
193201
uname -a
@@ -199,6 +207,10 @@ jobs:
199207
- run: pip install poetry==2.2.1
200208
- run: pip install poetry-plugin-shell==1.0.1
201209

210+
- name: Install Global Plugins # (needed for windows due to separate C:\ and D:\ drives)
211+
if: runner.os == 'Windows'
212+
run: poetry self add "poethepoet[poetry-plugin]>=0.35.0,<0.36.0"
213+
202214
- name: Install Python Dependencies
203215
run: poetry --project iceoryx2-ffi/python install
204216

doc/release-notes/iceoryx2-unreleased.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
[#1312](https://github.com/eclipse-iceoryx/iceoryx2/issues/1312)
3636
* Bump wheel from 0.45.1 to 0.46.3 in /iceoryx2-ffi/python
3737
[#1316](https://github.com/eclipse-iceoryx/iceoryx2/issues/1316)
38+
* Fix Python type translation for integer types (32-bit)
39+
[#1333](https://github.com/eclipse-iceoryx/iceoryx2/issues/1333)
3840

3941
### Refactoring
4042

iceoryx2-ffi/python/python-src/iceoryx2/type_name.py

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,84 @@
1818
T = TypeVar("T")
1919

2020

21-
def get_type_name(t: Type[T]) -> Any:
22-
"""Generates a human readable type name from a given type."""
23-
if hasattr(t, "type_name"):
24-
return t.type_name()
25-
26-
if t.__name__ == "c_ubyte" and ctypes.sizeof(t) == 1:
21+
def _get_unsigned_int_type_name(t: Type[T]) -> Any:
22+
"""Generates a human readable type name from a given unsigned integer type."""
23+
if ctypes.sizeof(t) == 1:
2724
return "u8"
28-
if t.__name__ == "c_ushort" and ctypes.sizeof(t) == 2:
25+
if ctypes.sizeof(t) == 2:
2926
return "u16"
30-
if t.__name__ == "c_uint" and ctypes.sizeof(t) == 4:
27+
if ctypes.sizeof(t) == 4:
3128
return "u32"
32-
if t.__name__ == "c_ulong" and ctypes.sizeof(t) == 8:
29+
if ctypes.sizeof(t) == 8:
3330
return "u64"
34-
if t.__name__ == "c_byte" and ctypes.sizeof(t) == 1:
31+
if ctypes.sizeof(t) == 16:
32+
return "u128"
33+
34+
return t.__name__
35+
36+
37+
def _get_signed_int_type_name(t: Type[T]) -> Any:
38+
"""Generates a human readable type name from a given signed integer type."""
39+
if ctypes.sizeof(t) == 1:
3540
return "i8"
36-
if t.__name__ == "c_short" and ctypes.sizeof(t) == 2:
41+
if ctypes.sizeof(t) == 2:
3742
return "i16"
38-
if t.__name__ == "c_int" and ctypes.sizeof(t) == 4:
43+
if ctypes.sizeof(t) == 4:
3944
return "i32"
40-
if t.__name__ == "c_long" and ctypes.sizeof(t) == 8:
45+
if ctypes.sizeof(t) == 8:
4146
return "i64"
42-
if t.__name__ == "c_bool":
43-
return "bool"
44-
if t.__name__ == "c_float" and ctypes.sizeof(t) == 4:
47+
if ctypes.sizeof(t) == 16:
48+
return "i128"
49+
50+
return t.__name__
51+
52+
53+
def _get_float_type_name(t: Type[T]) -> Any:
54+
"""Generates a human readable type name from a given float type."""
55+
if ctypes.sizeof(t) == 4:
4556
return "f32"
46-
if t.__name__ == "c_double" and ctypes.sizeof(t) == 8:
57+
if ctypes.sizeof(t) == 8:
4758
return "f64"
59+
if ctypes.sizeof(t) == 16:
60+
return "f128"
61+
62+
return t.__name__
63+
64+
65+
def get_type_name(t: Type[T]) -> Any:
66+
"""Generates a human readable type name from a given type."""
67+
if hasattr(t, "type_name"):
68+
return t.type_name()
69+
70+
if t.__name__ in (
71+
"c_ubyte",
72+
"c_ushort",
73+
"c_uint",
74+
"c_ulong",
75+
"c_ulonglong",
76+
"c_uint8",
77+
"c_uint16",
78+
"c_uint32",
79+
"c_uint64",
80+
"size_t",
81+
):
82+
return _get_unsigned_int_type_name(t)
83+
if t.__name__ in (
84+
"c_byte",
85+
"c_short",
86+
"c_int",
87+
"c_long",
88+
"c_longlong",
89+
"c_int8",
90+
"c_int16",
91+
"c_int32",
92+
"c_int64",
93+
"ssize_t",
94+
):
95+
return _get_signed_int_type_name(t)
96+
if t.__name__ in ("c_float", "c_double", "c_longdouble"):
97+
return _get_float_type_name(t)
98+
if t.__name__ == "c_bool":
99+
return "bool"
48100

49101
return t.__name__

iceoryx2-ffi/python/tests/get_typename_tests.py

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,61 @@
1717

1818

1919
def test_type_names_are_translated_correctly() -> None:
20-
assert iox2.get_type_name(ctypes.c_ubyte) == "u8"
21-
assert iox2.get_type_name(ctypes.c_uint8) == "u8"
20+
assert iox2.get_type_name(ctypes.c_bool) == "bool"
21+
assert iox2.get_type_name(ctypes.c_float) == "f32"
22+
assert iox2.get_type_name(ctypes.c_double) == "f64"
2223

23-
assert iox2.get_type_name(ctypes.c_ushort) == "u16"
24-
assert iox2.get_type_name(ctypes.c_uint16) == "u16"
24+
if ctypes.sizeof(ctypes.c_longdouble) == 16:
25+
assert iox2.get_type_name(ctypes.c_longdouble) == "f128"
2526

26-
assert iox2.get_type_name(ctypes.c_uint) == "u32"
27-
assert iox2.get_type_name(ctypes.c_uint32) == "u32"
2827

29-
assert iox2.get_type_name(ctypes.c_ulong) == "u64"
28+
def test_integer_type_names_are_translated_correctly() -> None:
29+
if ctypes.sizeof(ctypes.c_ubyte) == 1:
30+
assert iox2.get_type_name(ctypes.c_ubyte) == "u8"
31+
32+
if ctypes.sizeof(ctypes.c_ushort) == 2:
33+
assert iox2.get_type_name(ctypes.c_ushort) == "u16"
34+
35+
if ctypes.sizeof(ctypes.c_uint) == 4:
36+
assert iox2.get_type_name(ctypes.c_uint) == "u32"
37+
38+
if ctypes.sizeof(ctypes.c_ulong) == 4:
39+
assert iox2.get_type_name(ctypes.c_ulong) == "u32"
40+
elif ctypes.sizeof(ctypes.c_ulong) == 8:
41+
assert iox2.get_type_name(ctypes.c_ulong) == "u64"
42+
43+
if ctypes.sizeof(ctypes.c_ulonglong) == 8:
44+
assert iox2.get_type_name(ctypes.c_ulonglong) == "u64"
45+
elif ctypes.sizeof(ctypes.c_ulonglong) == 16:
46+
assert iox2.get_type_name(ctypes.c_ulonglong) == "u128"
47+
48+
if ctypes.sizeof(ctypes.c_byte) == 1:
49+
assert iox2.get_type_name(ctypes.c_byte) == "i8"
50+
51+
if ctypes.sizeof(ctypes.c_short) == 2:
52+
assert iox2.get_type_name(ctypes.c_short) == "i16"
53+
54+
if ctypes.sizeof(ctypes.c_int) == 4:
55+
assert iox2.get_type_name(ctypes.c_int) == "i32"
56+
57+
if ctypes.sizeof(ctypes.c_long) == 4:
58+
assert iox2.get_type_name(ctypes.c_long) == "i32"
59+
elif ctypes.sizeof(ctypes.c_long) == 8:
60+
assert iox2.get_type_name(ctypes.c_long) == "i64"
61+
62+
if ctypes.sizeof(ctypes.c_longlong) == 8:
63+
assert iox2.get_type_name(ctypes.c_longlong) == "i64"
64+
elif ctypes.sizeof(ctypes.c_longlong) == 16:
65+
assert iox2.get_type_name(ctypes.c_longlong) == "i128"
66+
67+
68+
def test_fixed_size_integer_type_names_are_translated_correctly() -> None:
69+
assert iox2.get_type_name(ctypes.c_uint8) == "u8"
70+
assert iox2.get_type_name(ctypes.c_uint16) == "u16"
71+
assert iox2.get_type_name(ctypes.c_uint32) == "u32"
3072
assert iox2.get_type_name(ctypes.c_uint64) == "u64"
3173

32-
assert iox2.get_type_name(ctypes.c_byte) == "i8"
3374
assert iox2.get_type_name(ctypes.c_int8) == "i8"
34-
35-
assert iox2.get_type_name(ctypes.c_short) == "i16"
3675
assert iox2.get_type_name(ctypes.c_int16) == "i16"
37-
38-
assert iox2.get_type_name(ctypes.c_int) == "i32"
3976
assert iox2.get_type_name(ctypes.c_int32) == "i32"
40-
41-
assert iox2.get_type_name(ctypes.c_long) == "i64"
4277
assert iox2.get_type_name(ctypes.c_int64) == "i64"
43-
44-
assert iox2.get_type_name(ctypes.c_bool) == "bool"
45-
assert iox2.get_type_name(ctypes.c_float) == "f32"
46-
assert iox2.get_type_name(ctypes.c_double) == "f64"

0 commit comments

Comments
 (0)