Skip to content

Commit ebed728

Browse files
authored
Merge pull request #3889 from daeun503/fix-with-test
Fix testEnterAttributeError2 in test_with.py
2 parents 256bbef + c8e599e commit ebed728

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

Lib/test/test_with.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ def fooLacksEnter():
119119
with foo: pass
120120
self.assertRaisesRegex(AttributeError, '__enter__', fooLacksEnter)
121121

122-
# TODO: RUSTPYTHON
123-
@unittest.expectedFailure
124122
def testEnterAttributeError2(self):
125123
class LacksEnterAndExit(object):
126124
pass

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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -775,20 +775,23 @@ impl ExecutingFrame<'_> {
775775
}
776776
bytecode::Instruction::SetupWith { end } => {
777777
let context_manager = self.pop_value();
778+
let enter_res = vm.call_special_method(
779+
context_manager.clone(),
780+
identifier!(vm, __enter__),
781+
(),
782+
)?;
778783
let exit = context_manager.get_attr(identifier!(vm, __exit__), vm)?;
779784
self.push_value(exit);
780-
// Call enter:
781-
let enter_res =
782-
vm.call_special_method(context_manager, identifier!(vm, __enter__), ())?;
783785
self.push_block(BlockType::Finally { handler: *end });
784786
self.push_value(enter_res);
785787
Ok(None)
786788
}
787789
bytecode::Instruction::BeforeAsyncWith => {
788790
let mgr = self.pop_value();
791+
let aenter_res =
792+
vm.call_special_method(mgr.clone(), identifier!(vm, __aenter__), ())?;
789793
let aexit = mgr.get_attr(identifier!(vm, __aexit__), vm)?;
790794
self.push_value(aexit);
791-
let aenter_res = vm.call_special_method(mgr, identifier!(vm, __aenter__), ())?;
792795
self.push_value(aenter_res);
793796

794797
Ok(None)

0 commit comments

Comments
 (0)