Skip to content

Commit

Permalink
[3.12] gh-115015: Argument Clinic: fix generated code for METH_METHOD…
Browse files Browse the repository at this point in the history
… methods without params (#115016) (#115067)

(cherry picked from commit 09096a1)
  • Loading branch information
erlend-aasland committed Feb 6, 2024
1 parent 5ddb274 commit 6f5e360
Show file tree
Hide file tree
Showing 30 changed files with 162 additions and 68 deletions.
4 changes: 2 additions & 2 deletions Lib/test/clinic.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4629,7 +4629,7 @@ Test_cls_no_params_impl(TestObj *self, PyTypeObject *cls);
static PyObject *
Test_cls_no_params(TestObj *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
if (nargs) {
if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
PyErr_SetString(PyExc_TypeError, "cls_no_params() takes no arguments");
return NULL;
}
Expand All @@ -4638,7 +4638,7 @@ Test_cls_no_params(TestObj *self, PyTypeObject *cls, PyObject *const *args, Py_s

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


/*[clinic input]
Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright 2012-2013 by Larry Hastings.
# Licensed to the PSF under a contributor agreement.

from functools import partial
from test import support, test_tools
from test.support import os_helper
from test.support.os_helper import TESTFN, unlink
Expand Down Expand Up @@ -2096,6 +2097,26 @@ def test_cloned_func_with_converter_exception_message(self):
func = getattr(ac_tester, name)
self.assertEqual(func(), name)

def test_meth_method_no_params(self):
obj = ac_tester.TestClass()
meth = obj.meth_method_no_params
check = partial(self.assertRaisesRegex, TypeError, "no arguments")
check(meth, 1)
check(meth, a=1)

def test_meth_method_no_params_capi(self):
from _testcapi import pyobject_vectorcall
obj = ac_tester.TestClass()
meth = obj.meth_method_no_params
pyobject_vectorcall(meth, None, None)
pyobject_vectorcall(meth, (), None)
pyobject_vectorcall(meth, (), ())
pyobject_vectorcall(meth, None, ())

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


class PermutationTests(unittest.TestCase):
"""Test permutation support functions."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fix a bug in Argument Clinic that generated incorrect code for methods with
no parameters that use the :ref:`METH_METHOD | METH_FASTCALL | METH_KEYWORDS
<METH_METHOD-METH_FASTCALL-METH_KEYWORDS>` calling convention. Only the
positional parameter count was checked; any keyword argument passed would be
silently accepted.
4 changes: 2 additions & 2 deletions Modules/_io/clinic/bufferedio.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Modules/_io/clinic/bytesio.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Modules/_io/clinic/fileio.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Modules/_io/clinic/iobase.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Modules/_io/clinic/textio.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Modules/_io/clinic/winconsoleio.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Modules/_sre/clinic/sre.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 46 additions & 1 deletion Modules/_testclinic.c
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,40 @@ clone_with_conv_f2_impl(PyObject *module, custom_t path)
}


/*[clinic input]
class _testclinic.TestClass "PyObject *" "PyObject"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=668a591c65bec947]*/

/*[clinic input]
_testclinic.TestClass.meth_method_no_params
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]*/
{
Py_RETURN_NONE;
}

static struct PyMethodDef test_class_methods[] = {
_TESTCLINIC_TESTCLASS_METH_METHOD_NO_PARAMS_METHODDEF
{NULL, NULL}
};

static PyTypeObject TestClass = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "_testclinic.TestClass",
.tp_basicsize = sizeof(PyObject),
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = PyType_GenericNew,
.tp_methods = test_class_methods,
};


static PyMethodDef tester_methods[] = {
TEST_EMPTY_FUNCTION_METHODDEF
OBJECTS_CONVERTER_METHODDEF
Expand Down Expand Up @@ -1262,7 +1296,18 @@ static struct PyModuleDef _testclinic_module = {
PyMODINIT_FUNC
PyInit__testclinic(void)
{
return PyModule_Create(&_testclinic_module);
PyObject *m = PyModule_Create(&_testclinic_module);
if (m == NULL) {
return NULL;
}
if (PyModule_AddType(m, &TestClass) < 0) {
goto error;
}
return m;

error:
Py_DECREF(m);
return NULL;
}

#undef RETURN_PACKED_ARGS
4 changes: 2 additions & 2 deletions Modules/cjkcodecs/clinic/multibytecodec.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Modules/clinic/_asynciomodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Modules/clinic/_curses_panel.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Modules/clinic/_dbmmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Modules/clinic/_elementtree.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6f5e360

Please sign in to comment.