Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 99ab0d4

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

File tree

Expand file treeCollapse file tree

3 files changed

+19
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+19
-0
lines changed

‎Lib/test/test_traceback.py

Copy file name to clipboardExpand all lines: Lib/test/test_traceback.py
+17Lines 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

+1Lines 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

Copy file name to clipboardExpand all lines: Python/compile.c
+1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.