Skip to content

Commit d0cad25

Browse files
committed
Don't use PY_CXX_CONST; add tests
1 parent 3ec6ef0 commit d0cad25

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

Doc/c-api/arg.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ API Functions
527527
.. versionadded:: next
528528
529529
530-
.. c:function:: int PyArg_ParseVectorAndKeywords(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames, const char * const *kwlist, ...)
530+
.. c:function:: int PyArg_ParseVectorAndKeywords(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames, const char *format, const char * const *kwlist, ...)
531531
532532
Parse the parameters of a function that takes both vector and keyword
533533
parameters into local variables (that is, a function using the

Include/cpython/modsupport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ PyAPI_FUNC(int) PyArg_ParseVectorAndKeywords(
1212
Py_ssize_t nargs,
1313
PyObject *kwnames,
1414
const char *format,
15-
PY_CXX_CONST char * const *kwlist,
15+
const char * const *kwlist,
1616
...);
1717

1818
// A data structure that can be used to run initialization code once in a

Lib/test/test_capi/test_modsupport.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,22 @@ def test_negative_freethreading(self, modname, minor, build):
152152
msg = "only compatible with free-threaded CPython"
153153
with self.assertRaisesRegex(ImportError, msg):
154154
_testcapi.pyabiinfo_check(modname, 1, minor, ft_flag, build, 0)
155+
156+
157+
class TestModsupport(unittest.TestCase):
158+
def test_pyarg_parsevector(self):
159+
func = _testcapi.pyarg_parsevector
160+
self.assertEqual(func(1, 2), (1, 2, 0))
161+
self.assertEqual(func(1, 2, 3), (1, 2, 3))
162+
self.assertRaises(TypeError, func, 1)
163+
self.assertRaises(TypeError, func, "str", 2)
164+
165+
def test_funcandkeywords(self):
166+
func = _testcapi.pyarg_parsevectorandkeywords
167+
self.assertEqual(func(1, 2), (1, 2, 0))
168+
self.assertEqual(func(1, 2, 3), (1, 2, 3))
169+
self.assertEqual(func(1, b=2), (1, 2, 0))
170+
self.assertEqual(func(1, b=2, c=3), (1, 2, 3))
171+
self.assertRaises(TypeError, func, 1)
172+
self.assertRaises(TypeError, func, "str", 2)
173+
self.assertRaises(TypeError, func, 1, z=2)

Modules/_testcapi/modsupport.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,36 @@ pyabiinfo_check(PyObject *Py_UNUSED(module), PyObject *args)
2525
Py_RETURN_NONE;
2626
}
2727

28+
static PyObject *
29+
pyarg_parsevector(PyObject* self, PyObject* const* args, Py_ssize_t nargs)
30+
{
31+
int a, b, c = 0;
32+
if (!PyArg_ParseVector(args, nargs, "ii|i", &a, &b, &c)) {
33+
return NULL;
34+
}
35+
return Py_BuildValue("iii", a, b, c);
36+
}
37+
38+
static PyObject *
39+
pyarg_parsevectorandkeywords(PyObject* self, PyObject* const* args,
40+
Py_ssize_t nargs, PyObject* kwnames)
41+
{
42+
int a, b, c = 0;
43+
const char *kwlist[] = {"a", "b", "c", NULL};
44+
if (!PyArg_ParseVectorAndKeywords(args, nargs, kwnames,
45+
"ii|i", kwlist,
46+
&a, &b, &c)) {
47+
return NULL;
48+
}
49+
return Py_BuildValue("iii", a, b, c);
50+
}
51+
2852
static PyMethodDef TestMethods[] = {
2953
{"pyabiinfo_check", pyabiinfo_check, METH_VARARGS},
54+
{"pyarg_parsevector", _PyCFunction_CAST(pyarg_parsevector), METH_FASTCALL},
55+
{"pyarg_parsevectorandkeywords",
56+
_PyCFunction_CAST(pyarg_parsevectorandkeywords),
57+
METH_FASTCALL | METH_KEYWORDS},
3058
{NULL},
3159
};
3260

0 commit comments

Comments
 (0)