Skip to content

Commit 551da59

Browse files
authored
bpo-45000: Raise SyntaxError when try to delete __debug__ (GH-27947)
Automerge-Triggered-By: GH:pablogsal
1 parent 24da544 commit 551da59

File tree

4 files changed

+12
-0
lines changed

4 files changed

+12
-0
lines changed

Doc/whatsnew/3.11.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ Other CPython Implementation Changes
176176
support :class:`typing.SupportsComplex` and :class:`typing.SupportsBytes` protocols.
177177
(Contributed by Mark Dickinson and Dong-hee Na in :issue:`24234`.)
178178

179+
* A :exc:`SyntaxError` (instead of a :exc:`NameError`) will be raised when deleting the :const:`__debug__` constant. (Contributed by Dong-hee Na in :issue:`45000`.)
180+
179181

180182
New Modules
181183
===========

Lib/test/test_syntax.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@
5959
Traceback (most recent call last):
6060
SyntaxError: cannot assign to __debug__
6161
62+
>>> del __debug__
63+
Traceback (most recent call last):
64+
SyntaxError: cannot delete __debug__
65+
6266
>>> f() = 1
6367
Traceback (most recent call last):
6468
SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
A :exc:`SyntaxError` is now raised when trying to delete :const:`__debug__`.
2+
Patch by Dong-hee Na.

Python/compile.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2343,6 +2343,10 @@ forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
23432343
compiler_error(c, "cannot assign to __debug__");
23442344
return 1;
23452345
}
2346+
if (ctx == Del && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2347+
compiler_error(c, "cannot delete __debug__");
2348+
return 1;
2349+
}
23462350
return 0;
23472351
}
23482352

0 commit comments

Comments
 (0)