2016-04-13 10:27:06 -04:00
|
|
|
# test simple async with execution
|
|
|
|
|
|
|
|
class AContext:
|
|
|
|
async def __aenter__(self):
|
|
|
|
print('enter')
|
2016-10-10 21:30:32 -04:00
|
|
|
return 1
|
2016-04-13 10:27:06 -04:00
|
|
|
async def __aexit__(self, exc_type, exc, tb):
|
2016-09-27 21:52:13 -04:00
|
|
|
print('exit', exc_type, exc)
|
2016-04-13 10:27:06 -04:00
|
|
|
|
|
|
|
async def f():
|
|
|
|
async with AContext():
|
|
|
|
print('body')
|
|
|
|
|
|
|
|
o = f()
|
|
|
|
try:
|
|
|
|
o.send(None)
|
|
|
|
except StopIteration:
|
|
|
|
print('finished')
|
2016-09-27 21:52:13 -04:00
|
|
|
|
|
|
|
async def g():
|
2016-10-10 21:30:32 -04:00
|
|
|
async with AContext() as ac:
|
|
|
|
print(ac)
|
2016-09-27 21:52:13 -04:00
|
|
|
raise ValueError('error')
|
|
|
|
|
|
|
|
o = g()
|
|
|
|
try:
|
|
|
|
o.send(None)
|
|
|
|
except ValueError:
|
|
|
|
print('ValueError')
|
2021-04-22 20:55:39 -04:00
|
|
|
|
|
|
|
# test raising BaseException to make sure it is handled by the async-with
|
|
|
|
async def h():
|
|
|
|
async with AContext():
|
|
|
|
raise BaseException
|
|
|
|
o = h()
|
|
|
|
try:
|
|
|
|
o.send(None)
|
|
|
|
except BaseException:
|
|
|
|
print('BaseException')
|