bpo-43914: Highlight invalid ranges in SyntaxErrors (#25525)
To improve the user experience understanding what part of the error messages associated with SyntaxErrors is wrong, we can highlight the whole error range and not only place the caret at the first character. In this way:
>>> foo(x, z for z in range(10), t, w)
File "<stdin>", line 1
foo(x, z for z in range(10), t, w)
^
SyntaxError: Generator expression must be parenthesized
becomes
>>> foo(x, z for z in range(10), t, w)
File "<stdin>", line 1
foo(x, z for z in range(10), t, w)
^^^^^^^^^^^^^^^^^^^^
SyntaxError: Generator expression must be parenthesized
diff --git a/Include/cpython/pyerrors.h b/Include/cpython/pyerrors.h
index 9d88e66..5e57129 100644
--- a/Include/cpython/pyerrors.h
+++ b/Include/cpython/pyerrors.h
@@ -20,6 +20,8 @@ typedef struct {
PyObject *filename;
PyObject *lineno;
PyObject *offset;
+ PyObject *end_lineno;
+ PyObject *end_offset;
PyObject *text;
PyObject *print_file_and_line;
} PySyntaxErrorObject;
@@ -148,6 +150,13 @@ PyAPI_FUNC(void) PyErr_SyntaxLocationObject(
int lineno,
int col_offset);
+PyAPI_FUNC(void) PyErr_RangedSyntaxLocationObject(
+ PyObject *filename,
+ int lineno,
+ int col_offset,
+ int end_lineno,
+ int end_col_offset);
+
PyAPI_FUNC(PyObject *) PyErr_ProgramTextObject(
PyObject *filename,
int lineno);