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: Missing token.COMMENT
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: fpom, iritkatriel, r.david.murray, serhiy.storchaka
Priority: normal Keywords:

Created on 2017-10-08 08:08 by fpom, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (9)
msg303901 - (view) Author: Franck Pommereau (fpom) Date: 2017-10-08 08:08
Module token does not have constant COMMENT. But tokenize produces it and it appears in tok_name.
msg303903 - (view) Author: Franck Pommereau (fpom) Date: 2017-10-08 09:16
Actually, comparing with the content of tok_name, all the following constants are missing:

COMMENT = 54
NL = 55
BACKQUOTE = 56
ATEQUAL = 57
msg303909 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-10-08 14:54
This is fixed in python3.  Do you have a use case for 2.7 or are you just noticing?
msg303911 - (view) Author: Franck Pommereau (fpom) Date: 2017-10-08 15:46
I have the problem still exists in 3.4.3 at least:

Python 3.4.3 (default, Nov 17 2016, 01:08:31) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import token
>>> token.COMMENT
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'COMMENT'

About the use case: I often use tokenize to build recursive descendant parsers for domain-specific languages. Not having all the token constants can be worked around with code below, but it would be so better to have a complete module...  :)

import token
_tok = next(tokenize.tokenize(io.BytesIO(b"").readline))
token.tok_name[_tok.type] = "BACKQUOTE"
for number, name in token.tok_name.items() :
    if not hasattr(token, name) :
        setattr(token, name, number)
msg303912 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-08 16:18
COMMENT and NL are added by the tokenize module. BACKQUOTE already is set, but to 25 instead of 56. ATEQUAL shouldn't be occurred in Python earlier than 3.5. How did you get your results?
msg303913 - (view) Author: Franck Pommereau (fpom) Date: 2017-10-08 16:38
I've just launched ipython3 (installed with pip):

$ ipython3
Python 3.4.3 (default, Nov 17 2016, 01:08:31) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import token, tokenize, io

In [2]: token.BACKQUOTE
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-ebc28b79826c> in <module>()
----> 1 token.BACKQUOTE

AttributeError: 'module' object has no attribute 'BACKQUOTE'

In [3]: _tok = next(tokenize.tokenize(io.BytesIO(b"").readline))

In [4]: token.tok_name[_tok.type] = "BACKQUOTE"

In [5]: for number, name in token.tok_name.items() :
   ...:     if not hasattr(token, name) :
   ...:         print(name, "=", number)
   ...:         
COMMENT = 54
NL = 55
BACKQUOTE = 56
ATEQUAL = 57
COMMENT = 58

If instead I use python3 as provided by my Linux Mint, the result is different indeed:

$ python3
Python 3.4.3 (default, Nov 17 2016, 01:08:31) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import token, tokenize, io
>>> token.BACKQUOTE
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'BACKQUOTE'
>>> _tok = next(tokenize.tokenize(io.BytesIO(b"").readline))
>>> token.tok_name[_tok.type] = "BACKQUOTE"
>>> for number, name in token.tok_name.items() :
...     if not hasattr(token, name) :
...         print(name, "=", number)
... 
COMMENT = 54
NL = 55
BACKQUOTE = 56
msg303915 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-10-08 19:48
3.4 and 3.5 are in security maintenance mode only, and this is not a security bug.  This was fixed in master (3.7) as a result of issue 25324.  The decision there was made to not backport it because no one had complained about the problem previously.  Now we have a complaint, so perhaps that decision should be revisited?

There's also Serhiy's issue 30455.  I'm not sure if he's thinking about backporting that one or not.

And then there is the 2.7 question.  I'm not sure this is strong enough motivation to fix it there, though I wouldn't object (consistency is good).
msg303929 - (view) Author: Franck Pommereau (fpom) Date: 2017-10-09 07:49
Thanks for the explanation! Fixing is up to you, Python developers. But since there's a simple workaround, maybe it doesn't worth the effort.
msg391961 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-04-26 18:21
Fixed in 3.7 and too late for earlier versions.
History
Date User Action Args
2022-04-11 14:58:53adminsetgithub: 75907
2021-04-26 18:21:45iritkatrielsetstatus: open -> closed

nosy: + iritkatriel
messages: + msg391961

resolution: out of date
stage: resolved
2017-10-09 07:49:48fpomsetmessages: + msg303929
2017-10-08 19:48:17r.david.murraysetmessages: + msg303915
2017-10-08 16:38:09fpomsetmessages: + msg303913
2017-10-08 16:18:56serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg303912
2017-10-08 15:46:01fpomsetmessages: + msg303911
2017-10-08 14:54:53r.david.murraysetnosy: + r.david.murray

messages: + msg303909
versions: - Python 3.4
2017-10-08 09:16:22fpomsetmessages: + msg303903
2017-10-08 08:08:56fpomcreate