aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRapptz <[email protected]>2017-05-09 20:35:18 -0400
committerRapptz <[email protected]>2017-05-09 20:35:18 -0400
commite557f69c8365665f5a636d0942e9a11e98bedb45 (patch)
tree4f04903e260e3cfe8aefc9771a927645589f835e
parentRemove Guild.change_vanity_invite in favour of Guild.edit (diff)
downloaddiscord.py-e557f69c8365665f5a636d0942e9a11e98bedb45.tar.xz
discord.py-e557f69c8365665f5a636d0942e9a11e98bedb45.zip
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.
-rw-r--r--discord/shard.py13
1 files changed, 11 insertions, 2 deletions
diff --git a/discord/shard.py b/discord/shard.py
index c2b0eaa6..ba0da9ff 100644
--- a/discord/shard.py
+++ b/discord/shard.py
@@ -68,6 +68,16 @@ class Shard:
return self._current
+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)