diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index 709fa374704f1f5..49ba244c079a3c1 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -474,7 +474,7 @@ def foo(a: int, b: str) -> str: NOT_TAKEN STORE_FAST 0 (e) -%4d L4: LOAD_FAST 0 (e) +%4d L4: LOAD_FAST_BORROW 0 (e) LOAD_ATTR 2 (__traceback__) STORE_FAST 1 (tb) L5: POP_EXCEPT @@ -482,7 +482,7 @@ def foo(a: int, b: str) -> str: STORE_FAST 0 (e) DELETE_FAST 0 (e) -%4d LOAD_FAST 1 (tb) +%4d LOAD_FAST_BORROW 1 (tb) RETURN_VALUE -- L6: LOAD_COMMON_CONSTANT 7 (None) @@ -731,7 +731,7 @@ def _tryfinallyconst(b): -- L3: PUSH_EXC_INFO -%4d LOAD_FAST 1 (b) +%4d LOAD_FAST_BORROW 1 (b) PUSH_NULL CALL 0 POP_TOP diff --git a/Lib/test/test_monitoring.py b/Lib/test/test_monitoring.py index 24cc51f4b4053ab..50b6bb5421bfdab 100644 --- a/Lib/test/test_monitoring.py +++ b/Lib/test/test_monitoring.py @@ -2029,8 +2029,8 @@ def func(): ('branch', 'func', 4, 4), ('line', 'func', 5), ('line', 'meth', 1), - ('jump', 'func', 5, '[offset=122]'), - ('branch', 'func', '[offset=126]', '[offset=132]'), + ('jump', 'func', 5, '[offset=124]'), + ('branch', 'func', '[offset=128]', '[offset=134]'), ('line', 'get_events', 11)]) self.check_events(func, recorders = FLOW_AND_LINE_RECORDERS, expected = [ @@ -2044,8 +2044,8 @@ def func(): ('line', 'func', 5), ('line', 'meth', 1), ('return', 'meth', None), - ('jump', 'func', 5, '[offset=122]'), - ('branch', 'func', '[offset=126]', '[offset=132]'), + ('jump', 'func', 5, '[offset=124]'), + ('branch', 'func', '[offset=128]', '[offset=134]'), ('return', 'func', None), ('line', 'get_events', 11)]) diff --git a/Lib/test/test_peepholer.py b/Lib/test/test_peepholer.py index fa8b5ceebb9f328..46721b64ffacafd 100644 --- a/Lib/test/test_peepholer.py +++ b/Lib/test/test_peepholer.py @@ -1156,6 +1156,56 @@ def f(self): print(heapify_max) self.assertInBytecode(f, "LOAD_FAST_BORROW", "self") + def test_load_fast_borrow_in_exception_handlers(self): + def f(x, g, h): + try: + return g(x) + except Exception: + return h(x) + self.assertNotInBytecode(f, "LOAD_FAST") + self.assertInBytecode(f, "LOAD_FAST_BORROW", "h") + + def g(x, k): + try: + k() + finally: + k(x) + self.assertNotInBytecode(g, "LOAD_FAST") + self.assertInBytecode(g, "LOAD_FAST_BORROW", "x") + + def h(x, k): + try: + k() + except ValueError: + try: + k(x) + except TypeError: + k(x, x) + self.assertNotInBytecode(h, "LOAD_FAST") + + async def coro(it, k): + async for v in it: + k(v) + self.assertNotInBytecode(coro, "LOAD_FAST") + + def killed(x, k): + try: + k() + except ValueError: + # x is rebound while its reference is still on the stack + k(x, (x := None)) + self.assertInBytecode(killed, "LOAD_FAST", "x") + self.assertNotInBytecode(killed, "LOAD_FAST_BORROW", "x") + + def stored(x, k): + try: + k() + except ValueError: + k(x) + y = x + self.assertInBytecode(stored, "LOAD_FAST_BORROW", "x") + self.assertInBytecode(stored, "LOAD_FAST", "x") + class DirectCfgOptimizerTests(CfgOptimizationTestCase): def test_optimize_cfg_const_index_out_of_range(self): diff --git a/Python/flowgraph.c b/Python/flowgraph.c index 7b8a3e9cf9b57bd..aa929548f4c2e74 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -2954,6 +2954,13 @@ optimize_load_fast(cfg_builder *g) int opcode = instr->i_opcode; int oparg = instr->i_oparg; assert(opcode != EXTENDED_ARG); + if (instr->i_except != NULL) { + // Exception handlers are only reachable through the + // exception edges once the SETUP_* pseudo-instructions + // have been converted, so make sure we analyze them too. + basicblock *handler = instr->i_except; + load_fast_push_block(&sp, handler, handler->b_startdepth); + } switch (opcode) { // Opcodes that load and store locals case DELETE_FAST: { @@ -3061,6 +3068,17 @@ optimize_load_fast(cfg_builder *g) break; } + case END_ASYNC_FOR: { + // The target of END_ASYNC_FOR is only used for + // monitoring, it is not a jump (see + // calculate_stackdepth()). + assert(_PyOpcode_num_popped(opcode, oparg) == 2); + assert(_PyOpcode_num_pushed(opcode, oparg) == 0); + ref_stack_pop(&refs); + ref_stack_pop(&refs); + break; + } + case END_SEND: { assert(_PyOpcode_num_popped(opcode, oparg) == 3); assert(_PyOpcode_num_pushed(opcode, oparg) == 1);