Commit 62c1802a authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

Port test_asyncgen to Python 3.10 (GH-4044)

The loop parameter of asyncio.sleep(), deprecated in Python 3.8, has
been removed in Python 3.10. asyncio.sleep() gets implicitly the loop
in Python 3.10 and it is safe to do the same on Python 3.6:
https://docs.python.org/dev/whatsnew/3.10.html#changes-in-the-python-api
parent dd4dfee3
......@@ -501,9 +501,9 @@ class AsyncGenAsyncioTest(unittest.TestCase):
def test_async_gen_asyncio_01(self):
async def gen():
yield 1
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
yield 2
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
return
yield 3
......@@ -513,7 +513,7 @@ class AsyncGenAsyncioTest(unittest.TestCase):
def test_async_gen_asyncio_02(self):
async def gen():
yield 1
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
yield 2
1 / ZERO
yield 3
......@@ -527,7 +527,7 @@ class AsyncGenAsyncioTest(unittest.TestCase):
class Gen:
async def __aiter__(self):
yield 1
await asyncio.sleep(0.01, loop=loop)
await asyncio.sleep(0.01)
yield 2
res = loop.run_until_complete(self.to_list(Gen()))
......@@ -536,13 +536,13 @@ class AsyncGenAsyncioTest(unittest.TestCase):
def test_async_gen_asyncio_anext_04(self):
async def foo():
yield 1
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
try:
yield 2
yield 3
except ZeroDivisionError:
yield 1000
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
yield 4
async def run1():
......@@ -693,7 +693,7 @@ class AsyncGenAsyncioTest(unittest.TestCase):
yield 1
1 / ZERO
finally:
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
yield 12
async def run():
......@@ -716,8 +716,8 @@ class AsyncGenAsyncioTest(unittest.TestCase):
yield 1
1 / ZERO
finally:
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
await asyncio.sleep(0.01)
DONE += 1
DONE += 1000
......@@ -743,8 +743,8 @@ class AsyncGenAsyncioTest(unittest.TestCase):
DONE += 1000
yield 2
finally:
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
await asyncio.sleep(0.01)
DONE += 1
DONE += 1000
......@@ -753,7 +753,7 @@ class AsyncGenAsyncioTest(unittest.TestCase):
it = gen.__aiter__()
self.assertEqual(await it.__anext__(), 1)
t = self.loop.create_task(it.__anext__())
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
await gen.aclose()
return t
......@@ -763,7 +763,7 @@ class AsyncGenAsyncioTest(unittest.TestCase):
# Silence ResourceWarnings
fut.cancel()
t.cancel()
self.loop.run_until_complete(asyncio.sleep(0.01, loop=self.loop))
self.loop.run_until_complete(asyncio.sleep(0.01))
@needs_py36_asyncio
def test_async_gen_asyncio_gc_aclose_09(self):
......@@ -775,8 +775,8 @@ class AsyncGenAsyncioTest(unittest.TestCase):
while True:
yield 1
finally:
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
await asyncio.sleep(0.01)
DONE = 1
async def run():
......@@ -785,7 +785,7 @@ class AsyncGenAsyncioTest(unittest.TestCase):
await g.__anext__()
del g
await asyncio.sleep(0.1, loop=self.loop)
await asyncio.sleep(0.1)
self.loop.run_until_complete(run())
self.assertEqual(DONE, 1)
......@@ -876,15 +876,15 @@ class AsyncGenAsyncioTest(unittest.TestCase):
async def gen():
nonlocal DONE
try:
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
v = yield 1
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
yield v * 2
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
return
finally:
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
await asyncio.sleep(0.01)
DONE = 1
async def run():
......@@ -906,21 +906,21 @@ class AsyncGenAsyncioTest(unittest.TestCase):
DONE = 0
async def sleep_n_crash(delay):
await asyncio.sleep(delay, loop=self.loop)
await asyncio.sleep(delay)
1 / ZERO
async def gen():
nonlocal DONE
try:
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
v = yield 1
await sleep_n_crash(0.01)
DONE += 1000
yield v * 2
finally:
assert sys.exc_info()[0] == ZeroDivisionError
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
await asyncio.sleep(0.01)
DONE += 1
async def run():
......@@ -939,7 +939,7 @@ class AsyncGenAsyncioTest(unittest.TestCase):
DONE = 0
async def sleep_n_crash(delay):
fut = asyncio.ensure_future(asyncio.sleep(delay, loop=self.loop),
fut = asyncio.ensure_future(asyncio.sleep(delay),
loop=self.loop)
self.loop.call_later(delay / 2, lambda: fut.cancel())
return await fut
......@@ -947,14 +947,14 @@ class AsyncGenAsyncioTest(unittest.TestCase):
async def gen():
nonlocal DONE
try:
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
v = yield 1
await sleep_n_crash(0.01)
DONE += 1000
yield v * 2
finally:
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
await asyncio.sleep(0.01)
DONE = 1
async def run():
......@@ -993,18 +993,18 @@ class AsyncGenAsyncioTest(unittest.TestCase):
async def gen():
nonlocal DONE
try:
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
try:
v = yield 1
except FooEr:
v = 1000
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
yield v * 2
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
# return
finally:
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
await asyncio.sleep(0.01)
DONE = 1
async def run():
......@@ -1029,7 +1029,7 @@ class AsyncGenAsyncioTest(unittest.TestCase):
pass
async def sleep_n_crash(delay):
fut = asyncio.ensure_future(asyncio.sleep(delay, loop=self.loop),
fut = asyncio.ensure_future(asyncio.sleep(delay),
loop=self.loop)
self.loop.call_later(delay / 2, lambda: fut.cancel())
return await fut
......@@ -1037,17 +1037,17 @@ class AsyncGenAsyncioTest(unittest.TestCase):
async def gen():
nonlocal DONE
try:
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
try:
v = yield 1
except FooEr:
await sleep_n_crash(0.01)
yield v * 2
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
# return
finally:
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
await asyncio.sleep(0.01)
DONE = 1
async def run():
......@@ -1147,10 +1147,10 @@ class AsyncGenAsyncioTest(unittest.TestCase):
async def waiter(timeout):
nonlocal finalized
try:
await asyncio.sleep(timeout, loop=self.loop)
await asyncio.sleep(timeout)
yield 1
finally:
await asyncio.sleep(0, loop=self.loop)
await asyncio.sleep(0)
finalized += 1
async def wait():
......@@ -1160,7 +1160,7 @@ class AsyncGenAsyncioTest(unittest.TestCase):
t1 = self.loop.create_task(wait())
t2 = self.loop.create_task(wait())
self.loop.run_until_complete(asyncio.sleep(0.1, loop=self.loop))
self.loop.run_until_complete(asyncio.sleep(0.1))
self.loop.run_until_complete(self.loop.shutdown_asyncgens())
self.assertEqual(finalized, 2)
......@@ -1168,7 +1168,7 @@ class AsyncGenAsyncioTest(unittest.TestCase):
# Silence warnings
t1.cancel()
t2.cancel()
self.loop.run_until_complete(asyncio.sleep(0.1, loop=self.loop))
self.loop.run_until_complete(asyncio.sleep(0.1))
@needs_py36_asyncio
def test_async_gen_asyncio_shutdown_02(self):
......@@ -1183,7 +1183,7 @@ class AsyncGenAsyncioTest(unittest.TestCase):
async def waiter(timeout):
try:
await asyncio.sleep(timeout, loop=self.loop)
await asyncio.sleep(timeout)
yield 1
finally:
1 / ZERO
......@@ -1193,7 +1193,7 @@ class AsyncGenAsyncioTest(unittest.TestCase):
pass
t = self.loop.create_task(wait())
self.loop.run_until_complete(asyncio.sleep(0.1, loop=self.loop))
self.loop.run_until_complete(asyncio.sleep(0.1))
self.loop.set_exception_handler(logger)
self.loop.run_until_complete(self.loop.shutdown_asyncgens())
......@@ -1202,7 +1202,7 @@ class AsyncGenAsyncioTest(unittest.TestCase):
# Silence warnings
t.cancel()
self.loop.run_until_complete(asyncio.sleep(0.1, loop=self.loop))
self.loop.run_until_complete(asyncio.sleep(0.1))
if __name__ == "__main__":
unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment