diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index ac79989796d..366269fe405 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -373,12 +373,8 @@ class AST_Tests(unittest.TestCase): self.assertEqual(x.right, 3) self.assertEqual(x.lineno, 0) - # node raises exception when not given enough arguments - self.assertRaises(TypeError, ast.BinOp, 1, 2) # node raises exception when given too many arguments self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4) - # node raises exception when not given enough arguments - self.assertRaises(TypeError, ast.BinOp, 1, 2, lineno=0) # node raises exception when given too many arguments self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4, lineno=0) diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py index 444cdea341d..096f5f8c749 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 */ diff --git a/Python/Python-ast.c b/Python/Python-ast.c index a6a49f78b1e..2759b2fe9c4 100644 --- a/Python/Python-ast.c +++ b/Python/Python-ast.c @@ -551,29 +551,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 */