1
+ import sys
1
2
import asyncio
2
3
import unittest
3
4
5
+
4
6
class ContextManager :
5
7
async def __aenter__ (self ):
6
8
print ("Entrada" )
@@ -41,15 +43,18 @@ async def a(s, m):
41
43
async for i in AIterWrap (range (0 , 2 )):
42
44
print (i )
43
45
ls .append (m )
44
- await asyncio .sleep (1 )
46
+ await asyncio .sleep (0.1 )
47
+
48
+
49
+ async def main ():
50
+ tasks = [
51
+ asyncio .create_task (c )
52
+ for c in [a (0 , "hello1" ), a (0.1 , "hello2" ), a (0.2 , "hello3" ), a (0.3 , "hello4" )]
53
+ ]
54
+ await asyncio .wait (tasks )
45
55
46
56
47
- loop = asyncio .get_event_loop ()
48
- loop .run_until_complete (
49
- asyncio .wait (
50
- [a (0 , "hello1" ), a (0.75 , "hello2" ), a (1.5 , "hello3" ), a (2.25 , "hello4" )]
51
- )
52
- )
57
+ asyncio .run (main (), debug = True )
53
58
54
59
55
60
assert ls == [
@@ -72,41 +77,43 @@ async def a(s, m):
72
77
]
73
78
74
79
75
- class TestAsyncWith (unittest .TestCase ):
76
- def testAenterAttributeError1 (self ):
77
- class LacksAenter (object ):
78
- async def __aexit__ (self , * exc ):
79
- pass
80
+ if sys .version_info < (3 , 11 , 0 ):
80
81
81
- async def foo ():
82
- async with LacksAenter ():
83
- pass
84
-
85
- with self .assertRaisesRegex (AttributeError , '__aenter__' ):
86
- foo ().send (None )
82
+ class TestAsyncWith (unittest .TestCase ):
83
+ def testAenterAttributeError1 (self ):
84
+ class LacksAenter (object ):
85
+ async def __aexit__ (self , * exc ):
86
+ pass
87
87
88
- def testAenterAttributeError2 ( self ):
89
- class LacksAenterAndAexit ( object ):
90
- pass
88
+ async def foo ( ):
89
+ async with LacksAenter ( ):
90
+ pass
91
91
92
- async def foo ():
93
- async with LacksAenterAndAexit ():
92
+ with self .assertRaisesRegex (AttributeError , "__aenter__" ):
93
+ foo ().send (None )
94
+
95
+ def testAenterAttributeError2 (self ):
96
+ class LacksAenterAndAexit (object ):
94
97
pass
95
98
96
- with self .assertRaisesRegex (AttributeError , '__aenter__' ):
97
- foo ().send (None )
99
+ async def foo ():
100
+ async with LacksAenterAndAexit ():
101
+ pass
98
102
99
- def testAexitAttributeError (self ):
100
- class LacksAexit (object ):
101
- async def __aenter__ (self ):
102
- pass
103
+ with self .assertRaisesRegex (AttributeError , "__aenter__" ):
104
+ foo ().send (None )
103
105
104
- async def foo ():
105
- async with LacksAexit ():
106
- pass
107
-
108
- with self .assertRaisesRegex (AttributeError , '__aexit__' ):
109
- foo ().send (None )
106
+ def testAexitAttributeError (self ):
107
+ class LacksAexit (object ):
108
+ async def __aenter__ (self ):
109
+ pass
110
+
111
+ async def foo ():
112
+ async with LacksAexit ():
113
+ pass
114
+
115
+ with self .assertRaisesRegex (AttributeError , "__aexit__" ):
116
+ foo ().send (None )
110
117
111
118
112
119
if __name__ == "__main__" :
0 commit comments