]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43950: ensure source_line is present when specialising the traceback (GH-27313)
authorBatuhan Taskaya <batuhan@python.org>
Sat, 24 Jul 2021 12:50:19 +0000 (15:50 +0300)
committerGitHub <noreply@github.com>
Sat, 24 Jul 2021 12:50:19 +0000 (13:50 +0100)
Lib/test/test_traceback.py
Python/traceback.c

index 4742eb1d2309b4e5d19b52d41b4664b93f520559..c87ce7245335a8a55961a4e56f18ebe17cefb361 100644 (file)
@@ -121,6 +121,31 @@ class TracebackCases(unittest.TestCase):
         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)
index e02caef6f9bce7cdf4a3ce210b5b8f1aeb1fbfa7..9418236abbf51858e2d481a6bb21721df3bf2dd2 100644 (file)
@@ -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;