-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-89855: Improve support of non-ASCII identifiers in IDLE #29381
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
Draft
serhiy-storchaka
wants to merge
2
commits into
python:main
Choose a base branch
from
serhiy-storchaka:idle-nonascii-identifiers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+27
−45
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,13 +14,6 @@ | |
# all ASCII chars that may be the first char of an identifier | ||
_ASCII_ID_FIRST_CHARS = frozenset(string.ascii_letters + "_") | ||
|
||
# lookup table for whether 7-bit ASCII chars are valid in a Python identifier | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
_IS_ASCII_ID_CHAR = [(chr(x) in _ASCII_ID_CHARS) for x in range(128)] | ||
# lookup table for whether 7-bit ASCII chars are valid as the first | ||
# char in a Python identifier | ||
_IS_ASCII_ID_FIRST_CHAR = \ | ||
[(chr(x) in _ASCII_ID_FIRST_CHARS) for x in range(128)] | ||
|
||
|
||
class HyperParser: | ||
def __init__(self, editwin, index): | ||
|
@@ -166,53 +159,47 @@ def _eat_identifier(cls, str, limit, pos): | |
|
||
This ignores non-identifier eywords are not identifiers. | ||
""" | ||
is_ascii_id_char = _IS_ASCII_ID_CHAR | ||
|
||
# Start at the end (pos) and work backwards. | ||
i = pos | ||
|
||
# Go backwards as long as the characters are valid ASCII | ||
# identifier characters. This is an optimization, since it | ||
# is faster in the common case where most of the characters | ||
# are ASCII. | ||
while i > limit and ( | ||
ord(str[i - 1]) < 128 and | ||
is_ascii_id_char[ord(str[i - 1])] | ||
): | ||
while i > limit and str[i - 1] in _ASCII_ID_CHARS: | ||
i -= 1 | ||
|
||
# If the above loop ended due to reaching a non-ASCII | ||
# character, continue going backwards using the most generic | ||
# test for whether a string contains only valid identifier | ||
# characters. | ||
if i > limit and ord(str[i - 1]) >= 128: | ||
while i - 4 >= limit and ('a' + str[i - 4:pos]).isidentifier(): | ||
if i > limit and str[i - 1] > '\x7f': | ||
while i - 4 >= limit and ('a' + str[i - 4:i]).isidentifier(): | ||
i -= 4 | ||
if i - 2 >= limit and ('a' + str[i - 2:pos]).isidentifier(): | ||
if i - 2 >= limit and ('a' + str[i - 2:i]).isidentifier(): | ||
i -= 2 | ||
if i - 1 >= limit and ('a' + str[i - 1:pos]).isidentifier(): | ||
if i - 1 >= limit and ('a' + str[i - 1]).isidentifier(): | ||
i -= 1 | ||
|
||
# The identifier candidate starts here. If it isn't a valid | ||
# identifier, don't eat anything. At this point that is only | ||
# possible if the first character isn't a valid first | ||
# character for an identifier. | ||
if not str[i:pos].isidentifier(): | ||
if i < pos and not str[i].isidentifier(): | ||
return 0 | ||
elif i < pos: | ||
# All characters in str[i:pos] are valid ASCII identifier | ||
# characters, so it is enough to check that the first is | ||
# valid as the first character of an identifier. | ||
if not _IS_ASCII_ID_FIRST_CHAR[ord(str[i])]: | ||
if str[i] not in _ASCII_ID_FIRST_CHARS: | ||
return 0 | ||
|
||
# All keywords are valid identifiers, but should not be | ||
# considered identifiers here, except for True, False and None. | ||
if i < pos and ( | ||
iskeyword(str[i:pos]) and | ||
str[i:pos] not in cls._ID_KEYWORDS | ||
): | ||
return 0 | ||
if i < pos: | ||
word = str[i:pos] | ||
if iskeyword(word) and word not in cls._ID_KEYWORDS: | ||
return 0 | ||
|
||
return pos - i | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/IDLE/2021-11-03-10-37-29.bpo-45692.QSuHbM.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Improve support of non-ASCII identifiers in IDLE | ||
(autoexpanding, autocompletion, undo, etc).y | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to test with long lines.