Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.12] gh-126937: ctypes: add test for maximum size of a struct field (GH-126938) (GH-127825) #127909

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Lib/test/test_ctypes/test_struct_fields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
import sys
from ctypes import *

class StructFieldsTestCase(unittest.TestCase):
Expand Down Expand Up @@ -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):
Expand Down
Loading