Skip to content

Commit fd5d1e7

Browse files
committed
Use case body location for POP_TOP after case test, to give better locations for BRANCH events.
1 parent a083633 commit fd5d1e7

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

Lib/test/test_monitoring.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,6 +1681,37 @@ async def foo():
16811681
('branch left', 'func', 12, 12)])
16821682

16831683

1684+
def test_async_for(self):
1685+
1686+
def func(v=1):
1687+
x = 0
1688+
for v in range(4):
1689+
match v:
1690+
case 1:
1691+
x += 1
1692+
case 2:
1693+
x += 2
1694+
case 3:
1695+
x += 3
1696+
return x
1697+
1698+
self.check_events(func, recorders = BRANCHES_RECORDERS, expected = [
1699+
('branch left', 'func', 2, 2),
1700+
('branch right', 'func', 4, 6),
1701+
('branch right', 'func', 6, 8),
1702+
('branch left', 'func', 8, 8),
1703+
('branch left', 'func', 2, 2),
1704+
('branch left', 'func', 4, 5),
1705+
('branch left', 'func', 2, 2),
1706+
('branch right', 'func', 4, 6),
1707+
('branch left', 'func', 6, 7),
1708+
('branch left', 'func', 2, 2),
1709+
('branch right', 'func', 4, 6),
1710+
('branch right', 'func', 6, 8),
1711+
('branch right', 'func', 8, 9),
1712+
('branch right', 'func', 2, 10)])
1713+
1714+
16841715
class TestBranchConsistency(MonitoringTestBase, unittest.TestCase):
16851716

16861717
def check_branches(self, func, tool=TEST_TOOL, recorders=BRANCH_OFFSET_RECORDERS):

Python/codegen.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6121,7 +6121,9 @@ codegen_match_inner(compiler *c, stmt_ty s, pattern_context *pc)
61216121
}
61226122
// Success! Pop the subject off, we're done with it:
61236123
if (i != cases - has_default - 1) {
6124-
ADDOP(c, LOC(m->pattern), POP_TOP);
6124+
/* Use the body location to give better locations for branch events */
6125+
assert(asdl_seq_LEN(m->body) > 0);
6126+
ADDOP(c, LOC(asdl_seq_GET(m->body, 0)), POP_TOP);
61256127
}
61266128
VISIT_SEQ(c, stmt, m->body);
61276129
ADDOP_JUMP(c, NO_LOCATION, JUMP, end);

0 commit comments

Comments
 (0)