bpo-29622: Make AST constructor to accept less than enough number of … · pythoncapi/cpython@4c78c52 · GitHub
Skip to content

Commit 4c78c52

Browse files
authored
bpo-29622: Make AST constructor to accept less than enough number of positional arguments (pythonGH-249)
bpo-29463 added optional "docstring" field to 4 AST types. While it is optional, it breaks backward compatibility because AST constructor requires number of positional argument is same to number of fields. AST types accepts empty arguments, and incomplete keyword arguments. But it's not big problem because field can be filled after creation, and checked when compiling. So stop requiring complete set of fields for positional arguments too.
1 parent 561ca80 commit 4c78c52

3 files changed

Lines changed: 34 additions & 42 deletions

File tree

Lib/test/test_ast.py

Lines changed: 0 additions & 4 deletions

Parser/asdl_c.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -664,29 +664,27 @@ def visitModule(self, mod):
664664
if (numfields == -1)
665665
goto cleanup;
666666
}
667+
667668
res = 0; /* if no error occurs, this stays 0 to the end */
668-
if (PyTuple_GET_SIZE(args) > 0) {
669-
if (numfields != PyTuple_GET_SIZE(args)) {
670-
PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
671-
"%zd positional argument%s",
672-
Py_TYPE(self)->tp_name,
673-
numfields == 0 ? "" : "either 0 or ",
674-
numfields, numfields == 1 ? "" : "s");
669+
if (numfields < PyTuple_GET_SIZE(args)) {
670+
PyErr_Format(PyExc_TypeError, "%.400s constructor takes at most "
671+
"%zd positional argument%s",
672+
Py_TYPE(self)->tp_name,
673+
numfields, numfields == 1 ? "" : "s");
674+
res = -1;
675+
goto cleanup;
676+
}
677+
for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
678+
/* cannot be reached when fields is NULL */
679+
PyObject *name = PySequence_GetItem(fields, i);
680+
if (!name) {
675681
res = -1;
676682
goto cleanup;
677683
}
678-
for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
679-
/* cannot be reached when fields is NULL */
680-
PyObject *name = PySequence_GetItem(fields, i);
681-
if (!name) {
682-
res = -1;
683-
goto cleanup;
684-
}
685-
res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
686-
Py_DECREF(name);
687-
if (res < 0)
688-
goto cleanup;
689-
}
684+
res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
685+
Py_DECREF(name);
686+
if (res < 0)
687+
goto cleanup;
690688
}
691689
if (kw) {
692690
i = 0; /* needed by PyDict_Next */

Python/Python-ast.c

Lines changed: 17 additions & 19 deletions

0 commit comments

Comments
 (0)