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

gh-114314: ctypes: remove stgdict and switch to heap types #116458

Merged
merged 60 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
6155211
Move paramfunc into a new struct, PyStgInfo
encukou Feb 16, 2024
0171c30
Move the StgInfo out of StgDictObject and into the classes themselves
encukou Feb 16, 2024
acd91e9
Move StgInfo init/retrieval right after the corresponding StgDict one
encukou Feb 19, 2024
55af504
Move `size` from StgDict to StgInfo
encukou Feb 19, 2024
27e5c50
Move `align` from StgDict to StgInfo
encukou Feb 19, 2024
01bc637
Move `length` from StgDict to StgInfo, except for sizeof calculation
encukou Feb 19, 2024
c8017e9
Move part of the sizeof calculation
encukou Feb 19, 2024
40b012f
Move `ffi_type_pointer` from StgDict to StgInfo
encukou Feb 19, 2024
537defb
Move `proto` from StgDict to StgInfo. Doesn't work just yet.
encukou Feb 20, 2024
ace6623
Hack: Reserve space for StgInfo in static type objects
encukou Feb 20, 2024
6f775b9
If StgInfo is not initialized, act as if it's not there
encukou Feb 20, 2024
4aaa06a
Move `getfunc` & `setfunc`
encukou Feb 20, 2024
6808524
Move `argtypes`
encukou Feb 20, 2024
f031c00
Move `converters`
encukou Feb 20, 2024
535176c
Move `restype`
encukou Feb 20, 2024
5260003
Move `checker`
encukou Feb 20, 2024
0d3017e
Move `flags`
encukou Feb 20, 2024
269b825
Move `format`
encukou Feb 20, 2024
946f474
Move `ndim`
encukou Feb 20, 2024
607ed23
Move `shape`
encukou Feb 21, 2024
cb126b7
Replace more uses of StgDict
encukou Feb 21, 2024
fd04c91
Don't pass dicts to PyCStgDict_clone
encukou Feb 21, 2024
8dce549
Don't set StgDict as type dict; use name `attrdict` for the type __di…
encukou Feb 21, 2024
6705e0e
Remove more uses of StgDict
encukou Feb 21, 2024
8f99377
Remove StgDict
encukou Feb 21, 2024
daea3e5
Remove StgDict from comments, names, and platform-specific code
encukou Feb 21, 2024
f67211f
Switch PyCSimpleType_new to _init
encukou Feb 23, 2024
02aa9e1
Switch _SimpleCData to a heap type
encukou Feb 23, 2024
13feac7
Convert Struct and Union type _new to _init
encukou Feb 23, 2024
a68417b
Switch PyCPointerType_new to _init
encukou Feb 23, 2024
ad5d7b4
Switch PyCFuncPtrType_new to _init
encukou Feb 23, 2024
8d5e230
Switch CFuncPtr to heap type
encukou Feb 23, 2024
cdfae13
Switch Array to heap type
encukou Feb 23, 2024
2d80960
Switch _Pointer to a heap type
encukou Feb 23, 2024
3337ba4
Switch _ctypes.Union to heap type
encukou Feb 23, 2024
596cb89
Switch Structure to heap type
encukou Feb 23, 2024
91feb64
Make the type-adding macros more regular
encukou Feb 23, 2024
77c64fe
Switch _CData to a heap type
encukou Feb 23, 2024
0be46a1
Visit/decref type in PyCData visit/dealloc
encukou Feb 23, 2024
9be3520
Remove _HackyHeapType
encukou Feb 23, 2024
e523c2f
Decref attrdicts
encukou Mar 1, 2024
fb70e43
Delegate CType_Type deallocation to PyType_Type.tp_dealloc
encukou Mar 5, 2024
cc59dea
Fix Windows-specific crashes
neonene Mar 5, 2024
9bd9eb1
Update comments
encukou Mar 6, 2024
946a0b1
Add tests for metaclass details
encukou Mar 6, 2024
970b41d
Fix getting the module state
encukou Mar 7, 2024
2a450ad
Add a blurb, even though this *should* not affect usage
encukou Mar 7, 2024
9878809
Apply suggestions from code review
encukou Mar 11, 2024
b2ba89a
Inline PyStgInfo_From* & PyStgInfo_Init for performance
encukou Mar 11, 2024
605c30c
Make CType_Type's clear & dealloc safer
encukou Mar 11, 2024
41dea1b
Remove self decrefs in error cases in init functions
encukou Mar 11, 2024
be0888f
Add `static` to PyType_Spec
encukou Mar 11, 2024
ac6eb38
Remove redundant comment
encukou Mar 11, 2024
33be37f
Apply suggestions from code review
encukou Mar 11, 2024
0b9d0a7
Fix comment
encukou Mar 12, 2024
f02bad6
PyErr_WriteUnraisable clears the exception
encukou Mar 12, 2024
16faac7
Call tp_init for argument validation
encukou Mar 20, 2024
2461e50
Add test for swapped type creation
neonene Mar 20, 2024
ab90768
Merge branch 'main' into ctypes-no-stgdict
encukou Mar 20, 2024
de054af
Fix test_swapped_type_creation
encukou Mar 20, 2024
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
16 changes: 16 additions & 0 deletions Lib/test/test_ctypes/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ def test_type_flags(self):
self.assertTrue(cls.__flags__ & Py_TPFLAGS_IMMUTABLETYPE)
self.assertFalse(cls.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION)

