Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3377d9a

Browse filesBrowse 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 3377d9a
Copy full SHA for 3377d9a

File tree

Expand file treeCollapse file tree

1 file changed

+14
-1
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+14
-1
lines changed

‎Lib/test/test_ctypes/test_struct_fields.py

Copy file name to clipboardExpand all lines: Lib/test/test_ctypes/test_struct_fields.py
+14-1Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22
import sys
3-
from ctypes import Structure, Union, sizeof, c_char, c_int, CField
3+
from ctypes import Structure, Union, sizeof, c_byte, c_char, c_int, CField
44
from ._support import Py_TPFLAGS_IMMUTABLETYPE, StructCheckMixin
55

66

@@ -75,6 +75,19 @@ def __init_subclass__(cls, **kwargs):
7575
'ctypes state is not initialized'):
7676
class Subclass(BrokenStructure): ...
7777

78+
def test_invalid_byte_size_raises(self):
79+
with self.assertRaises(ValueError) as cm:
80+
CField(
81+
name="a",
82+
type=c_byte,
83+
byte_size=2, # Wrong size: c_byte is only 1 byte
84+
byte_offset=2,
85+
index=1,
86+
_internal_use=True
87+
)
88+
89+
self.assertIn("does not match type size", str(cm.exception))
90+
7891
def test_max_field_size_gh126937(self):
7992
# Classes for big structs should be created successfully.
8093
# (But they most likely can't be instantiated.)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.