https://github.com/python/cpython/commit/4f3f1d2bc0864b4d70552d0c198829ce2efdceb8
commit: 4f3f1d2bc0864b4d70552d0c198829ce2efdceb8
branch: main
author: Cody Maloney <[email protected]>
committer: asvetlov <[email protected]>
date: 2026-11-22T17:07:10+01:00
summary:

gh-150097: Move _testclinic to multi-phase init (#162406)

Convert the DeprStar*/DeprKwd* test types to heap types, and add a
HeapTestClass so the defining_class tests run against both a static
and a heap type. TestClass and the VcNew/VcNewBase/VcKwOnly vectorcall
types stay static; VcInit remains the heap vectorcall type.

Co-authored-by: Claude Opus 5.5 <[email protected]>

files:
M Lib/test/test_clinic.py
M Modules/_testclinic.c
M Tools/c-analyzer/cpython/globals-to-fix.tsv

diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py
index 4e0b0137e9faa0b..775628251f79f4b 100644
--- a/Lib/test/test_clinic.py
+++ b/Lib/test/test_clinic.py
@@ -2,7 +2,7 @@
 # Copyright 2012-2013 by Larry Hastings.
 # Licensed to the PSF under a contributor agreement.
 
-from functools import partial
+from functools import partial, wraps
 from test import support, test_tools
 from test.support import force_not_colorized_test_class
 from test.support import os_helper
@@ -4621,6 +4621,16 @@ def test_file_dest(self):
 except ImportError:
     ac_tester = None
 
+def static_and_heap(test):
+    # Run test with both a static type and a heap type.
+    @wraps(test)
+    def wrapper(self):
+        for cls in ac_tester.TestClass, ac_tester.HeapTestClass:
+            with self.subTest(cls=cls):
+                test(self, cls)
+    return wrapper
+
+
 @unittest.skipIf(ac_tester is None, "_testclinic is missing")
 class ClinicFunctionalTest(unittest.TestCase):
     locals().update((name, getattr(ac_tester, name))
@@ -5295,43 +5305,46 @@ def 
test_cloned_func_with_converter_exception_message(self):
                 func = getattr(ac_tester, name)
                 self.assertEqual(func(), name)
 
-    def test_get_defining_class(self):
-        obj = ac_tester.TestClass()
+    @static_and_heap
+    def test_get_defining_class(self, cls):
+        obj = cls()
         meth = obj.get_defining_class
-        self.assertIs(obj.get_defining_class(), ac_tester.TestClass)
+        self.assertIs(obj.get_defining_class(), cls)
 
         # 'defining_class' argument is a positional only argument
         with self.assertRaises(TypeError):
-            obj.get_defining_class_arg(cls=ac_tester.TestClass)
+            obj.get_defining_class_arg(cls=cls)
 
         check = partial(self.assertRaisesRegex, TypeError, "no arguments")
         check(meth, 1)
         check(meth, a=1)
 
-    def test_get_defining_class_capi(self):
+    @static_and_heap
+    def test_get_defining_class_capi(self, cls):
         from _testcapi import pyobject_vectorcall
-        obj = ac_tester.TestClass()
+        obj = cls()
         meth = obj.get_defining_class
         pyobject_vectorcall(meth, None, None)
         pyobject_vectorcall(meth, (), None)
         pyobject_vectorcall(meth, (), ())
         pyobject_vectorcall(meth, None, ())
-        self.assertIs(pyobject_vectorcall(meth, (), ()), ac_tester.TestClass)
+        self.assertIs(pyobject_vectorcall(meth, (), ()), cls)
 
         check = partial(self.assertRaisesRegex, TypeError, "no arguments")
         check(pyobject_vectorcall, meth, (1,), None)
         check(pyobject_vectorcall, meth, (1,), ("a",))
 
-    def test_get_defining_class_arg(self):
-        obj = ac_tester.TestClass()
+    @static_and_heap
+    def test_get_defining_class_arg(self, cls):
+        obj = cls()
         self.assertEqual(obj.get_defining_class_arg("arg"),
-                         (ac_tester.TestClass, "arg"))
+                         (cls, "arg"))
         self.assertEqual(obj.get_defining_class_arg(arg=123),
-                         (ac_tester.TestClass, 123))
+                         (cls, 123))
 
         # 'defining_class' argument is a positional only argument
         with self.assertRaises(TypeError):
-            obj.get_defining_class_arg(cls=ac_tester.TestClass, arg="arg")
+            obj.get_defining_class_arg(cls=cls, arg="arg")
 
         # wrong number of arguments
         with self.assertRaises(TypeError):
@@ -5339,9 +5352,9 @@ def test_get_defining_class_arg(self):
         with self.assertRaises(TypeError):
             obj.get_defining_class_arg("arg1", "arg2")
 
-    def test_defclass_varpos(self):
+    @static_and_heap
+    def test_defclass_varpos(self, cls):
         # fn(*args)
-        cls = ac_tester.TestClass
         obj = cls()
         fn = obj.defclass_varpos
         self.assertEqual(fn(), (cls, ()))
@@ -5351,9 +5364,9 @@ def test_defclass_varpos(self):
         self.assertEqual(fn(obj), (cls, ()))
         self.assertEqual(fn(obj, 1, 2), (cls, (1, 2)))
 
-    def test_defclass_posonly_varpos(self):
+    @static_and_heap
+    def test_defclass_posonly_varpos(self, cls):
         # fn(a, b, /, *args)
-        cls = ac_tester.TestClass
         obj = cls()
         fn = obj.defclass_posonly_varpos
         errmsg = 'expected at least 2 arguments'
diff --git a/Modules/_testclinic.c b/Modules/_testclinic.c
index b875647598c8c0a..007dd30068c38f4 100644
--- a/Modules/_testclinic.c
+++ b/Modules/_testclinic.c
@@ -1869,6 +1869,20 @@ static PyTypeObject TestClass = {
     .tp_methods = test_class_methods,
 };
 
