bpo-42908: Mark cleanup code at end of try-except and with artificial… · python/cpython@3bd6035 · GitHub
Skip to content

Commit 3bd6035

Browse files
authored
bpo-42908: Mark cleanup code at end of try-except and with artificial (#24202)
* Mark bytecodes at end of try-except as artificial. * Make sure that the CFG is consistent throughout optimiization. * Extend line-number propagation logic so that implicit returns after 'try-except' or 'with' have the correct line numbers. * Update importlib
1 parent 2396614 commit 3bd6035

6 files changed

Lines changed: 4473 additions & 4359 deletions

File tree

Lib/test/test_dis.py

Lines changed: 1 addition & 1 deletion

Lib/test/test_sys_settrace.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,46 @@ def func():
916916
(7, 'line'),
917917
(7, 'return')])
918918

919+
def test_if_false_in_with(self):
920+
921+
class C:
922+
def __enter__(self):
923+
return self
924+
def __exit__(*args):
925+
pass
926+
927+
def func():
928+
with C():
929+
if False:
930+
pass
931+
932+
self.run_and_compare(func,
933+
[(0, 'call'),
934+
(1, 'line'),
935+
(-5, 'call'),
936+
(-4, 'line'),
937+
(-4, 'return'),
938+
(2, 'line'),
939+
(-3, 'call'),
940+
(-2, 'line'),
941+
(-2, 'return'),
942+
(2, 'return')])
943+
944+
def test_if_false_in_try_except(self):
945+
946+
def func():
947+
try:
948+
if False:
949+
pass
950+
except Exception:
951+
X
952+
953+
self.run_and_compare(func,
954+
[(0, 'call'),
955+
(1, 'line'),
956+
(2, 'line'),
957+
(2, 'return')])
958+
919959

920960
class SkipLineEventsTraceTestCase(TraceTestCase):
921961
"""Repeat the trace tests, but with per-line events skipped"""

Python/compile.c

Lines changed: 104 additions & 31 deletions

0 commit comments

Comments
 (0)