[Python-checkins] bpo-36052: Raise a SyntaxError when assigning a value to __debug__ with := (GH-11958)

Pablo Galindo webhook-mailer at python.org
Thu Feb 21 05:11:58 EST 2019


https://github.com/python/cpython/commit/3ad91673057d410bf9f8c53df6bb8aa18c4b68ca
commit: 3ad91673057d410bf9f8c53df6bb8aa18c4b68ca
branch: master
author: Stéphane Wirtel <stephane at wirtel.be>
committer: Pablo Galindo <Pablogsal at gmail.com>
date: 2019-02-21T10:11:53Z
summary:

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.

files:
A Misc/NEWS.d/next/Core and Builtins/2019-02-20-17-57-31.bpo-36052.l8lJSi.rst
M Lib/test/test_syntax.py
M Python/ast.c

diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py
index ce1de4b319f2..a0f487d4ed76 100644
--- a/Lib/test/test_syntax.py
+++ b/Lib/test/test_syntax.py
@@ -51,6 +51,10 @@
 Traceback (most recent call last):
 SyntaxError: cannot assign to __debug__
 
+>>> (__debug__ := 1)
+Traceback (most recent call last):
+SyntaxError: cannot assign to __debug__
+
 >>> f() = 1
 Traceback (most recent call last):
 SyntaxError: cannot assign to function call
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-02-20-17-57-31.bpo-36052.l8lJSi.rst b/Misc/NEWS.d/next/Core and Builtins/2019-02-20-17-57-31.bpo-36052.l8lJSi.rst
new file mode 100644
index 000000000000..1d2094895d57
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2019-02-20-17-57-31.bpo-36052.l8lJSi.rst	
@@ -0,0 +1,2 @@
+Raise a :exc:`SyntaxError` when assigning a value to `__debug__` with the
+Assignment Operator. Contributed by Stéphane Wirtel and Pablo Galindo.
diff --git a/Python/ast.c b/Python/ast.c
index e721ac1c0a4a..62ff868de5e4 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -1084,7 +1084,7 @@ set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
                 return 0;
             break;
         case Name_kind:
-            if (ctx == Store) {
+            if (ctx == Store || ctx == NamedStore) {
                 if (forbidden_name(c, e->v.Name.id, n, 0))
                     return 0; /* forbidden_name() calls ast_error() */
             }



More information about the Python-checkins mailing list