gh-117613: Enhance test_clinic @defining_class tests (#117896)

This commit is contained in:
Victor Stinner 2024-04-16 09:32:51 +02:00 committed by GitHub
parent 6dc661bc9f
commit 2cc916e147
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 122 additions and 21 deletions

View File

@ -793,6 +793,7 @@ _PyStaticObjects_CheckRefcnt(PyInterpreterState *interp) {
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(alias));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(allow_code));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(append));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(arg));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(argdefs));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(args));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(arguments));

View File

@ -282,6 +282,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(alias)
STRUCT_FOR_ID(allow_code)
STRUCT_FOR_ID(append)
STRUCT_FOR_ID(arg)
STRUCT_FOR_ID(argdefs)
STRUCT_FOR_ID(args)
STRUCT_FOR_ID(arguments)

View File

@ -791,6 +791,7 @@ extern "C" {
INIT_ID(alias), \
INIT_ID(allow_code), \
INIT_ID(append), \
INIT_ID(arg), \
INIT_ID(argdefs), \
INIT_ID(args), \
INIT_ID(arguments), \

View File

@ -687,6 +687,9 @@ _PyUnicode_InitStaticStrings(PyInterpreterState *interp) {
string = &_Py_ID(append);
assert(_PyUnicode_CheckConsistency(string, 1));
_PyUnicode_InternInPlace(interp, &string);
string = &_Py_ID(arg);
assert(_PyUnicode_CheckConsistency(string, 1));
_PyUnicode_InternInPlace(interp, &string);
string = &_Py_ID(argdefs);
assert(_PyUnicode_CheckConsistency(string, 1));
_PyUnicode_InternInPlace(interp, &string);

View File

@ -3393,26 +3393,50 @@ class ClinicFunctionalTest(unittest.TestCase):
func = getattr(ac_tester, name)
self.assertEqual(func(), name)
def test_meth_method_no_params(self):
def test_get_defining_class(self):
obj = ac_tester.TestClass()
meth = obj.meth_method_no_params
meth = obj.get_defining_class
self.assertIs(obj.get_defining_class(), ac_tester.TestClass)
# 'defining_class' argument is a positional only argument
with self.assertRaises(TypeError):
obj.get_defining_class_arg(cls=ac_tester.TestClass)
check = partial(self.assertRaisesRegex, TypeError, "no arguments")
check(meth, 1)
check(meth, a=1)
def test_meth_method_no_params_capi(self):
def test_get_defining_class_capi(self):
from _testcapi import pyobject_vectorcall
obj = ac_tester.TestClass()
meth = obj.meth_method_no_params
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)
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()
self.assertEqual(obj.get_defining_class_arg("arg"),
(ac_tester.TestClass, "arg"))
self.assertEqual(obj.get_defining_class_arg(arg=123),
(ac_tester.TestClass, 123))
# 'defining_class' argument is a positional only argument
with self.assertRaises(TypeError):
obj.get_defining_class_arg(cls=ac_tester.TestClass, arg="arg")
# wrong number of arguments
with self.assertRaises(TypeError):
obj.get_defining_class_arg()
with self.assertRaises(TypeError):
obj.get_defining_class_arg("arg1", "arg2")
def test_depr_star_new(self):
cls = ac_tester.DeprStarNew
cls()

View File

@ -1219,21 +1219,36 @@ class _testclinic.TestClass "PyObject *" "PyObject"
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=668a591c65bec947]*/
/*[clinic input]
_testclinic.TestClass.meth_method_no_params
_testclinic.TestClass.get_defining_class
cls: defining_class
/
[clinic start generated code]*/
static PyObject *
_testclinic_TestClass_meth_method_no_params_impl(PyObject *self,
PyTypeObject *cls)
/*[clinic end generated code: output=c140f100080c2fc8 input=6bd34503d11c63c1]*/
_testclinic_TestClass_get_defining_class_impl(PyObject *self,
PyTypeObject *cls)
/*[clinic end generated code: output=94f9b0b5f7add930 input=537c59417471dee3]*/
{
Py_RETURN_NONE;
return Py_NewRef(cls);
}
/*[clinic input]
_testclinic.TestClass.get_defining_class_arg
cls: defining_class
arg: object
[clinic start generated code]*/
static PyObject *
_testclinic_TestClass_get_defining_class_arg_impl(PyObject *self,
PyTypeObject *cls,
PyObject *arg)
/*[clinic end generated code: output=fe7e49d96cbb7718 input=d1b83d3b853af6d9]*/
{
return Py_BuildValue("(OO)", cls, arg);
}
static struct PyMethodDef test_class_methods[] = {
_TESTCLINIC_TESTCLASS_METH_METHOD_NO_PARAMS_METHODDEF
_TESTCLINIC_TESTCLASS_GET_DEFINING_CLASS_METHODDEF
_TESTCLINIC_TESTCLASS_GET_DEFINING_CLASS_ARG_METHODDEF
{NULL, NULL}
};

