Skip to content

gh-102114: Make dis print more concise tracebacks for syntax errors in str inputs #102115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 15, 2023
6 changes: 3 additions & 3 deletions Lib/dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,13 @@ def check(expected, **kwargs):
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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy