diff options
| author | Simon McVittie <simon.mcvittie@collabora.co.uk> | 2006-12-08 16:10:12 +0000 |
|---|---|---|
| committer | Simon McVittie <simon.mcvittie@collabora.co.uk> | 2006-12-08 16:10:12 +0000 |
| commit | 380b44d38d333092bc9fe2eae8b7a836cb2791c7 (patch) | |
| tree | 24c076244fd24ef36f85727064a958e6247a8273 | |
| parent | 93b846e642ea1830b40aee6dfc600b8cd18b51bf (diff) | |
| download | dbus-python-380b44d38d333092bc9fe2eae8b7a836cb2791c7.tar.gz | |
_dbus_bindings/conn-impl.h: Be more careful that referenced Connections always have a DBusConnection
| -rw-r--r-- | _dbus_bindings/conn-impl.h | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/_dbus_bindings/conn-impl.h b/_dbus_bindings/conn-impl.h index 3c87e4e..4396a04 100644 --- a/_dbus_bindings/conn-impl.h +++ b/_dbus_bindings/conn-impl.h @@ -243,11 +243,19 @@ static dbus_int32_t _connection_python_slot; static DBusConnection * Connection_BorrowDBusConnection(PyObject *self) { + DBusConnection *dbc; + if (!Connection_Check(self)) { PyErr_SetString(PyExc_TypeError, "A dbus.Connection is required"); return NULL; } - return ((Connection *)self)->conn; + dbc = ((Connection *)self)->conn; + if (!dbc) { + PyErr_SetString(PyExc_RuntimeError, "Connection is in an invalid " + "state: no DBusConnection"); + return NULL; + } + return dbc; } /* Internal C API =================================================== */ @@ -480,10 +488,22 @@ Connection_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs) static void Connection_tp_dealloc(Connection *self) { DBusConnection *conn = self->conn; - self->conn = NULL; + PyObject *filters = self->filters; + PyObject *object_paths = self->object_paths; DBG("Deallocating Connection at %p (DBusConnection at %p)", self, conn); + self->filters = NULL; + Py_XDECREF(filters); + self->object_paths = NULL; + Py_XDECREF(object_paths); + + /* make sure to do this last to preserve the invariant that + * self->conn is always non-NULL for any referenced Connection + * (until the filters and object paths were freed, we might have been + * in a reference cycle!) + */ + self->conn = NULL; if (conn) { Py_BEGIN_ALLOW_THREADS dbus_connection_close(conn); @@ -492,8 +512,6 @@ static void Connection_tp_dealloc(Connection *self) dbus_connection_unref(conn); } - Py_XDECREF(self->filters); - Py_XDECREF(self->object_paths); (self->ob_type->tp_free)((PyObject *)self); } |
