@@ -1869,6 +1869,20 @@ static PyTypeObject TestClass = {
18691869 .tp_methods = test_class_methods ,
18701870};
18711871
1872+ /* Heap type sharing TestClass's methods, to test defining_class on both. */
1873+ static PyType_Slot HeapTestClass_slots [] = {
1874+ {Py_tp_new , PyType_GenericNew },
1875+ {Py_tp_methods , test_class_methods },
1876+ {0 , NULL },
1877+ };
1878+
1879+ static PyType_Spec HeapTestClass_spec = {
1880+ .name = "_testclinic.HeapTestClass" ,
1881+ .basicsize = sizeof (PyObject ),
1882+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE ,
1883+ .slots = HeapTestClass_slots ,
1884+ };
1885+
18721886
18731887/*[clinic input]
18741888output push
@@ -1923,13 +1937,17 @@ static struct PyMethodDef depr_star_new_methods[] = {
19231937 {NULL , NULL }
19241938};
19251939
1926- static PyTypeObject DeprStarNew = {
1927- PyVarObject_HEAD_INIT (NULL , 0 )
1928- .tp_name = "_testclinic.DeprStarNew" ,
1929- .tp_basicsize = sizeof (PyObject ),
1930- .tp_new = depr_star_new ,
1931- .tp_flags = Py_TPFLAGS_DEFAULT ,
1932- .tp_methods = depr_star_new_methods ,
1940+ static PyType_Slot DeprStarNew_slots [] = {
1941+ {Py_tp_new , depr_star_new },
1942+ {Py_tp_methods , depr_star_new_methods },
1943+ {0 , NULL },
1944+ };
1945+
1946+ static PyType_Spec DeprStarNew_spec = {
1947+ .name = "_testclinic.DeprStarNew" ,
1948+ .basicsize = sizeof (PyObject ),
1949+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE ,
1950+ .slots = DeprStarNew_slots ,
19331951};
19341952
19351953
@@ -1964,14 +1982,18 @@ static struct PyMethodDef depr_star_init_methods[] = {
19641982 {NULL , NULL }
19651983};
19661984
1967- static PyTypeObject DeprStarInit = {
1968- PyVarObject_HEAD_INIT (NULL , 0 )
1969- .tp_name = "_testclinic.DeprStarInit" ,
1970- .tp_basicsize = sizeof (PyObject ),
1971- .tp_new = PyType_GenericNew ,
1972- .tp_init = depr_star_init ,
1973- .tp_flags = Py_TPFLAGS_DEFAULT ,
1974- .tp_methods = depr_star_init_methods ,
1985+ static PyType_Slot DeprStarInit_slots [] = {
1986+ {Py_tp_new , PyType_GenericNew },
1987+ {Py_tp_init , depr_star_init },
1988+ {Py_tp_methods , depr_star_init_methods },
1989+ {0 , NULL },
1990+ };
1991+
1992+ static PyType_Spec DeprStarInit_spec = {
1993+ .name = "_testclinic.DeprStarInit" ,
1994+ .basicsize = sizeof (PyObject ),
1995+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE ,
1996+ .slots = DeprStarInit_slots ,
19751997};
19761998
19771999
@@ -1995,13 +2017,17 @@ depr_star_init_noinline_impl(PyObject *self, PyObject *a, PyObject *b,
19952017 return 0 ;
19962018}
19972019
1998- static PyTypeObject DeprStarInitNoInline = {
1999- PyVarObject_HEAD_INIT (NULL , 0 )
2000- .tp_name = "_testclinic.DeprStarInitNoInline" ,
2001- .tp_basicsize = sizeof (PyObject ),
2002- .tp_new = PyType_GenericNew ,
2003- .tp_init = depr_star_init_noinline ,
2004- .tp_flags = Py_TPFLAGS_DEFAULT ,
2020+ static PyType_Slot DeprStarInitNoInline_slots [] = {
2021+ {Py_tp_new , PyType_GenericNew },
2022+ {Py_tp_init , depr_star_init_noinline },
2023+ {0 , NULL },
2024+ };
2025+
2026+ static PyType_Spec DeprStarInitNoInline_spec = {
2027+ .name = "_testclinic.DeprStarInitNoInline" ,
2028+ .basicsize = sizeof (PyObject ),
2029+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE ,
2030+ .slots = DeprStarInitNoInline_slots ,
20052031};
20062032
20072033
@@ -2021,12 +2047,16 @@ depr_kwd_new_impl(PyTypeObject *type, PyObject *a)
20212047 return type -> tp_alloc (type , 0 );
20222048}
20232049
2024- static PyTypeObject DeprKwdNew = {
2025- PyVarObject_HEAD_INIT (NULL , 0 )
2026- .tp_name = "_testclinic.DeprKwdNew" ,
2027- .tp_basicsize = sizeof (PyObject ),
2028- .tp_new = depr_kwd_new ,
2029- .tp_flags = Py_TPFLAGS_DEFAULT ,
2050+ static PyType_Slot DeprKwdNew_slots [] = {
2051+ {Py_tp_new , depr_kwd_new },
2052+ {0 , NULL },
2053+ };
2054+
2055+ static PyType_Spec DeprKwdNew_spec = {
2056+ .name = "_testclinic.DeprKwdNew" ,
2057+ .basicsize = sizeof (PyObject ),
2058+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE ,
2059+ .slots = DeprKwdNew_slots ,
20302060};
20312061
20322062
@@ -2045,13 +2075,17 @@ depr_kwd_init_impl(PyObject *self, PyObject *a)
20452075 return 0 ;
20462076}
20472077
2048- static PyTypeObject DeprKwdInit = {
2049- PyVarObject_HEAD_INIT (NULL , 0 )
2050- .tp_name = "_testclinic.DeprKwdInit" ,
2051- .tp_basicsize = sizeof (PyObject ),
2052- .tp_new = PyType_GenericNew ,
2053- .tp_init = depr_kwd_init ,
2054- .tp_flags = Py_TPFLAGS_DEFAULT ,
2078+ static PyType_Slot DeprKwdInit_slots [] = {
2079+ {Py_tp_new , PyType_GenericNew },
2080+ {Py_tp_init , depr_kwd_init },
2081+ {0 , NULL },
2082+ };
2083+
2084+ static PyType_Spec DeprKwdInit_spec = {
2085+ .name = "_testclinic.DeprKwdInit" ,
2086+ .basicsize = sizeof (PyObject ),
2087+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE ,
2088+ .slots = DeprKwdInit_slots ,
20552089};
20562090
20572091
@@ -2075,13 +2109,17 @@ depr_kwd_init_noinline_impl(PyObject *self, PyObject *a, PyObject *b,
20752109 return 0 ;
20762110}
20772111
2078- static PyTypeObject DeprKwdInitNoInline = {
2079- PyVarObject_HEAD_INIT (NULL , 0 )
2080- .tp_name = "_testclinic.DeprKwdInitNoInline" ,
2081- .tp_basicsize = sizeof (PyObject ),
2082- .tp_new = PyType_GenericNew ,
2083- .tp_init = depr_kwd_init_noinline ,
2084- .tp_flags = Py_TPFLAGS_DEFAULT ,
2112+ static PyType_Slot DeprKwdInitNoInline_slots [] = {
2113+ {Py_tp_new , PyType_GenericNew },
2114+ {Py_tp_init , depr_kwd_init_noinline },
2115+ {0 , NULL },
2116+ };
2117+
2118+ static PyType_Spec DeprKwdInitNoInline_spec = {
2119+ .name = "_testclinic.DeprKwdInitNoInline" ,
2120+ .basicsize = sizeof (PyObject ),
2121+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE ,
2122+ .slots = DeprKwdInitNoInline_slots ,
20852123};
20862124
20872125
@@ -2843,62 +2881,56 @@ static PyMethodDef tester_methods[] = {
28432881 {NULL , NULL }
28442882};
28452883
2884+ static int
2885+ _testclinic_exec (PyObject * m )
2886+ {
2887+ PyTypeObject * static_types [] = {
2888+ & TestClass , & VcNew_Type , & VcNewBase_Type , & VcKwOnly_Type ,
2889+ };
2890+ for (size_t i = 0 ; i < Py_ARRAY_LENGTH (static_types ); i ++ ) {
2891+ if (PyModule_AddType (m , static_types [i ]) < 0 ) {
2892+ return -1 ;
2893+ }
2894+ }
2895+
2896+ PyType_Spec * heap_specs [] = {
2897+ & HeapTestClass_spec ,
2898+ & DeprStarNew_spec , & DeprStarInit_spec , & DeprStarInitNoInline_spec ,
2899+ & DeprKwdNew_spec , & DeprKwdInit_spec , & DeprKwdInitNoInline_spec ,
2900+ & VcInit_spec ,
2901+ };
2902+ for (size_t i = 0 ; i < Py_ARRAY_LENGTH (heap_specs ); i ++ ) {
2903+ PyObject * type = PyType_FromModuleAndSpec (m , heap_specs [i ], NULL );
2904+ if (type == NULL ) {
2905+ return -1 ;
2906+ }
2907+ int rc = PyModule_AddType (m , (PyTypeObject * )type );
2908+ Py_DECREF (type );
2909+ if (rc < 0 ) {
2910+ return -1 ;
2911+ }
2912+ }
2913+ return 0 ;
2914+ }
2915+
2916+ static PyModuleDef_Slot _testclinic_slots [] = {
2917+ {Py_mod_exec , _testclinic_exec },
2918+ {Py_mod_gil , Py_MOD_GIL_NOT_USED },
2919+ {0 , NULL },
2920+ };
2921+
28462922static struct PyModuleDef _testclinic_module = {
28472923 PyModuleDef_HEAD_INIT ,
28482924 .m_name = "_testclinic" ,
28492925 .m_size = 0 ,
28502926 .m_methods = tester_methods ,
2927+ .m_slots = _testclinic_slots ,
28512928};
28522929
28532930PyMODINIT_FUNC
28542931PyInit__testclinic (void )
28552932{
2856- PyObject * m = PyModule_Create (& _testclinic_module );
2857- if (m == NULL ) {
2858- return NULL ;
2859- }
2860- #ifdef Py_GIL_DISABLED
2861- PyUnstable_Module_SetGIL (m , Py_MOD_GIL_NOT_USED );
2862- #endif
2863- if (PyModule_AddType (m , & TestClass ) < 0 ) {
2864- goto error ;
2865- }
2866- if (PyModule_AddType (m , & DeprStarNew ) < 0 ) {
2867- goto error ;
2868- }
2869- if (PyModule_AddType (m , & DeprStarInit ) < 0 ) {
2870- goto error ;
2871- }
2872- if (PyModule_AddType (m , & DeprStarInitNoInline ) < 0 ) {
2873- goto error ;
2874- }
2875- if (PyModule_AddType (m , & DeprKwdNew ) < 0 ) {
2876- goto error ;
2877- }
2878- if (PyModule_AddType (m , & DeprKwdInit ) < 0 ) {
2879- goto error ;
2880- }
2881- if (PyModule_AddType (m , & DeprKwdInitNoInline ) < 0 ) {
2882- goto error ;
2883- }
2884- if (PyModule_AddType (m , & VcNew_Type ) < 0 ) {
2885- goto error ;
2886- }
2887- if (PyModule_Add (m , "VcInit" ,
2888- PyType_FromModuleAndSpec (m , & VcInit_spec , NULL )) < 0 ) {
2889- goto error ;
2890- }
2891- if (PyModule_AddType (m , & VcNewBase_Type ) < 0 ) {
2892- goto error ;
2893- }
2894- if (PyModule_AddType (m , & VcKwOnly_Type ) < 0 ) {
2895- goto error ;
2896- }
2897- return m ;
2898-
2899- error :
2900- Py_DECREF (m );
2901- return NULL ;
2933+ return PyModuleDef_Init (& _testclinic_module );
29022934}
29032935
29042936#undef RETURN_PACKED_ARGS
0 commit comments