FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-130273: Fix incorrectly colored error locations when wide unicode characters exists by HarryLHW · Pull Request #130277 · python/cpython · GitHub

/ cpython Public
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
46 changes: 35 additions & 11 deletions Lib/traceback.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -656,18 +656,29 @@ def output_line(lineno):
line = result[-1]
colorized_line_parts = []
colorized_carets_parts = []

for color, group in itertools.groupby(itertools.zip_longest(line, carets, fillvalue=""), key=lambda x: x[1]):
caret_group = list(group)
if color == "^":
colorized_line_parts.append(ANSIColors.BOLD_RED + "".join(char for char, _ in caret_group) + ANSIColors.RESET)
colorized_carets_parts.append(ANSIColors.BOLD_RED + "".join(caret for _, caret in caret_group) + ANSIColors.RESET)
elif color == "~":
colorized_line_parts.append(ANSIColors.RED + "".join(char for char, _ in caret_group) + ANSIColors.RESET)
colorized_carets_parts.append(ANSIColors.RED + "".join(caret for _, caret in caret_group) + ANSIColors.RESET)
line_part_col = 0
for char, group in itertools.groupby(carets):
carets_part = "".join(group)
width = len(carets_part)
if char == "^":
color = ANSIColors.BOLD_RED
elif char == "~":
color = ANSIColors.RED
else:
colorized_line_parts.append("".join(char for char, _ in caret_group))
colorized_carets_parts.append("".join(caret for _, caret in caret_group))
colorized_carets_parts.append(carets_part)
line_part_col, line_part = _offset_at_width(line, width, start=line_part_col)
colorized_line_parts.append(line_part)
continue

colorized_carets_parts.append(color)
colorized_carets_parts.append(carets_part)
colorized_carets_parts.append(ANSIColors.RESET)

colorized_line_parts.append(color)
line_part_col, line_part = _offset_at_width(line, width, start=line_part_col)
colorized_line_parts.append(line_part)
colorized_line_parts.append(ANSIColors.RESET)
colorized_line_parts.append(line[line_part_col:])

colorized_line = "".join(colorized_line_parts)
colorized_carets = "".join(colorized_carets_parts)
Expand Down Expand Up @@ -965,6 +976,19 @@ def _display_width(line, offset=None):
for char in line[:offset]
)

def _offset_at_width(line, width, start=0):
col = start + width
part = line[start:col]
if part.isascii():
return col, part

from unicodedata import east_asian_width

for i in range(start, len(line)):
if width <= 0:
return i, line[start:i]
width -= 2 if east_asian_width(line[i]) in _WIDE_CHAR_SPECIFIERS else 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

does this correctly handle zero-width characters? e.g. the '\u0301' from 'cafe\u0301'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

No.
However, _display_width() does not correctly handle zero-width characters for carets positions either.

>>> café = 1/0
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    café = 1/0
            ~^~
ZeroDivisionError: division by zero
>>> 

They can be fixed in the same way. I wonder how I can find all the zero-width characters.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It looks like there are many zero-width characters: https://wcwidth.readthedocs.io/en/latest/specs.html#width-of-0

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This is how wcwidth implements wcswidth() and wcwidth(): https://wcwidth.readthedocs.io/en/latest/_modules/wcwidth/wcwidth.html#wcswidth
Maybe it is too complicated for our traceback? Could we have a simple way that can handle most of the cases?

return len(line), line[start:]


class _ExceptionPrintContext:
Expand Down

Back | FazBrowse Home | New Git URL