import unittest
import sys
-from ctypes import Structure, Union, sizeof, c_char, c_int, CField
+from ctypes import Structure, Union, sizeof, c_byte, c_char, c_int, CField
from ._support import Py_TPFLAGS_IMMUTABLETYPE, StructCheckMixin
'ctypes state is not initialized'):
class Subclass(BrokenStructure): ...
+ def test_invalid_byte_size_raises_gh132470(self):
+ with self.assertRaisesRegex(ValueError, r"does not match type size"):
+ CField(
+ name="a",
+ type=c_byte,
+ byte_size=2, # Wrong size: c_byte is only 1 byte
+ byte_offset=2,
+ index=1,
+ _internal_use=True
+ )
+
def test_max_field_size_gh126937(self):
# Classes for big structs should be created successfully.
# (But they most likely can't be instantiated.)
"type of field %R must be a C type", name);
goto error;
}
- assert(byte_size == info->size);
+ if (byte_size != info->size) {
+ PyErr_Format(PyExc_ValueError,
+ "byte size of field %R (%zd) does not match type size (%zd)",
+ name, byte_size, info->size);
+ goto error;
+ }
Py_ssize_t bitfield_size = 0;
Py_ssize_t bit_offset = 0;