Improved handling of syntax errors.

Expand set of errors caught in set_context().  Some new errors, some
old error messages changed for consistency.

Fixed error checking in generator expression code.  The first set of
tests were impossible condition given the grammar.  In general, the
ast code uses REQ() for those sanity checks.

Fix some error handling for augmented assignments.  As comments in the
code explain, set_context() ought to work here, but I got unexpected
crashes when I tried it.  Should come back to this.

Add note to Grammar that yield expression is a special case.

Add doctest cases for SyntaxErrors raised by ast.c.
This commit is contained in:
Jeremy Hylton 2006-01-27 15:18:39 +00:00
parent 3d344e8b07
commit c960f26044
4 changed files with 314 additions and 36 deletions

View file

@ -137,7 +137,7 @@
>>> (y for y in (1,2)) = 10
Traceback (most recent call last):
...
SyntaxError: assignment to generator expression not possible (<doctest test.test_genexps.__test__.doctests[40]>, line 1)
SyntaxError: can't assign to generator expression (<doctest test.test_genexps.__test__.doctests[40]>, line 1)
>>> (y for y in (1,2)) += 10
Traceback (most recent call last):

View file

@ -1,3 +1,239 @@
"""This module tests SyntaxErrors.
Here's an example of the sort of thing that is tested.
>>> def f(x):
... global x
Traceback (most recent call last):
SyntaxError: name 'x' is local and global
The tests are all raise SyntaxErrors. They were created by checking
each C call that raises SyntaxError. There are several modules that
raise these exceptions-- ast.c, compile.c, future.c, pythonrun.c, and
symtable.c.
The parser itself outlaws a lot of invalid syntax. None of these
errors are tested here at the moment. We should add some tests; since
there are infinitely many programs with invalid syntax, we would need
to be judicious in selecting some.
The compiler generates a synthetic module name for code executed by
doctest. Since all the code comes from the same module, a suffix like
[1] is appended to the module name, As a consequence, changing the
order of tests in this module means renumbering all the errors after
it. (Maybe we should enable the ellipsis option for these tests.)
In ast.c, syntax errors are raised by calling ast_error().
Errors from set_context():
TODO(jhylton): "assignment to None" is inconsistent with other messages
>>> obj.None = 1
Traceback (most recent call last):
SyntaxError: assignment to None (<doctest test.test_syntax[1]>, line 1)
>>> None = 1
Traceback (most recent call last):
SyntaxError: assignment to None (<doctest test.test_syntax[2]>, line 1)
It's a syntax error to assign to the empty tuple. Why isn't it an
error to assign to the empty list? It will always raise some error at
runtime.
>>> () = 1
Traceback (most recent call last):
SyntaxError: can't assign to () (<doctest test.test_syntax[3]>, line 1)
>>> f() = 1
Traceback (most recent call last):
SyntaxError: can't assign to function call (<doctest test.test_syntax[4]>, line 1)
>>> del f()
Traceback (most recent call last):
SyntaxError: can't delete function call (<doctest test.test_syntax[5]>, line 1)
>>> a + 1 = 2
Traceback (most recent call last):
SyntaxError: can't assign to operator (<doctest test.test_syntax[6]>, line 1)
>>> (x for x in x) = 1
Traceback (most recent call last):
SyntaxError: can't assign to generator expression (<doctest test.test_syntax[7]>, line 1)
>>> 1 = 1
Traceback (most recent call last):
SyntaxError: can't assign to literal (<doctest test.test_syntax[8]>, line 1)
>>> "abc" = 1
Traceback (most recent call last):
SyntaxError: can't assign to literal (<doctest test.test_syntax[9]>, line 1)
>>> `1` = 1
Traceback (most recent call last):
SyntaxError: can't assign to repr (<doctest test.test_syntax[10]>, line 1)
If the left-hand side of an assignment is a list or tuple, an illegal
expression inside that contain should still cause a syntax error.
This test just checks a couple of cases rather than enumerating all of
them.
>>> (a, "b", c) = (1, 2, 3)
Traceback (most recent call last):
SyntaxError: can't assign to literal (<doctest test.test_syntax[11]>, line 1)
>>> [a, b, c + 1] = [1, 2, 3]
Traceback (most recent call last):
SyntaxError: can't assign to operator (<doctest test.test_syntax[12]>, line 1)
From compiler_complex_args():
>>> def f(None=1):
... pass
Traceback (most recent call last):
SyntaxError: assignment to None (<doctest test.test_syntax[13]>, line 1)
From ast_for_arguments():
>>> def f(x, y=1, z):
... pass
Traceback (most recent call last):
SyntaxError: non-default argument follows default argument (<doctest test.test_syntax[14]>, line 1)
>>> def f(x, None):
... pass
Traceback (most recent call last):
SyntaxError: assignment to None (<doctest test.test_syntax[15]>, line 1)
>>> def f(*None):
... pass
Traceback (most recent call last):
SyntaxError: assignment to None (<doctest test.test_syntax[16]>, line 1)
>>> def f(**None):
... pass
Traceback (most recent call last):
SyntaxError: assignment to None (<doctest test.test_syntax[17]>, line 1)
From ast_for_funcdef():
>>> def None(x):
... pass
Traceback (most recent call last):
SyntaxError: assignment to None (<doctest test.test_syntax[18]>, line 1)
From ast_for_call():
>>> def f(it, *varargs):
... return list(it)
>>> L = range(10)
>>> f(x for x in L)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> f(x for x in L, 1)
Traceback (most recent call last):
SyntaxError: Generator expression must be parenthesized if not sole argument (<doctest test.test_syntax[22]>, line 1)
>>> f((x for x in L), 1)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11,
... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22,
... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33,
... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44,
... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55,
... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66,
... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77,
... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88,
... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99,
... i100, i101, i102, i103, i104, i105, i106, i107, i108,
... i109, i110, i111, i112, i113, i114, i115, i116, i117,
... i118, i119, i120, i121, i122, i123, i124, i125, i126,
... i127, i128, i129, i130, i131, i132, i133, i134, i135,
... i136, i137, i138, i139, i140, i141, i142, i143, i144,
... i145, i146, i147, i148, i149, i150, i151, i152, i153,
... i154, i155, i156, i157, i158, i159, i160, i161, i162,
... i163, i164, i165, i166, i167, i168, i169, i170, i171,
... i172, i173, i174, i175, i176, i177, i178, i179, i180,
... i181, i182, i183, i184, i185, i186, i187, i188, i189,
... i190, i191, i192, i193, i194, i195, i196, i197, i198,
... i199, i200, i201, i202, i203, i204, i205, i206, i207,
... i208, i209, i210, i211, i212, i213, i214, i215, i216,
... i217, i218, i219, i220, i221, i222, i223, i224, i225,
... i226, i227, i228, i229, i230, i231, i232, i233, i234,
... i235, i236, i237, i238, i239, i240, i241, i242, i243,
... i244, i245, i246, i247, i248, i249, i250, i251, i252,
... i253, i254, i255)
Traceback (most recent call last):
SyntaxError: more than 255 arguments (<doctest test.test_syntax[24]>, line 1)
The actual error cases counts positional arguments, keyword arguments,
and generator expression arguments separately. This test combines the
three.
>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11,
... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22,
... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33,
... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44,
... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55,
... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66,
... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77,
... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88,
... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99,
... i100, i101, i102, i103, i104, i105, i106, i107, i108,
... i109, i110, i111, i112, i113, i114, i115, i116, i117,
... i118, i119, i120, i121, i122, i123, i124, i125, i126,
... i127, i128, i129, i130, i131, i132, i133, i134, i135,
... i136, i137, i138, i139, i140, i141, i142, i143, i144,
... i145, i146, i147, i148, i149, i150, i151, i152, i153,
... i154, i155, i156, i157, i158, i159, i160, i161, i162,
... i163, i164, i165, i166, i167, i168, i169, i170, i171,
... i172, i173, i174, i175, i176, i177, i178, i179, i180,
... i181, i182, i183, i184, i185, i186, i187, i188, i189,
... i190, i191, i192, i193, i194, i195, i196, i197, i198,
... i199, i200, i201, i202, i203, i204, i205, i206, i207,
... i208, i209, i210, i211, i212, i213, i214, i215, i216,
... i217, i218, i219, i220, i221, i222, i223, i224, i225,
... i226, i227, i228, i229, i230, i231, i232, i233, i234,
... i235, i236, i237, i238, i239, i240, i241, i242, i243,
... (x for x in i244), i245, i246, i247, i248, i249, i250, i251,
... i252=1, i253=1, i254=1, i255=1)
Traceback (most recent call last):
SyntaxError: more than 255 arguments (<doctest test.test_syntax[25]>, line 1)
>>> f(lambda x: x[0] = 3)
Traceback (most recent call last):
SyntaxError: lambda cannot contain assignment (<doctest test.test_syntax[26]>, line 1)
The grammar accepts any test (basically, any expression) in the
keyword slot of a call site. Test a few different options.
>>> f(x()=2)
Traceback (most recent call last):
SyntaxError: keyword can't be an expression (<doctest test.test_syntax[27]>, line 1)
>>> f(a or b=1)
Traceback (most recent call last):
SyntaxError: keyword can't be an expression (<doctest test.test_syntax[28]>, line 1)
>>> f(x.y=1)
Traceback (most recent call last):
SyntaxError: keyword can't be an expression (<doctest test.test_syntax[29]>, line 1)
From ast_for_expr_stmt():
>>> (x for x in x) += 1
Traceback (most recent call last):
SyntaxError: augmented assignment to generator expression not possible (<doctest test.test_syntax[30]>, line 1)
>>> None += 1
Traceback (most recent call last):
SyntaxError: assignment to None (<doctest test.test_syntax[31]>, line 1)
>>> f() += 1
Traceback (most recent call last):
SyntaxError: illegal expression for augmented assignment (<doctest test.test_syntax[32]>, line 1)
"""
import re
import unittest
import warnings
@ -56,6 +292,8 @@ def test_delete_deref(self):
def test_main():
test_support.run_unittest(SyntaxTestCase)
from test import test_syntax
test_support.run_doctest(test_syntax, verbosity=True)
if __name__ == "__main__":
test_main()

View file

@ -55,6 +55,7 @@ module Python
| Dict(expr* keys, expr* values)
| ListComp(expr elt, comprehension* generators)
| GeneratorExp(expr elt, comprehension* generators)
-- the grammar constrains where yield expressions can occur
| Yield(expr? value)
-- need sequences for compare to distinguish between
-- x < 4 < 3 and (x < 4) < 3

View file

@ -329,6 +329,19 @@ static int
set_context(expr_ty e, expr_context_ty ctx, const node *n)
{
asdl_seq *s = NULL;
/* If a particular expression type can't be used for assign / delete,
set expr_name to its name and an error message will be generated.
*/
const char* expr_name = NULL;
/* The ast defines augmented store and load contexts, but the
implementation here doesn't actually use them. The code may be
a little more complex than necessary as a result. It also means
that expressions in an augmented assignment have no context.
Consider restructuring so that augmented assignment uses
set_context(), too
*/
assert(ctx != AugStore && ctx != AugLoad);
switch (e->kind) {
case Attribute_kind:
@ -358,30 +371,50 @@ set_context(expr_ty e, expr_context_ty ctx, const node *n)
e->v.Tuple.ctx = ctx;
s = e->v.Tuple.elts;
break;
case Lambda_kind:
expr_name = "lambda";
break;
case Call_kind:
if (ctx == Store)
return ast_error(n, "can't assign to function call");
else if (ctx == Del)
return ast_error(n, "can't delete function call");
else
return ast_error(n, "unexpected operation on function call");
expr_name = "function call";
break;
case BoolOp_kind:
case BinOp_kind:
return ast_error(n, "can't assign to operator");
case UnaryOp_kind:
expr_name = "operator";
break;
case GeneratorExp_kind:
return ast_error(n, "assignment to generator expression "
"not possible");
expr_name = "generator expression";
break;
case ListComp_kind:
expr_name = "list comprehension";
break;
case Dict_kind:
case Num_kind:
case Str_kind:
return ast_error(n, "can't assign to literal");
default: {
char buf[300];
PyOS_snprintf(buf, sizeof(buf),
"unexpected expression in assignment %d (line %d)",
e->kind, e->lineno);
return ast_error(n, buf);
}
expr_name = "literal";
break;
case Compare_kind:
expr_name = "comparison";
break;
case Repr_kind:
expr_name = "repr";
break;
default:
PyErr_Format(PyExc_SystemError,
"unexpected expression in assignment %d (line %d)",
e->kind, e->lineno);
return 0;
}
/* Check for error string set by switch */
if (expr_name) {
char buf[300];
PyOS_snprintf(buf, sizeof(buf),
"can't %s %s",
ctx == Store ? "assign to" : "delete",
expr_name);
return ast_error(n, buf);
}
/* If the LHS is a list or tuple, we need to set the assignment
context for all the tuple elements.
*/
@ -699,12 +732,8 @@ ast_for_decorator(struct compiling *c, const node *n)
expr_ty name_expr;
REQ(n, decorator);
if ((NCH(n) < 3 && NCH(n) != 5 && NCH(n) != 6)
|| TYPE(CHILD(n, 0)) != AT || TYPE(RCHILD(n, -1)) != NEWLINE) {
ast_error(n, "Invalid decorator node");
return NULL;
}
REQ(CHILD(n, 0), AT);
REQ(RCHILD(n, -1), NEWLINE);
name_expr = ast_for_dotted_name(c, CHILD(n, 1));
if (!name_expr)
@ -1610,7 +1639,7 @@ ast_for_call(struct compiling *c, const node *n, expr_ty func)
}
}
if (ngens > 1 || (ngens && (nargs || nkeywords))) {
ast_error(n, "Generator expression must be parenthesised "
ast_error(n, "Generator expression must be parenthesized "
"if not sole argument");
return NULL;
}
@ -1779,18 +1808,28 @@ ast_for_expr_stmt(struct compiling *c, const node *n)
if (!expr1)
return NULL;
if (expr1->kind == GeneratorExp_kind) {
ast_error(ch, "augmented assignment to generator "
"expression not possible");
return NULL;
// TODO(jhylton): Figure out why set_context() can't be used here.
switch (expr1->kind) {
case GeneratorExp_kind:
ast_error(ch, "augmented assignment to generator "
"expression not possible");
return NULL;
case Name_kind: {
const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
ast_error(ch, "assignment to None");
return NULL;
}
break;
}
case Attribute_kind:
case Subscript_kind:
break;
default:
ast_error(ch, "illegal expression for augmented "
"assignment");
return NULL;
}
if (expr1->kind == Name_kind) {
char *var_name = PyString_AS_STRING(expr1->v.Name.id);
if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
ast_error(ch, "assignment to None");
return NULL;
}
}
ch = CHILD(n, 2);
if (TYPE(ch) == testlist)