diff options
author | Simon McVittie <simon.mcvittie@collabora.co.uk> | 2007-05-16 11:05:29 +0100 |
---|---|---|
committer | Simon McVittie <simon.mcvittie@collabora.co.uk> | 2007-05-16 11:05:29 +0100 |
commit | b052211a63cf833ac394ca529c344c288e921401 (patch) | |
tree | 0b2d71dcd15e806127d4a18f4ba6d8dedffd584f /_dbus_bindings | |
parent | df88e33eb69ef1528f76d06429d451b8274bd69b (diff) | |
download | dbus-python-b052211a63cf833ac394ca529c344c288e921401.tar.gz |
Implement DBusException in pure Python; add get_dbus_name() method and name= keyword argument
Diffstat (limited to '_dbus_bindings')
-rw-r--r-- | _dbus_bindings/bus.c | 5 | ||||
-rw-r--r-- | _dbus_bindings/conn-methods.c | 6 | ||||
-rw-r--r-- | _dbus_bindings/dbus_bindings-internal.h | 2 | ||||
-rw-r--r-- | _dbus_bindings/exceptions.c | 99 | ||||
-rw-r--r-- | _dbus_bindings/message.c | 6 | ||||
-rw-r--r-- | _dbus_bindings/module.c | 2 |
6 files changed, 67 insertions, 53 deletions
diff --git a/_dbus_bindings/bus.c b/_dbus_bindings/bus.c index 1e3b81f..5d671ba 100644 --- a/_dbus_bindings/bus.c +++ b/_dbus_bindings/bus.c @@ -103,9 +103,8 @@ DBusPyConnection_GetUniqueName(Connection *self, PyObject *args UNUSED) name = dbus_bus_get_unique_name(self->conn); Py_END_ALLOW_THREADS if (!name) { - PyErr_SetString(DBusPyException, "This connection has no unique " - "name yet"); - return NULL; + return DBusPyException_SetString("This connection has no unique name " + "yet"); } return PyString_FromString(name); } diff --git a/_dbus_bindings/conn-methods.c b/_dbus_bindings/conn-methods.c index 6c1f90f..1b41c46 100644 --- a/_dbus_bindings/conn-methods.c +++ b/_dbus_bindings/conn-methods.c @@ -405,9 +405,8 @@ Connection_send_message_with_reply(Connection *self, PyObject *args, PyObject *k if (!pending) { /* connection is disconnected (doesn't return FALSE!) */ - PyErr_SetString (DBusPyException, "Connection is disconnected - " + return DBusPyException_SetString ("Connection is disconnected - " "unable to make method call"); - return NULL; } return DBusPyPendingCall_ConsumeDBusPendingCall(pending, callable); @@ -473,6 +472,9 @@ Connection_send_message_with_reply_and_block(Connection *self, PyObject *args) timeout_ms, &error); Py_END_ALLOW_THREADS + /* FIXME: if we instead used send_with_reply and blocked on the resulting + * PendingCall, then we could get all args from the error, not just + * the first */ if (!reply) { return DBusPyException_ConsumeError(&error); } diff --git a/_dbus_bindings/dbus_bindings-internal.h b/_dbus_bindings/dbus_bindings-internal.h index 392f58d..2e7f6ff 100644 --- a/_dbus_bindings/dbus_bindings-internal.h +++ b/_dbus_bindings/dbus_bindings-internal.h @@ -65,7 +65,7 @@ extern dbus_bool_t dbus_py_init_bus_types(void); extern dbus_bool_t dbus_py_insert_bus_types(PyObject *this_module); /* exceptions.c */ -extern PyObject *DBusPyException; +extern PyObject *DBusPyException_SetString(const char *msg); extern PyObject *DBusPyException_ConsumeError(DBusError *error); extern dbus_bool_t dbus_py_init_exception_types(void); extern dbus_bool_t dbus_py_insert_exception_types(PyObject *this_module); diff --git a/_dbus_bindings/exceptions.c b/_dbus_bindings/exceptions.c index 3578de1..c5554ba 100644 --- a/_dbus_bindings/exceptions.c +++ b/_dbus_bindings/exceptions.c @@ -22,60 +22,75 @@ #include "dbus_bindings-internal.h" -PyObject *DBusPyException; +static PyObject *imported_dbus_exception = NULL; -PyDoc_STRVAR(DBusException__doc__, "Represents any D-Bus-related error."); +static dbus_bool_t +import_exception(void) +{ + PyObject *name; + PyObject *exceptions; + + if (imported_dbus_exception != NULL) { + return TRUE; + } + + name = PyString_FromString("dbus.exceptions"); + if (name == NULL) { + return FALSE; + } + exceptions = PyImport_Import(name); + Py_DECREF(name); + if (exceptions == NULL) { + return FALSE; + } + imported_dbus_exception = PyObject_GetAttrString(exceptions, + "DBusException"); + Py_DECREF(exceptions); + + return (imported_dbus_exception != NULL); +} PyObject * -DBusPyException_ConsumeError(DBusError *error) +DBusPyException_SetString(const char *msg) { - PyErr_Format(DBusPyException, "%s: %s", - error->name, error->message); - dbus_error_free(error); + if (imported_dbus_exception != NULL || import_exception()) { + PyErr_SetString(imported_dbus_exception, msg); + } return NULL; } -dbus_bool_t -dbus_py_init_exception_types(void) +PyObject * +DBusPyException_ConsumeError(DBusError *error) { - PyObject *bases; - PyObject *dict = PyDict_New(); - PyObject *name; + PyObject *exc_value = NULL; - if (!dict) - return 0; - if (PyDict_SetItemString(dict, "__doc__", - PyString_FromString(DBusException__doc__)) < 0) - return 0; - if (PyDict_SetItemString(dict, "__module__", - PyString_FromString("dbus")) < 0) - return 0; - bases = Py_BuildValue("(O)", (PyObject *)PyExc_Exception); - if (!bases) { - Py_DECREF(dict); - return 0; + if (imported_dbus_exception == NULL && !import_exception()) { + goto finally; } - name = PyString_FromString("DBusException"); - if (!name) { - Py_DECREF(dict); - Py_DECREF(bases); - return 0; - } - DBusPyException = PyClass_New(bases, dict, name); - Py_DECREF(bases); - Py_DECREF(dict); - if (!DBusPyException) - return 0; - return 1; -} -dbus_bool_t -dbus_py_insert_exception_types(PyObject *this_module) -{ - if (PyModule_AddObject(this_module, "DBusException", DBusPyException) < 0) { - return 0; + exc_value = PyObject_CallFunction(imported_dbus_exception, + "s", + error->message ? error->message + : ""); + if (error->name) { + PyObject *name = PyString_FromString(error->name); + int ret; + + if (!name) + goto finally; + ret = PyObject_SetAttrString(exc_value, "_dbus_error_name", name); + Py_DECREF(name); + if (ret < 0) { + goto finally; + } } - return 1; + + PyErr_SetObject(imported_dbus_exception, exc_value); + +finally: + Py_XDECREF(exc_value); + dbus_error_free(error); + return NULL; } /* vim:set ft=c cino< sw=4 sts=4 et: */ diff --git a/_dbus_bindings/message.c b/_dbus_bindings/message.c index 436690e..3887aaa 100644 --- a/_dbus_bindings/message.c +++ b/_dbus_bindings/message.c @@ -36,9 +36,9 @@ static inline int Message_Check(PyObject *o) PyObject * DBusPy_RaiseUnusableMessage(void) { - PyErr_SetString(DBusPyException, - "Message object is uninitialized, or has become unusable " - "due to error while appending arguments"); + DBusPyException_SetString("Message object is uninitialized, or has become " + "unusable due to error while appending " + "arguments"); return NULL; } diff --git a/_dbus_bindings/module.c b/_dbus_bindings/module.c index cb74ac2..a0e5c95 100644 --- a/_dbus_bindings/module.c +++ b/_dbus_bindings/module.c @@ -254,7 +254,6 @@ init_dbus_bindings(void) } if (!dbus_py_init_generic()) return; - if (!dbus_py_init_exception_types()) return; if (!dbus_py_init_abstract()) return; if (!dbus_py_init_signature()) return; if (!dbus_py_init_int_types()) return; @@ -270,7 +269,6 @@ init_dbus_bindings(void) this_module = Py_InitModule3("_dbus_bindings", module_functions, module_doc); if (!this_module) return; - if (!dbus_py_insert_exception_types(this_module)) return; if (!dbus_py_insert_abstract_types(this_module)) return; if (!dbus_py_insert_signature(this_module)) return; if (!dbus_py_insert_int_types(this_module)) return; |