[Python-checkins] bpo-43950: ensure source_line is present when specialising the traceback (GH-27313)

pablogsal webhook-mailer at python.org
Sat Jul 24 08:50:36 EDT 2021


https://github.com/python/cpython/commit/c8362314cce53a5b59da7523fbdfa00f122aa319
commit: c8362314cce53a5b59da7523fbdfa00f122aa319
branch: main
author: Batuhan Taskaya <batuhan at python.org>
committer: pablogsal <Pablogsal at gmail.com>
date: 2021-07-24T13:50:19+01:00
summary:

bpo-43950: ensure source_line is present when specialising the traceback (GH-27313)

files:
M Lib/test/test_traceback.py
M Python/traceback.c

diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index 4742eb1d2309b..c87ce7245335a 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -121,6 +121,31 @@ def test_no_caret_with_no_debug_ranges_flag_python_traceback(self):
         finally:
             unlink(TESTFN)
 
+    def test_recursion_error_during_traceback(self):
+        code = textwrap.dedent("""
+                import sys
+                from weakref import ref
+
+                sys.setrecursionlimit(15)
+
+                def f():
+                    ref(lambda: 0, [])
+                    f()
+
+                try:
+                    f()
+                except RecursionError:
+                    pass
+        """)
+        try:
+            with open(TESTFN, 'w') as f:
+                f.write(code)
+
+            rc, _, _ = assert_python_ok(TESTFN)
+            self.assertEqual(rc, 0)
+        finally:
+            unlink(TESTFN)
+
     def test_bad_indentation(self):
         err = self.get_exception_format(self.syntax_error_bad_indentation,
                                         IndentationError)
diff --git a/Python/traceback.c b/Python/traceback.c
index e02caef6f9bce..9418236abbf51 100644
--- a/Python/traceback.c
+++ b/Python/traceback.c
@@ -699,11 +699,11 @@ tb_displayline(PyTracebackObject* tb, PyObject *f, PyObject *filename, int linen
     Py_DECREF(line);
     if (err != 0)
         return err;
+
     int truncation = _TRACEBACK_SOURCE_LINE_INDENT;
     PyObject* source_line = NULL;
-
     if (_Py_DisplaySourceLine(f, filename, lineno, _TRACEBACK_SOURCE_LINE_INDENT,
-                               &truncation, &source_line) != 0) {
+                               &truncation, &source_line) != 0 || !source_line) {
         /* ignore errors since we can't report them, can we? */
         err = ignore_source_errors();
         goto done;



More information about the Python-checkins mailing list