Skip to content

Commit 852fa9f

Browse files
committed
Merged revisions 84209, 84214 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r84209 | amaury.forgeotdarc | 2010-08-19 19:43:15 +0200 (jeu., 19 août 2010) | 5 lines Check the return values for all functions returning an ast node. Failure to do it may result in strange error messages or even crashes, in admittedly convoluted cases that are normally syntax errors, like: def f(*xx, __debug__): pass ........ r84214 | amaury.forgeotdarc | 2010-08-19 23:32:38 +0200 (jeu., 19 août 2010) | 3 lines Add tests for r84209 (crashes in the Ast builder) Also remove one tab, and move a check closer to the possible failure. ........
1 parent 421b74e commit 852fa9f

2 files changed

Lines changed: 26 additions & 26 deletions

File tree

Lib/test/test_syntax.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,12 @@
474474
File "<doctest test.test_syntax[50]>", line 1
475475
SyntaxError: can't assign to literal
476476
477+
Corner-case that used to crash:
478+
479+
>>> def f(*xx, **__debug__): pass
480+
Traceback (most recent call last):
481+
SyntaxError: cannot assign to __debug__
482+
477483
"""
478484

479485
import re

Python/ast.c

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -688,10 +688,10 @@ ast_for_arguments(struct compiling *c, const node *n)
688688
}
689689
args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
690690
if (!args && n_args)
691-
return NULL; /* Don't need to goto error; no objects allocated */
691+
return NULL;
692692
defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
693693
if (!defaults && n_defaults)
694-
return NULL; /* Don't need to goto error; no objects allocated */
694+
return NULL;
695695

696696
/* fpdef: NAME | '(' fplist ')'
697697
fplist: fpdef (',' fpdef)* [',']
@@ -711,7 +711,7 @@ ast_for_arguments(struct compiling *c, const node *n)
711711
if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
712712
expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
713713
if (!expression)
714-
goto error;
714+
return NULL;
715715
assert(defaults != NULL);
716716
asdl_seq_SET(defaults, j++, expression);
717717
i += 2;
@@ -722,11 +722,11 @@ ast_for_arguments(struct compiling *c, const node *n)
722722
def f((x, (y))): pass will just incur the tuple unpacking warning. */
723723
if (parenthesized && !complex_args) {
724724
ast_error(n, "parenthesized arg with default");
725-
goto error;
725+
return NULL;
726726
}
727727
ast_error(n,
728728
"non-default argument follows default argument");
729-
goto error;
729+
return NULL;
730730
}
731731
if (NCH(ch) == 3) {
732732
ch = CHILD(ch, 1);
@@ -735,11 +735,11 @@ ast_for_arguments(struct compiling *c, const node *n)
735735
/* We have complex arguments, setup for unpacking. */
736736
if (Py_Py3kWarningFlag && !ast_warn(c, ch,
737737
"tuple parameter unpacking has been removed in 3.x"))
738-
goto error;
738+
return NULL;
739739
complex_args = 1;
740740
asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
741741
if (!asdl_seq_GET(args, k-1))
742-
goto error;
742+
return NULL;
743743
} else {
744744
/* def foo((x)): setup for checking NAME below. */
745745
/* Loop because there can be many parens and tuple
@@ -754,55 +754,50 @@ ast_for_arguments(struct compiling *c, const node *n)
754754
PyObject *id;
755755
expr_ty name;
756756
if (!forbidden_check(c, n, STR(CHILD(ch, 0))))
757-
goto error;
757+
return NULL;
758758
id = NEW_IDENTIFIER(CHILD(ch, 0));
759759
if (!id)
760-
goto error;
760+
return NULL;
761761
name = Name(id, Param, LINENO(ch), ch->n_col_offset,
762762
c->c_arena);
763763
if (!name)
764-
goto error;
764+
return NULL;
765765
asdl_seq_SET(args, k++, name);
766766

767767
}
768768
i += 2; /* the name and the comma */
769769
if (parenthesized && Py_Py3kWarningFlag &&
770770
!ast_warn(c, ch, "parenthesized argument names "
771771
"are invalid in 3.x"))
772-
goto error;
772+
return NULL;
773773

774774
break;
775775
}
776776
case STAR:
777777
if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
778-
goto error;
778+
return NULL;
779779
vararg = NEW_IDENTIFIER(CHILD(n, i+1));
780780
if (!vararg)
781-
goto error;
781+
return NULL;
782782
i += 3;
783783
break;
784784
case DOUBLESTAR:
785785
if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
786-
goto error;
786+
return NULL;
787787
kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
788788
if (!kwarg)
789-
goto error;
789+
return NULL;
790790
i += 3;
791791
break;
792792
default:
793793
PyErr_Format(PyExc_SystemError,
794794
"unexpected node in varargslist: %d @ %d",
795795
TYPE(ch), i);
796-
goto error;
796+
return NULL;
797797
}
798798
}
799799

800800
return arguments(args, vararg, kwarg, defaults, c->c_arena);
801-
802-
error:
803-
Py_XDECREF(vararg);
804-
Py_XDECREF(kwarg);
805-
return NULL;
806801
}
807802

808803
static expr_ty
@@ -887,9 +882,9 @@ ast_for_decorators(struct compiling *c, const node *n)
887882

888883
for (i = 0; i < NCH(n); i++) {
889884
d = ast_for_decorator(c, CHILD(n, i));
890-
if (!d)
891-
return NULL;
892-
asdl_seq_SET(decorator_seq, i, d);
885+
if (!d)
886+
return NULL;
887+
asdl_seq_SET(decorator_seq, i, d);
893888
}
894889
return decorator_seq;
895890
}
@@ -2247,11 +2242,10 @@ ast_for_expr_stmt(struct compiling *c, const node *n)
22472242
return NULL;
22482243
}
22492244
e = ast_for_testlist(c, ch);
2250-
2251-
/* set context to assign */
22522245
if (!e)
22532246
return NULL;
22542247

2248+
/* set context to assign */
22552249
if (!set_context(c, e, Store, CHILD(n, i)))
22562250
return NULL;
22572251

0 commit comments

Comments
 (0)