diff options
| author | Rapptz <[email protected]> | 2017-01-25 05:31:53 -0500 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2017-01-25 05:31:53 -0500 |
| commit | 234fd5180f3381d6e2a4054c23c99a5b15e7a637 (patch) | |
| tree | 18eb05b667964955a52d4479ddc066901015123a /discord/client.py | |
| parent | Implement User.profile coroutine to get a user's profile. (diff) | |
| download | discord.py-234fd5180f3381d6e2a4054c23c99a5b15e7a637.tar.xz discord.py-234fd5180f3381d6e2a4054c23c99a5b15e7a637.zip | |
Optimise attribute access when dispatching.
Diffstat (limited to 'discord/client.py')
| -rw-r--r-- | discord/client.py | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/discord/client.py b/discord/client.py index c9ada5df..c93a4972 100644 --- a/discord/client.py +++ b/discord/client.py @@ -248,14 +248,14 @@ class Client: object.__setattr__(self, name, value) @asyncio.coroutine - def _run_event(self, event, *args, **kwargs): + def _run_event(self, coro, event_name, *args, **kwargs): try: - yield from getattr(self, event)(*args, **kwargs) + yield from coro(*args, **kwargs) except asyncio.CancelledError: pass except Exception: try: - yield from self.on_error(event, *args, **kwargs) + yield from self.on_error(event_name, *args, **kwargs) except asyncio.CancelledError: pass @@ -264,11 +264,19 @@ class Client: method = 'on_' + event handler = 'handle_' + event - if hasattr(self, handler): - getattr(self, handler)(*args, **kwargs) + try: + actual_handler = getattr(self, handler) + except AttributeError: + pass + else: + actual_handler(*args, **kwargs) - if hasattr(self, method): - compat.create_task(self._run_event(method, *args, **kwargs), loop=self.loop) + try: + coro = getattr(self, method) + except AttributeError: + pass + else: + compat.create_task(self._run_event(coro, method, *args, **kwargs), loop=self.loop) @asyncio.coroutine def on_error(self, event_method, *args, **kwargs): |