homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: IDLE mis-coloring "print"
Type: behavior Stage: needs patch
Components: IDLE Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: python-dev, rhettinger, terry.reedy
Priority: normal Keywords: patch

Created on 2014年03月22日 23:28 by rhettinger, last changed 2022年04月11日 14:58 by admin. This issue is now closed.

Messages (4)
msg214522 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014年03月22日 23:28
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
msg215120 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014年03月29日 07:31
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.
msg215162 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014年03月30日 04:01
New changeset 6f87f50ecab7 by Raymond Hettinger in branch '2.7':
Issue #21029: IDLE now colors print consistently as a keyword.
http://hg.python.org/cpython/rev/6f87f50ecab7 
msg215163 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014年03月30日 04:02
Thanks Terry. Your solution works perfectly.
History
Date User Action Args
2022年04月11日 14:58:00adminsetgithub: 65228
2014年03月30日 04:02:49rhettingersetstatus: open -> closed
resolution: fixed
messages: + msg215163
2014年03月30日 04:01:57python-devsetnosy: + python-dev
messages: + msg215162
2014年03月29日 07:31:37terry.reedysetnosy: + terry.reedy
messages: + msg215120

assignee: terry.reedy
keywords: + patch
stage: needs patch
2014年03月22日 23:28:49rhettingercreate

AltStyle によって変換されたページ (->オリジナル) /