summaryrefslogtreecommitdiffstats
path: root/Modules/_testcapimodule.c
diff options
context:
space:
mode:
authorLarry Hastings <larry@hastings.org>2012-03-20 20:06:16 (GMT)
committerLarry Hastings <larry@hastings.org>2012-03-20 20:06:16 (GMT)
commit83a9f4869958f992b2964404f43dcc596c5a7892 (patch)
tree2fcd7a09a5b14d67ead54dd827872d9cecff75b5 /Modules/_testcapimodule.c
parent2a886412ba8d7a197c55b15aa2d89530c0a2f68a (diff)
downloadcpython-83a9f4869958f992b2964404f43dcc596c5a7892.zip
cpython-83a9f4869958f992b2964404f43dcc596c5a7892.tar.gz
cpython-83a9f4869958f992b2964404f43dcc596c5a7892.tar.bz2
Issue #14328: Add keyword-only parameters to PyArg_ParseTupleAndKeywords.
They're optional-only for now (unlike in pure Python) but that's all I needed. The syntax can easily be relaxed if we want to support required keyword-only arguments for extension types in the future.
Diffstat (limited to 'Modules/_testcapimodule.c')
-rw-r--r--Modules/_testcapimodule.c20
1 files changed, 19 insertions, 1 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 9cafa73..093f205 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -801,7 +801,8 @@ getargs_tuple(PyObject *self, PyObject *args)
}
/* test PyArg_ParseTupleAndKeywords */
-static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
+static PyObject *
+getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
{
static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL};
static char *fmt="(ii)i|(i(ii))(iii)i";
@@ -816,6 +817,21 @@ static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwar
int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]);
}
+/* test PyArg_ParseTupleAndKeywords keyword-only arguments */
+static PyObject *
+getargs_keyword_only(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+ static char *keywords[] = {"required", "optional", "keyword_only", NULL};
+ int required = -1;
+ int optional = -1;
+ int keyword_only = -1;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i$i", keywords,
+ &required, &optional, &keyword_only))
+ return NULL;
+ return Py_BuildValue("iii", required, optional, keyword_only);
+}
+
/* Functions to call PyArg_ParseTuple with integer format codes,
and return the result.
*/
@@ -2400,6 +2416,8 @@ static PyMethodDef TestMethods[] = {
{"getargs_tuple", getargs_tuple, METH_VARARGS},
{"getargs_keywords", (PyCFunction)getargs_keywords,
METH_VARARGS|METH_KEYWORDS},
+ {"getargs_keyword_only", (PyCFunction)getargs_keyword_only,
+ METH_VARARGS|METH_KEYWORDS},
{"getargs_b", getargs_b, METH_VARARGS},
{"getargs_B", getargs_B, METH_VARARGS},
{"getargs_h", getargs_h, METH_VARARGS},