Skip to content

Commit f89cb47

Browse files
committed
Move to a helper for emitting no positional args error
1 parent 93c43b5 commit f89cb47

4 files changed

Lines changed: 21 additions & 15 deletions

File tree

‎Include/internal/pycore_modsupport.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ PyAPI_FUNC(int) _PyArg_NoPositional(const char *funcname, PyObject *args);
2020
#define _PyArg_NoPositional(funcname, args) \
2121
((args) == NULL || _PyArg_NoPositional((funcname), (args)))
2222

23+
// Export for Argument Clinic generated code in shared extensions
24+
PyAPI_FUNC(int) _PyArg_NoPositionalStack(const char *funcname,
25+
Py_ssize_t nargs);
26+
#define _PyArg_NoPositionalStack(funcname, nargs) \
27+
((nargs) == 0 || _PyArg_NoPositionalStack((funcname), (nargs)))
28+
2329
// Export for '_asyncio' shared extension
2430
PyAPI_FUNC(int) _PyArg_NoKeywords(const char *funcname, PyObject *kwargs);
2531
#define _PyArg_NoKeywords(funcname, kwargs) \

‎Lib/test/clinic.test.c‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4995,12 +4995,8 @@ Test_vectorcall(PyObject *type, PyObject *const *args,
49954995
/* Make sure the type object is immutable: the generated
49964996
* vectorcall doesn't deal e.g. with users reassigning __init__. */
49974997
assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
4998-
if (nargs) {
4999-
PyErr_SetString(PyExc_TypeError,
5000-
"Test() takes no positional arguments");
5001-
goto exit;
5002-
}
5003-
if (!_PyArg_NoKwnames("Test", kwnames)) {
4998+
if (!_PyArg_NoPositionalStack("Test", nargs) ||
4999+
!_PyArg_NoKwnames("Test", kwnames)) {
50045000
goto exit;
50055001
}
50065002
self = _PyType_CAST(type)->tp_new(_PyType_CAST(type),
@@ -5021,7 +5017,7 @@ Test_vectorcall(PyObject *type, PyObject *const *args,
50215017

50225018
static int
50235019
Test___init___impl(TestObj *self)
5024-
/*[clinic end generated code: output=3798499df7a60323 input=645fe693f6c6b9b6]*/
5020+
/*[clinic end generated code: output=ce04b9d7beacd894 input=645fe693f6c6b9b6]*/
50255021

50265022

50275023
/*[clinic input]

‎Python/getargs.c‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2933,6 +2933,7 @@ _PyArg_UnpackStack(PyObject *const *args, Py_ssize_t nargs, const char *name,
29332933
#undef _PyArg_NoKeywords
29342934
#undef _PyArg_NoKwnames
29352935
#undef _PyArg_NoPositional
2936+
#undef _PyArg_NoPositionalStack
29362937

29372938
/* For type constructors that don't take keyword args
29382939
*
@@ -2967,8 +2968,15 @@ _PyArg_NoPositional(const char *funcname, PyObject *args)
29672968
PyErr_BadInternalCall();
29682969
return 0;
29692970
}
2970-
if (PyTuple_GET_SIZE(args) == 0)
2971+
return _PyArg_NoPositionalStack(funcname, PyTuple_GET_SIZE(args));
2972+
}
2973+
2974+
int
2975+
_PyArg_NoPositionalStack(const char *funcname, Py_ssize_t nargs)
2976+
{
2977+
if (nargs == 0) {
29712978
return 1;
2979+
}
29722980

29732981
PyErr_Format(PyExc_TypeError, "%.200s() takes no positional arguments",
29742982
funcname);

‎Tools/clinic/libclinic/parse_args.py‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,14 +1713,10 @@ def parse_vectorcall_no_args(self) -> None:
17131713
"""No keyword or positional arguments."""
17141714
parser_code = self._vectorcall_type_check()
17151715
self.codegen.add_include('pycore_modsupport.h',
1716-
'_PyArg_NoKwnames()')
1716+
'_PyArg_NoPositionalStack()')
17171717
parser_code.append(libclinic.normalize_snippet("""
1718-
if (nargs) {{
1719-
PyErr_SetString(PyExc_TypeError,
1720-
"{name}() takes no positional arguments");
1721-
goto exit;
1722-
}}
1723-
if (!_PyArg_NoKwnames("{name}", kwnames)) {{
1718+
if (!_PyArg_NoPositionalStack("{name}", nargs) ||
1719+
!_PyArg_NoKwnames("{name}", kwnames)) {{
17241720
goto exit;
17251721
}}
17261722
""", indent=4))

0 commit comments

Comments
 (0)