Skip to content

Commit 4074f95

Browse files
committed
Use METH_O method type for SdBus.call(_async)
METH_O methods only take a single argument. It does not look like it constructs any tuples to make the call so it should be as fast as METH_FASTCALL without being only available for unlimited API in Python 3.9. Benchmarks did not reveal any significant impact.
1 parent aa58a8c commit 4074f95

File tree

1 file changed

+8
-27
lines changed

1 file changed

+8
-27
lines changed

src/sdbus/sd_bus_internals_bus.c

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -168,22 +168,10 @@ static SdBusMessageObject* SdBus_new_signal_message(SdBusObject* self, PyObject*
168168
return new_message_object;
169169
}
170170

171-
#ifndef Py_LIMITED_API
172-
static int _check_sdbus_message(PyObject* something) {
173-
return PyType_IsSubtype(Py_TYPE(something), (PyTypeObject*)SdBusMessage_class);
174-
}
175-
176-
static SdBusMessageObject* SdBus_call(SdBusObject* self, PyObject* const* args, Py_ssize_t nargs) {
177-
// TODO: Check reference counting
178-
SD_BUS_PY_CHECK_ARGS_NUMBER(1);
179-
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, _check_sdbus_message);
180-
181-
SdBusMessageObject* call_message = (SdBusMessageObject*)args[0];
182-
#else
183-
static SdBusMessageObject* SdBus_call(SdBusObject* self, PyObject* args) {
171+
static SdBusMessageObject* SdBus_call(SdBusObject* self, PyObject* arg) {
184172
SdBusMessageObject* call_message = NULL;
185-
CALL_PYTHON_BOOL_CHECK(PyArg_ParseTuple(args, "O", &call_message, NULL));
186-
#endif
173+
CALL_PYTHON_BOOL_CHECK(PyArg_Parse(arg, "O!", SdBusMessage_class, &call_message, NULL));
174+
187175
SdBusMessageObject* reply_message_object CLEANUP_SD_BUS_MESSAGE =
188176
(SdBusMessageObject*)CALL_PYTHON_AND_CHECK(SD_BUS_PY_CLASS_DUNDER_NEW(SdBusMessage_class));
189177

@@ -316,17 +304,10 @@ int SdBus_async_callback(sd_bus_message* m,
316304
return 0;
317305
}
318306

319-
#ifndef Py_LIMITED_API
320-
static PyObject* SdBus_call_async(SdBusObject* self, PyObject* const* args, Py_ssize_t nargs) {
321-
SD_BUS_PY_CHECK_ARGS_NUMBER(1);
322-
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, _check_sdbus_message);
323-
324-
SdBusMessageObject* call_message = (SdBusMessageObject*)args[0];
325-
#else
326-
static PyObject* SdBus_call_async(SdBusObject* self, PyObject* args) {
307+
static PyObject* SdBus_call_async(SdBusObject* self, PyObject* arg) {
327308
SdBusMessageObject* call_message = NULL;
328-
CALL_PYTHON_BOOL_CHECK(PyArg_ParseTuple(args, "O", &call_message, NULL));
329-
#endif
309+
CALL_PYTHON_BOOL_CHECK(PyArg_Parse(arg, "O!", SdBusMessage_class, &call_message, NULL));
310+
330311
PyObject* running_loop = CALL_PYTHON_AND_CHECK(_get_or_bind_loop(self));
331312

332313
PyObject* new_future = CALL_PYTHON_AND_CHECK(PyObject_CallMethod(running_loop, "create_future", ""));
@@ -717,8 +698,8 @@ static PyObject* SdBus_asyncio_update_fd_watchers(SdBusObject* self) {
717698
}
718699

719700
static PyMethodDef SdBus_methods[] = {
720-
{"call", (SD_BUS_PY_FUNC_TYPE)SdBus_call, SD_BUS_PY_METH, PyDoc_STR("Send message and block until the reply.")},
721-
{"call_async", (SD_BUS_PY_FUNC_TYPE)SdBus_call_async, SD_BUS_PY_METH, PyDoc_STR("Async send message, returns awaitable future.")},
701+
{"call", (PyCFunction)SdBus_call, METH_O, PyDoc_STR("Send message and block until the reply.")},
702+
{"call_async", (PyCFunction)SdBus_call_async, METH_O, PyDoc_STR("Async send message, returns awaitable future.")},
722703
{"process", (PyCFunction)SdBus_process, METH_NOARGS, PyDoc_STR("Process pending IO work.")},
723704
{"get_fd", (SD_BUS_PY_FUNC_TYPE)SdBus_get_fd, SD_BUS_PY_METH, PyDoc_STR("Get file descriptor to poll on.")},
724705
{"new_method_call_message", (SD_BUS_PY_FUNC_TYPE)SdBus_new_method_call_message, SD_BUS_PY_METH, PyDoc_STR("Create new empty method call message.")},

0 commit comments

Comments
 (0)