Skip to content

Commit a4540da

Browse files
committed
Allow wildcard signal match by passing None
Pass None to `bus.get_signal_queue_async` instead of destination_name, object_path, interface_name or member_name to have a wildcard match.
1 parent a1422d2 commit a4540da

4 files changed

Lines changed: 39 additions & 11 deletions

File tree

src/sdbus/sd_bus_internals.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@
111111
} \
112112
new_char_ptr; \
113113
})
114+
115+
#define SD_BUS_PY_UNICODE_AS_CHAR_PTR_OPTIONAL(py_object) \
116+
({ \
117+
const char* new_char_ptr_or_null = NULL; \
118+
if (Py_None != py_object) { \
119+
new_char_ptr_or_null = SD_BUS_PY_UNICODE_AS_CHAR_PTR(py_object); \
120+
} \
121+
new_char_ptr_or_null; \
122+
})
114123
#endif
115124

116125
#define CALL_PYTHON_ITER(iter, iter_end) \

src/sdbus/sd_bus_internals.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ def add_interface(self, new_interface: SdBusInterface,
180180

181181
def get_signal_queue_async(
182182
self,
183-
destination_name: str, object_path: str,
184-
interface_name: str, member_name: str,
183+
destination_name: Optional[str], object_path: Optional[str],
184+
interface_name: Optional[str], member_name: Optional[str],
185185
/
186186
) -> Future[Queue[SdBusMessage]]:
187187
raise NotImplementedError(__STUB_ERROR)

src/sdbus/sd_bus_internals_bus.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -420,25 +420,31 @@ int _SdBus_match_signal_instant_callback(sd_bus_message* m, void* userdata, sd_b
420420
}
421421

422422
#ifndef Py_LIMITED_API
423+
424+
static int _unicode_or_none(PyObject* some_object) {
425+
return (PyUnicode_Check(some_object) || (Py_None == some_object));
426+
}
427+
423428
static PyObject* SdBus_get_signal_queue(SdBusObject* self, PyObject* const* args, Py_ssize_t nargs) {
424429
SD_BUS_PY_CHECK_ARGS_NUMBER(4);
425-
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
426-
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
427-
SD_BUS_PY_CHECK_ARG_TYPE(2, PyUnicode_Type);
428-
SD_BUS_PY_CHECK_ARG_TYPE(3, PyUnicode_Type);
429430

430-
const char* sender_service_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
431-
const char* path_name_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[1]);
432-
const char* interface_name_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[2]);
433-
const char* member_name_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[3]);
431+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, _unicode_or_none);
432+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, _unicode_or_none);
433+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(2, _unicode_or_none);
434+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(3, _unicode_or_none);
435+
436+
const char* sender_service_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR_OPTIONAL(args[0]);
437+
const char* path_name_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR_OPTIONAL(args[1]);
438+
const char* interface_name_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR_OPTIONAL(args[2]);
439+
const char* member_name_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR_OPTIONAL(args[3]);
434440
#else
435441
static PyObject* SdBus_get_signal_queue(SdBusObject* self, PyObject* args) {
436442
const char* sender_service_char_ptr = NULL;
437443
const char* path_name_char_ptr = NULL;
438444
const char* interface_name_char_ptr = NULL;
439445
const char* member_name_char_ptr = NULL;
440446
CALL_PYTHON_BOOL_CHECK(
441-
PyArg_ParseTuple(args, "ssss", &sender_service_char_ptr, &path_name_char_ptr, &interface_name_char_ptr, &member_name_char_ptr, NULL));
447+
PyArg_ParseTuple(args, "zzzz", &sender_service_char_ptr, &path_name_char_ptr, &interface_name_char_ptr, &member_name_char_ptr, NULL));
442448
#endif
443449
SdBusSlotObject* new_slot CLEANUP_SD_BUS_SLOT = (SdBusSlotObject*)CALL_PYTHON_AND_CHECK(PyObject_CallFunctionObjArgs(SdBusSlot_class, NULL));
444450

test/test_sd_bus_async.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,3 +595,16 @@ async def too_long_wait() -> None:
595595
...
596596

597597
self.assertRaises(SdBusLibraryError, t1.result)
598+
599+
async def test_singal_queue_wildcard_match(self) -> None:
600+
test_object, test_object_connection = initialize_object(self.bus)
601+
602+
message_queue = await self.bus.get_signal_queue_async(
603+
'org.example.test',
604+
None, None, None)
605+
606+
test_object.test_signal.emit(('test', 'signal'))
607+
608+
message = await wait_for(message_queue.get(), timeout=1)
609+
self.assertEqual(message.get_member(),
610+
test_object.test_signal.dbus_signal.signal_name)

0 commit comments

Comments
 (0)