Complete the .m4 renaming
[dbus-python-phuang.git] / _dbus_bindings / conn.c
blob06a3f75b0c83b790188764a019e142fe3d5f1d9a
1 /* Implementation of the _dbus_bindings Connection type, a Python wrapper
2 * for DBusConnection. See also conn-methods.c.
4 * Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/>
6 * Licensed under the Academic Free License version 2.1
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus_bindings-internal.h"
25 #include "conn-internal.h"
27 /* Connection definition ============================================ */
29 PyDoc_STRVAR(Connection_tp_doc,
30 "A D-Bus connection.\n"
31 "\n"
32 "::\n"
33 "\n"
34 " Connection(address, mainloop=None) -> Connection\n"
37 /* D-Bus Connection user data slot, containing an owned reference to either
38 * the Connection, or a weakref to the Connection.
40 static dbus_int32_t _connection_python_slot;
42 /* C API for main-loop hooks ======================================== */
44 /* Return a borrowed reference to the DBusConnection which underlies this
45 * Connection. */
46 DBusConnection *
47 DBusPyConnection_BorrowDBusConnection(PyObject *self)
49 DBusConnection *dbc;
51 TRACE(self);
52 if (!DBusPyConnection_Check(self)) {
53 PyErr_SetString(PyExc_TypeError, "A dbus.Connection is required");
54 return NULL;
56 dbc = ((Connection *)self)->conn;
57 if (!dbc) {
58 PyErr_SetString(PyExc_RuntimeError, "Connection is in an invalid "
59 "state: no DBusConnection");
60 return NULL;
62 return dbc;
65 /* Internal C API =================================================== */
67 /* Pass a message through a handler. */
68 DBusHandlerResult
69 DBusPyConnection_HandleMessage(Connection *conn,
70 PyObject *msg,
71 PyObject *callable)
73 PyObject *obj;
75 TRACE(conn);
76 obj = PyObject_CallFunctionObjArgs(callable, conn, msg,
77 NULL);
78 if (obj == Py_None) {
79 DBG("%p: OK, handler %p returned None", conn, callable);
80 Py_DECREF(obj);
81 return DBUS_HANDLER_RESULT_HANDLED;
83 else if (obj == Py_NotImplemented) {
84 DBG("%p: handler %p returned NotImplemented, continuing",
85 conn, callable);
86 Py_DECREF(obj);
87 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
89 else if (!obj) {
90 if (PyErr_ExceptionMatches(PyExc_MemoryError)) {
91 DBG_EXC("%p: handler %p caused OOM", conn, callable);
92 PyErr_Clear();
93 return DBUS_HANDLER_RESULT_NEED_MEMORY;
95 DBG_EXC("%p: handler %p raised exception", conn, callable);
96 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
98 else {
99 long i = PyInt_AsLong(obj);
100 DBG("%p: handler %p returned %ld", conn, callable, i);
101 Py_DECREF(obj);
102 if (i == -1 && PyErr_Occurred()) {
103 PyErr_SetString(PyExc_TypeError, "Return from D-Bus message "
104 "handler callback should be None, "
105 "NotImplemented or integer");
106 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
108 else if (i == DBUS_HANDLER_RESULT_HANDLED ||
109 i == DBUS_HANDLER_RESULT_NOT_YET_HANDLED ||
110 i == DBUS_HANDLER_RESULT_NEED_MEMORY) {
111 return i;
113 else {
114 PyErr_Format(PyExc_ValueError, "Integer return from "
115 "D-Bus message handler callback should "
116 "be a DBUS_HANDLER_RESULT_... constant, "
117 "not %d", (int)i);
118 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
123 /* On KeyError or if unregistration is in progress, return None. */
124 PyObject *
125 DBusPyConnection_GetObjectPathHandlers(PyObject *self, PyObject *path)
127 PyObject *callbacks;
129 TRACE(self);
130 callbacks = PyDict_GetItem(((Connection *)self)->object_paths, path);
131 if (!callbacks) {
132 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
133 PyErr_Clear();
134 Py_RETURN_NONE;
137 Py_INCREF(callbacks);
138 return callbacks;
141 /* Return a new reference to a Python Connection or subclass corresponding
142 * to the DBusConnection conn. For use in callbacks.
144 * Raises AssertionError if the DBusConnection does not have a Connection.
146 PyObject *
147 DBusPyConnection_ExistingFromDBusConnection(DBusConnection *conn)
149 PyObject *self, *ref;
151 Py_BEGIN_ALLOW_THREADS
152 ref = (PyObject *)dbus_connection_get_data(conn,
153 _connection_python_slot);
154 Py_END_ALLOW_THREADS
155 if (ref) {
156 DBG("(DBusConnection *)%p has weak reference at %p", conn, ref);
157 self = PyWeakref_GetObject(ref); /* still a borrowed ref */
158 if (self && self != Py_None && DBusPyConnection_Check(self)) {
159 DBG("(DBusConnection *)%p has weak reference at %p pointing to %p",
160 conn, ref, self);
161 TRACE(self);
162 Py_INCREF(self);
163 TRACE(self);
164 return self;
168 PyErr_SetString(PyExc_AssertionError,
169 "D-Bus connection does not have a Connection "
170 "instance associated with it");
171 return NULL;
174 /* Return a new reference to a Python Connection or subclass (given by cls)
175 * corresponding to the DBusConnection conn, which must have been newly
176 * created. For use by the Connection and Bus constructors.
178 * Raises AssertionError if the DBusConnection already has a Connection.
180 PyObject *
181 DBusPyConnection_NewConsumingDBusConnection(PyTypeObject *cls,
182 DBusConnection *conn,
183 PyObject *mainloop)
185 Connection *self = NULL;
186 PyObject *ref;
187 dbus_bool_t ok;
189 DBG("%s(cls=%p, conn=%p, mainloop=%p)", __func__, cls, conn, mainloop);
190 DBUS_PY_RAISE_VIA_NULL_IF_FAIL(conn);
192 Py_BEGIN_ALLOW_THREADS
193 ref = (PyObject *)dbus_connection_get_data(conn,
194 _connection_python_slot);
195 Py_END_ALLOW_THREADS
196 if (ref) {
197 self = (Connection *)PyWeakref_GetObject(ref);
198 ref = NULL;
199 if (self && (PyObject *)self != Py_None) {
200 self = NULL;
201 PyErr_SetString(PyExc_AssertionError,
202 "Newly created D-Bus connection already has a "
203 "Connection instance associated with it");
204 DBG("%s() fail - assertion failed, DBusPyConn has a DBusConn already", __func__);
205 DBG_WHEREAMI;
206 return NULL;
209 ref = NULL;
211 /* Change mainloop from a borrowed reference to an owned reference */
212 if (!mainloop || mainloop == Py_None) {
213 mainloop = dbus_py_get_default_main_loop();
214 if (!mainloop)
215 goto err;
217 else {
218 Py_INCREF(mainloop);
221 DBG("Constructing Connection from DBusConnection at %p", conn);
223 self = (Connection *)(cls->tp_alloc(cls, 0));
224 if (!self) goto err;
225 TRACE(self);
227 DBG_WHEREAMI;
229 self->has_mainloop = (mainloop != Py_None);
230 self->conn = NULL;
231 self->filters = PyList_New(0);
232 if (!self->filters) goto err;
233 self->object_paths = PyDict_New();
234 if (!self->object_paths) goto err;
236 ref = PyWeakref_NewRef((PyObject *)self, NULL);
237 if (!ref) goto err;
238 DBG("Created weak ref %p to (Connection *)%p for (DBusConnection *)%p",
239 ref, self, conn);
241 Py_BEGIN_ALLOW_THREADS
242 ok = dbus_connection_set_data(conn, _connection_python_slot,
243 (void *)ref,
244 (DBusFreeFunction)dbus_py_take_gil_and_xdecref);
245 Py_END_ALLOW_THREADS
247 if (ok) {
248 DBG("Attached weak ref %p ((Connection *)%p) to (DBusConnection *)%p",
249 ref, self, conn);
250 ref = NULL; /* don't DECREF it - the DBusConnection owns it now */
252 else {
253 DBG("Failed to attached weak ref %p ((Connection *)%p) to "
254 "(DBusConnection *)%p - will dispose of it", ref, self, conn);
255 PyErr_NoMemory();
256 goto err;
259 DBUS_PY_RAISE_VIA_GOTO_IF_FAIL(conn, err);
260 self->conn = conn;
261 /* the DBusPyConnection will close it now */
262 conn = NULL;
264 if (self->has_mainloop
265 && !dbus_py_set_up_connection((PyObject *)self, mainloop)) {
266 goto err;
269 Py_DECREF(mainloop);
271 DBG("%s() -> %p", __func__, self);
272 TRACE(self);
273 return (PyObject *)self;
275 err:
276 DBG("Failed to construct Connection from DBusConnection at %p", conn);
277 Py_XDECREF(mainloop);
278 Py_XDECREF(self);
279 Py_XDECREF(ref);
280 if (conn) {
281 Py_BEGIN_ALLOW_THREADS
282 dbus_connection_close(conn);
283 dbus_connection_unref(conn);
284 Py_END_ALLOW_THREADS
286 DBG("%s() fail", __func__);
287 DBG_WHEREAMI;
288 return NULL;
291 /* Connection type-methods ========================================== */
293 /* "Constructor" (the real constructor is Connection_NewFromDBusConnection,
294 * to which this delegates). */
295 static PyObject *
296 Connection_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
298 DBusConnection *conn;
299 const char *address;
300 DBusError error;
301 PyObject *self, *mainloop = NULL;
302 static char *argnames[] = {"address", "mainloop", NULL};
304 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|O", argnames,
305 &address, &mainloop)) {
306 return NULL;
309 dbus_error_init(&error);
311 /* We always open a private connection (at the libdbus level). Sharing
312 * is done in Python, to keep things simple. */
313 Py_BEGIN_ALLOW_THREADS
314 conn = dbus_connection_open_private(address, &error);
315 Py_END_ALLOW_THREADS
317 if (!conn) {
318 DBusPyException_ConsumeError(&error);
319 return NULL;
321 self = DBusPyConnection_NewConsumingDBusConnection(cls, conn, mainloop);
322 TRACE(self);
324 return self;
327 /* Destructor */
328 static void Connection_tp_dealloc(Connection *self)
330 DBusConnection *conn = self->conn;
331 PyObject *et, *ev, *etb;
332 PyObject *filters = self->filters;
333 PyObject *object_paths = self->object_paths;
335 /* avoid clobbering any pending exception */
336 PyErr_Fetch(&et, &ev, &etb);
338 if (self->weaklist) {
339 PyObject_ClearWeakRefs((PyObject *)self);
342 TRACE(self);
343 DBG("Deallocating Connection at %p (DBusConnection at %p)", self, conn);
344 DBG_WHEREAMI;
346 DBG("Connection at %p: deleting callbacks", self);
347 self->filters = NULL;
348 Py_XDECREF(filters);
349 self->object_paths = NULL;
350 Py_XDECREF(object_paths);
352 if (conn) {
353 /* Might trigger callbacks if we're unlucky... */
354 DBG("Connection at %p has a conn, closing it...", self);
355 Py_BEGIN_ALLOW_THREADS
356 dbus_connection_close(conn);
357 Py_END_ALLOW_THREADS
360 /* make sure to do this last to preserve the invariant that
361 * self->conn is always non-NULL for any referenced Connection
362 * (until the filters and object paths were freed, we might have been
363 * in a reference cycle!)
365 DBG("Connection at %p: nulling self->conn", self);
366 self->conn = NULL;
368 if (conn) {
369 DBG("Connection at %p: unreffing conn", self);
370 dbus_connection_unref(conn);
373 DBG("Connection at %p: freeing self", self);
374 PyErr_Restore(et, ev, etb);
375 (self->ob_type->tp_free)((PyObject *)self);
378 /* Connection type object =========================================== */
380 PyTypeObject DBusPyConnection_Type = {
381 PyObject_HEAD_INIT(NULL)
382 0, /*ob_size*/
383 "_dbus_bindings.Connection", /*tp_name*/
384 sizeof(Connection), /*tp_basicsize*/
385 0, /*tp_itemsize*/
386 /* methods */
387 (destructor)Connection_tp_dealloc,
388 0, /*tp_print*/
389 0, /*tp_getattr*/
390 0, /*tp_setattr*/
391 0, /*tp_compare*/
392 0, /*tp_repr*/
393 0, /*tp_as_number*/
394 0, /*tp_as_sequence*/
395 0, /*tp_as_mapping*/
396 0, /*tp_hash*/
397 0, /*tp_call*/
398 0, /*tp_str*/
399 0, /*tp_getattro*/
400 0, /*tp_setattro*/
401 0, /*tp_as_buffer*/
402 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS | Py_TPFLAGS_BASETYPE,
403 Connection_tp_doc, /*tp_doc*/
404 0, /*tp_traverse*/
405 0, /*tp_clear*/
406 0, /*tp_richcompare*/
407 offsetof(Connection, weaklist), /*tp_weaklistoffset*/
408 0, /*tp_iter*/
409 0, /*tp_iternext*/
410 DBusPyConnection_tp_methods, /*tp_methods*/
411 0, /*tp_members*/
412 0, /*tp_getset*/
413 0, /*tp_base*/
414 0, /*tp_dict*/
415 0, /*tp_descr_get*/
416 0, /*tp_descr_set*/
417 0, /*tp_dictoffset*/
418 0, /*tp_init*/
419 0, /*tp_alloc*/
420 Connection_tp_new, /*tp_new*/
421 0, /*tp_free*/
422 0, /*tp_is_gc*/
425 dbus_bool_t
426 dbus_py_init_conn_types(void)
428 /* Get a slot to store our weakref on DBus Connections */
429 _connection_python_slot = -1;
430 if (!dbus_connection_allocate_data_slot(&_connection_python_slot))
431 return FALSE;
432 if (PyType_Ready(&DBusPyConnection_Type) < 0)
433 return FALSE;
434 return TRUE;
437 dbus_bool_t
438 dbus_py_insert_conn_types(PyObject *this_module)
440 if (PyModule_AddObject(this_module, "Connection",
441 (PyObject *)&DBusPyConnection_Type) < 0) return FALSE;
442 return TRUE;
445 /* vim:set ft=c cino< sw=4 sts=4 et: */