]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-36052: Raise a SyntaxError when assigning a value to __debug__ with := (GH-11958)
authorStéphane Wirtel <stephane@wirtel.be>
Thu, 21 Feb 2019 10:11:53 +0000 (11:11 +0100)
committerPablo Galindo <Pablogsal@gmail.com>
Thu, 21 Feb 2019 10:11:53 +0000 (10:11 +0000)
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.

Lib/test/test_syntax.py
Misc/NEWS.d/next/Core and Builtins/2019-02-20-17-57-31.bpo-36052.l8lJSi.rst [new file with mode: 0644]
Python/ast.c

index ce1de4b319f20f958b01ebbdacffa92f1c3b2a64..a0f487d4ed76b489ad2c7beb1bda8d2f2b64dbe6 100644 (file)
@@ -51,6 +51,10 @@ SyntaxError: cannot assign to __debug__
 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 (file)
index 0000000..1d20948
--- /dev/null
@@ -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.
index e721ac1c0a4a15de9216e59c9118f7cc2d52a1ad..62ff868de5e483f136c62e0c0b9557cb8f1a2f5d 100644 (file)
@@ -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() */
             }