From 8eb934f8449ad958162a3e38ed0fdb428d0e9eb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Hrn=C4=8Diar?= Date: Wed, 22 Feb 2023 09:33:42 +0100 Subject: [PATCH] Use asyncio.run instead of get_event_loop() Fixes: #154 --- src/tests/test.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/tests/test.py b/src/tests/test.py index be9f851..59cd721 100644 --- a/src/tests/test.py +++ b/src/tests/test.py @@ -3,7 +3,6 @@ import unittest import decimal import inspect -from asyncio import get_event_loop from collections import defaultdict, ChainMap, abc as c from decorator import dispatch_on, contextmanager, decorator try: @@ -28,9 +27,22 @@ async def before_after(coro, *args, **kwargs): return "" + (await coro(*args, **kwargs)) + "" +def asyncio_run(awaitable): + """asyncio.run for Python 3.5""" + try: + from asyncio import run + aio_run = run + except ImportError: + # Python 3.5 + from asyncio import get_event_loop + return get_event_loop().run_until_complete(awaitable) + else: + return aio_run(awaitable) + + @decorator def coro_to_func(coro, *args, **kw): - return get_event_loop().run_until_complete(coro(*args, **kw)) + return asyncio_run(coro(*args, **kw)) class CoroutineTestCase(unittest.TestCase): @@ -39,7 +51,7 @@ def test_before_after(self): async def coro(x): return x self.assertTrue(inspect.iscoroutinefunction(coro)) - out = get_event_loop().run_until_complete(coro('x')) + out = asyncio_run(coro('x')) self.assertEqual(out, 'x') def test_coro_to_func(self):