]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-102114: Make dis print more concise tracebacks for syntax errors in str inputs...
authorchgnrdv <52372310+chgnrdv@users.noreply.github.com>
Sat, 15 Apr 2023 05:53:31 +0000 (08:53 +0300)
committerGitHub <noreply@github.com>
Sat, 15 Apr 2023 05:53:31 +0000 (06:53 +0100)
Lib/dis.py
Lib/test/test_dis.py
Misc/NEWS.d/next/Library/2023-02-21-14-57-34.gh-issue-102114.uUDQzb.rst [new file with mode: 0644]

index b39b2835330135e451f20806cadc649ab662bada..6a7fcb8a1a0071c2c81ba6df14d400933cf13d71 100644 (file)
@@ -64,10 +64,10 @@ def _try_compile(source, name):
        expect code objects
     """
     try:
-        c = compile(source, name, 'eval')
+        return compile(source, name, 'eval')
     except SyntaxError:
-        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.
index 0a60a979614d5219a4ea31b49b4058472812db26..2d5c73c9adc920d6a528d0a059615243df776ab6 100644 (file)
@@ -1067,6 +1067,13 @@ class DisTests(DisTestBase):
         check(dis_nested_2, depth=None)
         check(dis_nested_2)
 
+    def test__try_compile_no_context_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):
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 (file)
index 0000000..4140c9a
--- /dev/null
@@ -0,0 +1 @@
+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.