From 9de4655645356c41fab23b85501ca0d0bb9c673d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Thu, 19 Sep 2024 00:08:35 +0200 Subject: [PATCH] Handle an empty AST body when reporting tracebacks - Fixes: rhbz#2311907 --- ...y-ast-body-when-reporting-tracebacks.patch | 83 +++++++++++++++++++ python3.13.spec | 10 ++- 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 00439-gh-122145-handle-an-empty-ast-body-when-reporting-tracebacks.patch diff --git a/00439-gh-122145-handle-an-empty-ast-body-when-reporting-tracebacks.patch b/00439-gh-122145-handle-an-empty-ast-body-when-reporting-tracebacks.patch new file mode 100644 index 0000000..9057e6b --- /dev/null +++ b/00439-gh-122145-handle-an-empty-ast-body-when-reporting-tracebacks.patch @@ -0,0 +1,83 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= + <10796600+picnixz@users.noreply.github.com> +Date: Wed, 18 Sep 2024 18:42:33 +0200 +Subject: [PATCH] 00439: gh-122145: Handle an empty AST body when reporting + tracebacks + +(cherry picked from commit 5cd50cb6eb28e525f0c838e049e900ea982a5a23) +--- + Lib/test/test_traceback.py | 35 +++++++++++++++++++ + Lib/traceback.py | 2 ++ + ...-07-23-12-38-14.gh-issue-122145.sTO8nX.rst | 3 ++ + 3 files changed, 40 insertions(+) + create mode 100644 Misc/NEWS.d/next/Library/2024-07-23-12-38-14.gh-issue-122145.sTO8nX.rst + +diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py +index 574d5dab97..a78aded4cc 100644 +--- a/Lib/test/test_traceback.py ++++ b/Lib/test/test_traceback.py +@@ -3304,6 +3304,41 @@ def format_frame_summary(self, frame_summary, colorize=False): + f' File "{__file__}", line {lno}, in f\n 1/0\n' + ) + ++ def test_summary_should_show_carets(self): ++ # See: https://github.com/python/cpython/issues/122353 ++ ++ # statement to execute and to get a ZeroDivisionError for a traceback ++ statement = "abcdef = 1 / 0 and 2.0" ++ colno = statement.index('1 / 0') ++ end_colno = colno + len('1 / 0') ++ ++ # Actual line to use when rendering the traceback ++ # and whose AST will be extracted (it will be empty). ++ cached_line = '# this line will be used during rendering' ++ self.addCleanup(unlink, TESTFN) ++ with open(TESTFN, "w") as file: ++ file.write(cached_line) ++ linecache.updatecache(TESTFN, {}) ++ ++ try: ++ exec(compile(statement, TESTFN, "exec")) ++ except ZeroDivisionError as exc: ++ # This is the simplest way to create a StackSummary ++ # whose FrameSummary items have their column offsets. ++ s = traceback.TracebackException.from_exception(exc).stack ++ self.assertIsInstance(s, traceback.StackSummary) ++ with unittest.mock.patch.object(s, '_should_show_carets', ++ wraps=s._should_show_carets) as ff: ++ self.assertEqual(len(s), 2) ++ self.assertListEqual( ++ s.format_frame_summary(s[1]).splitlines(), ++ [ ++ f' File "{TESTFN}", line 1, in ', ++ f' {cached_line}' ++ ] ++ ) ++ ff.assert_called_with(colno, end_colno, [cached_line], None) ++ + class Unrepresentable: + def __repr__(self) -> str: + raise Exception("Unrepresentable") +diff --git a/Lib/traceback.py b/Lib/traceback.py +index 3e708c6f86..0fe7187a0c 100644 +--- a/Lib/traceback.py ++++ b/Lib/traceback.py +@@ -698,6 +698,8 @@ def _should_show_carets(self, start_offset, end_offset, all_lines, anchors): + with suppress(SyntaxError, ImportError): + import ast + tree = ast.parse('\n'.join(all_lines)) ++ if not tree.body: ++ return False + statement = tree.body[0] + value = None + def _spawns_full_line(value): +diff --git a/Misc/NEWS.d/next/Library/2024-07-23-12-38-14.gh-issue-122145.sTO8nX.rst b/Misc/NEWS.d/next/Library/2024-07-23-12-38-14.gh-issue-122145.sTO8nX.rst +new file mode 100644 +index 0000000000..a4282f12d9 +--- /dev/null ++++ b/Misc/NEWS.d/next/Library/2024-07-23-12-38-14.gh-issue-122145.sTO8nX.rst +@@ -0,0 +1,3 @@ ++Fix an issue when reporting tracebacks corresponding to Python code ++emitting an empty AST body. ++Patch by Nikita Sobolev and Bénédikt Tran. diff --git a/python3.13.spec b/python3.13.spec index 7770c7f..4b37f74 100644 --- a/python3.13.spec +++ b/python3.13.spec @@ -17,7 +17,7 @@ URL: https://www.python.org/ %global prerel rc2 %global upstream_version %{general_version}%{?prerel} Version: %{general_version}%{?prerel:~%{prerel}} -Release: 1%{?dist} +Release: 2%{?dist} License: Python-2.0.1 @@ -390,6 +390,10 @@ Source11: idle3.appdata.xml # pypa/distutils integration: https://github.com/pypa/distutils/pull/70 Patch251: 00251-change-user-install-location.patch +# 00439 # 5c3ace49fc6242246c8e54f2f904d483fb51aefa +# gh-122145: Handle an empty AST body when reporting tracebacks +Patch439: 00439-gh-122145-handle-an-empty-ast-body-when-reporting-tracebacks.patch + # (New patches go here ^^^) # # When adding new patches to "python" and "python3" in Fedora, EL, etc., @@ -1731,6 +1735,10 @@ CheckPython freethreading # ====================================================== %changelog +* Wed Sep 18 2024 Miro Hrončok - 3.13.0~rc2-2 +- Handle an empty AST body when reporting tracebacks +- Fixes: rhbz#2311907 + * Sat Sep 07 2024 Karolina Surma - 3.13.0~rc2-1 - Update to Python 3.13.0rc2