From 92b4f2a7ebb2469c96dfe06c46357b536f00c8cd Mon Sep 17 00:00:00 2001 From: nicoco Date: Thu, 23 Feb 2023 23:33:13 +0100 Subject: [PATCH] xep_0050:allow partial coroutines as handlers --- slixmpp/plugins/xep_0050/adhoc.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/slixmpp/plugins/xep_0050/adhoc.py b/slixmpp/plugins/xep_0050/adhoc.py index b101056e..587fa3eb 100644 --- a/slixmpp/plugins/xep_0050/adhoc.py +++ b/slixmpp/plugins/xep_0050/adhoc.py @@ -4,6 +4,7 @@ # This file is part of Slixmpp. # See the file LICENSE for copying permission. import asyncio +import functools import logging import time @@ -619,10 +620,16 @@ class XEP_0050(BasePlugin): 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): if handler is None: 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) return await handler(*args) else: