From: Thomas Kluyver Date: Wed, 15 Feb 2023 14:15:15 +0000 (+0000) Subject: Re-deprecate make_current() & clear_current() methods X-Git-Tag: v6.3.0b1~8^2~4 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fcef2a94bc93868897fe344d3604863ca60da192;p=thirdparty%2Ftornado.git Re-deprecate make_current() & clear_current() methods --- diff --git a/tornado/ioloop.py b/tornado/ioloop.py index cad985396..ce21ad44b 100644 --- a/tornado/ioloop.py +++ b/tornado/ioloop.py @@ -33,6 +33,7 @@ import sys import time import math import random +import warnings from inspect import isawaitable from tornado.concurrent import ( @@ -287,6 +288,10 @@ class IOLoop(Configurable): .. versionchanged:: 5.0 This method also sets the current `asyncio` event loop. + + .. deprecated:: 6.2 + Setting and clearing the current event loop through Tornado is + deprecated. Use ``asyncio.set_event_loop`` instead if you need this. """ # The asyncio event loops override this method. raise NotImplementedError() @@ -299,7 +304,13 @@ class IOLoop(Configurable): .. versionchanged:: 5.0 This method also clears the current `asyncio` event loop. + .. deprecated:: 6.2 """ + warnings.warn( + "clear_current is deprecated", + DeprecationWarning, + stacklevel=2, + ) IOLoop._clear_current() @staticmethod diff --git a/tornado/platform/asyncio.py b/tornado/platform/asyncio.py index 779e339c9..a3fbdc37e 100644 --- a/tornado/platform/asyncio.py +++ b/tornado/platform/asyncio.py @@ -328,6 +328,11 @@ class AsyncIOLoop(BaseAsyncIOLoop): super().close(all_fds=all_fds) def make_current(self) -> None: + warnings.warn( + "make_current is deprecated; start the event loop first", + DeprecationWarning, + stacklevel=2, + ) if not self.is_current: try: self.old_asyncio = asyncio.get_event_loop() diff --git a/tornado/testing.py b/tornado/testing.py index cadb28bec..0ef097656 100644 --- a/tornado/testing.py +++ b/tornado/testing.py @@ -190,8 +190,12 @@ class AsyncTestCase(unittest.TestCase): module=r"tornado\..*", ) super().setUp() + try: + self._prev_aio_loop = asyncio.get_event_loop() + except RuntimeError: + self._prev_aio_loop = None # type: ignore[assignment] self.io_loop = self.get_new_ioloop() - self.io_loop.make_current() + asyncio.set_event_loop(self.io_loop.asyncio_loop) # type: ignore[attr-defined] def tearDown(self) -> None: # Native coroutines tend to produce warnings if they're not @@ -226,7 +230,7 @@ class AsyncTestCase(unittest.TestCase): # Clean up Subprocess, so it can be used again with a new ioloop. Subprocess.uninitialize() - self.io_loop.clear_current() + asyncio.set_event_loop(self._prev_aio_loop) if not isinstance(self.io_loop, _NON_OWNED_IOLOOPS): # Try to clean up any file descriptors left open in the ioloop. # This avoids leaks, especially when tests are run repeatedly