|  | 
| 1 | 1 | import unittest | 
|  | 2 | +import sys | 
| 2 | 3 | from ctypes import Structure, Union, sizeof, c_char, c_int | 
| 3 | 4 | from ._support import (CField, Py_TPFLAGS_DISALLOW_INSTANTIATION, | 
| 4 | 5 |                        Py_TPFLAGS_IMMUTABLETYPE) | 
| @@ -75,6 +76,27 @@ def __init_subclass__(cls, **kwargs): | 
| 75 | 76 |                                     'ctypes state is not initialized'): | 
| 76 | 77 |             class Subclass(BrokenStructure): ... | 
| 77 | 78 | 
 | 
|  | 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 | + | 
| 78 | 100 |     # __set__ and __get__ should raise a TypeError in case their self | 
| 79 | 101 |     # argument is not a ctype instance. | 
| 80 | 102 |     def test___set__(self): | 
|  | 
0 commit comments