From: Thomas Kluyver Date: Thu, 16 Feb 2023 12:59:02 +0000 (+0000) Subject: Default make_current -> True, remove check for existing event loop X-Git-Tag: v6.3.0b1~8^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=85e395e659059b0194c575a85cb42065776d1a9b;p=thirdparty%2Ftornado.git Default make_current -> True, remove check for existing event loop --- diff --git a/tornado/ioloop.py b/tornado/ioloop.py index d698a97e1..bea2ca0b2 100644 --- a/tornado/ioloop.py +++ b/tornado/ioloop.py @@ -145,9 +145,9 @@ class IOLoop(Configurable): event loop. .. versionchanged:: 6.3 - ``make_current=False`` is now the default when creating an IOLoop - + ``make_current=True`` is now the default when creating an IOLoop - previously the default was to make the event loop current if there wasn't - already a current one. Passing ``make_current=True`` is deprecated. + already a current one. """ # These constants were originally based on constants from the epoll module. @@ -298,6 +298,14 @@ class IOLoop(Configurable): Setting and clearing the current event loop through Tornado is deprecated. Use ``asyncio.set_event_loop`` instead if you need this. """ + warnings.warn( + "make_current is deprecated; start the event loop first", + DeprecationWarning, + stacklevel=2, + ) + self._make_current() + + def _make_current(self): # The asyncio event loops override this method. raise NotImplementedError() @@ -341,18 +349,9 @@ class IOLoop(Configurable): return AsyncIOLoop - def initialize(self, make_current: bool = False) -> None: + def initialize(self, make_current: bool = True) -> None: if make_current: - warnings.warn( - "IOLoop(make_current=True) is deprecated", - DeprecationWarning, - stacklevel=2, - ) - current = IOLoop.current(instance=False) - # AsyncIO loops can already be current by this point. - if current is not None and current is not self: - raise RuntimeError("current IOLoop already exists") - self.make_current() + self._make_current() def close(self, all_fds: bool = False) -> None: """Closes the `IOLoop`, freeing any resources used. diff --git a/tornado/platform/asyncio.py b/tornado/platform/asyncio.py index a3fbdc37e..a9fa89004 100644 --- a/tornado/platform/asyncio.py +++ b/tornado/platform/asyncio.py @@ -276,7 +276,7 @@ class AsyncIOMainLoop(BaseAsyncIOLoop): def initialize(self, **kwargs: Any) -> None: # type: ignore super().initialize(asyncio.get_event_loop(), **kwargs) - def make_current(self) -> None: + def _make_current(self) -> None: # AsyncIOMainLoop already refers to the current asyncio loop so # nothing to do here. pass @@ -327,12 +327,7 @@ class AsyncIOLoop(BaseAsyncIOLoop): self._clear_current() 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, - ) + def _make_current(self): if not self.is_current: try: self.old_asyncio = asyncio.get_event_loop() diff --git a/tornado/test/ioloop_test.py b/tornado/test/ioloop_test.py index a0ce493a9..7de392f83 100644 --- a/tornado/test/ioloop_test.py +++ b/tornado/test/ioloop_test.py @@ -472,11 +472,6 @@ class TestIOLoopCurrent(unittest.TestCase): def test_force_current(self): self.io_loop = IOLoop(make_current=True) self.assertIs(self.io_loop, IOLoop.current()) - with self.assertRaises(RuntimeError): - # A second make_current=True construction cannot succeed. - IOLoop(make_current=True) - # current() was not affected by the failed construction. - self.assertIs(self.io_loop, IOLoop.current()) class TestIOLoopCurrentAsync(AsyncTestCase):