mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
bpo-31708: Allow async generator expressions in synchronous functions (#3905)
This commit is contained in:
parent
faa135acbf
commit
b8ab9d3fc8
5 changed files with 52 additions and 9 deletions
|
@ -1037,5 +1037,37 @@ class AsyncGenAsyncioTest(unittest.TestCase):
|
|||
t.cancel()
|
||||
self.loop.run_until_complete(asyncio.sleep(0.1, loop=self.loop))
|
||||
|
||||
def test_async_gen_expression_01(self):
|
||||
async def arange(n):
|
||||
for i in range(n):
|
||||
await asyncio.sleep(0.01, loop=self.loop)
|
||||
yield i
|
||||
|
||||
def make_arange(n):
|
||||
# This syntax is legal starting with Python 3.7
|
||||
return (i * 2 async for i in arange(n))
|
||||
|
||||
async def run():
|
||||
return [i async for i in make_arange(10)]
|
||||
|
||||
res = self.loop.run_until_complete(run())
|
||||
self.assertEqual(res, [i * 2 for i in range(10)])
|
||||
|
||||
def test_async_gen_expression_02(self):
|
||||
async def wrap(n):
|
||||
await asyncio.sleep(0.01, loop=self.loop)
|
||||
return n
|
||||
|
||||
def make_arange(n):
|
||||
# This syntax is legal starting with Python 3.7
|
||||
return (i * 2 for i in range(n) if await wrap(i))
|
||||
|
||||
async def run():
|
||||
return [i async for i in make_arange(10)]
|
||||
|
||||
res = self.loop.run_until_complete(run())
|
||||
self.assertEqual(res, [i * 2 for i in range(1, 10)])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue