Skip to content

Commit 86f907e

Browse files
committed
Fix SetupAsyncWith stack level handling
1 parent ce2d81e commit 86f907e

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

bytecode/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,13 @@ impl Instruction {
903903
Reverse { .. } => 0,
904904
GetAwaitable => 0,
905905
BeforeAsyncWith => 1,
906-
SetupAsyncWith { .. } => 0,
906+
SetupAsyncWith { .. } => {
907+
if jump {
908+
-1
909+
} else {
910+
0
911+
}
912+
}
907913
GetAIter => 0,
908914
GetANext => 1,
909915
EndAsyncFor => -1,

compiler/src/compile.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2563,6 +2563,23 @@ if True and False and False:
25632563
"\
25642564
if (True and False) or (False and True):
25652565
pass
2566+
"
2567+
));
2568+
}
2569+
2570+
#[test]
2571+
fn test_nested_double_async_with() {
2572+
assert_dis_snapshot!(compile_exec(
2573+
"\
2574+
for stop_exc in (StopIteration('spam'), StopAsyncIteration('ham')):
2575+
with self.subTest(type=type(stop_exc)):
2576+
try:
2577+
async with woohoo():
2578+
raise stop_exc
2579+
except Exception as ex:
2580+
self.assertIs(ex, stop_exc)
2581+
else:
2582+
self.fail(f'{stop_exc} was suppressed')
25662583
"
25672584
));
25682585
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
source: compiler/src/compile.rs
3+
expression: "compile_exec(\"\\\nfor stop_exc in (StopIteration('spam'), StopAsyncIteration('ham')):\n with self.subTest(type=type(stop_exc)):\n try:\n async with woohoo():\n raise stop_exc\n except Exception as ex:\n self.assertIs(ex, stop_exc)\n else:\n self.fail(f'{stop_exc} was suppressed')\n\")"
4+
---
5+
0 SetupLoop
6+
1 LoadNameAny (0, StopIteration)
7+
2 LoadConst ("spam")
8+
3 CallFunctionPositional (1)
9+
4 LoadNameAny (1, StopAsyncIteration)
10+
5 LoadConst ("ham")
11+
6 CallFunctionPositional (1)
12+
7 BuildTuple (2, false)
13+
8 GetIter
14+
>> 9 ForIter (68)
15+
10 StoreLocal (2, stop_exc)
16+
11 LoadNameAny (3, self)
17+
12 LoadAttr (subTest)
18+
13 LoadNameAny (5, type)
19+
14 LoadNameAny (2, stop_exc)
20+
15 CallFunctionPositional (1)
21+
16 LoadConst (("type"))
22+
17 CallFunctionKeyword (1)
23+
18 SetupWith (65)
24+
19 Pop
25+
20 SetupExcept (40)
26+
21 LoadNameAny (6, woohoo)
27+
22 CallFunctionPositional (0)
28+
23 BeforeAsyncWith
29+
24 GetAwaitable
30+
25 LoadConst (None)
31+
26 YieldFrom
32+
27 SetupAsyncWith (33)
33+
28 Pop
34+
29 LoadNameAny (2, stop_exc)
35+
30 Raise (Raise)
36+
31 PopBlock
37+
32 EnterFinally
38+
>> 33 WithCleanupStart
39+
34 GetAwaitable
40+
35 LoadConst (None)
41+
36 YieldFrom
42+
37 WithCleanupFinish
43+
38 PopBlock
44+
39 Jump (54)
45+
>> 40 Duplicate
46+
41 LoadNameAny (7, Exception)
47+
42 CompareOperation (ExceptionMatch)
48+
43 JumpIfFalse (53)
49+
44 StoreLocal (8, ex)
50+
45 LoadNameAny (3, self)
51+
46 LoadAttr (assertIs)
52+
47 LoadNameAny (8, ex)
53+
48 LoadNameAny (2, stop_exc)
54+
49 CallFunctionPositional (2)
55+
50 Pop
56+
51 PopException
57+
52 Jump (63)
58+
>> 53 Raise (Reraise)
59+
>> 54 LoadNameAny (3, self)
60+
55 LoadAttr (fail)
61+
56 LoadConst ("")
62+
57 LoadNameAny (2, stop_exc)
63+
58 FormatValue (None)
64+
59 LoadConst (" was suppressed")
65+
60 BuildString (2)
66+
61 CallFunctionPositional (1)
67+
62 Pop
68+
>> 63 PopBlock
69+
64 EnterFinally
70+
>> 65 WithCleanupStart
71+
66 WithCleanupFinish
72+
67 Jump (9)
73+
>> 68 PopBlock
74+
69 LoadConst (None)
75+
70 ReturnValue
76+

vm/src/frame.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,9 @@ impl ExecutingFrame<'_> {
803803
Ok(None)
804804
}
805805
bytecode::Instruction::SetupAsyncWith { end } => {
806+
let enter_res = self.pop_value();
806807
self.push_block(BlockType::Finally { handler: *end });
808+
self.push_value(enter_res);
807809
Ok(None)
808810
}
809811
bytecode::Instruction::WithCleanupStart => {

0 commit comments

Comments
 (0)