Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,15 +474,15 @@ 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
LOAD_COMMON_CONSTANT 7 (None)
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)
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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)])

Expand Down
50 changes: 50 additions & 0 deletions Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
18 changes: 18 additions & 0 deletions Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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);
Expand Down
Loading