Skip to content

Commit ead9e78

Browse files
[3.11] gh-115015: Argument Clinic: fix generated code for METH_METHOD methods without params (#115016) (#115069)
(cherry picked from commit 09096a1)
1 parent 319e695 commit ead9e78

22 files changed

+143
-48
lines changed

Lib/test/clinic.test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3891,7 +3891,7 @@ Test_cls_no_params_impl(TestObj *self, PyTypeObject *cls);
38913891
static PyObject *
38923892
Test_cls_no_params(TestObj *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
38933893
{
3894-
if (nargs) {
3894+
if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
38953895
PyErr_SetString(PyExc_TypeError, "cls_no_params() takes no arguments");
38963896
return NULL;
38973897
}
@@ -3900,7 +3900,7 @@ Test_cls_no_params(TestObj *self, PyTypeObject *cls, PyObject *const *args, Py_s
39003900

39013901
static PyObject *
39023902
Test_cls_no_params_impl(TestObj *self, PyTypeObject *cls)
3903-
/*[clinic end generated code: output=cc8845f22cff3dcb input=e7e2e4e344e96a11]*/
3903+
/*[clinic end generated code: output=4d68b4652c144af3 input=e7e2e4e344e96a11]*/
39043904

39053905

39063906
/*[clinic input]

Lib/test/test_clinic.py

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

5+
from functools import partial
56
from test import support, test_tools
67
from test.support import os_helper
78
from test.support import SHORT_TIMEOUT, requires_subprocess
@@ -2032,6 +2033,26 @@ def test_gh_99240_double_free(self):
20322033
with self.assertRaisesRegex(TypeError, expected_error):
20332034
ac_tester.gh_99240_double_free('a', '\0b')
20342035

2036+
def test_meth_method_no_params(self):
2037+
obj = ac_tester.TestClass()
2038+
meth = obj.meth_method_no_params
2039+
check = partial(self.assertRaisesRegex, TypeError, "no arguments")
2040+
check(meth, 1)
2041+
check(meth, a=1)
2042+
2043+
def test_meth_method_no_params_capi(self):
2044+
from _testcapi import pyobject_vectorcall
2045+
obj = ac_tester.TestClass()
2046+
meth = obj.meth_method_no_params
2047+
pyobject_vectorcall(meth, None, None)
2048+
pyobject_vectorcall(meth, (), None)
2049+
pyobject_vectorcall(meth, (), ())
2050+
pyobject_vectorcall(meth, None, ())
2051+
2052+
check = partial(self.assertRaisesRegex, TypeError, "no arguments")
2053+
check(pyobject_vectorcall, meth, (1,), None)
2054+
check(pyobject_vectorcall, meth, (1,), ("a",))
2055+
20352056

20362057
class PermutationTests(unittest.TestCase):
20372058
"""Test permutation support functions."""
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix a bug in Argument Clinic that generated incorrect code for methods with
2+
no parameters that use the :ref:`METH_METHOD | METH_FASTCALL | METH_KEYWORDS
3+
<METH_METHOD-METH_FASTCALL-METH_KEYWORDS>` calling convention. Only the
4+
positional parameter count was checked; any keyword argument passed would be
5+
silently accepted.

Modules/_sre/clinic/sre.c.h

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

Modules/_testclinic.c

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,40 @@ gh_99240_double_free_impl(PyObject *module, char *a, char *b)
11171117
}
11181118

11191119

1120+
/*[clinic input]
1121+
class _testclinic.TestClass "PyObject *" "PyObject"
1122+
[clinic start generated code]*/
1123+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=668a591c65bec947]*/
1124+
1125+
/*[clinic input]
1126+
_testclinic.TestClass.meth_method_no_params
1127+
cls: defining_class
1128+
/
1129+
[clinic start generated code]*/
1130+
1131+
static PyObject *
1132+
_testclinic_TestClass_meth_method_no_params_impl(PyObject *self,
1133+
PyTypeObject *cls)
1134+
/*[clinic end generated code: output=c140f100080c2fc8 input=6bd34503d11c63c1]*/
1135+
{
1136+
Py_RETURN_NONE;
1137+
}
1138+
1139+
static struct PyMethodDef test_class_methods[] = {
1140+
_TESTCLINIC_TESTCLASS_METH_METHOD_NO_PARAMS_METHODDEF
1141+
{NULL, NULL}
1142+
};
1143+
1144+
static PyTypeObject TestClass = {
1145+
PyVarObject_HEAD_INIT(NULL, 0)
1146+
.tp_name = "_testclinic.TestClass",
1147+
.tp_basicsize = sizeof(PyObject),
1148+
.tp_flags = Py_TPFLAGS_DEFAULT,
1149+
.tp_new = PyType_GenericNew,
1150+
.tp_methods = test_class_methods,
1151+
};
1152+
1153+
11201154
static PyMethodDef tester_methods[] = {
11211155
TEST_EMPTY_FUNCTION_METHODDEF
11221156
OBJECTS_CONVERTER_METHODDEF
@@ -1181,7 +1215,18 @@ static struct PyModuleDef _testclinic_module = {
11811215
PyMODINIT_FUNC
11821216
PyInit__testclinic(void)
11831217
{
1184-
return PyModule_Create(&_testclinic_module);
1218+
PyObject *m = PyModule_Create(&_testclinic_module);
1219+
if (m == NULL) {
1220+
return NULL;
1221+
}
1222+
if (PyModule_AddType(m, &TestClass) < 0) {
1223+
goto error;
1224+
}
1225+
return m;
1226+
1227+
error:
1228+
Py_DECREF(m);
1229+
return NULL;
11851230
}
11861231

11871232
#undef RETURN_PACKED_ARGS

Modules/cjkcodecs/clinic/multibytecodec.c.h

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

Modules/clinic/_curses_panel.c.h

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

Modules/clinic/_dbmmodule.c.h

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

Modules/clinic/_gdbmmodule.c.h

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

Modules/clinic/_lsprof.c.h

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

Modules/clinic/_queuemodule.c.h

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

Modules/clinic/_testclinic.c.h

Lines changed: 23 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/clinic/_testmultiphase.c.h

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

Modules/clinic/arraymodule.c.h

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

Modules/clinic/md5module.c.h

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

Modules/clinic/posixmodule.c.h

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

0 commit comments

Comments
 (0)