This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: __debug__ is a keyword but not a keyword
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: BTaskaya, bup, cheryl.sabella, matrixise, serhiy.storchaka, steven.daprano, xtreak
Priority: normal Keywords:

Created on 2019-02-15 12:22 by bup, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg335605 - (view) Author: Dan Snider (bup) * Date: 2019-02-15 12:22
keyword.py is used by stuff like the idle colorizer to help determine if an identifier is considered a keyword but it doesn't identify __debug__ despite the fact that the parser treats it exactly the same as None, True, and False. I could not find a more recent issue to bring this back up than #34464 and there it was suggested a issue be made so here it is. 

As mentioned on that previous issue, currently keyword.py builds the list automatically by scanning "Python/graminit.c" but since there is no "__debug__" token to be found in that file it doesn't get added to kwlist.

There is a file that groups the keywords True, False, None, and __debug__: ast.c. But there's no reason for it to be that complicated when nothing would break by for example adding on line 54 of keyword.py the statement "kwlist += ['__debug__']?

Actually, I'm interested in knowing why __debug__ is a keyword in the first place. I'm terrible at searching apparently so there might be more but from what I can tell, the only thing the docs have to say about __debug__ really is the following tautology: "The value for the built-in variable [__debug__] is determined when the interpreter starts."
msg335606 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-02-15 12:40
I'm not sure that __debug__ is a proper keyword. Unlike None, if you monkey-patch builtins, you can modify it:

py> builtins.__dict__['__debug__'] = 'Surprise!'
py> __debug__
'Surprise!'

py> builtins.__dict__['None'] = 'Surprise!'
py> None
py>


And it is not listed here:

https://docs.python.org/3.7/reference/lexical_analysis.html#keywords

So __debug__ appears to me to be in a strange grey area, part regular name and part keyword, but not quite either.
msg335685 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-02-16 14:10
See also issue34464
msg358767 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2019-12-21 08:34
import builtins
builtins.__dict__['__debug__'] = 'Surprise!'
builtins.__dict__['None'] = 'Surprise!'

print(__debug__)
print(None)

 $ ./python ../t.py
True
None

I guess this is related to Python/ast_opt.c transformation

    case Name_kind:
        if (_PyUnicode_EqualToASCIIString(node_->v.Name.id, "__debug__")) {
            return make_const(node_, PyBool_FromLong(!optimize_), ctx_);
        }
        break;
msg362482 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2020-02-22 23:08
Serhiy wrote in msg336099 that __debug__ isn't a keyword and the error message was changed in 3.8.  Due to that, I'm going to close this as not a bug.
History
Date User Action Args
2022-04-11 14:59:11adminsetgithub: 80181
2020-02-22 23:08:37cheryl.sabellasetstatus: open -> closed

nosy: + cheryl.sabella
messages: + msg362482

resolution: not a bug
stage: resolved
2019-12-21 08:34:17BTaskayasetmessages: + msg358767
2019-12-01 19:20:32BTaskayasetnosy: + BTaskaya
2019-02-20 15:58:21xtreaksetnosy: + serhiy.storchaka
2019-02-16 14:10:18xtreaksetnosy: + xtreak
messages: + msg335685
2019-02-15 12:58:37matrixisesetnosy: + matrixise
2019-02-15 12:40:41steven.dapranosetnosy: + steven.daprano
messages: + msg335606
2019-02-15 12:22:08bupcreate