From: Ben Darnell Date: Tue, 28 Dec 2021 19:52:52 +0000 (-0500) Subject: asyncio: Avoid deprecation warning in start on py310 X-Git-Tag: v6.2.0b1~36^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2cecd60353377d20a1a67d6fb646ccdc24b7ba91;p=thirdparty%2Ftornado.git asyncio: Avoid deprecation warning in start on py310 Also avoid deprecated asyncio.get_event_loop in asyncio_test. --- diff --git a/tornado/platform/asyncio.py b/tornado/platform/asyncio.py index a983384ff..a244777a3 100644 --- a/tornado/platform/asyncio.py +++ b/tornado/platform/asyncio.py @@ -191,7 +191,9 @@ class BaseAsyncIOLoop(IOLoop): def start(self) -> None: try: - old_loop = asyncio.get_event_loop() + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + old_loop = asyncio.get_event_loop() except (RuntimeError, AssertionError): old_loop = None # type: ignore try: diff --git a/tornado/test/asyncio_test.py b/tornado/test/asyncio_test.py index e4db2995b..a6d36ff27 100644 --- a/tornado/test/asyncio_test.py +++ b/tornado/test/asyncio_test.py @@ -32,7 +32,10 @@ class AsyncIOLoopTest(AsyncTestCase): def test_asyncio_callback(self): # Basic test that the asyncio loop is set up correctly. - asyncio.get_event_loop().call_soon(self.stop) + async def add_callback(): + asyncio.get_event_loop().call_soon(self.stop) + + self.asyncio_loop.run_until_complete(add_callback()) self.wait() @gen_test @@ -90,21 +93,15 @@ class AsyncIOLoopTest(AsyncTestCase): # Asyncio only supports coroutines that yield asyncio-compatible # Futures (which our Future is since 5.0). self.assertEqual( - asyncio.get_event_loop().run_until_complete( - native_coroutine_without_adapter() - ), + self.asyncio_loop.run_until_complete(native_coroutine_without_adapter()), 42, ) self.assertEqual( - asyncio.get_event_loop().run_until_complete( - native_coroutine_with_adapter() - ), + self.asyncio_loop.run_until_complete(native_coroutine_with_adapter()), 42, ) self.assertEqual( - asyncio.get_event_loop().run_until_complete( - native_coroutine_with_adapter2() - ), + self.asyncio_loop.run_until_complete(native_coroutine_with_adapter2()), 42, ) @@ -119,7 +116,7 @@ class LeakTest(unittest.TestCase): asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy()) def tearDown(self): - asyncio.get_event_loop().close() + asyncio.get_event_loop_policy().get_event_loop().close() asyncio.set_event_loop_policy(self.orig_policy) def test_ioloop_close_leak(self):