Skip to content
2 changes: 2 additions & 0 deletions Lib/dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

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 @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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