Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d51c144

Browse filesBrowse files
encukouMelissa0x1f992ZeroIntensityterryjreedy
authored
[3.13] gh-126937: ctypes: add test for maximum size of a struct field (GH-126938) (GH-127825)
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>
1 parent 2a78663 commit d51c144
Copy full SHA for d51c144

File tree

1 file changed

+22
-0
lines changed
Filter options

1 file changed

+22
-0
lines changed

‎Lib/test/test_ctypes/test_struct_fields.py

Copy file name to clipboardExpand all lines: Lib/test/test_ctypes/test_struct_fields.py
+22Lines 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 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.