Skip to content

Commit d036855

Browse files
authored
[3.12] gh-126937: ctypes: add test for maximum size of a struct field (GH-126938) (GH-127825) (GH-127909)
This backports the *test* from GH-126938, with changed limit and exception class. Co-authored-by: Melissa0x1f992 <70096546+Melissa0x1f992@users.noreply.github.com> Co-authored-by: Peter Bierma <zintensitydev@gmail.com> Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu> (cherry-picked from d51c144)
1 parent 58916eb commit d036855

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Lib/test/test_ctypes/test_struct_fields.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
import sys
23
from ctypes import *
34

45
class StructFieldsTestCase(unittest.TestCase):
@@ -69,6 +70,27 @@ def __init_subclass__(cls, **kwargs):
6970
'ctypes state is not initialized'):
7071
class Subclass(BrokenStructure): ...
7172

73+
def test_max_field_size_gh126937(self):
74+
# Classes for big structs should be created successfully.
75+
# (But they most likely can't be instantiated.)
76+
# The size must fit in Py_ssize_t.
77+
78+
class X(Structure):
79+
_fields_ = [('char', c_char),]
80+
max_field_size = sys.maxsize
81+
82+
class Y(Structure):
83+
_fields_ = [('largeField', X * max_field_size)]
84+
class Z(Structure):
85+
_fields_ = [('largeField', c_char * max_field_size)]
86+
87+
with self.assertRaises(OverflowError):
88+
class TooBig(Structure):
89+
_fields_ = [('largeField', X * (max_field_size + 1))]
90+
with self.assertRaises(OverflowError):
91+
class TooBig(Structure):
92+
_fields_ = [('largeField', c_char * (max_field_size + 1))]
93+
7294
# __set__ and __get__ should raise a TypeError in case their self
7395
# argument is not a ctype instance.
7496
def test___set__(self):

0 commit comments

Comments
 (0)