[Python-checkins] bpo-45000: Raise SyntaxError when try to delete __debug__ (GH-27947) (GH-27957)

pablogsal webhook-mailer at python.org
Thu Aug 26 05:52:31 EDT 2021


https://github.com/python/cpython/commit/32c1caa87f68a650f2d009a589a1db30484499cb
commit: 32c1caa87f68a650f2d009a589a1db30484499cb
branch: 3.10
author: Dong-hee Na <donghee.na at python.org>
committer: pablogsal <Pablogsal at gmail.com>
date: 2021-08-26T10:52:21+01:00
summary:

bpo-45000: Raise SyntaxError when try to delete __debug__ (GH-27947) (GH-27957)

(cherry picked from commit 551da597a0996b0fb3af425f48aa5bc63ea6b963)

files:
A Misc/NEWS.d/next/Core and Builtins/2021-08-25-23-17-32.bpo-45000.XjmyLl.rst
M Lib/test/test_syntax.py
M Python/compile.c

diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py
index 88503dcaad99b..43780ce273ef4 100644
--- a/Lib/test/test_syntax.py
+++ b/Lib/test/test_syntax.py
@@ -59,6 +59,10 @@
 Traceback (most recent call last):
 SyntaxError: cannot assign to __debug__
 
+>>> del __debug__
+Traceback (most recent call last):
+SyntaxError: cannot delete __debug__
+
 >>> f() = 1
 Traceback (most recent call last):
 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-08-25-23-17-32.bpo-45000.XjmyLl.rst b/Misc/NEWS.d/next/Core and Builtins/2021-08-25-23-17-32.bpo-45000.XjmyLl.rst
new file mode 100644
index 0000000000000..96c95cc6e0296
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-08-25-23-17-32.bpo-45000.XjmyLl.rst	
@@ -0,0 +1,2 @@
+A :exc:`SyntaxError` is now raised when trying to delete :const:`__debug__`.
+Patch by Dong-hee Na.
diff --git a/Python/compile.c b/Python/compile.c
index a2378992fd408..d55b0beaec77c 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -2264,6 +2264,10 @@ forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
         compiler_error(c, "cannot assign to __debug__");
         return 1;
     }
+    if (ctx == Del && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
+        compiler_error(c, "cannot delete __debug__");
+        return 1;
+    }
     return 0;
 }
 



More information about the Python-checkins mailing list