Repository navigation
PEP 3131 -- Supporting Non-ASCII Identifiers #607
Copy link
Copy link
Closed
Description
Python3 already support non-ASCII identifiers, pep-3131. But yapf fails to parse.
See example below, 中 is a non-ASCII token.
$ echo '中=1' | yapf
Traceback (most recent call last):
File "/usr/local/miniconda3/lib/python3.7/site-packages/yapf/yapflib/pytree_utils.py", line 115, in ParseCodeToTree
tree = parser_driver.parse_string(code, debug=False)
File "/usr/local/miniconda3/lib/python3.7/lib2to3/pgen2/driver.py", line 104, in parse_string
return self.parse_tokens(tokens, debug)
File "/usr/local/miniconda3/lib/python3.7/lib2to3/pgen2/driver.py", line 72, in parse_tokens
if p.addtoken(type, value, (prefix, start)):
File "/usr/local/miniconda3/lib/python3.7/lib2to3/pgen2/parse.py", line 116, in addtoken
ilabel = self.classify(type, value, context)
File "/usr/local/miniconda3/lib/python3.7/lib2to3/pgen2/parse.py", line 172, in classify
raise ParseError("bad token", type, value, context)
lib2to3.pgen2.parse.ParseError: bad token: type=58, value='中', context=('', (1, 0))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/miniconda3/lib/python3.7/site-packages/yapf/yapflib/yapf_api.py", line 129, in FormatCode
tree = pytree_utils.ParseCodeToTree(unformatted_source)
File "/usr/local/miniconda3/lib/python3.7/site-packages/yapf/yapflib/pytree_utils.py", line 121, in ParseCodeToTree
tree = parser_driver.parse_string(code, debug=False)
File "/usr/local/miniconda3/lib/python3.7/lib2to3/pgen2/driver.py", line 104, in parse_string
return self.parse_tokens(tokens, debug)
File "/usr/local/miniconda3/lib/python3.7/lib2to3/pgen2/driver.py", line 72, in parse_tokens
if p.addtoken(type, value, (prefix, start)):
File "/usr/local/miniconda3/lib/python3.7/lib2to3/pgen2/parse.py", line 116, in addtoken
ilabel = self.classify(type, value, context)
File "/usr/local/miniconda3/lib/python3.7/lib2to3/pgen2/parse.py", line 172, in classify
raise ParseError("bad token", type, value, context)
lib2to3.pgen2.parse.ParseError: bad token: type=58, value='中', context=('', (1, 0))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/miniconda3/bin/yapf", line 10, in <module>
sys.exit(run_main())
File "/usr/local/miniconda3/lib/python3.7/site-packages/yapf/__init__.py", line 313, in run_main
sys.exit(main(sys.argv))
File "/usr/local/miniconda3/lib/python3.7/site-packages/yapf/__init__.py", line 183, in main
verify=args.verify)
File "/usr/local/miniconda3/lib/python3.7/site-packages/yapf/yapflib/yapf_api.py", line 131, in FormatCode
raise parse.ParseError(filename + ': ' + e.message)
AttributeError: 'ParseError' object has no attribute 'message'```
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
kamahen commentedon Aug 25, 2018
Strange. Here's some code I wrote for testing lib2to3's encoding utility ... yapf handles it fine. So, maybe the problem is with a one-line program?
When run, this outputs:
and yapf reformats the code to:
holymonson commentedon Aug 25, 2018
No, this bug is found when parsing file, the one-line program is just a sample.
Below example is clearer.
kamahen commentedon Aug 25, 2018
Which version of Python are you using? Your sample program worked for me with Python 3.5, 3.6, 3.7 (I don't have anything older installed).
holymonson commentedon Aug 25, 2018
kamahen commentedon Aug 25, 2018
I have the August 6 version of Python, but that shouldn't matter because I got similar results with 3.5 and 3.6.
This is how I ran my test:
holymonson commentedon Aug 26, 2018
tokenizerecognizes'a'and'a中'asNAME, but'中'asERRORTOKEN.It seems should blame tokenize.py#L504 , only those
tokeninitialized withnamechars = string.ascii_letters + '_'can be recognized asNAME, while Python 3 Interpreter dosen't restrict that.However,
lib2to3.pgen2seems a parser aiming for converting Python 2 code to Python 3, not parsing Python 3 code (I'm not sure). And it may retire when the End-Of-Life date of Python 2 (2020-01-01) comes.@kamahen , I don't know why your python works, is there a parser other than
lib2to3.pgen2.tokenize?kamahen commentedon Aug 26, 2018
@holymonson - I can reproduce your results.
I agree -- it looks like a bug in lib2to3.pgen2.tokenize.
holymonson commentedon Aug 27, 2018
xfer python/cpython#8950