Skip to content

Commit 09096a1

Browse files
gh-115015: Argument Clinic: fix generated code for METH_METHOD methods without params (#115016)
1 parent 4aa4f09 commit 09096a1

30 files changed

+153
-68
lines changed

Lib/test/clinic.test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4625,7 +4625,7 @@ Test_cls_no_params_impl(TestObj *self, PyTypeObject *cls);
46254625
static PyObject *
46264626
Test_cls_no_params(TestObj *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
46274627
{
4628-
if (nargs) {
4628+
if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
46294629
PyErr_SetString(PyExc_TypeError, "cls_no_params() takes no arguments");
46304630
return NULL;
46314631
}
@@ -4634,7 +4634,7 @@ Test_cls_no_params(TestObj *self, PyTypeObject *cls, PyObject *const *args, Py_s
46344634

46354635
static PyObject *
46364636
Test_cls_no_params_impl(TestObj *self, PyTypeObject *cls)
4637-
/*[clinic end generated code: output=cc8845f22cff3dcb input=e7e2e4e344e96a11]*/
4637+
/*[clinic end generated code: output=4d68b4652c144af3 input=e7e2e4e344e96a11]*/
46384638

46394639

46404640
/*[clinic input]

Lib/test/test_clinic.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3288,6 +3288,26 @@ def test_cloned_func_with_converter_exception_message(self):
32883288
func = getattr(ac_tester, name)
32893289
self.assertEqual(func(), name)
32903290

3291+
def test_meth_method_no_params(self):
3292+
obj = ac_tester.TestClass()
3293+
meth = obj.meth_method_no_params
3294+
check = partial(self.assertRaisesRegex, TypeError, "no arguments")
3295+
check(meth, 1)
3296+
check(meth, a=1)
3297+
3298+
def test_meth_method_no_params_capi(self):
3299+
from _testcapi import pyobject_vectorcall
3300+
obj = ac_tester.TestClass()
3301+
meth = obj.meth_method_no_params
3302+
pyobject_vectorcall(meth, None, None)
3303+
pyobject_vectorcall(meth, (), None)
3304+
pyobject_vectorcall(meth, (), ())
3305+
pyobject_vectorcall(meth, None, ())
3306+
3307+
check = partial(self.assertRaisesRegex, TypeError, "no arguments")
3308+
check(pyobject_vectorcall, meth, (1,), None)
3309+
check(pyobject_vectorcall, meth, (1,), ("a",))
3310+
32913311
def test_depr_star_new(self):
32923312
cls = ac_tester.DeprStarNew
32933313
cls()
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/_io/clinic/bufferedio.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/_io/clinic/bytesio.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/_io/clinic/fileio.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/_io/clinic/iobase.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/_io/clinic/textio.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/_io/clinic/winconsoleio.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/_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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,40 @@ clone_with_conv_f2_impl(PyObject *module, custom_t path)
12131213
}
12141214

12151215

1216+
/*[clinic input]
1217+
class _testclinic.TestClass "PyObject *" "PyObject"
1218+
[clinic start generated code]*/
1219+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=668a591c65bec947]*/
1220+
1221+
/*[clinic input]
1222+
_testclinic.TestClass.meth_method_no_params
1223+
cls: defining_class
1224+
/
1225+
[clinic start generated code]*/
1226+
1227+
static PyObject *
1228+
_testclinic_TestClass_meth_method_no_params_impl(PyObject *self,
1229+
PyTypeObject *cls)
1230+
/*[clinic end generated code: output=c140f100080c2fc8 input=6bd34503d11c63c1]*/
1231+
{
1232+
Py_RETURN_NONE;
1233+
}
1234+
1235+
static struct PyMethodDef test_class_methods[] = {
1236+
_TESTCLINIC_TESTCLASS_METH_METHOD_NO_PARAMS_METHODDEF
1237+
{NULL, NULL}
1238+
};
1239+
1240+
static PyTypeObject TestClass = {
1241+
PyVarObject_HEAD_INIT(NULL, 0)
1242+
.tp_name = "_testclinic.TestClass",
1243+
.tp_basicsize = sizeof(PyObject),
1244+
.tp_flags = Py_TPFLAGS_DEFAULT,
1245+
.tp_new = PyType_GenericNew,
1246+
.tp_methods = test_class_methods,
1247+
};
1248+
1249+
12161250
/*[clinic input]
12171251
output push
12181252
destination deprstar new file '{dirname}/clinic/_testclinic_depr.c.h'
@@ -1906,6 +1940,9 @@ PyInit__testclinic(void)
19061940
if (m == NULL) {
19071941
return NULL;
19081942
}
1943+
if (PyModule_AddType(m, &TestClass) < 0) {
1944+
goto error;
1945+
}
19091946
if (PyModule_AddType(m, &DeprStarNew) < 0) {
19101947
goto error;
19111948
}

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/_asynciomodule.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/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: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/clinic/_elementtree.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.

0 commit comments

Comments
 (0)