Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,23 @@ def f_with_binary_operator():
result_lines = self.get_exception(f_with_binary_operator)
self.assertEqual(result_lines, expected_error.splitlines())

def test_caret_for_failed_assertion(self):
def f_assert():
test = 3
assert test == 1 and test == 2, "Bug found?"

lineno_f = f_assert.__code__.co_firstlineno
expected_error = (
'Traceback (most recent call last):\n'
f' File "{__file__}", line {self.callable_line}, in get_exception\n'
' callable()\n'
f' File "{__file__}", line {lineno_f+2}, in f_assert\n'
' assert test == 1 and test == 2, "Bug found?"\n'
' ^^^^^^^^^^^^^^^^^^^^^^^\n'
)
result_lines = self.get_exception(f_assert)
self.assertEqual(result_lines, expected_error.splitlines())

def test_traceback_specialization_with_syntax_error(self):
bytecode = compile("1 / 0 / 1 / 2\n", TESTFN, "exec")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix location of the error on a failed assertion.
1 change: 1 addition & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -4050,6 +4050,7 @@ compiler_assert(struct compiler *c, stmt_ty s)
ADDOP_I(c, PRECALL, 0);
ADDOP_I(c, CALL, 0);
}
SET_LOC(c, s->v.Assert.test);
ADDOP_I(c, RAISE_VARARGS, 1);
compiler_use_next_block(c, end);
return 1;
Expand Down