Skip to content

Commit c8e599e

Browse files
committed
Fix BeforeAsyncWith
1 parent a563d07 commit c8e599e

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

extra_tests/snippets/syntax_async.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import asyncio
2-
2+
import unittest
33

44
class ContextManager:
55
async def __aenter__(self):
@@ -70,3 +70,44 @@ async def a(s, m):
7070
"hello3",
7171
"hello4",
7272
]
73+
74+
75+
class TestAsyncWith(unittest.TestCase):
76+
def testAenterAttributeError1(self):
77+
class LacksAenter(object):
78+
async def __aexit__(self, *exc):
79+
pass
80+
81+
async def foo():
82+
async with LacksAenter():
83+
pass
84+
85+
with self.assertRaisesRegex(AttributeError, '__aenter__'):
86+
foo().send(None)
87+
88+
def testAenterAttributeError2(self):
89+
class LacksAenterAndAexit(object):
90+
pass
91+
92+
async def foo():
93+
async with LacksAenterAndAexit():
94+
pass
95+
96+
with self.assertRaisesRegex(AttributeError, '__aenter__'):
97+
foo().send(None)
98+
99+
def testAexitAttributeError(self):
100+
class LacksAexit(object):
101+
async def __aenter__(self):
102+
pass
103+
104+
async def foo():
105+
async with LacksAexit():
106+
pass
107+
108+
with self.assertRaisesRegex(AttributeError, '__aexit__'):
109+
foo().send(None)
110+
111+
112+
if __name__ == "__main__":
113+
unittest.main()

vm/src/frame.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,9 +788,10 @@ impl ExecutingFrame<'_> {
788788
}
789789
bytecode::Instruction::BeforeAsyncWith => {
790790
let mgr = self.pop_value();
791+
let aenter_res =
792+
vm.call_special_method(mgr.clone(), identifier!(vm, __aenter__), ())?;
791793
let aexit = mgr.get_attr(identifier!(vm, __aexit__), vm)?;
792794
self.push_value(aexit);
793-
let aenter_res = vm.call_special_method(mgr, identifier!(vm, __aenter__), ())?;
794795
self.push_value(aenter_res);
795796

796797
Ok(None)

0 commit comments

Comments
 (0)