Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b6429a2

Browse files
committed
validate_varargslist(): Fix two bugs in this function, one that affected
it when *args and/or **kw are used, and one when they are not. This closes bug #125375: "parser.tuple2ast() failure on valid parse tree".
1 parent ed911b8 commit b6429a2

1 file changed

Lines changed: 20 additions & 11 deletions

File tree

Modules/parsermodule.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,12 +1098,17 @@ validate_varargslist(node *tree)
10981098
int res = validate_ntype(tree, varargslist) && (nch != 0);
10991099
int sym;
11001100

1101+
if (!res)
1102+
return 0;
11011103
if (nch < 1) {
11021104
err_string("varargslist missing child nodes");
11031105
return 0;
11041106
}
11051107
sym = TYPE(CHILD(tree, 0));
11061108
if (sym == STAR || sym == DOUBLESTAR)
1109+
/* whole thing matches:
1110+
* '*' NAME [',' '**' NAME] | '**' NAME
1111+
*/
11071112
res = validate_varargslist_trailer(tree, 0);
11081113
else if (sym == fpdef) {
11091114
int i = 0;
@@ -1127,18 +1132,24 @@ validate_varargslist(node *tree)
11271132
}
11281133
if (res && i < nch) {
11291134
res = validate_comma(CHILD(tree, i));
1130-
if (res)
1131-
++i;
1135+
++i;
1136+
if (res && i < nch
1137+
&& (TYPE(CHILD(tree, i)) == DOUBLESTAR
1138+
|| TYPE(CHILD(tree, i)) == STAR))
1139+
break;
11321140
}
11331141
}
1134-
/* handle '*' NAME [',' '**' NAME] | '**' NAME */
1142+
/* ... '*' NAME [',' '**' NAME] | '**' NAME
1143+
* i --^^^
1144+
*/
11351145
if (res)
11361146
res = validate_varargslist_trailer(tree, i);
11371147
}
11381148
else {
11391149
/*
11401150
* fpdef ['=' test] (',' fpdef ['=' test])* [',']
11411151
*/
1152+
/* strip trailing comma node */
11421153
if (sym == COMMA) {
11431154
res = validate_comma(CHILD(tree, nch-1));
11441155
if (!res)
@@ -1150,9 +1161,9 @@ validate_varargslist(node *tree)
11501161
*/
11511162
res = validate_fpdef(CHILD(tree, 0));
11521163
++i;
1153-
if (res && (i+2 < nch) && TYPE(CHILD(tree, 1)) == EQUAL) {
1154-
res = (validate_equal(CHILD(tree, 1))
1155-
&& validate_test(CHILD(tree, 2)));
1164+
if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1165+
res = (validate_equal(CHILD(tree, i))
1166+
&& validate_test(CHILD(tree, i+1)));
11561167
i += 2;
11571168
}
11581169
/*
@@ -1163,12 +1174,10 @@ validate_varargslist(node *tree)
11631174
res = (validate_comma(CHILD(tree, i))
11641175
&& validate_fpdef(CHILD(tree, i+1)));
11651176
i += 2;
1166-
if (res && (nch - i) >= 2
1167-
&& TYPE(CHILD(tree, i)) == COMMA) {
1168-
res = (validate_comma(CHILD(tree, i))
1177+
if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1178+
res = (validate_equal(CHILD(tree, i))
11691179
&& validate_test(CHILD(tree, i+1)));
1170-
if (res)
1171-
i += 2;
1180+
i += 2;
11721181
}
11731182
}
11741183
if (res && nch - i != 0) {

0 commit comments

Comments
 (0)