summaryrefslogtreecommitdiffstats
path: root/Parser
diff options
context:
space:
mode:
authorINADA Naoki <methane@users.noreply.github.com>2017-02-23 17:48:17 (GMT)
committerGitHub <noreply@github.com>2017-02-23 17:48:17 (GMT)
commit4c78c527d215c37472145152cb0e95f196cdddc9 (patch)
treed35b832dec5db996f48e1ef9c40ae987c9fd1855 /Parser
parent561ca80cffd37445bac82ff820b8f52b98c50517 (diff)
downloadcpython-4c78c527d215c37472145152cb0e95f196cdddc9.zip
cpython-4c78c527d215c37472145152cb0e95f196cdddc9.tar.gz
cpython-4c78c527d215c37472145152cb0e95f196cdddc9.tar.bz2
bpo-29622: Make AST constructor to accept less than enough number of positional arguments (GH-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.
Diffstat (limited to 'Parser')
-rw-r--r--Parser/asdl_c.py36
1 files changed, 17 insertions, 19 deletions
diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py
index 444cdea..096f5f8 100644
--- a/Parser/asdl_c.py
+++ b/Parser/asdl_c.py
@@ -664,29 +664,27 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
if (numfields == -1)
goto cleanup;
}
+
res = 0; /* if no error occurs, this stays 0 to the end */
- if (PyTuple_GET_SIZE(args) > 0) {
- if (numfields != PyTuple_GET_SIZE(args)) {
- PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
- "%zd positional argument%s",
- Py_TYPE(self)->tp_name,
- numfields == 0 ? "" : "either 0 or ",
- numfields, numfields == 1 ? "" : "s");
+ if (numfields < PyTuple_GET_SIZE(args)) {
+ PyErr_Format(PyExc_TypeError, "%.400s constructor takes at most "
+ "%zd positional argument%s",
+ Py_TYPE(self)->tp_name,
+ numfields, numfields == 1 ? "" : "s");
+ res = -1;
+ goto cleanup;
+ }
+ for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
+ /* cannot be reached when fields is NULL */
+ PyObject *name = PySequence_GetItem(fields, i);
+ if (!name) {
res = -1;
goto cleanup;
}
- for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
- /* cannot be reached when fields is NULL */
- PyObject *name = PySequence_GetItem(fields, i);
- if (!name) {
- res = -1;
- goto cleanup;
- }
- res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
- Py_DECREF(name);
- if (res < 0)
- goto cleanup;
- }
+ res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
+ Py_DECREF(name);
+ if (res < 0)
+ goto cleanup;
}
if (kw) {
i = 0; /* needed by PyDict_Next */