def test_metaclass_details(self):
# Abstract classes (whose metaclass __init__ was not called) can't be
# instantiated directly
NewArray = PyCArrayType.__new__(PyCArrayType, 'NewArray', (Array,), {})
for cls in Array, NewArray:
with self.subTest(cls=cls):
with self.assertRaisesRegex(TypeError, "abstract class"):
obj = cls()

# Cannot call the metaclass __init__ more than once
class T(Array):
_type_ = c_int
_length_ = 13
with self.assertRaisesRegex(SystemError, "already initialized"):
PyCArrayType.__init__(T, 'ptr', (), {})

def test_simple(self):
# create classes holding simple numeric types, and check
# various properties.
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_ctypes/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_pyobject(self):

def test_unsupported_restype_1(self):
# Only "fundamental" result types are supported for callback
# functions, the type must have a non-NULL stgdict->setfunc.
# functions, the type must have a non-NULL stginfo->setfunc.
# POINTER(c_double), for example, is not supported.

prototype = self.functype.__func__(POINTER(c_double))
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_ctypes/test_funcptr.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def test_type_flags(self):
self.assertTrue(_CFuncPtr.__flags__ & Py_TPFLAGS_IMMUTABLETYPE)
self.assertFalse(_CFuncPtr.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION)

def test_metaclass_details(self):
# Cannot call the metaclass __init__ more than once
CdeclCallback = CFUNCTYPE(c_int, c_int, c_int)
with self.assertRaisesRegex(SystemError, "already initialized"):
PyCFuncPtrType.__init__(CdeclCallback, 'ptr', (), {})

def test_basic(self):
X = WINFUNCTYPE(c_int, c_int, c_int)

Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_ctypes/test_pointers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ def test_type_flags(self):
self.assertTrue(_Pointer.__flags__ & Py_TPFLAGS_IMMUTABLETYPE)
self.assertFalse(_Pointer.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION)

def test_metaclass_details(self):
# Cannot call the metaclass __init__ more than once
with self.assertRaisesRegex(SystemError, "already initialized"):
PyCPointerType.__init__(POINTER(c_byte), 'ptr', (), {})

def test_pointer_crash(self):

class A(POINTER(c_ulong)):
Expand Down
23 changes: 23 additions & 0 deletions Lib/test/test_ctypes/test_simplesubclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,29 @@ def test_type_flags(self):
self.assertTrue(_SimpleCData.__flags__ & Py_TPFLAGS_IMMUTABLETYPE)
self.assertFalse(_SimpleCData.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION)

def test_metaclass_details(self):
# Abstract classes (whose metaclass __init__ was not called) can't be
# instantiated directly
NewT = PyCSimpleType.__new__(PyCSimpleType, 'NewT', (_SimpleCData,), {})
for cls in _SimpleCData, NewT:
with self.subTest(cls=cls):
with self.assertRaisesRegex(TypeError, "abstract class"):
obj = cls()

# Cannot call the metaclass __init__ more than once
class T(_SimpleCData):
_type_ = "i"
with self.assertRaisesRegex(SystemError, "already initialized"):
PyCSimpleType.__init__(T, 'ptr', (), {})

def test_swapped_type_creation(self):
cls = PyCSimpleType.__new__(PyCSimpleType, '', (), {'_type_': 'i'})
with self.assertRaises(TypeError):
PyCSimpleType.__init__(cls)
PyCSimpleType.__init__(cls, '', (), {'_type_': 'i'})
self.assertEqual(cls.__ctype_le__.__dict__.get('_type_'), 'i')
self.assertEqual(cls.__ctype_be__.__dict__.get('_type_'), 'i')

