Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4c78c52

Browse files
authored
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 constructorrequires 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 parent561ca80 commit4c78c52

File tree

3 files changed

+34
-42
lines changed

3 files changed

+34
-42
lines changed

‎Lib/test/test_ast.py‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,8 @@ def test_nodeclasses(self):
373373
self.assertEqual(x.right,3)
374374
self.assertEqual(x.lineno,0)
375375

376-
# node raises exception when not given enough arguments
377-
self.assertRaises(TypeError,ast.BinOp,1,2)
378376
# node raises exception when given too many arguments
379377
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)
382378
# node raises exception when given too many arguments
383379
self.assertRaises(TypeError,ast.BinOp,1,2,3,4,lineno=0)
384380

‎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
Original file line numberDiff line numberDiff line change
@@ -551,29 +551,27 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
551551
if (numfields==-1)
552552
gotocleanup;
553553
}
554+
554555
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+
gotocleanup;
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) {
562568
res=-1;
563569
gotocleanup;
564570
}
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-
gotocleanup;
571-
}
572-
res=PyObject_SetAttr(self,name,PyTuple_GET_ITEM(args,i));
573-
Py_DECREF(name);
574-
if (res<0)
575-
gotocleanup;
576-
}
571+
res=PyObject_SetAttr(self,name,PyTuple_GET_ITEM(args,i));
572+
Py_DECREF(name);
573+
if (res<0)
574+
gotocleanup;
577575
}
578576
if (kw) {
579577
i=0;/* needed by PyDict_Next */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2026 Movatter.jp