From e557f69c8365665f5a636d0942e9a11e98bedb45 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Tue, 9 May 2017 20:35:18 -0400 Subject: [PATCH] Make sure that websockets.connect is a coroutine. In 3.5.0 and 3.5.1 asyncio.ensure_future requires a Future or a coroutine otherwise a TypeError is raised. The issue is that the websockets.connect call is an awaitable rather than a coroutine. asyncio.ensure_future did not gain support for awaitables until 3.5.2. This patch allows 3.5.0 and 3.5.1 to connect regardless of their python version. --- discord/shard.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/discord/shard.py b/discord/shard.py index c2b0eaa6f..ba0da9ff1 100644 --- a/discord/shard.py +++ b/discord/shard.py @@ -68,6 +68,16 @@ class Shard: return self._current +@asyncio.coroutine +def _ensure_coroutine_connect(gateway, loop): + # In 3.5+ websockets.connect does not return a coroutine, but an awaitable. + # The problem is that in 3.5.0 and in some cases 3.5.1, asyncio.ensure_future and + # by proxy, asyncio.wait_for, do not accept awaitables, but rather futures or coroutines. + # By wrapping it up into this function we ensure that it's in a coroutine and not an awaitable + # even for 3.5.0 users. + ws = yield from websockets.connect(gateway, loop=loop, klass=DiscordWebSocket) + return ws + class AutoShardedClient(Client): """A client similar to :class:`Client` except it handles the complications of sharding for the user into a more manageable and transparent single @@ -182,8 +192,7 @@ class AutoShardedClient(Client): @asyncio.coroutine def launch_shard(self, gateway, shard_id): try: - ws = yield from asyncio.wait_for(websockets.connect(gateway, loop=self.loop, klass=DiscordWebSocket), - loop=self.loop, timeout=180.0) + ws = yield from asyncio.wait_for(_ensure_coroutine_connect(gateway, self.loop), loop=self.loop, timeout=180.0) except Exception as e: log.info('Failed to connect for shard_id: %s. Retrying...', shard_id) yield from asyncio.sleep(5.0, loop=self.loop)