From 271917994138714ec76251194ecbfff49383a611 Mon Sep 17 00:00:00 2001 From: chgnrdv Date: Tue, 21 Feb 2023 16:45:52 +0300 Subject: [PATCH 1/8] Make _try_compile print shorter traceback This commit reworks code of '_try_compile' function so that it print shorter traceback if its 'source' argument contains indentation/syntax error --- Lib/dis.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/dis.py b/Lib/dis.py index 9edde6ae8258da..bccaedf53e1926 100644 --- a/Lib/dis.py +++ b/Lib/dis.py @@ -66,6 +66,8 @@ def _try_compile(source, name): try: c = compile(source, name, 'eval') except SyntaxError: + c = None + if not c: c = compile(source, name, 'exec') return c From 2f29d43a55c508e6e7be324420cd4631d417b7c3 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 21 Feb 2023 14:32:15 +0000 Subject: [PATCH 2/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-02-21-14-32-13.gh-issue-102114.CXyyFV.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-02-21-14-32-13.gh-issue-102114.CXyyFV.rst diff --git a/Misc/NEWS.d/next/Library/2023-02-21-14-32-13.gh-issue-102114.CXyyFV.rst b/Misc/NEWS.d/next/Library/2023-02-21-14-32-13.gh-issue-102114.CXyyFV.rst new file mode 100644 index 00000000000000..8b54e1c7e16836 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-02-21-14-32-13.gh-issue-102114.CXyyFV.rst @@ -0,0 +1 @@ +Functions from :module:`dis` module that accept source code string as argument now print shorter traceback if given string contains syntax/indentation error From ca52d731b6c3259f327839d8351e8070608723aa Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 21 Feb 2023 14:57:36 +0000 Subject: [PATCH 3/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-02-21-14-57-34.gh-issue-102114.uUDQzb.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-02-21-14-57-34.gh-issue-102114.uUDQzb.rst diff --git a/Misc/NEWS.d/next/Library/2023-02-21-14-57-34.gh-issue-102114.uUDQzb.rst b/Misc/NEWS.d/next/Library/2023-02-21-14-57-34.gh-issue-102114.uUDQzb.rst new file mode 100644 index 00000000000000..1531b18f0d0017 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-02-21-14-57-34.gh-issue-102114.uUDQzb.rst @@ -0,0 +1 @@ +Functions from :mod:`dis` module that accept source code string as argument now print shorter traceback if given string contains syntax/indentation error From e652f89e8c6360576eb33bfce30e93c689731d40 Mon Sep 17 00:00:00 2001 From: chgnrdv <52372310+chgnrdv@users.noreply.github.com> Date: Tue, 21 Feb 2023 17:59:26 +0300 Subject: [PATCH 4/8] remove old NEWS entry file --- .../next/Library/2023-02-21-14-32-13.gh-issue-102114.CXyyFV.rst | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Misc/NEWS.d/next/Library/2023-02-21-14-32-13.gh-issue-102114.CXyyFV.rst diff --git a/Misc/NEWS.d/next/Library/2023-02-21-14-32-13.gh-issue-102114.CXyyFV.rst b/Misc/NEWS.d/next/Library/2023-02-21-14-32-13.gh-issue-102114.CXyyFV.rst deleted file mode 100644 index 8b54e1c7e16836..00000000000000 --- a/Misc/NEWS.d/next/Library/2023-02-21-14-32-13.gh-issue-102114.CXyyFV.rst +++ /dev/null @@ -1 +0,0 @@ -Functions from :module:`dis` module that accept source code string as argument now print shorter traceback if given string contains syntax/indentation error From 413b6818b9ba050fd05a55f804a7eef36db17818 Mon Sep 17 00:00:00 2001 From: Radislav Chugunov Date: Fri, 14 Apr 2023 20:31:14 +0300 Subject: [PATCH 5/8] added test --- Lib/test/test_dis.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index 9086824dd6f40c..8b27da4f9fd5d6 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -1065,6 +1065,13 @@ def check(expected, **kwargs): check(dis_nested_2, depth=None) check(dis_nested_2) + def test__try_compile_no_another_exc_on_error(self): + # see gh-102114 + try: + dis._try_compile(")", "") + except Exception as e: + self.assertIsNone(e.__context__) + @staticmethod def code_quicken(f, times=ADAPTIVE_WARMUP_DELAY): for _ in range(times): From 35a687ece2d8fb3abd7a105eeeb3df4ecea62e63 Mon Sep 17 00:00:00 2001 From: chgnrdv <52372310+chgnrdv@users.noreply.github.com> Date: Fri, 14 Apr 2023 21:02:25 +0300 Subject: [PATCH 6/8] Update Lib/test/test_dis.py Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- Lib/test/test_dis.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index 8b27da4f9fd5d6..1a0f4ac164a498 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -1065,7 +1065,7 @@ def check(expected, **kwargs): check(dis_nested_2, depth=None) check(dis_nested_2) - def test__try_compile_no_another_exc_on_error(self): + def test__try_compile_no_context_exc_on_error(self): # see gh-102114 try: dis._try_compile(")", "") From 0a97e857459ce90f9a08e4317278db9dcbd65e12 Mon Sep 17 00:00:00 2001 From: chgnrdv <52372310+chgnrdv@users.noreply.github.com> Date: Fri, 14 Apr 2023 21:31:58 +0300 Subject: [PATCH 7/8] Update 2023-02-21-14-57-34.gh-issue-102114.uUDQzb.rst --- .../next/Library/2023-02-21-14-57-34.gh-issue-102114.uUDQzb.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-02-21-14-57-34.gh-issue-102114.uUDQzb.rst b/Misc/NEWS.d/next/Library/2023-02-21-14-57-34.gh-issue-102114.uUDQzb.rst index 1531b18f0d0017..4140c9a96cd272 100644 --- a/Misc/NEWS.d/next/Library/2023-02-21-14-57-34.gh-issue-102114.uUDQzb.rst +++ b/Misc/NEWS.d/next/Library/2023-02-21-14-57-34.gh-issue-102114.uUDQzb.rst @@ -1 +1 @@ -Functions from :mod:`dis` module that accept source code string as argument now print shorter traceback if given string contains syntax/indentation error +Functions in the :mod:`dis` module that accept a source code string as argument now print a more concise traceback when the string contains a syntax or indentation error. From 5ff58bbe9ec634204b2925216d50b17d0fc0029a Mon Sep 17 00:00:00 2001 From: Radislav Chugunov Date: Fri, 14 Apr 2023 22:54:33 +0300 Subject: [PATCH 8/8] get rid of 'c' variable --- Lib/dis.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Lib/dis.py b/Lib/dis.py index 310ed994d54505..6a7fcb8a1a0071 100644 --- a/Lib/dis.py +++ b/Lib/dis.py @@ -64,12 +64,10 @@ def _try_compile(source, name): expect code objects """ try: - c = compile(source, name, 'eval') + return compile(source, name, 'eval') except SyntaxError: - c = None - if not c: - c = compile(source, name, 'exec') - return c + pass + return compile(source, name, 'exec') def dis(x=None, *, file=None, depth=None, show_caches=False, adaptive=False): """Disassemble classes, methods, functions, and other compiled objects.