File tree Expand file tree Collapse file tree 2 files changed +44
-2
lines changed Expand file tree Collapse file tree 2 files changed +44
-2
lines changed Original file line number Diff line number Diff line change 1
1
import asyncio
2
-
2
+ import unittest
3
3
4
4
class ContextManager :
5
5
async def __aenter__ (self ):
@@ -70,3 +70,44 @@ async def a(s, m):
70
70
"hello3" ,
71
71
"hello4" ,
72
72
]
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 ()
Original file line number Diff line number Diff line change @@ -788,9 +788,10 @@ impl ExecutingFrame<'_> {
788
788
}
789
789
bytecode:: Instruction :: BeforeAsyncWith => {
790
790
let mgr = self . pop_value ( ) ;
791
+ let aenter_res =
792
+ vm. call_special_method ( mgr. clone ( ) , identifier ! ( vm, __aenter__) , ( ) ) ?;
791
793
let aexit = mgr. get_attr ( identifier ! ( vm, __aexit__) , vm) ?;
792
794
self . push_value ( aexit) ;
793
- let aenter_res = vm. call_special_method ( mgr, identifier ! ( vm, __aenter__) , ( ) ) ?;
794
795
self . push_value ( aenter_res) ;
795
796
796
797
Ok ( None )
You can’t perform that action at this time.
0 commit comments