From bac6a4b2bf2fc98401e0de7168c4a5cf31b08b37 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 18 Nov 2021 15:33:03 +0100 Subject: [PATCH] Replace asyncio.ensure_future() with asyncio.create_task() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The latter function got introduced in Python 3.7, which is conveniently our MSPV, so let’s use that. --- slixmpp/plugins/xep_0045/muc.py | 2 +- slixmpp/plugins/xep_0163.py | 4 ++-- slixmpp/plugins/xep_0199/ping.py | 2 +- slixmpp/test/slixtest.py | 2 +- slixmpp/xmlstream/handler/coroutine_callback.py | 6 +++--- slixmpp/xmlstream/xmlstream.py | 16 ++++++++-------- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/slixmpp/plugins/xep_0045/muc.py b/slixmpp/plugins/xep_0045/muc.py index 21f5c896..bfb8e571 100644 --- a/slixmpp/plugins/xep_0045/muc.py +++ b/slixmpp/plugins/xep_0045/muc.py @@ -342,7 +342,7 @@ class XEP_0045(BasePlugin): maxchars = 9 else: maxstanzas = int(maxhistory) - return asyncio.ensure_future( + return asyncio.create_task( self.join_muc_wait( room=room, nick=nick, diff --git a/slixmpp/plugins/xep_0163.py b/slixmpp/plugins/xep_0163.py index 46ca4235..07cef975 100644 --- a/slixmpp/plugins/xep_0163.py +++ b/slixmpp/plugins/xep_0163.py @@ -61,7 +61,7 @@ class XEP_0163(BasePlugin): for ns in namespace: self.xmpp['xep_0030'].add_feature('%s+notify' % ns, jid=jid) - asyncio.ensure_future( + asyncio.create_task( self.xmpp['xep_0115'].update_caps(jid, broadcast=False), loop=self.xmpp.loop, ) @@ -82,7 +82,7 @@ class XEP_0163(BasePlugin): for ns in namespace: self.xmpp['xep_0030'].del_feature(jid=jid, feature='%s+notify' % namespace) - asyncio.ensure_future( + asyncio.create_task( self.xmpp['xep_0115'].update_caps(jid, broadcast=False), loop=self.xmpp.loop, ) diff --git a/slixmpp/plugins/xep_0199/ping.py b/slixmpp/plugins/xep_0199/ping.py index 03d272dd..11234bab 100644 --- a/slixmpp/plugins/xep_0199/ping.py +++ b/slixmpp/plugins/xep_0199/ping.py @@ -118,7 +118,7 @@ class XEP_0199(BasePlugin): tmp_futures.append(future) self.__pending_futures = tmp_futures - future = asyncio.ensure_future( + future = asyncio.create_task( self._keepalive(event), loop=self.xmpp.loop, ) diff --git a/slixmpp/test/slixtest.py b/slixmpp/test/slixtest.py index 0d05a4ac..1c7f12fa 100644 --- a/slixmpp/test/slixtest.py +++ b/slixmpp/test/slixtest.py @@ -622,7 +622,7 @@ class SlixTest(unittest.TestCase): def wait_for_send_queue(self): loop = asyncio.get_event_loop() - future = asyncio.ensure_future(self.xmpp.run_filters(), loop=loop) + future = asyncio.create_task(self.xmpp.run_filters(), loop=loop) queue = self.xmpp.waiting_queue loop.run_until_complete(queue.join()) future.cancel() diff --git a/slixmpp/xmlstream/handler/coroutine_callback.py b/slixmpp/xmlstream/handler/coroutine_callback.py index 524cca54..1ebe3ae4 100644 --- a/slixmpp/xmlstream/handler/coroutine_callback.py +++ b/slixmpp/xmlstream/handler/coroutine_callback.py @@ -6,7 +6,7 @@ # :license: MIT, see LICENSE for more details from __future__ import annotations -from asyncio import iscoroutinefunction, ensure_future +import asyncio from typing import Optional, Callable, Awaitable, TYPE_CHECKING from slixmpp.xmlstream.stanzabase import StanzaBase @@ -52,7 +52,7 @@ class CoroutineCallback(BaseHandler): pointer: CoroutineFunction, once: bool = False, instream: bool = False, stream: Optional[XMLStream] = None): BaseHandler.__init__(self, name, matcher, stream) - if not iscoroutinefunction(pointer): + if not asyncio.iscoroutinefunction(pointer): raise ValueError("Given function is not a coroutine") async def pointer_wrapper(stanza: StanzaBase) -> None: @@ -87,7 +87,7 @@ class CoroutineCallback(BaseHandler): :meth:`prerun()`. Defaults to ``False``. """ if not self._instream or instream: - ensure_future(self._pointer(payload)) + asyncio.create_task(self._pointer(payload)) if self._once: self._destroy = True del self._pointer diff --git a/slixmpp/xmlstream/xmlstream.py b/slixmpp/xmlstream/xmlstream.py index 30f99071..9955360a 100644 --- a/slixmpp/xmlstream/xmlstream.py +++ b/slixmpp/xmlstream/xmlstream.py @@ -415,7 +415,7 @@ class XMLStream(asyncio.BaseProtocol): """ if self._run_out_filters is None or self._run_out_filters.done(): - self._run_out_filters = asyncio.ensure_future( + self._run_out_filters = asyncio.create_task( self.run_filters(), loop=self.loop, ) @@ -439,7 +439,7 @@ class XMLStream(asyncio.BaseProtocol): self.disable_starttls = disable_starttls self.event("connecting") - self._current_connection_attempt = asyncio.ensure_future( + self._current_connection_attempt = asyncio.create_task( self._connect_routine(), loop=self.loop, ) @@ -486,7 +486,7 @@ class XMLStream(asyncio.BaseProtocol): if self._current_connection_attempt is None: return self._connect_loop_wait = self._connect_loop_wait * 2 + 1 - self._current_connection_attempt = asyncio.ensure_future( + self._current_connection_attempt = asyncio.create_task( self._connect_routine(), loop=self.loop, ) @@ -653,12 +653,12 @@ class XMLStream(asyncio.BaseProtocol): self.disconnect_reason = reason if self.waiting_queue.empty() or ignore_send_queue: self.cancel_connection_attempt() - return asyncio.ensure_future( + return asyncio.create_task( self._end_stream_wait(wait, reason=reason), loop=self.loop, ) else: - return asyncio.ensure_future( + return asyncio.create_task( self._consume_send_queue_before_disconnecting(reason, wait), loop=self.loop, ) @@ -1058,7 +1058,7 @@ class XMLStream(asyncio.BaseProtocol): old_exception(e) else: self.exception(e) - asyncio.ensure_future( + asyncio.create_task( handler_callback_routine(handler_callback), loop=self.loop, ) @@ -1224,7 +1224,7 @@ class XMLStream(asyncio.BaseProtocol): ) if pending: self.__slow_tasks.append(task) - asyncio.ensure_future( + asyncio.create_task( self._continue_slow_send( task, already_run_filters @@ -1431,7 +1431,7 @@ class XMLStream(asyncio.BaseProtocol): :param coroutine: The coroutine to wrap. """ - return asyncio.ensure_future( + return asyncio.create_task( coroutine, loop=self.loop, )