-
-
Notifications
You must be signed in to change notification settings - Fork 30k
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
IDLE mis-coloring "print" #65228
Comments
Sometimes, IDLE displays "print" in orange, identifying it as a keyword and sometimes "print" is displayed in "purple" identifying it as a built-in function. >>> print 'hello' # This print is orange
hello
>>> for i in range(1):
print ('hello') # This print is purple hello |
start with no indent: orange The underlying cause is that 'print' is both in keyword.kwlist and in module __builtin__ (for use when the print_function future import suppresses recognition of print as keyword). Hence it is in both the partial regexes that get concatenated together at the top of ColorDelegator.py and used with .match on lines 201 and 232. The proximal cause is that .match in recolorize_main, 201, 232 gives different answers without or with a leading space. import keyword
import __builtin__
import re
def any(name, alternates):
"Return a named group pattern matching list of alternates."
return "(?P<%s>" % name + "|".join(alternates) + ")"
kw = r"\b" + any("KEYWORD", keyword.kwlist) + r"\b"
builtinlist = [str(name) for name in dir(__builtin__)]
builtin = r"([^.'\"\\#]\b|^)" + any("BUILTIN", builtinlist) + r"\b"
prog = re.compile(kw + "|" + builtin , re.S)
print(prog.match('print').groupdict().items())
print(prog.match(' print').groupdict().items())
>>>
[('BUILTIN', None), ('KEYWORD', 'print')]
[('BUILTIN', 'print'), ('KEYWORD', None)] The prefix [^.'\\"\\\\#] added to builtin but not kw matches a space. Removing 'print' from builtinlist prevents it from matching as a builtin. I think this is the right thing to do since Idle cannot know how 'print' will be interpreted, so should use the keyword default. builtinlist.remove('print') I am not going to write a test for this. |
New changeset 6f87f50ecab7 by Raymond Hettinger in branch '2.7': |
Thanks Terry. Your solution works perfectly. |
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: