https://github.com/python/cpython/commit/d0368556b53023961835efb42ab9600a7055068e commit: d0368556b53023961835efb42ab9600a7055068e branch: 3.12 author: Petr Viktorin <[email protected]> committer: encukou <[email protected]> date: 2024-12-13T16:51:55+01:00 summary:
[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 <[email protected]> Co-authored-by: Peter Bierma <[email protected]> Co-authored-by: Terry Jan Reedy <[email protected]> (cherry-picked from d51c1444e36e2cfdb33087f165819a7cd6774a9e) files: M Lib/test/test_ctypes/test_struct_fields.py diff --git a/Lib/test/test_ctypes/test_struct_fields.py b/Lib/test/test_ctypes/test_struct_fields.py index e444f5e1f77919..2e7cd511886d41 100644 --- a/Lib/test/test_ctypes/test_struct_fields.py +++ b/Lib/test/test_ctypes/test_struct_fields.py @@ -1,4 +1,5 @@ import unittest +import sys from ctypes import * class StructFieldsTestCase(unittest.TestCase): @@ -69,6 +70,27 @@ def __init_subclass__(cls, **kwargs): 'ctypes state is not initialized'): class Subclass(BrokenStructure): ... + def test_max_field_size_gh126937(self): + # Classes for big structs should be created successfully. + # (But they most likely can't be instantiated.) + # The size must fit in Py_ssize_t. + + class X(Structure): + _fields_ = [('char', c_char),] + max_field_size = sys.maxsize + + class Y(Structure): + _fields_ = [('largeField', X * max_field_size)] + class Z(Structure): + _fields_ = [('largeField', c_char * max_field_size)] + + with self.assertRaises(OverflowError): + class TooBig(Structure): + _fields_ = [('largeField', X * (max_field_size + 1))] + with self.assertRaises(OverflowError): + class TooBig(Structure): + _fields_ = [('largeField', c_char * (max_field_size + 1))] + # __set__ and __get__ should raise a TypeError in case their self # argument is not a ctype instance. def test___set__(self): _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-checkins.python.org/ Member address: [email protected]