View File

@ -3142,25 +3142,81 @@ exit:
return return_value;
}
PyDoc_STRVAR(_testclinic_TestClass_meth_method_no_params__doc__,
"meth_method_no_params($self, /)\n"
PyDoc_STRVAR(_testclinic_TestClass_get_defining_class__doc__,
"get_defining_class($self, /)\n"
"--\n"
"\n");
#define _TESTCLINIC_TESTCLASS_METH_METHOD_NO_PARAMS_METHODDEF \
{"meth_method_no_params", _PyCFunction_CAST(_testclinic_TestClass_meth_method_no_params), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _testclinic_TestClass_meth_method_no_params__doc__},
#define _TESTCLINIC_TESTCLASS_GET_DEFINING_CLASS_METHODDEF \
{"get_defining_class", _PyCFunction_CAST(_testclinic_TestClass_get_defining_class), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _testclinic_TestClass_get_defining_class__doc__},
static PyObject *
_testclinic_TestClass_meth_method_no_params_impl(PyObject *self,
PyTypeObject *cls);
_testclinic_TestClass_get_defining_class_impl(PyObject *self,
PyTypeObject *cls);
static PyObject *
_testclinic_TestClass_meth_method_no_params(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
_testclinic_TestClass_get_defining_class(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
PyErr_SetString(PyExc_TypeError, "meth_method_no_params() takes no arguments");
PyErr_SetString(PyExc_TypeError, "get_defining_class() takes no arguments");
return NULL;
}
return _testclinic_TestClass_meth_method_no_params_impl(self, cls);
return _testclinic_TestClass_get_defining_class_impl(self, cls);
}
/*[clinic end generated code: output=6520c1ca5392a3f0 input=a9049054013a1b77]*/
PyDoc_STRVAR(_testclinic_TestClass_get_defining_class_arg__doc__,
"get_defining_class_arg($self, /, arg)\n"
"--\n"
"\n");
#define _TESTCLINIC_TESTCLASS_GET_DEFINING_CLASS_ARG_METHODDEF \
{"get_defining_class_arg", _PyCFunction_CAST(_testclinic_TestClass_get_defining_class_arg), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _testclinic_TestClass_get_defining_class_arg__doc__},
static PyObject *
_testclinic_TestClass_get_defining_class_arg_impl(PyObject *self,
PyTypeObject *cls,
PyObject *arg);
static PyObject *
_testclinic_TestClass_get_defining_class_arg(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
#define NUM_KEYWORDS 2
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(arg), },
};
#undef NUM_KEYWORDS
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
#else // !Py_BUILD_CORE
# define KWTUPLE NULL
#endif // !Py_BUILD_CORE
static const char * const _keywords[] = {"arg", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "get_defining_class_arg",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
PyObject *argsbuf[1];
PyObject *arg;
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
if (!args) {
goto exit;
}
arg = args[0];
return_value = _testclinic_TestClass_get_defining_class_arg_impl(self, cls, arg);
exit:
return return_value;
}
/*[clinic end generated code: output=71b2a15aa86c2bcf input=a9049054013a1b77]*/