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

Skip to content

Commit 3ad9167

Browse files
matrixisepablogsal
authored andcommitted
bpo-36052: Raise a SyntaxError when assigning a value to __debug__ with := (GH-11958)
Trying to assign a value to __debug__ using the assignment operator is supposed to fail, but a missing check for forbidden names when setting the context in the ast was preventing this behaviour.
1 parent ea6207d commit 3ad9167

3 files changed

Lines changed: 7 additions & 1 deletion

File tree

Lib/test/test_syntax.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
Traceback (most recent call last):
5252
SyntaxError: cannot assign to __debug__
5353
54+
>>> (__debug__ := 1)
55+
Traceback (most recent call last):
56+
SyntaxError: cannot assign to __debug__
57+
5458
>>> f() = 1
5559
Traceback (most recent call last):
5660
SyntaxError: cannot assign to function call
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Raise a :exc:`SyntaxError` when assigning a value to `__debug__` with the
2+
Assignment Operator. Contributed by Stéphane Wirtel and Pablo Galindo.

Python/ast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
10841084
return 0;
10851085
break;
10861086
case Name_kind:
1087-
if (ctx == Store) {
1087+
if (ctx == Store || ctx == NamedStore) {
10881088
if (forbidden_name(c, e->v.Name.id, n, 0))
10891089
return 0; /* forbidden_name() calls ast_error() */
10901090
}

0 commit comments

Comments
 (0)