mirror of
https://github.com/python/cpython.git
synced 2025-12-15 21:44:50 +00:00
PEP 0492 -- Coroutines with async and await syntax. Issue #24017.
This commit is contained in:
parent
4e6bf4b3da
commit
7544508f02
72 changed files with 9261 additions and 5739 deletions
|
|
@ -2,6 +2,7 @@
|
|||
# This just tests whether the parser accepts them all.
|
||||
|
||||
from test.support import check_syntax_error
|
||||
import inspect
|
||||
import unittest
|
||||
import sys
|
||||
# testing import *
|
||||
|
|
@ -1034,6 +1035,92 @@ class GrammarTests(unittest.TestCase):
|
|||
m @= 42
|
||||
self.assertEqual(m.other, 42)
|
||||
|
||||
def test_async_await(self):
|
||||
async = 1
|
||||
await = 2
|
||||
self.assertEqual(async, 1)
|
||||
|
||||
def async():
|
||||
nonlocal await
|
||||
await = 10
|
||||
async()
|
||||
self.assertEqual(await, 10)
|
||||
|
||||
self.assertFalse(bool(async.__code__.co_flags & inspect.CO_COROUTINE))
|
||||
|
||||
async def test():
|
||||
def sum():
|
||||
async = 1
|
||||
await = 41
|
||||
return async + await
|
||||
|
||||
if 1:
|
||||
await someobj()
|
||||
|
||||
self.assertEqual(test.__name__, 'test')
|
||||
self.assertTrue(bool(test.__code__.co_flags & inspect.CO_COROUTINE))
|
||||
|
||||
def decorator(func):
|
||||
setattr(func, '_marked', True)
|
||||
return func
|
||||
|
||||
@decorator
|
||||
async def test2():
|
||||
return 22
|
||||
self.assertTrue(test2._marked)
|
||||
self.assertEqual(test2.__name__, 'test2')
|
||||
self.assertTrue(bool(test2.__code__.co_flags & inspect.CO_COROUTINE))
|
||||
|
||||
def test_async_for(self):
|
||||
class Done(Exception): pass
|
||||
|
||||
class AIter:
|
||||
async def __aiter__(self):
|
||||
return self
|
||||
async def __anext__(self):
|
||||
raise StopAsyncIteration
|
||||
|
||||
async def foo():
|
||||
async for i in AIter():
|
||||
pass
|
||||
async for i, j in AIter():
|
||||
pass
|
||||
async for i in AIter():
|
||||
pass
|
||||
else:
|
||||
pass
|
||||
raise Done
|
||||
|
||||
with self.assertRaises(Done):
|
||||
foo().send(None)
|
||||
|
||||
def test_async_with(self):
|
||||
class Done(Exception): pass
|
||||
|
||||
class manager:
|
||||
async def __aenter__(self):
|
||||
return (1, 2)
|
||||
async def __aexit__(self, *exc):
|
||||
return False
|
||||
|
||||
async def foo():
|
||||
async with manager():
|
||||
pass
|
||||
async with manager() as x:
|
||||
pass
|
||||
async with manager() as (x, y):
|
||||
pass
|
||||
async with manager(), manager():
|
||||
pass
|
||||
async with manager() as x, manager() as y:
|
||||
pass
|
||||
async with manager() as x, manager():
|
||||
pass
|
||||
raise Done
|
||||
|
||||
with self.assertRaises(Done):
|
||||
foo().send(None)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue