Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.1k
gh-130273: Fix traceback color output with unicode characters#142529
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
Changes from all commits
306a6908edad117947033e8d23cd467656e05ad91e37a4fd913182177b3fa874ddf024f094135File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| """Extract, format and print information about Python stack traces.""" | ||
| import collections.abc | ||
| import functools | ||
| import itertools | ||
| import linecache | ||
| import os | ||
| import re | ||
| import sys | ||
| import textwrap | ||
| import types | ||
| @@ -684,12 +686,12 @@ def output_line(lineno): | ||
| colorized_line_parts = [] | ||
| colorized_carets_parts = [] | ||
| for color, group in itertools.groupby(itertools.zip_longest(line, carets, fillvalue=""), key=lambda x: x[1]): | ||
| for color, group in itertools.groupby(_zip_display_width(line, carets), key=lambda x: x[1]): | ||
| caret_group = list(group) | ||
| if color == "^": | ||
| if "^" in color: | ||
| colorized_line_parts.append(theme.error_highlight + "".join(char for char, _ in caret_group) + theme.reset) | ||
| colorized_carets_parts.append(theme.error_highlight + "".join(caret for _, caret in caret_group) + theme.reset) | ||
| elif color == "~": | ||
| elif "~" in color: | ||
| colorized_line_parts.append(theme.error_range + "".join(char for char, _ in caret_group) + theme.reset) | ||
| colorized_carets_parts.append(theme.error_range + "".join(caret for _, caret in caret_group) + theme.reset) | ||
| else: | ||
| @@ -971,26 +973,64 @@ def setup_positions(expr, force_valid=True): | ||
| return None | ||
| _WIDE_CHAR_SPECIFIERS = "WF" | ||
| def _zip_display_width(line, carets): | ||
| carets = iter(carets) | ||
| if line.isascii() and '\x1a' not in line: | ||
| for char in line: | ||
| yield char, next(carets, "") | ||
| return | ||
| import unicodedata | ||
| for char in unicodedata.iter_graphemes(line): | ||
| char = str(char) | ||
| char_width = _display_width(char) | ||
| yield char, "".join(itertools.islice(carets, char_width)) | ||
ambv marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| @functools.cache | ||
| def _str_width(c: str) -> int: | ||
| # copied from _pyrepl.utils to fix gh-130273 | ||
| if ord(c) < 128: | ||
| return 1 | ||
| import unicodedata | ||
| # gh-139246 for zero-width joiner and combining characters | ||
| if unicodedata.combining(c): | ||
| return 0 | ||
| category = unicodedata.category(c) | ||
| if category == "Cf" and c != "\u00ad": | ||
| return 0 | ||
| w = unicodedata.east_asian_width(c) | ||
| if w in ("N", "Na", "H", "A"): | ||
| return 1 | ||
| return 2 | ||
| _ANSI_ESCAPE_SEQUENCE = re.compile(r"\x1b\[[ -@]*[A-~]") | ||
| def _wlen(s: str) -> int: | ||
| # copied from _pyrepl.utils to fix gh-130273 | ||
| if len(s) == 1 and s != "\x1a": | ||
| return _str_width(s) | ||
| length = sum(_str_width(i) for i in s) | ||
| # remove lengths of any escape sequences | ||
| sequence = _ANSI_ESCAPE_SEQUENCE.findall(s) | ||
| ctrl_z_cnt = s.count("\x1a") | ||
| return length - sum(len(i) for i in sequence) + ctrl_z_cnt | ||
| def _display_width(line, offset=None): | ||
| """Calculate the extra amount of width space the given source | ||
| code segment might take if it were to be displayed on a fixed | ||
| width output device. Supports wide unicode characters and emojis.""" | ||
| if offset is None: | ||
| offset = len(line) | ||
| # Fast track for ASCII-only strings | ||
| if line.isascii(): | ||
| return offset | ||
| return _wlen(line) | ||
| import unicodedata | ||
| return sum( | ||
| 2 if unicodedata.east_asian_width(char) in _WIDE_CHAR_SPECIFIERS else 1 | ||
| for char in line[:offset] | ||
| ) | ||
| return _wlen(line[:offset]) | ||
ambv marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| def _format_note(note, indent, theme): | ||
StanFromIreland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix traceback color output with Unicode characters. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.