@@ -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
133157static 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