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)
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)
# 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
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: