Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,40 @@ def test_caret_wide_char(self):
# "女女女 for 女女女 in range(30)" has display width 30
self.assertEqual(err[2].count("^"), 30)

def test_mismatched_bracket_carets(self):
cases = (
('obj{"some long expression"]', 3, 26,
' ^ ^\n'),
('obj[func(x[store[arg, a[i], b[j])])[y]]', 16, 32,
' ^ ^\n'),
('x = ("é女"]', 4, 9,
' ^ ^\n'),
('α = obj{1]', 7, 9,
' ^ ^\n'),
("f'{a[4)}'", 4, 6,
' ^ ^\n'),
)
for source, opening, closing, expected_carets in cases:
with self.subTest(source=source):
with self.assertRaises(SyntaxError) as cm:
compile(source, '<test>', 'exec')
exc = cm.exception
self.assertEqual(exc.offset, closing + 1)
self.assertEqual(exc._opening_offset, opening + 1)
formatted = traceback.format_exception_only(exc)
self.assertEqual(formatted[2], expected_carets)
colorized = ''.join(traceback.format_exception_only(exc, colorize=True))
self.assertEqual(
re.sub(r'\x1b\[[0-9;]*m', '', colorized), ''.join(formatted)
)

# The existing one-caret display remains for brackets on different lines.
with self.assertRaises(SyntaxError) as cm:
compile('x = (\n 1 + ]', '<test>', 'exec')
exc = cm.exception
self.assertFalse(hasattr(exc, '_opening_offset'))
self.assertEqual(traceback.format_exception_only(exc)[2], ' ^\n')

def test_nocaret(self):
exc = SyntaxError("error", ("x.py", 23, None, "bad syntax"))
err = traceback.format_exception_only(SyntaxError, exc)
Expand Down
25 changes: 23 additions & 2 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,7 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
self.msg = exc_value.msg
self._is_syntax_error = True
self._exc_metadata = getattr(exc_value, "_metadata", None)
self._opening_offset = getattr(exc_value, "_opening_offset", None)
elif exc_type and issubclass(exc_type, ImportError) and \
getattr(exc_value, "name_from", None) is not None:
wrong_name = getattr(exc_value, "name_from", None)
Expand Down Expand Up @@ -1592,21 +1593,41 @@ def _format_syntax_error(self, stype, **kwargs):
dp_colno = _display_width(ltext, colno)
highlighted = ltext[colno:end_colno]
caret_count = _display_width(highlighted) if highlighted else (end_colno - colno)
opening_colno = None
if (
isinstance(self._opening_offset, int)
and self.lineno == self.end_lineno
):
opening_colno = self._opening_offset - 1 - spaces
if not 0 <= opening_colno < colno:
opening_colno = None
carets = '^' * caret_count
if opening_colno is not None:
dp_opening_colno = _display_width(ltext, opening_colno)
carets = '^' + ' ' * (dp_colno - dp_opening_colno - 1) + carets
dp_colno = dp_opening_colno
start_color = end_color = ""
if colorize:
# colorize from colno to end_colno
# Colorize the later range first so the earlier
# opening column is unaffected by escape sequences.
ltext = (
ltext[:colno] +
theme.error_highlight + ltext[colno:end_colno] + theme.reset +
ltext[end_colno:]
)
if opening_colno is not None:
ltext = (
ltext[:opening_colno] +
theme.error_highlight + ltext[opening_colno] + theme.reset +
ltext[opening_colno + 1:]
)
start_color = theme.error_highlight
end_color = theme.reset
yield ' {}\n'.format(ltext)
yield ' {}{}{}{}\n'.format(
' ' * dp_colno,
start_color,
'^' * caret_count,
carets,
end_color,
)
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
When mismatched brackets or parentheses are on the same line, the
:exc:`SyntaxError` display now points to both the opening and closing
delimiters.
3 changes: 2 additions & 1 deletion Parser/lexer/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,8 @@ _PyLexer_get_normal(struct tok_state *tok, ftstring_state *current, struct token
c, opening, tok->parenlinenostack[tok->level]));
}
else {
return MAKE_TOKEN(_PyTokenizer_syntaxerror(tok,
return MAKE_TOKEN(_PyTokenizer_syntaxerror_with_opening(
tok, tok->parencolstack[tok->level],
"closing parenthesis '%c' does not match "
"opening parenthesis '%c'",
c, opening));
Expand Down
33 changes: 33 additions & 0 deletions Parser/tokenizer/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,39 @@ _PyTokenizer_syntaxerror(struct tok_state *tok, const char *format, ...)
return ret;
}

int
_PyTokenizer_syntaxerror_with_opening(struct tok_state *tok,
int opening_col_offset,
const char *format, ...)
{
va_list vargs;
va_start(vargs, format);
const char *line = _PyTok_SourcePointer(&tok->source, tok->line_start);
int ret = _syntaxerror_range(
tok, line, tok->cur - tok->line_start, tok->lineno,
format, -1, -1, vargs);
va_end(vargs);

/* Leave the error location on the closing bracket, but make the
opening bracket's character column available to traceback. */
if (PyErr_ExceptionMatches(PyExc_SyntaxError)) {
PyObject *exc = PyErr_GetRaisedException();
PyObject *offset = PyLong_FromLong(
byte_col_to_char_col(line, opening_col_offset + 1));
if (offset != NULL) {
if (PyObject_SetAttrString(exc, "_opening_offset", offset) < 0) {
PyErr_Clear();
}
Py_DECREF(offset);
}
else {
PyErr_Clear();
}
PyErr_SetRaisedException(exc);
}
return ret;
}

int
_PyTokenizer_syntaxerror_known_range(struct tok_state *tok,
int col_offset, int end_col_offset,
Expand Down
3 changes: 3 additions & 0 deletions Parser/tokenizer/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include "../lexer/state.h"

int _PyTokenizer_syntaxerror(struct tok_state *tok, const char *format, ...);
/* opening_col_offset is a 0-based byte column on the current line. */
int _PyTokenizer_syntaxerror_with_opening(
struct tok_state *tok, int opening_col_offset, const char *format, ...);
/* Positive range columns are 1-based byte columns. A start column of -1
derives the character column from the reporting cursor; an end column of
-1 uses the start column. */
Expand Down
Loading