From da8d72c953369b872a12c13f136ada77a786714a Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 17 Sep 2018 15:17:29 +0300 Subject: bpo-12458: Fix line numbers for multiline expressions. (GH-8774) --- Python/compile.c | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) (limited to 'Python/compile.c') diff --git a/Python/compile.c b/Python/compile.c index c6a667c29e..ebd73fb723 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -4089,10 +4089,6 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type, is_async_generator = c->u->u_ste->ste_coroutine; if (is_async_generator && !is_async_function && type != COMP_GENEXP) { - if (e->lineno > c->u->u_lineno) { - c->u->u_lineno = e->lineno; - c->u->u_lineno_set = 0; - } compiler_error(c, "asynchronous comprehension outside of " "an asynchronous function"); goto error_in_scope; @@ -4430,17 +4426,8 @@ compiler_with(struct compiler *c, stmt_ty s, int pos) } static int -compiler_visit_expr(struct compiler *c, expr_ty e) +compiler_visit_expr1(struct compiler *c, expr_ty e) { - /* If expr e has a different line number than the last expr/stmt, - set a new line number for the next instruction. - */ - if (e->lineno > c->u->u_lineno) { - c->u->u_lineno = e->lineno; - c->u->u_lineno_set = 0; - } - /* Updating the column offset is always harmless. */ - c->u->u_col_offset = e->col_offset; switch (e->kind) { case BoolOp_kind: return compiler_boolop(c, e); @@ -4609,6 +4596,31 @@ compiler_visit_expr(struct compiler *c, expr_ty e) return 1; } +static int +compiler_visit_expr(struct compiler *c, expr_ty e) +{ + /* If expr e has a different line number than the last expr/stmt, + set a new line number for the next instruction. + */ + int old_lineno = c->u->u_lineno; + int old_col_offset = c->u->u_col_offset; + if (e->lineno != c->u->u_lineno) { + c->u->u_lineno = e->lineno; + c->u->u_lineno_set = 0; + } + /* Updating the column offset is always harmless. */ + c->u->u_col_offset = e->col_offset; + + int res = compiler_visit_expr1(c, e); + + if (old_lineno != c->u->u_lineno) { + c->u->u_lineno = old_lineno; + c->u->u_lineno_set = 0; + } + c->u->u_col_offset = old_col_offset; + return res; +} + static int compiler_augassign(struct compiler *c, stmt_ty s) { -- cgit v1.2.1