Optimise attribute access when dispatching.
This commit is contained in:
parent
02f30f21c4
commit
234fd5180f
@ -248,14 +248,14 @@ class Client:
|
|||||||
object.__setattr__(self, name, value)
|
object.__setattr__(self, name, value)
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def _run_event(self, event, *args, **kwargs):
|
def _run_event(self, coro, event_name, *args, **kwargs):
|
||||||
try:
|
try:
|
||||||
yield from getattr(self, event)(*args, **kwargs)
|
yield from coro(*args, **kwargs)
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
pass
|
pass
|
||||||
except Exception:
|
except Exception:
|
||||||
try:
|
try:
|
||||||
yield from self.on_error(event, *args, **kwargs)
|
yield from self.on_error(event_name, *args, **kwargs)
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -264,11 +264,19 @@ class Client:
|
|||||||
method = 'on_' + event
|
method = 'on_' + event
|
||||||
handler = 'handle_' + event
|
handler = 'handle_' + event
|
||||||
|
|
||||||
if hasattr(self, handler):
|
try:
|
||||||
getattr(self, handler)(*args, **kwargs)
|
actual_handler = getattr(self, handler)
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
actual_handler(*args, **kwargs)
|
||||||
|
|
||||||
if hasattr(self, method):
|
try:
|
||||||
compat.create_task(self._run_event(method, *args, **kwargs), loop=self.loop)
|
coro = getattr(self, method)
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
compat.create_task(self._run_event(coro, method, *args, **kwargs), loop=self.loop)
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def on_error(self, event_method, *args, **kwargs):
|
def on_error(self, event_method, *args, **kwargs):
|
||||||
|
@ -164,24 +164,11 @@ class BotBase(GroupMixin):
|
|||||||
|
|
||||||
# internal helpers
|
# internal helpers
|
||||||
|
|
||||||
@asyncio.coroutine
|
|
||||||
def _run_extra(self, coro, event_name, *args, **kwargs):
|
|
||||||
try:
|
|
||||||
yield from coro(*args, **kwargs)
|
|
||||||
except asyncio.CancelledError:
|
|
||||||
pass
|
|
||||||
except Exception:
|
|
||||||
try:
|
|
||||||
yield from self.on_error(event_name, *args, **kwargs)
|
|
||||||
except asyncio.CancelledError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def dispatch(self, event_name, *args, **kwargs):
|
def dispatch(self, event_name, *args, **kwargs):
|
||||||
super().dispatch(event_name, *args, **kwargs)
|
super().dispatch(event_name, *args, **kwargs)
|
||||||
ev = 'on_' + event_name
|
ev = 'on_' + event_name
|
||||||
if ev in self.extra_events:
|
for event in self.extra_events.get(ev, []):
|
||||||
for event in self.extra_events[ev]:
|
coro = self._run_event(event, event_name, *args, **kwargs)
|
||||||
coro = self._run_extra(event, event_name, *args, **kwargs)
|
|
||||||
discord.compat.create_task(coro, loop=self.loop)
|
discord.compat.create_task(coro, loop=self.loop)
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
|
Loading…
x
Reference in New Issue
Block a user