You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Make tokenize recognize Other_ID_Start and Other_ID_Continue chars #68382
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.)
changed the title [-]tokenize yield an ERRORTOKEN if an identifier uses Other_ID_Start or Other_ID_Continue[/-][+]tokenize fails on some Other_ID_Start or Other_ID_Continue[/+]on Mar 15, 2018
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
212E ; Other_ID_Start # So ESTIMATED SYMBOL
309B..309C ; Other_ID_Start # Sk [2] KATAKANA-HIRAGANA VOICED SOUND MARK..KATAKANA-HIRAGANA SEMI-VOICED SOUND MARK
# Total code points: 6
00B7 ; Other_ID_Continue # Po MIDDLE DOT
0387 ; Other_ID_Continue # Po GREEK ANO TELEIA
1369..1371 ; Other_ID_Continue # No [9] ETHIOPIC DIGIT ONE..ETHIOPIC DIGIT NINE
19DA ; Other_ID_Continue # No NEW TAI LUE THAM DIGIT ONE
# Total code points: 12
Codepoints of '℘·' opening example:
'0x2118' Other_Id_start Sm Script Capital P
'0xb7' Other_Id_continue P0 Middle dot
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.
changed the title [-]tokenize fails on some Other_ID_Start or Other_ID_Continue[/-][+]Make tokenize recognize Other_ID_Start and Other_ID_Continue chars[/+]on Jan 16, 2022
I recently ran into this while working with asttokens, hence the PR above. IPython's tracebacks rely on asttokens, and entering this code:
In [1]: ℘℘=1
...: print(℘℘ .℘℘ )
crashes with a huge traceback which ends with:
Expected token NAME:None, got ERRORTOKEN:'℘' on line 2 col 11
Here's the workaround in that PR:
defpatched_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]fortokinoriginal_tokens:
if (
tok.typein (tokenize.NAME, tokenize.ERRORTOKEN, tokenize.NUMBER)
# Only combine tokens if they have no whitespace in betweenand (notgrouporgroup[-1].end==tok.start)
):
group.append(tok)
else:
forcombined_tokenincombine_tokens(group):
yieldcombined_tokengroup= []
yieldtokforcombined_tokenincombine_tokens(group):
yieldcombined_tokendefcombine_tokens(group):
# type: (List[tokenize.TokenInfo]) -> List[tokenize.TokenInfo]ifnotany(tok.type==tokenize.ERRORTOKENfortokingroup) orlen({tok.linefortokingroup}) !=1:
returngroupreturn [
tokenize.TokenInfo(
type=tokenize.NAME,
string="".join(t.stringfortingroup),
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.
Activity
JoshuaLandau commentedon May 14, 2015
This is valid:
℘· = 1 print(℘·) #>>> 1But this gives an error token:
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
meadori commentedon May 11, 2016
Attached is a first cut patch for this. (CC'd haypo as a unicode expert).
terryjreedy commentedon Mar 15, 2018
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.)
[-]tokenize yield an ERRORTOKEN if an identifier uses Other_ID_Start or Other_ID_Continue[/-][+]tokenize fails on some Other_ID_Start or Other_ID_Continue[/+]iritkatriel commentedon Jan 16, 2022
Reproduced on 3.11.
terryjreedy commentedon Jan 16, 2022
Udated doc link, which appears to be same:
https://docs.python.org/3.11/reference/lexical_analysis.html#identifiers
Updated property list linked in above:
https://www.unicode.org/Public/14.0.0/ucd/PropList.txt
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
212E ; Other_ID_Start # So ESTIMATED SYMBOL
309B..309C ; Other_ID_Start # Sk [2] KATAKANA-HIRAGANA VOICED SOUND MARK..KATAKANA-HIRAGANA SEMI-VOICED SOUND MARK
# Total code points: 6
00B7 ; Other_ID_Continue # Po MIDDLE DOT
0387 ; Other_ID_Continue # Po GREEK ANO TELEIA
1369..1371 ; Other_ID_Continue # No [9] ETHIOPIC DIGIT ONE..ETHIOPIC DIGIT NINE
19DA ; Other_ID_Continue # No NEW TAI LUE THAM DIGIT ONE
# Total code points: 12
Codepoints of '℘·' opening example:
'0x2118' Other_Id_start Sm Script Capital P
'0xb7' Other_Id_continue P0 Middle dot
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.
[-]tokenize fails on some Other_ID_Start or Other_ID_Continue[/-][+]Make tokenize recognize Other_ID_Start and Other_ID_Continue chars[/+]alexmojaki commentedon Aug 1, 2022
I recently ran into this while working with
asttokens, hence the PR above. IPython's tracebacks rely onasttokens, and entering this code:crashes with a huge traceback which ends with:
Here's the workaround in that PR:
This seems to work but it's obviously a shaky hack. A proper fix would be great.
lysnikolaou commentedon Jun 19, 2023
This has been fixed in 3.12.