From 2c98faada6f268d32c4031f7d09d9d5e6a2f46e3 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Sat, 8 Nov 2008 18:38:54 +0000 Subject: check for assignment to __debug__ during AST generation Also, give assignment to None a better error message --- Lib/test/test_syntax.py | 18 ++++++++---------- Python/ast.c | 4 +++- Python/compile.c | 6 ------ 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py index e978a6e..794564a 100644 --- a/Lib/test/test_syntax.py +++ b/Lib/test/test_syntax.py @@ -27,15 +27,13 @@ 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 (, line 1) +SyntaxError: cannot assign to None (, line 1) >>> None = 1 Traceback (most recent call last): -SyntaxError: assignment to None (, line 1) +SyntaxError: cannot assign to None (, 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 @@ -95,7 +93,7 @@ From compiler_complex_args(): >>> def f(None=1): ... pass Traceback (most recent call last): -SyntaxError: assignment to None (, line 1) +SyntaxError: cannot assign to None (, line 1) From ast_for_arguments(): @@ -108,17 +106,17 @@ SyntaxError: non-default argument follows default argument (>> def f(x, None): ... pass Traceback (most recent call last): -SyntaxError: assignment to None (, line 1) +SyntaxError: cannot assign to None (, line 1) >>> def f(*None): ... pass Traceback (most recent call last): -SyntaxError: assignment to None (, line 1) +SyntaxError: cannot assign to None (, line 1) >>> def f(**None): ... pass Traceback (most recent call last): -SyntaxError: assignment to None (, line 1) +SyntaxError: cannot assign to None (, line 1) From ast_for_funcdef(): @@ -126,7 +124,7 @@ From ast_for_funcdef(): >>> def None(x): ... pass Traceback (most recent call last): -SyntaxError: assignment to None (, line 1) +SyntaxError: cannot assign to None (, line 1) From ast_for_call(): @@ -231,7 +229,7 @@ Traceback (most recent call last): SyntaxError: augmented assignment to generator expression not possible (, line 1) >>> None += 1 Traceback (most recent call last): -SyntaxError: assignment to None (, line 1) +SyntaxError: cannot assign to None (, line 1) >>> f() += 1 Traceback (most recent call last): SyntaxError: illegal expression for augmented assignment (, line 1) diff --git a/Python/ast.c b/Python/ast.c index 29b0e82..6eb3aa4 100644 --- a/Python/ast.c +++ b/Python/ast.c @@ -130,7 +130,9 @@ static int forbidden_check(struct compiling *c, const node *n, const char *x) { if (!strcmp(x, "None")) - return ast_error(n, "assignment to None"); + return ast_error(n, "cannot assign to None"); + if (!strcmp(x, "__debug__")) + return ast_error(n, "cannot assign to __debug__"); if (Py_Py3kWarningFlag) { if (!(strcmp(x, "True") && strcmp(x, "False")) && !ast_warn(c, n, "assignment to True or False is forbidden in 3.x")) diff --git a/Python/compile.c b/Python/compile.c index 264fdcd..88d54ab 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2344,12 +2344,6 @@ compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) PyObject *mangled; /* XXX AugStore isn't used anywhere! */ - /* First check for assignment to __debug__. Param? */ - if ((ctx == Store || ctx == AugStore || ctx == Del) - && !strcmp(PyString_AS_STRING(name), "__debug__")) { - return compiler_error(c, "can not assign to __debug__"); - } - mangled = _Py_Mangle(c->u->u_private, name); if (!mangled) return 0; -- cgit v0.12