-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make tokenize recognize Other_ID_Start and Other_ID_Continue chars #68382
Comments
|
This is valid: ℘· = 1
print(℘·)
#>>> 1But this gives an error token: from io import BytesIO
from tokenize import tokenize
stream = BytesIO("℘·".encode("utf-8"))
print(*tokenize(stream.read), sep="\n")
#>>> TokenInfo(type=56 (ENCODING), string='utf-8', start=(0, 0), end=(0, 0), line='')
#>>> TokenInfo(type=53 (ERRORTOKEN), string='℘', start=(1, 0), end=(1, 1), line='℘·')
#>>> TokenInfo(type=53 (ERRORTOKEN), string='·', start=(1, 1), end=(1, 2), line='℘·')
#>>> TokenInfo(type=0 (ENDMARKER), string='', start=(2, 0), end=(2, 0), line='')This is a continuation of http://bugs.python.org/issue9712. I'm not able to reopen the issue, so I thought I should report it anew. It is tokenize that is wrong - Other_ID_Start and Other_ID_Continue are documented to be valid: https://docs.python.org/3.5/reference/lexical_analysis.html#identifiers |
|
Attached is a first cut patch for this. (CC'd haypo as a unicode expert). |
|
I closed bpo-1693050 as a duplicate of bpo-12731 (the /w issue). I left bpo-9712 closed and closed bpo-32987 and marked both as duplicates of this. In msg313814 of the latter, Serhiy indicates which start and continue identifier characters are currently matched by \W for re and regex. He gives there a fix for this that he says requires the /w issue to be fixed. It is similar to the posted patch. He says that without \w fixed, another 2000+ chars need to be added. Perhaps the v0 patch needs more tests (I don't know.) He also says that re support for properties, bpo-12734, would make things even better. Three of the characters in the patch are too obscure for Firefox on Window2 and print as boxes. Some others I do not recognize. And I could not type any of them. I thought we had a policy of using \u or \U escapes even in tests to avoid such problems. (I notice that there are already non-ascii chars in the context.) |
|
Reproduced on 3.11. |
|
Udated doc link, which appears to be same: Updated property list linked in above: Relevant content for this issue: 1885..1886 ; Other_ID_Start # Mn [2] MONGOLIAN LETTER ALI GALI BALUDA..MONGOLIAN LETTER ALI GALI THREE BALUDA 2118 ; Other_ID_Start # Sm SCRIPT CAPITAL P 00B7 ; Other_ID_Continue # Po MIDDLE DOT Codepoints of '℘·' opening example: Except for the two Mongolian start characters, Meador's patch hardcodes the 'Other' characters, thereby adding them without waiting for re to be fixed. While this will miss new additions without manual updates, it is better than missing everything for however many years. I will make a PR with the additions and looks at the new tests. |
|
I recently ran into this while working with In [1]: ℘℘ = 1
...: print(℘℘ .℘℘ )crashes with a huge traceback which ends with: Here's the workaround in that PR: def patched_generate_tokens(original_tokens):
# type: (Iterator[tokenize.TokenInfo]) -> Iterator[tokenize.TokenInfo]
"""
Fixes tokens yielded by `tokenize.generate_tokens` to handle more non-ASCII characters in identifiers.
Workaround for https://github.com/python/cpython/issues/68382.
Should only be used when tokenizing a string that is known to be valid syntax,
because it assumes that error tokens are not actually errors.
Combines groups of consecutive NAME, NUMBER, and/or ERRORTOKEN tokens into a single NAME token.
"""
group = [] # type: List[tokenize.TokenInfo]
for tok in original_tokens:
if (
tok.type in (tokenize.NAME, tokenize.ERRORTOKEN, tokenize.NUMBER)
# Only combine tokens if they have no whitespace in between
and (not group or group[-1].end == tok.start)
):
group.append(tok)
else:
for combined_token in combine_tokens(group):
yield combined_token
group = []
yield tok
for combined_token in combine_tokens(group):
yield combined_token
def combine_tokens(group):
# type: (List[tokenize.TokenInfo]) -> List[tokenize.TokenInfo]
if not any(tok.type == tokenize.ERRORTOKEN for tok in group) or len({tok.line for tok in group}) != 1:
return group
return [
tokenize.TokenInfo(
type=tokenize.NAME,
string="".join(t.string for t in group),
start=group[0].start,
end=group[-1].end,
line=group[0].line,
)
]This seems to work but it's obviously a shaky hack. A proper fix would be great. |
|
This has been fixed in 3.12. |


Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: