Skip to content

Commit 280eebf

Browse files
gh-98278: Highlight both mismatched brackets in SyntaxError
1 parent 2c2d801 commit 280eebf

6 files changed

Lines changed: 98 additions & 3 deletions

File tree

‎Lib/test/test_traceback.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,40 @@ def test_caret_wide_char(self):
148148
# "女女女 for 女女女 in range(30)" has display width 30
149149
self.assertEqual(err[2].count("^"), 30)
150150

151+
def test_mismatched_bracket_carets(self):
152+
cases = (
153+
('obj{"some long expression"]', 3, 26,
154+
' ^ ^\n'),
155+
('obj[func(x[store[arg, a[i], b[j])])[y]]', 16, 32,
156+
' ^ ^\n'),
157+
('x = ("é女"]', 4, 9,
158+
' ^ ^\n'),
159+
('α = obj{1]', 7, 9,
160+
' ^ ^\n'),
161+
("f'{a[4)}'", 4, 6,
162+
' ^ ^\n'),
163+
)
164+
for source, opening, closing, expected_carets in cases:
165+
with self.subTest(source=source):
166+
with self.assertRaises(SyntaxError) as cm:
167+
compile(source, '<test>', 'exec')
168+
exc = cm.exception
169+
self.assertEqual(exc.offset, closing + 1)
170+
self.assertEqual(exc._opening_offset, opening + 1)
171+
formatted = traceback.format_exception_only(exc)
172+
self.assertEqual(formatted[2], expected_carets)
173+
colorized = ''.join(traceback.format_exception_only(exc, colorize=True))
174+
self.assertEqual(
175+
re.sub(r'\x1b\[[0-9;]*m', '', colorized), ''.join(formatted)
176+
)
177+
178+
# The existing one-caret display remains for brackets on different lines.
179+
with self.assertRaises(SyntaxError) as cm:
180+
compile('x = (\n 1 + ]', '<test>', 'exec')
181+
exc = cm.exception
182+
self.assertFalse(hasattr(exc, '_opening_offset'))
183+
self.assertEqual(traceback.format_exception_only(exc)[2], ' ^\n')
184+
151185
def test_nocaret(self):
152186
exc = SyntaxError("error", ("x.py", 23, None, "bad syntax"))
153187
err = traceback.format_exception_only(SyntaxError, exc)

‎Lib/traceback.py‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,7 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
11831183
self.msg = exc_value.msg
11841184
self._is_syntax_error = True
11851185
self._exc_metadata = getattr(exc_value, "_metadata", None)
1186+
self._opening_offset = getattr(exc_value, "_opening_offset", None)
11861187
elif exc_type and issubclass(exc_type, ImportError) and \
11871188
getattr(exc_value, "name_from", None) is not None:
11881189
wrong_name = getattr(exc_value, "name_from", None)
@@ -1592,21 +1593,41 @@ def _format_syntax_error(self, stype, **kwargs):
15921593
dp_colno = _display_width(ltext, colno)
15931594
highlighted = ltext[colno:end_colno]
15941595
caret_count = _display_width(highlighted) if highlighted else (end_colno - colno)
1596+
opening_colno = None
1597+
if (
1598+
isinstance(self._opening_offset, int)
1599+
and self.lineno == self.end_lineno
1600+
):
1601+
opening_colno = self._opening_offset - 1 - spaces
1602+
if not 0 <= opening_colno < colno:
1603+
opening_colno = None
1604+
carets = '^' * caret_count
1605+
if opening_colno is not None:
1606+
dp_opening_colno = _display_width(ltext, opening_colno)
1607+
carets = '^' + ' ' * (dp_colno - dp_opening_colno - 1) + carets
1608+
dp_colno = dp_opening_colno
15951609
start_color = end_color = ""
15961610
if colorize:
1597-
# colorize from colno to end_colno
1611+
# Colorize the later range first so the earlier
1612+
# opening column is unaffected by escape sequences.
15981613
ltext = (
15991614
ltext[:colno] +
16001615
theme.error_highlight + ltext[colno:end_colno] + theme.reset +
16011616
ltext[end_colno:]
16021617
)
1618+
if opening_colno is not None:
1619+
ltext = (
1620+
ltext[:opening_colno] +
1621+
theme.error_highlight + ltext[opening_colno] + theme.reset +
1622+
ltext[opening_colno + 1:]
1623+
)
16031624
start_color = theme.error_highlight
16041625
end_color = theme.reset
16051626
yield ' {}\n'.format(ltext)
16061627
yield ' {}{}{}{}\n'.format(
16071628
' ' * dp_colno,
16081629
start_color,
1609-
'^' * caret_count,
1630+
carets,
16101631
end_color,
16111632
)
16121633
else:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
When mismatched brackets or parentheses are on the same line, the
2+
:exc:`SyntaxError` display now points to both the opening and closing
3+
delimiters.

‎Parser/lexer/lexer.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,8 @@ _PyLexer_get_normal(struct tok_state *tok, ftstring_state *current, struct token
472472
c, opening, tok->parenlinenostack[tok->level]));
473473
}
474474
else {
475-
return MAKE_TOKEN(_PyTokenizer_syntaxerror(tok,
475+
return MAKE_TOKEN(_PyTokenizer_syntaxerror_with_opening(
476+
tok, tok->parencolstack[tok->level],
476477
"closing parenthesis '%c' does not match "
477478
"opening parenthesis '%c'",
478479
c, opening));

‎Parser/tokenizer/helpers.c‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,39 @@ _PyTokenizer_syntaxerror(struct tok_state *tok, const char *format, ...)
9898
return ret;
9999
}
100100

101+
int
102+
_PyTokenizer_syntaxerror_with_opening(struct tok_state *tok,
103+
int opening_col_offset,
104+
const char *format, ...)
105+
{
106+
va_list vargs;
107+
va_start(vargs, format);
108+
const char *line = _PyTok_SourcePointer(&tok->source, tok->line_start);
109+
int ret = _syntaxerror_range(
110+
tok, line, tok->cur - tok->line_start, tok->lineno,
111+
format, -1, -1, vargs);
112+
va_end(vargs);
113+
114+
/* Leave the error location on the closing bracket, but make the
115+
opening bracket's character column available to traceback. */
116+
if (PyErr_ExceptionMatches(PyExc_SyntaxError)) {
117+
PyObject *exc = PyErr_GetRaisedException();
118+
PyObject *offset = PyLong_FromLong(
119+
byte_col_to_char_col(line, opening_col_offset + 1));
120+
if (offset != NULL) {
121+
if (PyObject_SetAttrString(exc, "_opening_offset", offset) < 0) {
122+
PyErr_Clear();
123+
}
124+
Py_DECREF(offset);
125+
}
126+
else {
127+
PyErr_Clear();
128+
}
129+
PyErr_SetRaisedException(exc);
130+
}
131+
return ret;
132+
}
133+
101134
int
102135
_PyTokenizer_syntaxerror_known_range(struct tok_state *tok,
103136
int col_offset, int end_col_offset,

‎Parser/tokenizer/helpers.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#include "../lexer/state.h"
77

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

0 commit comments

Comments
 (0)