Skip to content

Commit 2cdcb5b

Browse files
committed
Skip "is in" test for bitfields of underaligned types (bug filed)
1 parent d52b8c9 commit 2cdcb5b

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

Lib/test/test_ctypes/_support.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222
Py_TPFLAGS_IMMUTABLETYPE = 1 << 8
2323

2424

25+
def is_underaligned(ctype):
26+
"""Return true when type's alignment is less than its size.
27+
28+
A famous example is 64-bit int on 32-bit x86.
29+
"""
30+
return ctypes.alignment(ctype) < ctypes.sizeof(ctype)
31+
32+
2533
class StructCheckMixin:
2634
def check_struct(self, structure):
2735
"""Assert that a structure is well-formed"""
@@ -70,8 +78,14 @@ def _check_struct_or_union(self, cls, is_struct):
7078
# byte_size
7179
self.assertEqual(field.byte_size, ctypes.sizeof(field.type))
7280
self.assertGreaterEqual(field.byte_size, 0)
73-
self.assertLessEqual(field.byte_offset + field.byte_size,
74-
cls_size)
81+
82+
# Check that the field is inside the struct.
83+
# See gh-130410 for why this is skipped for bitfields of
84+
# underaligned types. Later in this function (see `bit_end`)
85+
# we assert that the value *bits* are inside the struct.
86+
if not (field.is_bitfield and is_underaligned(field.type)):
87+
self.assertLessEqual(field.byte_offset + field.byte_size,
88+
cls_size)
7589

7690
# size
7791
self.assertGreaterEqual(field.size, 0)

0 commit comments

Comments
 (0)