GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-103089: Colorize only the beginning of very long lines in IDLE (#1… · python/cpython@b54ae4c

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Lib/idlelib
    • colorizer.py
    • idle_test
      • test_colorizer.py
  • Misc/NEWS.d/next/IDLE
    • 2026-09-17-02-00-00.gh-issue-103089.longline.rst
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Lib/idlelib/colorizer.py‎

Copy file name to clipboardExpand all lines: Lib/idlelib/colorizer.py
+32-4Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
DEBUG = False
1010

11+
# Adding a tag to a line takes time proportional to the number of tags
12+
# already in the line, so only the beginning of a line is colorized;
13+
# the rest is usually not visible anyway (gh-103089).
14+
MAX_COLORIZED_LINE = 2000
15+
1116

1217
def any(name, alternates):
1318
"Return a named group pattern matching list of alternates."
@@ -350,16 +355,39 @@ def _add_tags_in_section(self, chars, head):
350355
`chars` is a string with the text to parse and to which
351356
highlighting is to be applied.
352357
353-
`head` is the index in the text widget where the text is found.
358+
`head` is the index in the text widget where the text is found.
354359
"""
355-
for m in self.prog.finditer(chars):
360+
# Positions are relative to the start of the current line, so that
361+
# Tk does not resolve them through the previous lines.
362+
line = int(head.split('.')[0])
363+
line_start = 0 # Offset of the current line in chars.
364+
tags = []
365+
pos = 0
366+
while True:
367+
m = self.prog.search(chars, pos)
368+
if m is None:
369+
break
356370
for name, matched_text in matched_named_groups(m):
357371
a, b = m.span(name)
358-
self._add_tag(a, b, head, name)
372+
tags.append((a - line_start, b - line_start, head, name))
359373
if matched_text in ("def", "class"):
360374
if m1 := self.idprog.match(chars, b):
361375
a, b = m1.span(1)
362-
self._add_tag(a, b, head, "DEFINITION")
376+
tags.append((a - line_start, b - line_start,
377+
head, "DEFINITION"))
378+
pos = m.end()
379+
if '\n' in m[0]:
380+
line += m[0].count('\n')
381+
line_start = m.start() + m[0].rindex('\n') + 1
382+
head = f"{line}.0"
383+
elif pos - line_start >= MAX_COLORIZED_LINE:
384+
# The rest of a long line is not colorized.
385+
pos = chars.find('\n', pos)
386+
if pos < 0:
387+
break
388+
# Adding a tag is faster if there are no tags after it.
389+
for args in reversed(tags):
390+
self._add_tag(*args)
363391

364392
def removecolors(self):
365393
"Remove all colorizing tags."
Collapse file

‎Lib/idlelib/idle_test/test_colorizer.py‎

Copy file name to clipboardExpand all lines: Lib/idlelib/idle_test/test_colorizer.py
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,18 @@ def test_long_multiline_string(self):
573573
e"""
574574
''')
575575
self._assert_highlighting(source, {'STRING': [('1.0', '5.4')]})
576+
source = '"""a\nb""" + str\n'
577+
self._assert_highlighting(source, {'STRING': [('1.0', '2.4')],
578+
'BUILTIN': [('2.7', '2.10')]})
579+
580+
def test_long_line(self):
581+
# gh-103089: only the first MAX_COLORIZED_LINE characters of a line
582+
# are colorized.
583+
n = colorizer.MAX_COLORIZED_LINE
584+
source = f"pass\n{'x' * (n - 3)}'a', 'b'\n'c'\n"
585+
self._assert_highlighting(source, {'KEYWORD': [('1.0', '1.4')],
586+
'STRING': [(f'2.{n-3}', f'2.{n}'),
587+
('3.0', '3.3')]})
576588

577589
@run_in_tk_mainloop(delay=50)
578590
def test_incremental_editing(self):
Collapse file

‎Misc/NEWS.d/next/IDLE/2026-09-17-02-00-00.gh-issue-103089.longline.rst‎

Copy file name to clipboard
+2Lines changed: 2 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
IDLE no longer hangs when opening a file with very long lines.
2+
Only the first 2,000 characters of a line are colorized.

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.