Skip to content

Commit 85f1ac3

Browse files
author
benjamin.peterson
committed
check for assignment to __debug__ during AST generation
Also, give assignment to None a better error message git-svn-id: http://svn.python.org/projects/python/trunk@67171 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent b512a77 commit 85f1ac3

3 files changed

Lines changed: 11 additions & 17 deletions

File tree

Lib/test/test_syntax.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,13 @@
2727
2828
Errors from set_context():
2929
30-
TODO(jhylton): "assignment to None" is inconsistent with other messages
31-
3230
>>> obj.None = 1
3331
Traceback (most recent call last):
34-
SyntaxError: assignment to None (<doctest test.test_syntax[1]>, line 1)
32+
SyntaxError: cannot assign to None (<doctest test.test_syntax[1]>, line 1)
3533
3634
>>> None = 1
3735
Traceback (most recent call last):
38-
SyntaxError: assignment to None (<doctest test.test_syntax[2]>, line 1)
36+
SyntaxError: cannot assign to None (<doctest test.test_syntax[2]>, line 1)
3937
4038
It's a syntax error to assign to the empty tuple. Why isn't it an
4139
error to assign to the empty list? It will always raise some error at
@@ -95,7 +93,7 @@
9593
>>> def f(None=1):
9694
... pass
9795
Traceback (most recent call last):
98-
SyntaxError: assignment to None (<doctest test.test_syntax[14]>, line 1)
96+
SyntaxError: cannot assign to None (<doctest test.test_syntax[14]>, line 1)
9997
10098
10199
From ast_for_arguments():
@@ -108,25 +106,25 @@
108106
>>> def f(x, None):
109107
... pass
110108
Traceback (most recent call last):
111-
SyntaxError: assignment to None (<doctest test.test_syntax[16]>, line 1)
109+
SyntaxError: cannot assign to None (<doctest test.test_syntax[16]>, line 1)
112110
113111
>>> def f(*None):
114112
... pass
115113
Traceback (most recent call last):
116-
SyntaxError: assignment to None (<doctest test.test_syntax[17]>, line 1)
114+
SyntaxError: cannot assign to None (<doctest test.test_syntax[17]>, line 1)
117115
118116
>>> def f(**None):
119117
... pass
120118
Traceback (most recent call last):
121-
SyntaxError: assignment to None (<doctest test.test_syntax[18]>, line 1)
119+
SyntaxError: cannot assign to None (<doctest test.test_syntax[18]>, line 1)
122120
123121
124122
From ast_for_funcdef():
125123
126124
>>> def None(x):
127125
... pass
128126
Traceback (most recent call last):
129-
SyntaxError: assignment to None (<doctest test.test_syntax[19]>, line 1)
127+
SyntaxError: cannot assign to None (<doctest test.test_syntax[19]>, line 1)
130128
131129
132130
From ast_for_call():
@@ -231,7 +229,7 @@
231229
SyntaxError: augmented assignment to generator expression not possible (<doctest test.test_syntax[31]>, line 1)
232230
>>> None += 1
233231
Traceback (most recent call last):
234-
SyntaxError: assignment to None (<doctest test.test_syntax[32]>, line 1)
232+
SyntaxError: cannot assign to None (<doctest test.test_syntax[32]>, line 1)
235233
>>> f() += 1
236234
Traceback (most recent call last):
237235
SyntaxError: illegal expression for augmented assignment (<doctest test.test_syntax[33]>, line 1)

Python/ast.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ static int
130130
forbidden_check(struct compiling *c, const node *n, const char *x)
131131
{
132132
if (!strcmp(x, "None"))
133-
return ast_error(n, "assignment to None");
133+
return ast_error(n, "cannot assign to None");
134+
if (!strcmp(x, "__debug__"))
135+
return ast_error(n, "cannot assign to __debug__");
134136
if (Py_Py3kWarningFlag) {
135137
if (!(strcmp(x, "True") && strcmp(x, "False")) &&
136138
!ast_warn(c, n, "assignment to True or False is forbidden in 3.x"))

Python/compile.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2344,12 +2344,6 @@ compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
23442344
PyObject *mangled;
23452345
/* XXX AugStore isn't used anywhere! */
23462346

2347-
/* First check for assignment to __debug__. Param? */
2348-
if ((ctx == Store || ctx == AugStore || ctx == Del)
2349-
&& !strcmp(PyString_AS_STRING(name), "__debug__")) {
2350-
return compiler_error(c, "can not assign to __debug__");
2351-
}
2352-
23532347
mangled = _Py_Mangle(c->u->u_private, name);
23542348
if (!mangled)
23552349
return 0;

0 commit comments

Comments
 (0)