Skip to content
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

Closed
rhettinger opened this issue Mar 22, 2014 · 4 comments
Closed

IDLE mis-coloring "print" #65228

rhettinger opened this issue Mar 22, 2014 · 4 comments
Assignees
Labels
topic-IDLE type-bug An unexpected behavior, bug, or error

Comments

@rhettinger
Copy link
Contributor

BPO 21029
Nosy @rhettinger, @terryjreedy

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:

assignee = 'https://github.com/terryjreedy'
closed_at = <Date 2014-03-30.04:02:49.318>
created_at = <Date 2014-03-22.23:28:49.152>
labels = ['expert-IDLE', 'type-bug']
title = 'IDLE mis-coloring "print"'
updated_at = <Date 2014-03-30.04:02:49.317>
user = 'https://github.com/rhettinger'

bugs.python.org fields:

activity = <Date 2014-03-30.04:02:49.317>
actor = 'rhettinger'
assignee = 'terry.reedy'
closed = True
closed_date = <Date 2014-03-30.04:02:49.318>
closer = 'rhettinger'
components = ['IDLE']
creation = <Date 2014-03-22.23:28:49.152>
creator = 'rhettinger'
dependencies = []
files = []
hgrepos = []
issue_num = 21029
keywords = ['patch']
message_count = 4.0
messages = ['214522', '215120', '215162', '215163']
nosy_count = 3.0
nosy_names = ['rhettinger', 'terry.reedy', 'python-dev']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'needs patch'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue21029'
versions = ['Python 2.7']

@rhettinger
Copy link
Contributor Author

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

@rhettinger rhettinger added topic-IDLE type-bug An unexpected behavior, bug, or error labels Mar 22, 2014
@terryjreedy
Copy link
Member

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.

@terryjreedy terryjreedy self-assigned this Mar 29, 2014
@python-dev
Copy link
Mannequin

python-dev mannequin commented Mar 30, 2014

New changeset 6f87f50ecab7 by Raymond Hettinger in branch '2.7':
Issue bpo-21029: IDLE now colors print consistently as a keyword.
http://hg.python.org/cpython/rev/6f87f50ecab7

@rhettinger
Copy link
Contributor Author

Thanks Terry. Your solution works perfectly.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-IDLE type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants