summaryrefslogtreecommitdiffstats
path: root/Python/errors.c
diff options
context:
space:
mode:
authorPablo Galindo <Pablogsal@gmail.com>2021-04-23 13:27:05 (GMT)
committerGitHub <noreply@github.com>2021-04-23 13:27:05 (GMT)
commita77aac4fca9723b8fd52a832f3e9df13beb25113 (patch)
treea504aa9fed91cd31849cdda3ecb1dad439a93778 /Python/errors.c
parent91b69b77cf5f78de6d35dea23098df34b6fd9e53 (diff)
downloadcpython-a77aac4fca9723b8fd52a832f3e9df13beb25113.zip
cpython-a77aac4fca9723b8fd52a832f3e9df13beb25113.tar.gz
cpython-a77aac4fca9723b8fd52a832f3e9df13beb25113.tar.bz2
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
Diffstat (limited to 'Python/errors.c')
-rw-r--r--Python/errors.c44
1 files changed, 42 insertions, 2 deletions
diff --git a/Python/errors.c b/Python/errors.c
index d73ba93..f743d30 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -1545,14 +1545,17 @@ PyErr_SyntaxLocation(const char *filename, int lineno)
If the exception is not a SyntaxError, also sets additional attributes
to make printing of exceptions believe it is a syntax error. */
-void
-PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)
+static void
+PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
+ int end_lineno, int end_col_offset)
{
PyObject *exc, *v, *tb, *tmp;
_Py_IDENTIFIER(filename);
_Py_IDENTIFIER(lineno);
+ _Py_IDENTIFIER(end_lineno);
_Py_IDENTIFIER(msg);
_Py_IDENTIFIER(offset);
+ _Py_IDENTIFIER(end_offset);
_Py_IDENTIFIER(print_file_and_line);
_Py_IDENTIFIER(text);
PyThreadState *tstate = _PyThreadState_GET();
@@ -1582,6 +1585,32 @@ PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)
_PyErr_Clear(tstate);
}
Py_XDECREF(tmp);
+
+ tmp = NULL;
+ if (end_lineno >= 0) {
+ tmp = PyLong_FromLong(end_lineno);
+ if (tmp == NULL) {
+ _PyErr_Clear(tstate);
+ }
+ }
+ if (_PyObject_SetAttrId(v, &PyId_end_lineno, tmp ? tmp : Py_None)) {
+ _PyErr_Clear(tstate);
+ }
+ Py_XDECREF(tmp);
+
+ tmp = NULL;
+ if (end_col_offset >= 0) {
+ tmp = PyLong_FromLong(end_col_offset);
+ if (tmp == NULL) {
+ _PyErr_Clear(tstate);
+ }
+ }
+ if (_PyObject_SetAttrId(v, &PyId_end_offset, tmp ? tmp : Py_None)) {
+ _PyErr_Clear(tstate);
+ }
+ Py_XDECREF(tmp);
+
+ tmp = NULL;
if (filename != NULL) {
if (_PyObject_SetAttrId(v, &PyId_filename, filename)) {
_PyErr_Clear(tstate);
@@ -1634,6 +1663,17 @@ PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)
}
void
+PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset) {
+ PyErr_SyntaxLocationObjectEx(filename, lineno, col_offset, lineno, -1);
+}
+
+void
+PyErr_RangedSyntaxLocationObject(PyObject *filename, int lineno, int col_offset,
+ int end_lineno, int end_col_offset) {
+ PyErr_SyntaxLocationObjectEx(filename, lineno, col_offset, end_lineno, end_col_offset);
+}
+
+void
PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
{
PyThreadState *tstate = _PyThreadState_GET();