Skip to content

Commit b0216f3

Browse files
author
benjamin.peterson
committed
fix #4720: the format to PyArg_ParseTupleAndKeywords can now start with '|'
git-svn-id: http://svn.python.org/projects/python/trunk@67905 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 09d30ec commit b0216f3

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

Modules/_testcapimodule.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,32 @@ test_u_code(PyObject *self)
518518
return Py_None;
519519
}
520520

521+
static PyObject *
522+
test_empty_argparse(PyObject *self)
523+
{
524+
/* Test that formats can begin with '|'. See issue #4720. */
525+
PyObject *tuple, *dict = NULL;
526+
static char *kwlist[] = {NULL};
527+
int result;
528+
tuple = PyTuple_New(0);
529+
if (!tuple)
530+
return NULL;
531+
if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0)
532+
goto done;
533+
dict = PyDict_New();
534+
if (!dict)
535+
goto done;
536+
result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist);
537+
done:
538+
Py_DECREF(tuple);
539+
Py_XDECREF(dict);
540+
if (result < 0)
541+
return NULL;
542+
else {
543+
Py_RETURN_NONE;
544+
}
545+
}
546+
521547
static PyObject *
522548
codec_incrementalencoder(PyObject *self, PyObject *args)
523549
{
@@ -780,6 +806,7 @@ static PyMethodDef TestMethods[] = {
780806
{"test_long_api", (PyCFunction)test_long_api, METH_NOARGS},
781807
{"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
782808
{"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
809+
{"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS},
783810
{"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS},
784811
{"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
785812
{"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS,

Python/getargs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1601,7 +1601,7 @@ vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
16011601
}
16021602
}
16031603

1604-
if (!IS_END_OF_FORMAT(*format)) {
1604+
if (!IS_END_OF_FORMAT(*format) && *format != '|') {
16051605
PyErr_Format(PyExc_RuntimeError,
16061606
"more argument specifiers than keyword list entries "
16071607
"(remaining format:'%s')", format);

0 commit comments

Comments
 (0)