Merge branch 'adhoc-partial-coroutine' into 'master'
xep_0050:allow partial coroutines as handlers See merge request poezio/slixmpp!231
This commit is contained in:
commit
e34fbfb28f
@ -4,6 +4,7 @@
|
|||||||
# This file is part of Slixmpp.
|
# This file is part of Slixmpp.
|
||||||
# See the file LICENSE for copying permission.
|
# See the file LICENSE for copying permission.
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import functools
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
|
|
||||||
@ -619,10 +620,16 @@ class XEP_0050(BasePlugin):
|
|||||||
self.terminate_command(session)
|
self.terminate_command(session)
|
||||||
|
|
||||||
|
|
||||||
|
def _iscoroutine_or_partial_coroutine(handler):
|
||||||
|
return asyncio.iscoroutinefunction(handler) \
|
||||||
|
or isinstance(handler, functools.partial) \
|
||||||
|
and asyncio.iscoroutinefunction(handler.func)
|
||||||
|
|
||||||
|
|
||||||
async def _await_if_needed(handler, *args):
|
async def _await_if_needed(handler, *args):
|
||||||
if handler is None:
|
if handler is None:
|
||||||
raise XMPPError("bad-request", text="The command is completed")
|
raise XMPPError("bad-request", text="The command is completed")
|
||||||
if asyncio.iscoroutinefunction(handler):
|
if _iscoroutine_or_partial_coroutine(handler):
|
||||||
log.debug(f"%s is async", handler)
|
log.debug(f"%s is async", handler)
|
||||||
return await handler(*args)
|
return await handler(*args)
|
||||||
else:
|
else:
|
||||||
|
Loading…
Reference in New Issue
Block a user