Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 99ab0d4

Browse files
authored
[3.11] gh-116034: fix location info on the error of a failed assertion (#116052)
1 parent f225740 commit 99ab0d4

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

Lib/test/test_traceback.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,23 @@ def f_with_binary_operator():
636636
result_lines = self.get_exception(f_with_binary_operator)
637637
self.assertEqual(result_lines, expected_error.splitlines())
638638

639+
def test_caret_for_failed_assertion(self):
640+
def f_assert():
641+
test = 3
642+
assert test == 1 and test == 2, "Bug found?"
643+
644+
lineno_f = f_assert.__code__.co_firstlineno
645+
expected_error = (
646+
'Traceback (most recent call last):\n'
647+
f' File "{__file__}", line {self.callable_line}, in get_exception\n'
648+
' callable()\n'
649+
f' File "{__file__}", line {lineno_f+2}, in f_assert\n'
650+
' assert test == 1 and test == 2, "Bug found?"\n'
651+
' ^^^^^^^^^^^^^^^^^^^^^^^\n'
652+
)
653+
result_lines = self.get_exception(f_assert)
654+
self.assertEqual(result_lines, expected_error.splitlines())
655+
639656
def test_traceback_specialization_with_syntax_error(self):
640657
bytecode = compile("1 / 0 / 1 / 2\n", TESTFN, "exec")
641658

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix location of the error on a failed assertion.

Python/compile.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4050,6 +4050,7 @@ compiler_assert(struct compiler *c, stmt_ty s)
40504050
ADDOP_I(c, PRECALL, 0);
40514051
ADDOP_I(c, CALL, 0);
40524052
}
4053+
SET_LOC(c, s->v.Assert.test);
40534054
ADDOP_I(c, RAISE_VARARGS, 1);
40544055
compiler_use_next_block(c, end);
40554056
return 1;

0 commit comments

Comments
 (0)