Skip to content

Commit 4f3f1d2

Browse files
cmaloneyclaude
andauthored
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 <noreply@anthropic.com>
1 parent f80a0fa commit 4f3f1d2

3 files changed

Lines changed: 150 additions & 111 deletions

File tree

‎Lib/test/test_clinic.py‎

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Copyright 2012-2013 by Larry Hastings.
33
# Licensed to the PSF under a contributor agreement.
44

5-
from functools import partial
5+
from functools import partial, wraps
66
from test import support, test_tools
77
from test.support import force_not_colorized_test_class
88
from test.support import os_helper
@@ -4621,6 +4621,16 @@ def test_file_dest(self):
46214621
except ImportError:
46224622
ac_tester = None
46234623

4624+
def static_and_heap(test):
4625+
# Run test with both a static type and a heap type.
4626+
@wraps(test)
4627+
def wrapper(self):
4628+
for cls in ac_tester.TestClass, ac_tester.HeapTestClass:
4629+
with self.subTest(cls=cls):
4630+
test(self, cls)
4631+
return wrapper
4632+
4633+
46244634
@unittest.skipIf(ac_tester is None, "_testclinic is missing")
46254635
class ClinicFunctionalTest(unittest.TestCase):
46264636
locals().update((name, getattr(ac_tester, name))
@@ -5295,53 +5305,56 @@ def test_cloned_func_with_converter_exception_message(self):
52955305
func = getattr(ac_tester, name)
52965306
self.assertEqual(func(), name)
52975307

5298-
def test_get_defining_class(self):
5299-
obj = ac_tester.TestClass()
5308+
@static_and_heap
5309+
def test_get_defining_class(self, cls):
5310+
obj = cls()
53005311
meth = obj.get_defining_class
5301-
self.assertIs(obj.get_defining_class(), ac_tester.TestClass)
5312+
self.assertIs(obj.get_defining_class(), cls)
53025313

53035314
# 'defining_class' argument is a positional only argument
53045315
with self.assertRaises(TypeError):
5305-
obj.get_defining_class_arg(cls=ac_tester.TestClass)
5316+
obj.get_defining_class_arg(cls=cls)
53065317

53075318
check = partial(self.assertRaisesRegex, TypeError, "no arguments")
53085319
check(meth, 1)
53095320
check(meth, a=1)
53105321

5311-
def test_get_defining_class_capi(self):
5322+
@static_and_heap
5323+
def test_get_defining_class_capi(self, cls):
53125324
from _testcapi import pyobject_vectorcall
5313-
obj = ac_tester.TestClass()
5325+
obj = cls()
53145326
meth = obj.get_defining_class
53155327
pyobject_vectorcall(meth, None, None)
53165328
pyobject_vectorcall(meth, (), None)
53175329
pyobject_vectorcall(meth, (), ())
53185330
pyobject_vectorcall(meth, None, ())
5319-
self.assertIs(pyobject_vectorcall(meth, (), ()), ac_tester.TestClass)
5331+
self.assertIs(pyobject_vectorcall(meth, (), ()), cls)
53205332

53215333
check = partial(self.assertRaisesRegex, TypeError, "no arguments")
53225334
check(pyobject_vectorcall, meth, (1,), None)
53235335
check(pyobject_vectorcall, meth, (1,), ("a",))
53245336

5325-
def test_get_defining_class_arg(self):
5326-
obj = ac_tester.TestClass()
5337+
@static_and_heap
5338+
def test_get_defining_class_arg(self, cls):
5339+
obj = cls()
53275340
self.assertEqual(obj.get_defining_class_arg("arg"),
5328-
(ac_tester.TestClass, "arg"))
5341+
(cls, "arg"))
53295342
self.assertEqual(obj.get_defining_class_arg(arg=123),
5330-
(ac_tester.TestClass, 123))
5343+
(cls, 123))
53315344

53325345
# 'defining_class' argument is a positional only argument
53335346
with self.assertRaises(TypeError):
5334-
obj.get_defining_class_arg(cls=ac_tester.TestClass, arg="arg")
5347+
obj.get_defining_class_arg(cls=cls, arg="arg")
53355348

53365349
# wrong number of arguments
53375350
with self.assertRaises(TypeError):
53385351
obj.get_defining_class_arg()
53395352
with self.assertRaises(TypeError):
53405353
obj.get_defining_class_arg("arg1", "arg2")
53415354

5342-
def test_defclass_varpos(self):
5355+
@static_and_heap
5356+
def test_defclass_varpos(self, cls):
53435357
# fn(*args)
5344-
cls = ac_tester.TestClass
53455358
obj = cls()
53465359
fn = obj.defclass_varpos
53475360
self.assertEqual(fn(), (cls, ()))
@@ -5351,9 +5364,9 @@ def test_defclass_varpos(self):
53515364
self.assertEqual(fn(obj), (cls, ()))
53525365
self.assertEqual(fn(obj, 1, 2), (cls, (1, 2)))
53535366

5354-
def test_defclass_posonly_varpos(self):
5367+
@static_and_heap
5368+
def test_defclass_posonly_varpos(self, cls):
53555369
# fn(a, b, /, *args)
5356-
cls = ac_tester.TestClass
53575370
obj = cls()
53585371
fn = obj.defclass_posonly_varpos
53595372
errmsg = 'expected at least 2 arguments'

‎Modules/_testclinic.c‎

Lines changed: 120 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -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]
18741888
output 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+
28462922
static 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

28532930
PyMODINIT_FUNC
28542931
PyInit__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

‎Tools/c-analyzer/cpython/globals-to-fix.tsv‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,6 @@ Modules/_testcapi/vectorcall.c - MethodDescriptorBase_Type -
344344
Modules/_testcapi/vectorcall.c - MethodDescriptorDerived_Type -
345345
Modules/_testcapi/vectorcall.c - MethodDescriptorNopGet_Type -
346346
Modules/_testcapi/vectorcall.c - MethodDescriptor2_Type -
347-
Modules/_testclinic.c - DeprStarInit -
348-
Modules/_testclinic.c - DeprStarInitNoInline -
349-
Modules/_testclinic.c - DeprStarNew -
350-
Modules/_testclinic.c - DeprKwdInit -
351-
Modules/_testclinic.c - DeprKwdInitNoInline -
352-
Modules/_testclinic.c - DeprKwdNew -
353347
Modules/_testclinic.c - TestClass -
354348
Modules/_testclinic.c - VcKwOnly_Type -
355349
Modules/_testclinic.c - VcNew_Type -

0 commit comments

Comments
 (0)