Uh oh!
There was an error while loading. Please reload this page.
gh-130273: Fix traceback color output with unicode characters - #142529
Conversation
Uh oh!
There was an error while loading. Please reload this page.
vstinner
commented
Dec 11, 2025
@serhiy-storchaka: Here is a PR about text width and Unicode characters :-) |
grayjk
commented
Jan 28, 2026
updated to use @serhiy-storchaka's recently added unicodedata.iter_graphemes |
grayjk
commented
Feb 18, 2026
@pablogsal@hauntsaninja as recent reviewers of traceback.py, would you mind taking look |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Uh oh!
There was an error while loading. Please reload this page.
StanFromIreland
commented
Mar 31, 2026
There are conflicts again I'm afraid, and mypy isn't happy either. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
Wulian233
commented
Apr 5, 2026
@pablogsal You maybe make wrong something :) |
ambv
commented
Apr 7, 2026
Thanks for your PR. I reverted the move of string-handling utils to traceback, they don't belong there This is because mypy type-checking introduction to the standard library relies on introduction of fully type-annotated libraries. I intend to keep it that way and therefore sprinkling It is now out of scope to format all of There's also a related behavioral change in |
There was a problem hiding this comment.
Pull request overview
This PR fixes misaligned color highlighting/underlining in traceback output when the source line contains wide Unicode characters or grapheme clusters, by switching caret/column calculations to use display width rather than raw character offsets.
Changes:
- Introduces grapheme-aware zipping between source characters and caret markers to keep colored segments aligned with terminal display width.
- Reworks
_display_width()to use_wlen()/_str_width()logic (including CTRL-Z and ANSI escape sequence handling) and adds targeted tests for wide/combining/ASCII edge cases. - Adds a NEWS fragment documenting the fix.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| Misc/NEWS.d/next/Library/2025-12-10-15-15-09.gh-issue-130273.iCfiY5.rst | Announces the traceback color alignment fix for Unicode characters. |
| Lib/traceback.py | Implements grapheme/display-width-aware caret-to-text alignment and updates width calculation helpers. |
| Lib/test/test_traceback.py | Adds regression tests for colorized tracebacks with wide/combining Unicode and ASCII display-width edge cases. |
| Lib/_pyrepl/utils.py | Minor whitespace-only change. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, @grayjk and @ambv, I could not cleanly backport this to |
Sorry, @grayjk and @ambv, I could not cleanly backport this to |
ambv
commented
Apr 7, 2026
There's some trivial conflicts that can be solved here, but the real problem is that WDYT, @serhiy-storchaka, is it worth creating a pure Python version of this segmentation purely for the backports? |
ambv
commented
Apr 7, 2026
To help you decide, I created #148218 so you can see what the implementation would look like. |
…ythonGH-142529) Account for the display width of Unicode characters so that colors and underlining in traceback output is correct. Co-authored-by: Łukasz Langa <lukasz@langa.pl> Co-authored-by: Victor Stinner <vstinner@python.org>
Account for the display width of unicode characters so that colors and underlining in traceback output is correct
Closes#130273