Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Lib/test/test_ctypes/test_struct_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ class S(Structure):
self.check_struct(S)
self.assertEqual(S.largeField.bit_size, size * 8)

def test_bitfield_overflow_error_message(self):
with self.assertRaisesRegex(
ValueError,
r"bit field 'x' overflows its type \(2 \+ 7 > 8\)",
):
CField(
name="x",
type=c_byte,
byte_size=1,
byte_offset=0,
index=0,
_internal_use=True,
bit_size=7,
bit_offset=2,
)

# __set__ and __get__ should raise a TypeError in case their self
# argument is not a ctype instance.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the ctypes bitfield overflow error message to report the correct offset and size calculation.
4 changes: 2 additions & 2 deletions Modules/_ctypes/cfield.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ PyCField_new_impl(PyTypeObject *type, PyObject *name, PyObject *proto,
if ((bitfield_size + bit_offset) > byte_size * 8) {
PyErr_Format(
PyExc_ValueError,
"bit field %R overflows its type (%zd + %zd >= %zd)",
name, bit_offset, byte_size*8);
"bit field %R overflows its type (%zd + %zd > %zd)",
name, bit_offset, bitfield_size, byte_size * 8);
goto error;
}
}
Expand Down
Loading