def test_compare(self):
self.assertEqual(MyInt(3), MyInt(3))
self.assertNotEqual(MyInt(42), MyInt(43))
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_ctypes/test_struct_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_cfield_inheritance_hierarchy(self):
def test_gh99275(self):
class BrokenStructure(Structure):
def __init_subclass__(cls, **kwargs):
cls._fields_ = [] # This line will fail, `stgdict` is not ready
cls._fields_ = [] # This line will fail, `stginfo` is not ready

with self.assertRaisesRegex(TypeError,
'ctypes state is not initialized'):
Expand Down
25 changes: 21 additions & 4 deletions Lib/test/test_ctypes/test_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ def test_type_flags(self):
self.assertTrue(Structure.__flags__ & Py_TPFLAGS_IMMUTABLETYPE)
self.assertFalse(Structure.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION)

def test_metaclass_details(self):
# Abstract classes (whose metaclass __init__ was not called) can't be
# instantiated directly
NewStructure = PyCStructType.__new__(PyCStructType, 'NewStructure',
(Structure,), {})
for cls in Structure, NewStructure:
with self.subTest(cls=cls):
with self.assertRaisesRegex(TypeError, "abstract class"):
obj = cls()

# Cannot call the metaclass __init__ more than once
class T(Structure):
_fields_ = [("x", c_char),
("y", c_char)]
with self.assertRaisesRegex(SystemError, "already initialized"):
PyCStructType.__init__(T, 'ptr', (), {})

def test_simple_structs(self):
for code, tp in self.formats.items():
class X(Structure):
Expand Down Expand Up @@ -507,8 +524,8 @@ def _test_issue18060(self, Vector):
@unittest.skipUnless(sys.byteorder == 'little', "can't test on this platform")
def test_issue18060_a(self):
# This test case calls
# PyCStructUnionType_update_stgdict() for each
# _fields_ assignment, and PyCStgDict_clone()
# PyCStructUnionType_update_stginfo() for each
# _fields_ assignment, and PyCStgInfo_clone()
# for the Mid and Vector class definitions.
class Base(Structure):
_fields_ = [('y', c_double),
Expand All @@ -523,7 +540,7 @@ class Vector(Mid): pass
@unittest.skipUnless(sys.byteorder == 'little', "can't test on this platform")
def test_issue18060_b(self):
# This test case calls
# PyCStructUnionType_update_stgdict() for each
# PyCStructUnionType_update_stginfo() for each
# _fields_ assignment.
class Base(Structure):
_fields_ = [('y', c_double),
Expand All @@ -538,7 +555,7 @@ class Vector(Mid):
@unittest.skipUnless(sys.byteorder == 'little', "can't test on this platform")
def test_issue18060_c(self):
# This test case calls
# PyCStructUnionType_update_stgdict() for each
# PyCStructUnionType_update_stginfo() for each
# _fields_ assignment.
class Base(Structure):
_fields_ = [('y', c_double)]
Expand Down
19 changes: 18 additions & 1 deletion Lib/test/test_ctypes/test_unions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from ctypes import Union
from ctypes import Union, c_char
from ._support import (_CData, UnionType, Py_TPFLAGS_DISALLOW_INSTANTIATION,
Py_TPFLAGS_IMMUTABLETYPE)

Expand All @@ -16,3 +16,20 @@ def test_type_flags(self):
with self.subTest(cls=Union):
self.assertTrue(Union.__flags__ & Py_TPFLAGS_IMMUTABLETYPE)
self.assertFalse(Union.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION)

def test_metaclass_details(self):
# Abstract classes (whose metaclass __init__ was not called) can't be
# instantiated directly
NewUnion = UnionType.__new__(UnionType, 'NewUnion',
(Union,), {})
for cls in Union, NewUnion:
with self.subTest(cls=cls):
with self.assertRaisesRegex(TypeError, "abstract class"):
obj = cls()

# Cannot call the metaclass __init__ more than once
class T(Union):
_fields_ = [("x", c_char),
("y", c_char)]
with self.assertRaisesRegex(SystemError, "already initialized"):
UnionType.__init__(T, 'ptr', (), {})
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
In :mod:`ctypes`, ctype data is now stored in type objects directly rather
than in a dict subclass. This is an internal change that should not affect
usage.
Loading
Loading