Skip to content

Commit c869028

Browse files
committed
Add regression test for invalid byte_size in ctypes.CField
Adds a test to ensure that creating a ctypes.CField with a byte_size that doesn't match the underlying C type size (e.g., 2 bytes for c_byte) correctly raises a ValueError. This test verifies the fix for gh-132470 and prevents future regressions where such mismatches could cause an abort.
1 parent 45aa6a3 commit c869028

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

Lib/test/leakers/test_ctypes.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
# Taken from Lib/test/test_ctypes/test_keeprefs.py, PointerToStructure.test().
33

4-
from ctypes import Structure, c_int, POINTER
4+
from ctypes import Structure, c_int, c_byte, POINTER, CField
55
import gc
66

77
def leak_inner():
@@ -13,3 +13,17 @@ class RECT(Structure):
1313
def leak():
1414
leak_inner()
1515
gc.collect()
16+
17+
18+
def test_invalid_byte_size_raises(self):
19+
with self.assertRaises(ValueError) as cm:
20+
CField(
21+
name="a",
22+
type=c_byte,
23+
byte_size=2, # Wrong size: c_byte is only 1 byte
24+
byte_offset=2,
25+
index=1,
26+
_internal_use=True
27+
)
28+
29+
self.assertIn("does not match type size", str(cm.exception))

0 commit comments

Comments
 (0)