Commit da8d72c9 authored by Serhiy Storchaka's avatar Serhiy Storchaka Committed by GitHub

bpo-12458: Fix line numbers for multiline expressions. (GH-8774)

parent 5e99b56d
...@@ -124,7 +124,8 @@ dis_bug708901 = """\ ...@@ -124,7 +124,8 @@ dis_bug708901 = """\
2 LOAD_CONST 1 (1) 2 LOAD_CONST 1 (1)
%3d 4 LOAD_CONST 2 (10) %3d 4 LOAD_CONST 2 (10)
6 CALL_FUNCTION 2
%3d 6 CALL_FUNCTION 2
8 GET_ITER 8 GET_ITER
>> 10 FOR_ITER 4 (to 16) >> 10 FOR_ITER 4 (to 16)
12 STORE_FAST 0 (res) 12 STORE_FAST 0 (res)
...@@ -134,6 +135,7 @@ dis_bug708901 = """\ ...@@ -134,6 +135,7 @@ dis_bug708901 = """\
18 RETURN_VALUE 18 RETURN_VALUE
""" % (bug708901.__code__.co_firstlineno + 1, """ % (bug708901.__code__.co_firstlineno + 1,
bug708901.__code__.co_firstlineno + 2, bug708901.__code__.co_firstlineno + 2,
bug708901.__code__.co_firstlineno + 1,
bug708901.__code__.co_firstlineno + 3) bug708901.__code__.co_firstlineno + 3)
...@@ -154,7 +156,8 @@ dis_bug1333982 = """\ ...@@ -154,7 +156,8 @@ dis_bug1333982 = """\
16 CALL_FUNCTION 1 16 CALL_FUNCTION 1
%3d 18 LOAD_CONST 4 (1) %3d 18 LOAD_CONST 4 (1)
20 BINARY_ADD
%3d 20 BINARY_ADD
22 CALL_FUNCTION 1 22 CALL_FUNCTION 1
24 RAISE_VARARGS 1 24 RAISE_VARARGS 1
...@@ -164,6 +167,7 @@ dis_bug1333982 = """\ ...@@ -164,6 +167,7 @@ dis_bug1333982 = """\
__file__, __file__,
bug1333982.__code__.co_firstlineno + 1, bug1333982.__code__.co_firstlineno + 1,
bug1333982.__code__.co_firstlineno + 2, bug1333982.__code__.co_firstlineno + 2,
bug1333982.__code__.co_firstlineno + 1,
bug1333982.__code__.co_firstlineno + 3) bug1333982.__code__.co_firstlineno + 3)
_BIG_LINENO_FORMAT = """\ _BIG_LINENO_FORMAT = """\
......
...@@ -2719,7 +2719,7 @@ Check doctest with a non-ascii filename: ...@@ -2719,7 +2719,7 @@ Check doctest with a non-ascii filename:
Exception raised: Exception raised:
Traceback (most recent call last): Traceback (most recent call last):
File ... File ...
compileflags, 1), test.globs) exec(compile(example.source, filename, "single",
File "<doctest foo-bär@baz[0]>", line 1, in <module> File "<doctest foo-bär@baz[0]>", line 1, in <module>
raise Exception('clé') raise Exception('clé')
Exception: clé Exception: clé
......
...@@ -400,7 +400,7 @@ class FaultHandlerTests(unittest.TestCase): ...@@ -400,7 +400,7 @@ class FaultHandlerTests(unittest.TestCase):
if filename: if filename:
lineno = 9 lineno = 9
elif fd is not None: elif fd is not None:
lineno = 12 lineno = 11
else: else:
lineno = 14 lineno = 14
expected = [ expected = [
......
...@@ -995,11 +995,11 @@ class TestStack(unittest.TestCase): ...@@ -995,11 +995,11 @@ class TestStack(unittest.TestCase):
s = some_inner(3, 4) s = some_inner(3, 4)
self.assertEqual( self.assertEqual(
[' File "%s", line %d, in some_inner\n' [' File "%s", line %d, in some_inner\n'
' traceback.walk_stack(None), capture_locals=True, limit=1)\n' ' return traceback.StackSummary.extract(\n'
' a = 1\n' ' a = 1\n'
' b = 2\n' ' b = 2\n'
' k = 3\n' ' k = 3\n'
' v = 4\n' % (__file__, some_inner.__code__.co_firstlineno + 4) ' v = 4\n' % (__file__, some_inner.__code__.co_firstlineno + 3)
], s.format()) ], s.format())
class TestTracebackException(unittest.TestCase): class TestTracebackException(unittest.TestCase):
......
...@@ -936,7 +936,7 @@ class TestCAPI(unittest.TestCase): ...@@ -936,7 +936,7 @@ class TestCAPI(unittest.TestCase):
return None return None
def track(self, release_gil=False, nframe=1): def track(self, release_gil=False, nframe=1):
frames = get_frames(nframe, 2) frames = get_frames(nframe, 1)
_testcapi.tracemalloc_track(self.domain, self.ptr, self.size, _testcapi.tracemalloc_track(self.domain, self.ptr, self.size,
release_gil) release_gil)
return frames return frames
......
Tracebacks show now correct line number for subexpressions in multiline
expressions. Tracebacks show now the line number of the first line for
multiline expressions instead of the line number of the last subexpression.
...@@ -4089,10 +4089,6 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type, ...@@ -4089,10 +4089,6 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
is_async_generator = c->u->u_ste->ste_coroutine; is_async_generator = c->u->u_ste->ste_coroutine;
if (is_async_generator && !is_async_function && type != COMP_GENEXP) { 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 " compiler_error(c, "asynchronous comprehension outside of "
"an asynchronous function"); "an asynchronous function");
goto error_in_scope; goto error_in_scope;
...@@ -4430,17 +4426,8 @@ compiler_with(struct compiler *c, stmt_ty s, int pos) ...@@ -4430,17 +4426,8 @@ compiler_with(struct compiler *c, stmt_ty s, int pos)
} }
static int 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) { switch (e->kind) {
case BoolOp_kind: case BoolOp_kind:
return compiler_boolop(c, e); return compiler_boolop(c, e);
...@@ -4609,6 +4596,31 @@ compiler_visit_expr(struct compiler *c, expr_ty e) ...@@ -4609,6 +4596,31 @@ compiler_visit_expr(struct compiler *c, expr_ty e)
return 1; 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 static int
compiler_augassign(struct compiler *c, stmt_ty s) compiler_augassign(struct compiler *c, stmt_ty s)
{ {
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment