bpo-29622: Make AST constructor to accept less than enough number of … · python/cpython@4c78c52 (original) (raw)

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -373,12 +373,8 @@ def test_nodeclasses(self):
373 373 self.assertEqual(x.right, 3)
374 374 self.assertEqual(x.lineno, 0)
375 375
376 -# node raises exception when not given enough arguments
377 -self.assertRaises(TypeError, ast.BinOp, 1, 2)
378 376 # node raises exception when given too many arguments
379 377 self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4)
380 -# node raises exception when not given enough arguments
381 -self.assertRaises(TypeError, ast.BinOp, 1, 2, lineno=0)
382 378 # node raises exception when given too many arguments
383 379 self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4, lineno=0)
384 380
Original file line number Diff line number Diff line change
@@ -664,29 +664,27 @@ def visitModule(self, mod):
664 664 if (numfields == -1)
665 665 goto cleanup;
666 666 }
667 +
667 668 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) {
675 681 res = -1;
676 682 goto cleanup;
677 683 }
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;
690 688 }
691 689 if (kw) {
692 690 i = 0; /* needed by PyDict_Next */
Original file line number Diff line number Diff line change
@@ -551,29 +551,27 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
551 551 if (numfields == -1)
552 552 goto cleanup;
553 553 }
554 +
554 555 res = 0; /* if no error occurs, this stays 0 to the end */
555 -if (PyTuple_GET_SIZE(args) > 0) {
556 -if (numfields != PyTuple_GET_SIZE(args)) {
557 -PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
558 -"%zd positional argument%s",
559 -Py_TYPE(self)->tp_name,
560 -numfields == 0 ? "" : "either 0 or ",
561 -numfields, numfields == 1 ? "" : "s");
556 +if (numfields < PyTuple_GET_SIZE(args)) {
557 +PyErr_Format(PyExc_TypeError, "%.400s constructor takes at most "
558 +"%zd positional argument%s",
559 +Py_TYPE(self)->tp_name,
560 +numfields, numfields == 1 ? "" : "s");
561 +res = -1;
562 + goto cleanup;
563 + }
564 +for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
565 +/* cannot be reached when fields is NULL */
566 +PyObject *name = PySequence_GetItem(fields, i);
567 +if (!name) {
562 568 res = -1;
563 569 goto cleanup;
564 570 }
565 -for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
566 -/* cannot be reached when fields is NULL */
567 -PyObject *name = PySequence_GetItem(fields, i);
568 -if (!name) {
569 -res = -1;
570 - goto cleanup;
571 - }
572 -res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
573 -Py_DECREF(name);
574 -if (res < 0)
575 - goto cleanup;
576 - }
571 +res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
572 +Py_DECREF(name);
573 +if (res < 0)
574 + goto cleanup;
577 575 }
578 576 if (kw) {
579 577 i = 0; /* needed by PyDict_Next */