aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/compile.c
diff options
context:
space:
mode:
authorPablo Galindo <Pablogsal@gmail.com>2021-04-23 14:27:05 +0100
committerGitHub <noreply@github.com>2021-04-23 14:27:05 +0100
commita77aac4fca9723b8fd52a832f3e9df13beb25113 (patch)
treea504aa9fed91cd31849cdda3ecb1dad439a93778 /Python/compile.c
parent91b69b77cf5f78de6d35dea23098df34b6fd9e53 (diff)
downloadcpython-a77aac4fca9723b8fd52a832f3e9df13beb25113.tar.gz
cpython-a77aac4fca9723b8fd52a832f3e9df13beb25113.zip
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/compile.c')
-rw-r--r--Python/compile.c13
1 files changed, 10 insertions, 3 deletions
diff --git a/Python/compile.c b/Python/compile.c
index 49a713b2b0c..1b7a2e83b16 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -197,6 +197,8 @@ struct compiler_unit {
int u_firstlineno; /* the first lineno of the block */
int u_lineno; /* the lineno for the current stmt */
int u_col_offset; /* the offset of the current stmt */
+ int u_end_lineno; /* the end line of the current stmt */
+ int u_end_col_offset; /* the end offset of the current stmt */
};
/* This struct captures the global state of a compilation.
@@ -641,6 +643,8 @@ compiler_enter_scope(struct compiler *c, identifier name,
u->u_firstlineno = lineno;
u->u_lineno = 0;
u->u_col_offset = 0;
+ u->u_end_lineno = 0;
+ u->u_end_col_offset = 0;
u->u_consts = PyDict_New();
if (!u->u_consts) {
compiler_unit_free(u);
@@ -911,7 +915,9 @@ compiler_next_instr(basicblock *b)
#define SET_LOC(c, x) \
(c)->u->u_lineno = (x)->lineno; \
- (c)->u->u_col_offset = (x)->col_offset;
+ (c)->u->u_col_offset = (x)->col_offset; \
+ (c)->u->u_end_lineno = (x)->end_lineno; \
+ (c)->u->u_end_col_offset = (x)->end_col_offset;
/* Return the stack effect of opcode with argument oparg.
@@ -5474,8 +5480,9 @@ compiler_error(struct compiler *c, const char *format, ...)
Py_INCREF(Py_None);
loc = Py_None;
}
- PyObject *args = Py_BuildValue("O(OiiO)", msg, c->c_filename,
- c->u->u_lineno, c->u->u_col_offset + 1, loc);
+ PyObject *args = Py_BuildValue("O(OiiOii)", msg, c->c_filename,
+ c->u->u_lineno, c->u->u_col_offset + 1, loc,
+ c->u->u_end_lineno, c->u->u_end_col_offset + 1);
Py_DECREF(msg);
if (args == NULL) {
goto exit;