Skip to content

Commit c836231

Browse files
authored
bpo-43950: ensure source_line is present when specialising the traceback (GH-27313)
1 parent a22b05d commit c836231

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

Lib/test/test_traceback.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,31 @@ def test_no_caret_with_no_debug_ranges_flag_python_traceback(self):
121121
finally:
122122
unlink(TESTFN)
123123

124+
def test_recursion_error_during_traceback(self):
125+
code = textwrap.dedent("""
126+
import sys
127+
from weakref import ref
128+
129+
sys.setrecursionlimit(15)
130+
131+
def f():
132+
ref(lambda: 0, [])
133+
f()
134+
135+
try:
136+
f()
137+
except RecursionError:
138+
pass
139+
""")
140+
try:
141+
with open(TESTFN, 'w') as f:
142+
f.write(code)
143+
144+
rc, _, _ = assert_python_ok(TESTFN)
145+
self.assertEqual(rc, 0)
146+
finally:
147+
unlink(TESTFN)
148+
124149
def test_bad_indentation(self):
125150
err = self.get_exception_format(self.syntax_error_bad_indentation,
126151
IndentationError)

Python/traceback.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,11 +699,11 @@ tb_displayline(PyTracebackObject* tb, PyObject *f, PyObject *filename, int linen
699699
Py_DECREF(line);
700700
if (err != 0)
701701
return err;
702+
702703
int truncation = _TRACEBACK_SOURCE_LINE_INDENT;
703704
PyObject* source_line = NULL;
704-
705705
if (_Py_DisplaySourceLine(f, filename, lineno, _TRACEBACK_SOURCE_LINE_INDENT,
706-
&truncation, &source_line) != 0) {
706+
&truncation, &source_line) != 0 || !source_line) {
707707
/* ignore errors since we can't report them, can we? */
708708
err = ignore_source_errors();
709709
goto done;

0 commit comments

Comments
 (0)