Skip to content

Commit 2cc916e

Browse files
authored
gh-117613: Enhance test_clinic @defining_class tests (#117896)
1 parent 6dc661b commit 2cc916e

File tree

7 files changed

+122
-21
lines changed

7 files changed

+122
-21
lines changed

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ struct _Py_global_strings {
282282
STRUCT_FOR_ID(alias)
283283
STRUCT_FOR_ID(allow_code)
284284
STRUCT_FOR_ID(append)
285+
STRUCT_FOR_ID(arg)
285286
STRUCT_FOR_ID(argdefs)
286287
STRUCT_FOR_ID(args)
287288
STRUCT_FOR_ID(arguments)

Include/internal/pycore_runtime_init_generated.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_unicodeobject_generated.h

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_clinic.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3393,26 +3393,50 @@ def test_cloned_func_with_converter_exception_message(self):
33933393
func = getattr(ac_tester, name)
33943394
self.assertEqual(func(), name)
33953395

3396-
def test_meth_method_no_params(self):
3396+
def test_get_defining_class(self):
33973397
obj = ac_tester.TestClass()
3398-
meth = obj.meth_method_no_params
3398+
meth = obj.get_defining_class
3399+
self.assertIs(obj.get_defining_class(), ac_tester.TestClass)
3400+
3401+
# 'defining_class' argument is a positional only argument
3402+
with self.assertRaises(TypeError):
3403+
obj.get_defining_class_arg(cls=ac_tester.TestClass)
3404+
33993405
check = partial(self.assertRaisesRegex, TypeError, "no arguments")
34003406
check(meth, 1)
34013407
check(meth, a=1)
34023408

3403-
def test_meth_method_no_params_capi(self):
3409+
def test_get_defining_class_capi(self):
34043410
from _testcapi import pyobject_vectorcall
34053411
obj = ac_tester.TestClass()
3406-
meth = obj.meth_method_no_params
3412+
meth = obj.get_defining_class
34073413
pyobject_vectorcall(meth, None, None)
34083414
pyobject_vectorcall(meth, (), None)
34093415
pyobject_vectorcall(meth, (), ())
34103416
pyobject_vectorcall(meth, None, ())
3417+
self.assertIs(pyobject_vectorcall(meth, (), ()), ac_tester.TestClass)
34113418

34123419
check = partial(self.assertRaisesRegex, TypeError, "no arguments")
34133420
check(pyobject_vectorcall, meth, (1,), None)
34143421
check(pyobject_vectorcall, meth, (1,), ("a",))
34153422

3423+
def test_get_defining_class_arg(self):
3424+
obj = ac_tester.TestClass()
3425+
self.assertEqual(obj.get_defining_class_arg("arg"),
3426+
(ac_tester.TestClass, "arg"))
3427+
self.assertEqual(obj.get_defining_class_arg(arg=123),
3428+
(ac_tester.TestClass, 123))
3429+
3430+
# 'defining_class' argument is a positional only argument
3431+
with self.assertRaises(TypeError):
3432+
obj.get_defining_class_arg(cls=ac_tester.TestClass, arg="arg")
3433+
3434+
# wrong number of arguments
3435+
with self.assertRaises(TypeError):
3436+
obj.get_defining_class_arg()
3437+
with self.assertRaises(TypeError):
3438+
obj.get_defining_class_arg("arg1", "arg2")
3439+
34163440
def test_depr_star_new(self):
34173441
cls = ac_tester.DeprStarNew
34183442
cls()

Modules/_testclinic.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,21 +1219,36 @@ class _testclinic.TestClass "PyObject *" "PyObject"
12191219
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=668a591c65bec947]*/
12201220

12211221
/*[clinic input]
1222-
_testclinic.TestClass.meth_method_no_params
1222+
_testclinic.TestClass.get_defining_class
12231223
cls: defining_class
1224-
/
12251224
[clinic start generated code]*/
12261225

12271226
static PyObject *
1228-
_testclinic_TestClass_meth_method_no_params_impl(PyObject *self,
1229-
PyTypeObject *cls)
1230-
/*[clinic end generated code: output=c140f100080c2fc8 input=6bd34503d11c63c1]*/
1227+
_testclinic_TestClass_get_defining_class_impl(PyObject *self,
1228+
PyTypeObject *cls)
1229+
/*[clinic end generated code: output=94f9b0b5f7add930 input=537c59417471dee3]*/
12311230
{
1232-
Py_RETURN_NONE;
1231+
return Py_NewRef(cls);
1232+
}
1233+
1234+
/*[clinic input]
1235+
_testclinic.TestClass.get_defining_class_arg
1236+
cls: defining_class
1237+
arg: object
1238+
[clinic start generated code]*/
1239+
1240+
static PyObject *
1241+
_testclinic_TestClass_get_defining_class_arg_impl(PyObject *self,
1242+
PyTypeObject *cls,
1243+
PyObject *arg)
1244+
/*[clinic end generated code: output=fe7e49d96cbb7718 input=d1b83d3b853af6d9]*/
1245+
{
1246+
return Py_BuildValue("(OO)", cls, arg);
12331247
}
12341248

12351249
static struct PyMethodDef test_class_methods[] = {
1236-
_TESTCLINIC_TESTCLASS_METH_METHOD_NO_PARAMS_METHODDEF
1250+
_TESTCLINIC_TESTCLASS_GET_DEFINING_CLASS_METHODDEF
1251+
_TESTCLINIC_TESTCLASS_GET_DEFINING_CLASS_ARG_METHODDEF
12371252
{NULL, NULL}
12381253
};
12391254

Modules/clinic/_testclinic.c.h

Lines changed: 66 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)