From 7f58853e3a2df34bf2fe77121fbb57cf3054f5ab Mon Sep 17 00:00:00 2001 From: Rapptz Date: Wed, 29 Mar 2017 04:35:06 -0400 Subject: [PATCH] Keep track of Emoji instances myself. WeakValueDictionary cleans up too late and brings too little benefit. Also clean up the state when the first READY is encountered for AutoShardedClient and when any READY is encountered in regular Client. --- discord/emoji.py | 2 +- discord/state.py | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/discord/emoji.py b/discord/emoji.py index 0e2158ec8..ff839c4d1 100644 --- a/discord/emoji.py +++ b/discord/emoji.py @@ -73,7 +73,7 @@ class Emoji(Hashable): A list of :class:`Role` that is allowed to use this emoji. If roles is empty, the emoji is unrestricted. """ - __slots__ = ('require_colons', 'managed', 'id', 'name', 'roles', 'guild', '_state', '__weakref__') + __slots__ = ('require_colons', 'managed', 'id', 'name', 'roles', 'guild', '_state') def __init__(self, *, guild, state, data): self.guild = guild diff --git a/discord/state.py b/discord/state.py index 72a4f01a5..7c71f7225 100644 --- a/discord/state.py +++ b/discord/state.py @@ -70,7 +70,7 @@ class ConnectionState: def clear(self): self.user = None self._users = weakref.WeakValueDictionary() - self._emojis = weakref.WeakValueDictionary() + self._emojis = {} self._calls = {} self._guilds = {} self._voice_clients = {} @@ -157,6 +157,11 @@ class ConnectionState: def _remove_guild(self, guild): self._guilds.pop(guild.id, None) + for emoji in guild.emojis: + self._emojis.pop(emoji.id, None) + + del guild + @property def emojis(self): return list(self._emojis.values()) @@ -249,6 +254,7 @@ class ConnectionState: def parse_ready(self, data): self._ready_state = ReadyState(launch=asyncio.Event(), guilds=[]) + self.clear() self.user = ClientUser(state=self, data=data['user']) guilds = self._ready_state.guilds @@ -760,7 +766,6 @@ class ConnectionState: class AutoShardedConnectionState(ConnectionState): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self._ready_state = ReadyState(launch=asyncio.Event(), guilds=[]) self._ready_task = None @asyncio.coroutine @@ -817,6 +822,7 @@ class AutoShardedConnectionState(ConnectionState): def parse_ready(self, data): if not hasattr(self, '_ready_state'): self._ready_state = ReadyState(launch=asyncio.Event(), guilds=[]) + self.clear() self.user = ClientUser(state=self, data=data['user'])