mirror of
https://github.com/python/cpython.git
synced 2025-12-23 09:19:18 +00:00
gh-142539: Fix traceback caret location calculation for SyntaxErrors with wide chars (#142540)
This commit is contained in:
parent
5b19c75b47
commit
b52e8ce4af
3 changed files with 28 additions and 5 deletions
|
|
@ -88,6 +88,12 @@ class TracebackCases(unittest.TestCase):
|
|||
def tokenizer_error_with_caret_range(self):
|
||||
compile("blech ( ", "?", "exec")
|
||||
|
||||
def syntax_error_with_caret_wide_char(self):
|
||||
compile("女女女=1; 女女女/", "?", "exec")
|
||||
|
||||
def syntax_error_with_caret_wide_char_range(self):
|
||||
compile("f(x, 女女女 for 女女女 in range(30), z)", "?", "exec")
|
||||
|
||||
def test_caret(self):
|
||||
err = self.get_exception_format(self.syntax_error_with_caret,
|
||||
SyntaxError)
|
||||
|
|
@ -125,6 +131,20 @@ class TracebackCases(unittest.TestCase):
|
|||
self.assertEqual(err[1].find("("), err[2].find("^")) # in the right place
|
||||
self.assertEqual(err[2].count("^"), 1)
|
||||
|
||||
def test_caret_wide_char(self):
|
||||
err = self.get_exception_format(self.syntax_error_with_caret_wide_char,
|
||||
SyntaxError)
|
||||
self.assertIn("^", err[2])
|
||||
# "女女女=1; 女女女/" has display width 17
|
||||
self.assertEqual(err[2].find("^"), 4 + 17)
|
||||
|
||||
err = self.get_exception_format(self.syntax_error_with_caret_wide_char_range,
|
||||
SyntaxError)
|
||||
self.assertIn("^", err[2])
|
||||
self.assertEqual(err[2].find("^"), 4 + 5)
|
||||
# "女女女 for 女女女 in range(30)" has display width 30
|
||||
self.assertEqual(err[2].count("^"), 30)
|
||||
|
||||
def test_nocaret(self):
|
||||
exc = SyntaxError("error", ("x.py", 23, None, "bad syntax"))
|
||||
err = traceback.format_exception_only(SyntaxError, exc)
|
||||
|
|
|
|||
|
|
@ -1464,10 +1464,11 @@ class TracebackException:
|
|||
# Convert 1-based column offset to 0-based index into stripped text
|
||||
colno = offset - 1 - spaces
|
||||
end_colno = end_offset - 1 - spaces
|
||||
caretspace = ' '
|
||||
if colno >= 0:
|
||||
# non-space whitespace (likes tabs) must be kept for alignment
|
||||
caretspace = ((c if c.isspace() else ' ') for c in ltext[:colno])
|
||||
# Calculate display width to account for wide characters
|
||||
dp_colno = _display_width(ltext, colno)
|
||||
highlighted = ltext[colno:end_colno]
|
||||
caret_count = _display_width(highlighted) if highlighted else (end_colno - colno)
|
||||
start_color = end_color = ""
|
||||
if colorize:
|
||||
# colorize from colno to end_colno
|
||||
|
|
@ -1480,9 +1481,9 @@ class TracebackException:
|
|||
end_color = theme.reset
|
||||
yield ' {}\n'.format(ltext)
|
||||
yield ' {}{}{}{}\n'.format(
|
||||
"".join(caretspace),
|
||||
' ' * dp_colno,
|
||||
start_color,
|
||||
('^' * (end_colno - colno)),
|
||||
'^' * caret_count,
|
||||
end_color,
|
||||
)
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
:mod:`traceback`: Fix location of carets in :exc:`SyntaxError`\s when the
|
||||
source contains wide characters.
|
||||
Loading…
Add table
Add a link
Reference in a new issue