Skip to content

Commit 9ff0c71

Browse files
Melissa0x1f992ZeroIntensityterryjreedyencukou
committed
pythongh-126937: ctypes: add test for maximum size of a struct field (pythonGH-126938)
This backports the test from pythonGH-126938, with changed limit and exception class. Co-authored-by: Melissa0x1f992 <[email protected]> Co-authored-by: Peter Bierma <[email protected]> Co-authored-by: Terry Jan Reedy <[email protected]> Co-authored-by: Petr Viktorin <[email protected]>
1 parent acf05aa commit 9ff0c71

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Lib/test/test_ctypes/test_struct_fields.py

+22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
import sys
23
from ctypes import Structure, Union, sizeof, c_char, c_int
34
from ._support import (CField, Py_TPFLAGS_DISALLOW_INSTANTIATION,
45
Py_TPFLAGS_IMMUTABLETYPE)
@@ -75,6 +76,27 @@ def __init_subclass__(cls, **kwargs):
7576
'ctypes state is not initialized'):
7677
class Subclass(BrokenStructure): ...
7778

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

0 commit comments

Comments
 (0)