Skip to content

Commit 4533f11

Browse files
committed
Added map_exception_to_dbus_error call
Maps Python exception to D-Bus error
1 parent 3ae81fc commit 4533f11

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/sdbus/sd_bus_internals.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ def decode_object_path(prefix: str, full_path: str) -> str:
243243
raise NotImplementedError(__STUB_ERROR)
244244

245245

246+
def map_exception_to_dbus_error(exc: Exception,
247+
dbus_error_name: str, /) -> None:
248+
... # We want to be able to generate docs without module
249+
250+
246251
def add_exception_mapping(exc: Exception, /) -> None:
247252
... # We want to be able to generate docs without module
248253

src/sdbus/sd_bus_internals_funcs.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,30 @@ static PyObject* decode_object_path(PyObject* Py_UNUSED(self), PyObject* args) {
129129
}
130130
}
131131

132+
#ifndef Py_LIMITED_API
133+
static PyObject* map_exception_to_dbus_error(PyObject* Py_UNUSED(self), PyObject* const* args, Py_ssize_t nargs) {
134+
SD_BUS_PY_CHECK_ARGS_NUMBER(2);
135+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyExceptionClass_Check);
136+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);
137+
PyObject* exception = args[0];
138+
PyObject* dbus_error_string = args[1];
139+
#else
140+
static PyObject* map_exception_to_dbus_error(PyObject* Py_UNUSED(self), PyObject* args) {
141+
PyObject* exception = NULL;
142+
PyObject* dbus_error_string = NULL;
143+
CALL_PYTHON_BOOL_CHECK(PyArg_ParseTuple(args, "Os", &exception, &dbus_error_string, NULL));
144+
#endif
145+
if (CALL_PYTHON_INT_CHECK(PyDict_Contains(dbus_error_to_exception_dict, dbus_error_string)) > 0) {
146+
PyErr_Format(PyExc_ValueError, "Dbus error %R is already mapped.", dbus_error_string);
147+
return NULL;
148+
}
149+
150+
CALL_PYTHON_INT_CHECK(PyDict_SetItem(dbus_error_to_exception_dict, dbus_error_string, exception));
151+
CALL_PYTHON_INT_CHECK(PyDict_SetItem(exception_to_dbus_error_dict, exception, dbus_error_string));
152+
153+
Py_RETURN_NONE;
154+
}
155+
132156
#ifndef Py_LIMITED_API
133157
static PyObject* add_exception_mapping(PyObject* Py_UNUSED(self), PyObject* const* args, Py_ssize_t nargs) {
134158
SD_BUS_PY_CHECK_ARGS_NUMBER(1);
@@ -259,6 +283,7 @@ PyMethodDef SdBusPyInternal_methods[] = {
259283
{"sd_bus_open_system_machine", (PyCFunction)sd_bus_py_open_system_machine, METH_VARARGS, "Open user bus in systemd-nspawn container"},
260284
{"encode_object_path", (SD_BUS_PY_FUNC_TYPE)encode_object_path, SD_BUS_PY_METH, "Encode object path with object path prefix and arbitrary string"},
261285
{"decode_object_path", (SD_BUS_PY_FUNC_TYPE)decode_object_path, SD_BUS_PY_METH, "Decode object path with object path prefix and arbitrary string"},
286+
{"map_exception_to_dbus_error", (SD_BUS_PY_FUNC_TYPE)map_exception_to_dbus_error, SD_BUS_PY_METH, "Map exception to a D-Bus error name"},
262287
{"add_exception_mapping", (SD_BUS_PY_FUNC_TYPE)add_exception_mapping, SD_BUS_PY_METH, "Add exception to the mapping of dbus error names"},
263288
{"is_interface_name_valid", (SD_BUS_PY_FUNC_TYPE)is_interface_name_valid, SD_BUS_PY_METH, "Is the string valid interface name?"},
264289
{"is_service_name_valid", (SD_BUS_PY_FUNC_TYPE)is_service_name_valid, SD_BUS_PY_METH, "Is the string valid service name?"},

0 commit comments

Comments
 (0)