diff --git a/src/dbus_fast/message_bus.pxd b/src/dbus_fast/message_bus.pxd index 037cd48b..69a955a6 100644 --- a/src/dbus_fast/message_bus.pxd +++ b/src/dbus_fast/message_bus.pxd @@ -16,8 +16,6 @@ cdef object assert_bus_name_valid cdef _expects_reply(Message msg) -cdef _swallow_unexpected_reply(Message msg) - cdef class SendReply: cdef object _bus diff --git a/src/dbus_fast/message_bus.py b/src/dbus_fast/message_bus.py index 5d26b904..7893e032 100644 --- a/src/dbus_fast/message_bus.py +++ b/src/dbus_fast/message_bus.py @@ -33,14 +33,9 @@ def _expects_reply(msg: _Message) -> bool: - """Whether a message expects a reply.""" return not (msg.flags.value & NO_REPLY_EXPECTED_VALUE) -def _swallow_unexpected_reply(msg: _Message) -> None: - """Swallow a reply if it's not expected.""" - - class SendReply: """A context manager to send a reply to a message.""" @@ -55,7 +50,8 @@ def __enter__(self): return self def __call__(self, reply: Message) -> None: - self._bus.send(reply) + if _expects_reply(self._msg): + self._bus.send(reply) def _exit( self, @@ -859,11 +855,9 @@ def _process_message(self, msg: _Message) -> None: if msg.message_type is MESSAGE_TYPE_CALL: if not handled: handler = self._find_message_handler(msg) - if not _expects_reply(msg): - handler(msg, _swallow_unexpected_reply) - return send_reply = SendReply(self, msg) + with send_reply: if handler: handler(msg, send_reply)