Skip to content

Commit 87ec86c

Browse files
authored
bpo-37207: Add _PyArg_NoKwnames() helper function (GH-18980)
1 parent c98f87f commit 87ec86c

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

Include/modsupport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,12 @@ PyAPI_FUNC(int) _PyArg_UnpackStack(
6060
...);
6161

6262
PyAPI_FUNC(int) _PyArg_NoKeywords(const char *funcname, PyObject *kwargs);
63+
PyAPI_FUNC(int) _PyArg_NoKwnames(const char *funcname, PyObject *kwnames);
6364
PyAPI_FUNC(int) _PyArg_NoPositional(const char *funcname, PyObject *args);
6465
#define _PyArg_NoKeywords(funcname, kwargs) \
6566
((kwargs) == NULL || _PyArg_NoKeywords((funcname), (kwargs)))
67+
#define _PyArg_NoKwnames(funcname, kwnames) \
68+
((kwnames) == NULL || _PyArg_NoKwnames((funcname), (kwnames)))
6669
#define _PyArg_NoPositional(funcname, args) \
6770
((args) == NULL || _PyArg_NoPositional((funcname), (args)))
6871

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add _PyArg_NoKwnames helper function. Patch by Dong-hee Na.

Objects/rangeobject.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ range_vectorcall(PyTypeObject *type, PyObject *const *args,
146146
size_t nargsf, PyObject *kwnames)
147147
{
148148
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
149-
if (kwnames && PyTuple_GET_SIZE(kwnames) != 0) {
150-
PyErr_Format(PyExc_TypeError, "range() takes no keyword arguments");
149+
if (!_PyArg_NoKwnames("range", kwnames)) {
151150
return NULL;
152151
}
153152
return range_from_array(type, args, nargs);

Objects/tupleobject.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,7 @@ static PyObject *
709709
tuple_vectorcall(PyObject *type, PyObject * const*args,
710710
size_t nargsf, PyObject *kwnames)
711711
{
712-
if (kwnames && PyTuple_GET_SIZE(kwnames) != 0) {
713-
PyErr_Format(PyExc_TypeError, "tuple() takes no keyword arguments");
712+
if (!_PyArg_NoKwnames("tuple", kwnames)) {
714713
return NULL;
715714
}
716715

Python/getargs.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2787,6 +2787,7 @@ _PyArg_UnpackStack(PyObject *const *args, Py_ssize_t nargs, const char *name,
27872787

27882788

27892789
#undef _PyArg_NoKeywords
2790+
#undef _PyArg_NoKwnames
27902791
#undef _PyArg_NoPositional
27912792

27922793
/* For type constructors that don't take keyword args
@@ -2813,7 +2814,6 @@ _PyArg_NoKeywords(const char *funcname, PyObject *kwargs)
28132814
return 0;
28142815
}
28152816

2816-
28172817
int
28182818
_PyArg_NoPositional(const char *funcname, PyObject *args)
28192819
{
@@ -2831,6 +2831,23 @@ _PyArg_NoPositional(const char *funcname, PyObject *args)
28312831
return 0;
28322832
}
28332833

2834+
int
2835+
_PyArg_NoKwnames(const char *funcname, PyObject *kwnames)
2836+
{
2837+
if (kwnames == NULL) {
2838+
return 1;
2839+
}
2840+
2841+
assert(PyTuple_CheckExact(kwnames));
2842+
2843+
if (PyTuple_GET_SIZE(kwnames) == 0) {
2844+
return 1;
2845+
}
2846+
2847+
PyErr_Format(PyExc_TypeError, "%s() takes no keyword arguments", funcname);
2848+
return 0;
2849+
}
2850+
28342851
void
28352852
_PyArg_Fini(void)
28362853
{

0 commit comments

Comments
 (0)