]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Re-deprecate make_current() & clear_current() methods
authorThomas Kluyver <thomas@kluyver.me.uk>
Wed, 15 Feb 2023 14:15:15 +0000 (14:15 +0000)
committerThomas Kluyver <thomas@kluyver.me.uk>
Wed, 15 Feb 2023 14:19:59 +0000 (14:19 +0000)
tornado/ioloop.py
tornado/platform/asyncio.py
tornado/testing.py

index cad98539671247133c333e4f90ddaf029962aa3b..ce21ad44bd5a2b016eeb9f7680865bacba4bc93e 100644 (file)
@@ -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
index 779e339c97fe242bfab13f4679cb1d858bee1f4d..a3fbdc37e39deaf63c86dda11fe13f2f5aca33a8 100644 (file)
@@ -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()
index cadb28becaceef15cebd054fbc812105b9d3846c..0ef097656ab8bd9059c548c6802f8c806cfd3194 100644 (file)
@@ -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