+/* Heap type sharing TestClass's methods, to test defining_class on both. */
+static PyType_Slot HeapTestClass_slots[] = {
+    {Py_tp_new, PyType_GenericNew},
+    {Py_tp_methods, test_class_methods},
+    {0, NULL},
+};
+
+static PyType_Spec HeapTestClass_spec = {
+    .name = "_testclinic.HeapTestClass",
+    .basicsize = sizeof(PyObject),
+    .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE,
+    .slots = HeapTestClass_slots,
+};
+
 
 /*[clinic input]
 output push
@@ -1923,13 +1937,17 @@ static struct PyMethodDef depr_star_new_methods[] = {
     {NULL, NULL}
 };
 
-static PyTypeObject DeprStarNew = {
-    PyVarObject_HEAD_INIT(NULL, 0)
-    .tp_name = "_testclinic.DeprStarNew",
-    .tp_basicsize = sizeof(PyObject),
-    .tp_new = depr_star_new,
-    .tp_flags = Py_TPFLAGS_DEFAULT,
-    .tp_methods = depr_star_new_methods,
+static PyType_Slot DeprStarNew_slots[] = {
+    {Py_tp_new, depr_star_new},
+    {Py_tp_methods, depr_star_new_methods},
+    {0, NULL},
+};
+
+static PyType_Spec DeprStarNew_spec = {
+    .name = "_testclinic.DeprStarNew",
+    .basicsize = sizeof(PyObject),
+    .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE,
+    .slots = DeprStarNew_slots,
 };
 
 
@@ -1964,14 +1982,18 @@ static struct PyMethodDef depr_star_init_methods[] = {
     {NULL, NULL}
 };
 
-static PyTypeObject DeprStarInit = {
-    PyVarObject_HEAD_INIT(NULL, 0)
-    .tp_name = "_testclinic.DeprStarInit",
-    .tp_basicsize = sizeof(PyObject),
-    .tp_new = PyType_GenericNew,
-    .tp_init = depr_star_init,
-    .tp_flags = Py_TPFLAGS_DEFAULT,
-    .tp_methods = depr_star_init_methods,
+static PyType_Slot DeprStarInit_slots[] = {
+    {Py_tp_new, PyType_GenericNew},
+    {Py_tp_init, depr_star_init},
+    {Py_tp_methods, depr_star_init_methods},
+    {0, NULL},
+};
+
+static PyType_Spec DeprStarInit_spec = {
+    .name = "_testclinic.DeprStarInit",
+    .basicsize = sizeof(PyObject),
+    .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE,
+    .slots = DeprStarInit_slots,
 };
 
 
@@ -1995,13 +2017,17 @@ depr_star_init_noinline_impl(PyObject *self, PyObject 
*a, PyObject *b,
     return 0;
 }
 
-static PyTypeObject DeprStarInitNoInline = {
-    PyVarObject_HEAD_INIT(NULL, 0)
-    .tp_name = "_testclinic.DeprStarInitNoInline",
-    .tp_basicsize = sizeof(PyObject),
-    .tp_new = PyType_GenericNew,
-    .tp_init = depr_star_init_noinline,
-    .tp_flags = Py_TPFLAGS_DEFAULT,
+static PyType_Slot DeprStarInitNoInline_slots[] = {
+    {Py_tp_new, PyType_GenericNew},
+    {Py_tp_init, depr_star_init_noinline},
+    {0, NULL},
+};
+
+static PyType_Spec DeprStarInitNoInline_spec = {
+    .name = "_testclinic.DeprStarInitNoInline",
+    .basicsize = sizeof(PyObject),
+    .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE,
+    .slots = DeprStarInitNoInline_slots,
 };
 
 
@@ -2021,12 +2047,16 @@ depr_kwd_new_impl(PyTypeObject *type, PyObject *a)
     return type->tp_alloc(type, 0);
 }
 
-static PyTypeObject DeprKwdNew = {
-    PyVarObject_HEAD_INIT(NULL, 0)
-    .tp_name = "_testclinic.DeprKwdNew",
-    .tp_basicsize = sizeof(PyObject),
-    .tp_new = depr_kwd_new,
-    .tp_flags = Py_TPFLAGS_DEFAULT,
+static PyType_Slot DeprKwdNew_slots[] = {
+    {Py_tp_new, depr_kwd_new},
+    {0, NULL},
+};
+
+static PyType_Spec DeprKwdNew_spec = {
+    .name = "_testclinic.DeprKwdNew",
+    .basicsize = sizeof(PyObject),
+    .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE,
+    .slots = DeprKwdNew_slots,
 };
 
 
@@ -2045,13 +2075,17 @@ depr_kwd_init_impl(PyObject *self, PyObject *a)
     return 0;
 }
 
-static PyTypeObject DeprKwdInit = {
-    PyVarObject_HEAD_INIT(NULL, 0)
-    .tp_name = "_testclinic.DeprKwdInit",
-    .tp_basicsize = sizeof(PyObject),
-    .tp_new = PyType_GenericNew,
-    .tp_init = depr_kwd_init,
-    .tp_flags = Py_TPFLAGS_DEFAULT,
+static PyType_Slot DeprKwdInit_slots[] = {
+    {Py_tp_new, PyType_GenericNew},
+    {Py_tp_init, depr_kwd_init},
+    {0, NULL},
+};
+
+static PyType_Spec DeprKwdInit_spec = {
+    .name = "_testclinic.DeprKwdInit",
+    .basicsize = sizeof(PyObject),
+    .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE,
+    .slots = DeprKwdInit_slots,
 };
 
 
@@ -2075,13 +2109,17 @@ depr_kwd_init_noinline_impl(PyObject *self, PyObject 
*a, PyObject *b,
     return 0;
 }
 
-static PyTypeObject DeprKwdInitNoInline = {
-    PyVarObject_HEAD_INIT(NULL, 0)
-    .tp_name = "_testclinic.DeprKwdInitNoInline",
-    .tp_basicsize = sizeof(PyObject),
-    .tp_new = PyType_GenericNew,
-    .tp_init = depr_kwd_init_noinline,
-    .tp_flags = Py_TPFLAGS_DEFAULT,
+static PyType_Slot DeprKwdInitNoInline_slots[] = {
+    {Py_tp_new, PyType_GenericNew},
+    {Py_tp_init, depr_kwd_init_noinline},
+    {0, NULL},
+};
+
+static PyType_Spec DeprKwdInitNoInline_spec = {
+    .name = "_testclinic.DeprKwdInitNoInline",
+    .basicsize = sizeof(PyObject),
+    .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE,
+    .slots = DeprKwdInitNoInline_slots,
 };
 
 
@@ -2843,62 +2881,56 @@ static PyMethodDef tester_methods[] = {
     {NULL, NULL}
 };
 
+static int
+_testclinic_exec(PyObject *m)
+{
+    PyTypeObject *static_types[] = {
+        &TestClass, &VcNew_Type, &VcNewBase_Type, &VcKwOnly_Type,
+    };
+    for (size_t i = 0; i < Py_ARRAY_LENGTH(static_types); i++) {
+        if (PyModule_AddType(m, static_types[i]) < 0) {
+            return -1;
+        }
+    }
+
+    PyType_Spec *heap_specs[] = {
+        &HeapTestClass_spec,
+        &DeprStarNew_spec, &DeprStarInit_spec, &DeprStarInitNoInline_spec,
+        &DeprKwdNew_spec, &DeprKwdInit_spec, &DeprKwdInitNoInline_spec,
+        &VcInit_spec,
+    };
+    for (size_t i = 0; i < Py_ARRAY_LENGTH(heap_specs); i++) {
+        PyObject *type = PyType_FromModuleAndSpec(m, heap_specs[i], NULL);
+        if (type == NULL) {
+            return -1;
+        }
+        int rc = PyModule_AddType(m, (PyTypeObject *)type);
+        Py_DECREF(type);
+        if (rc < 0) {
+            return -1;
+        }
+    }
+    return 0;
+}
+
+static PyModuleDef_Slot _testclinic_slots[] = {
+    {Py_mod_exec, _testclinic_exec},
+    {Py_mod_gil, Py_MOD_GIL_NOT_USED},
+    {0, NULL},
+};
+
 static struct PyModuleDef _testclinic_module = {
     PyModuleDef_HEAD_INIT,
     .m_name = "_testclinic",
     .m_size = 0,
     .m_methods = tester_methods,
+    .m_slots = _testclinic_slots,
 };
 
 PyMODINIT_FUNC
 PyInit__testclinic(void)
 {
-    PyObject *m = PyModule_Create(&_testclinic_module);
-    if (m == NULL) {
-        return NULL;
-    }
-#ifdef Py_GIL_DISABLED
-    PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
-#endif
-    if (PyModule_AddType(m, &TestClass) < 0) {
-        goto error;
-    }
-    if (PyModule_AddType(m, &DeprStarNew) < 0) {
-        goto error;
-    }
-    if (PyModule_AddType(m, &DeprStarInit) < 0) {
-        goto error;
-    }
-    if (PyModule_AddType(m, &DeprStarInitNoInline) < 0) {
-        goto error;
-    }
-    if (PyModule_AddType(m, &DeprKwdNew) < 0) {
-        goto error;
-    }
-    if (PyModule_AddType(m, &DeprKwdInit) < 0) {
-        goto error;
-    }
-    if (PyModule_AddType(m, &DeprKwdInitNoInline) < 0) {
-        goto error;
-    }
-    if (PyModule_AddType(m, &VcNew_Type) < 0) {
-        goto error;
-    }
-    if (PyModule_Add(m, "VcInit",
-                     PyType_FromModuleAndSpec(m, &VcInit_spec, NULL)) < 0) {
-        goto error;
-    }
-    if (PyModule_AddType(m, &VcNewBase_Type) < 0) {
-        goto error;
-    }
-    if (PyModule_AddType(m, &VcKwOnly_Type) < 0) {
-        goto error;
-    }
-    return m;
-
-error:
-    Py_DECREF(m);
-    return NULL;
+    return PyModuleDef_Init(&_testclinic_module);
 }
 
 #undef RETURN_PACKED_ARGS
diff --git a/Tools/c-analyzer/cpython/globals-to-fix.tsv 
b/Tools/c-analyzer/cpython/globals-to-fix.tsv
index e8bb6b7661e12cf..0d0eb929aa116a5 100644
--- a/Tools/c-analyzer/cpython/globals-to-fix.tsv
+++ b/Tools/c-analyzer/cpython/globals-to-fix.tsv
@@ -344,12 +344,6 @@ Modules/_testcapi/vectorcall.c     -       
MethodDescriptorBase_Type       -
 Modules/_testcapi/vectorcall.c -       MethodDescriptorDerived_Type    -
 Modules/_testcapi/vectorcall.c -       MethodDescriptorNopGet_Type     -
 Modules/_testcapi/vectorcall.c -       MethodDescriptor2_Type  -
-Modules/_testclinic.c  -       DeprStarInit    -
-Modules/_testclinic.c  -       DeprStarInitNoInline    -
-Modules/_testclinic.c  -       DeprStarNew     -
-Modules/_testclinic.c  -       DeprKwdInit     -
-Modules/_testclinic.c  -       DeprKwdInitNoInline     -
-Modules/_testclinic.c  -       DeprKwdNew      -
 Modules/_testclinic.c  -       TestClass       -
 Modules/_testclinic.c  -       VcKwOnly_Type   -
 Modules/_testclinic.c  -       VcNew_Type      -

_______________________________________________
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]

Reply via email to