Issue 21029: IDLE mis-coloring "print" (original) (raw)
start with no indent: orange add any indent: turns purple delete indent: back to orange same in edit window change does not happen with other keywords (or 3.x)
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.