diff options
author | Simon McVittie <simon.mcvittie@collabora.co.uk> | 2008-07-15 19:15:42 +0100 |
---|---|---|
committer | Simon McVittie <simon.mcvittie@collabora.co.uk> | 2008-07-15 19:15:42 +0100 |
commit | bdc76e63da5ca9e017cfbea6c1ce1b0e21ebf706 (patch) | |
tree | 0692438d2f00a3c0c2fa5c8c1ba9abafd54b1d84 | |
parent | 0f0193180b7853cfea486d0aef1b60d4fa067ed2 (diff) | |
download | dbus-python-bdc76e63da5ca9e017cfbea6c1ce1b0e21ebf706.tar.gz |
Refactor DBusPyConnection_NewForBus and make it go via DbusPyConnection_Type.tp_new
Now that we have the LibDBusConnection temporary objects, we don't have
to do strange sideways inheritance, we can just chain up to the superclass
constructor.
-rw-r--r-- | _dbus_bindings/bus.c | 85 |
1 files changed, 59 insertions, 26 deletions
diff --git a/_dbus_bindings/bus.c b/_dbus_bindings/bus.c index 5d64a6a..7ab0d95 100644 --- a/_dbus_bindings/bus.c +++ b/_dbus_bindings/bus.c @@ -33,8 +33,6 @@ DBusPyConnection_NewForBus(PyTypeObject *cls, PyObject *args, PyObject *kwargs) DBusConnection *conn; DBusError error; Connection *self; - dbus_bool_t ret; - long type; static char *argnames[] = {"address_or_type", "mainloop", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO", argnames, @@ -45,6 +43,8 @@ DBusPyConnection_NewForBus(PyTypeObject *cls, PyObject *args, PyObject *kwargs) dbus_error_init(&error); if (first && PyString_Check(first)) { + dbus_bool_t ret; + /* It's a custom address. First connect to it, then register. */ self = (Connection *)(DBusPyConnection_Type.tp_new)(cls, args, kwargs); @@ -62,37 +62,70 @@ DBusPyConnection_NewForBus(PyTypeObject *cls, PyObject *args, PyObject *kwargs) return (PyObject *)self; } + else if (!first || PyInt_Check(first)) { + long type; + PyObject *libdbusconn; + PyObject *new_args; + PyObject *new_kwargs; + + /* If the first argument isn't a string, it must be an integer + representing one of the well-known bus types. The default is + DBUS_BUS_SESSION. */ + + if (first) { + type = PyInt_AsLong(first); + + if (type != DBUS_BUS_SESSION && type != DBUS_BUS_SYSTEM + && type != DBUS_BUS_STARTER) { + PyErr_Format(PyExc_ValueError, "Unknown bus type %ld", type); + return NULL; + } + } + else { + type = DBUS_BUS_SESSION; + } - /* If the first argument isn't a string, it must be an integer - representing one of the well-known bus types. */ + Py_BEGIN_ALLOW_THREADS + conn = dbus_bus_get_private(type, &error); + Py_END_ALLOW_THREADS - if (first && !PyInt_Check(first)) { - PyErr_SetString(PyExc_TypeError, "A string address or an integer " - "bus type is required"); - return NULL; - } - if (first) { - type = PyInt_AsLong(first); - } - else { - type = DBUS_BUS_SESSION; - } + if (!conn) { + DBusPyException_ConsumeError(&error); + return NULL; + } - if (type != DBUS_BUS_SESSION && type != DBUS_BUS_SYSTEM - && type != DBUS_BUS_STARTER) { - PyErr_Format(PyExc_ValueError, "Unknown bus type %d", (int)type); - return NULL; - } + libdbusconn = DBusPyLibDBusConnection_New (conn); + dbus_connection_unref (conn); - Py_BEGIN_ALLOW_THREADS - conn = dbus_bus_get_private(type, &error); - Py_END_ALLOW_THREADS + if (!libdbusconn) + return NULL; + + new_args = PyTuple_Pack(2, libdbusconn, mainloop ? mainloop : Py_None); + Py_DECREF(libdbusconn); + + if (!new_args) { + return NULL; + } + + new_kwargs = PyDict_New(); - if (!conn) { - DBusPyException_ConsumeError(&error); + if (!new_kwargs) { + Py_DECREF(new_args); + return NULL; + } + + self = (Connection *)(DBusPyConnection_Type.tp_new)(cls, new_args, + new_kwargs); + Py_DECREF(new_args); + Py_DECREF(new_kwargs); + + return (PyObject *)self; /* whether NULL or not */ + } + else { + PyErr_SetString(PyExc_TypeError, "A string address or an integer " + "bus type is required"); return NULL; } - return DBusPyConnection_NewConsumingDBusConnection(cls, conn, mainloop); } PyObject * |