From 9d9322f9faf49f7ac8b4c3048f1333a9d7cabb48 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 15 Jul 2008 18:54:54 +0100 Subject: Add LibDBusConnection in _dbus_bindings --- _dbus_bindings/Makefile.am | 1 + _dbus_bindings/conn-internal.h | 5 ++ _dbus_bindings/dbus_bindings-internal.h | 16 ++++- _dbus_bindings/libdbusconn.c | 124 ++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 _dbus_bindings/libdbusconn.c diff --git a/_dbus_bindings/Makefile.am b/_dbus_bindings/Makefile.am index e9e2421..c6cd1ee 100644 --- a/_dbus_bindings/Makefile.am +++ b/_dbus_bindings/Makefile.am @@ -17,6 +17,7 @@ _dbus_bindings_la_SOURCES = \ float.c \ generic.c \ int.c \ + libdbusconn.c \ mainloop.c \ message-append.c \ message.c \ diff --git a/_dbus_bindings/conn-internal.h b/_dbus_bindings/conn-internal.h index 4f83c39..f4c7a80 100644 --- a/_dbus_bindings/conn-internal.h +++ b/_dbus_bindings/conn-internal.h @@ -46,6 +46,11 @@ typedef struct { dbus_bool_t has_mainloop; } Connection; +typedef struct { + PyObject_HEAD + DBusConnection *conn; +} DBusPyLibDBusConnection; + extern struct PyMethodDef DBusPyConnection_tp_methods[]; extern DBusHandlerResult DBusPyConnection_HandleMessage(Connection *, PyObject *, diff --git a/_dbus_bindings/dbus_bindings-internal.h b/_dbus_bindings/dbus_bindings-internal.h index 6b801c6..efc9f40 100644 --- a/_dbus_bindings/dbus_bindings-internal.h +++ b/_dbus_bindings/dbus_bindings-internal.h @@ -52,17 +52,27 @@ typedef int Py_ssize_t; static inline int type##_Check (PyObject *o) \ { \ return (PyObject_TypeCheck (o, &type##_Type)); \ +} \ +static inline int type##_CheckExact (PyObject *o) \ +{ \ + return ((o)->ob_type == &type##_Type); \ } +PyMODINIT_FUNC init_dbus_bindings(void); + /* conn.c */ extern PyTypeObject DBusPyConnection_Type; DEFINE_CHECK(DBusPyConnection) -extern PyObject *DBusPyConnection_NewConsumingDBusConnection(PyTypeObject *, - DBusConnection *, - PyObject *); extern dbus_bool_t dbus_py_init_conn_types(void); extern dbus_bool_t dbus_py_insert_conn_types(PyObject *this_module); +/* libdbusconn.c */ +extern PyTypeObject DBusPyLibDBusConnection_Type; +DEFINE_CHECK(DBusPyLibDBusConnection) +PyObject *DBusPyLibDBusConnection_New(DBusConnection *conn); +extern dbus_bool_t dbus_py_init_libdbus_conn_types(void); +extern dbus_bool_t dbus_py_insert_libdbus_conn_types(PyObject *this_module); + /* bus.c */ extern dbus_bool_t dbus_py_init_bus_types(void); extern dbus_bool_t dbus_py_insert_bus_types(PyObject *this_module); diff --git a/_dbus_bindings/libdbusconn.c b/_dbus_bindings/libdbusconn.c new file mode 100644 index 0000000..9bd8def --- /dev/null +++ b/_dbus_bindings/libdbusconn.c @@ -0,0 +1,124 @@ +/* An extremely thin wrapper around a libdbus Connection, for use by + * Server. + * + * Copyright (C) 2008 Collabora Ltd. + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include "dbus_bindings-internal.h" +#include "conn-internal.h" + +PyDoc_STRVAR(DBusPyLibDBusConnection_tp_doc, +"A reference to a ``DBusConnection`` from ``libdbus``, which might not\n" +"have been attached to a `dbus.connection.Connection` yet.\n" +"\n" +"Cannot be instantiated from Python. The only use of this object is to\n" +"pass it to the ``dbus.connection.Connection`` constructor instead of an\n" +"address.\n" +); + +/** Create a DBusPyLibDBusConnection from a DBusConnection. + */ +PyObject * +DBusPyLibDBusConnection_New(DBusConnection *conn) +{ + DBusPyLibDBusConnection *self = NULL; + + DBUS_PY_RAISE_VIA_NULL_IF_FAIL(conn); + + self = (DBusPyLibDBusConnection *)(DBusPyLibDBusConnection_Type.tp_alloc( + &DBusPyLibDBusConnection_Type, 0)); + + if (!self) + return NULL; + + self->conn = dbus_connection_ref (conn); + + return (PyObject *)self; +} + +/* Destructor */ +static void +DBusPyLibDBusConnection_tp_dealloc(Connection *self) +{ + DBusConnection *conn = self->conn; + PyObject *et, *ev, *etb; + + /* avoid clobbering any pending exception */ + PyErr_Fetch(&et, &ev, &etb); + + self->conn = NULL; + + if (conn) { + dbus_connection_unref(conn); + } + + PyErr_Restore(et, ev, etb); + (self->ob_type->tp_free)((PyObject *) self); +} + +PyTypeObject DBusPyLibDBusConnection_Type = { + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "_dbus_bindings._LibDBusConnection", + sizeof(DBusPyLibDBusConnection), + 0, /*tp_itemsize*/ + /* methods */ + (destructor)DBusPyLibDBusConnection_tp_dealloc, + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT, + DBusPyLibDBusConnection_tp_doc, +}; + +dbus_bool_t +dbus_py_init_libdbus_conn_types(void) +{ + if (PyType_Ready(&DBusPyLibDBusConnection_Type) < 0) + return FALSE; + + return TRUE; +} + +dbus_bool_t +dbus_py_insert_libdbus_conn_types(PyObject *this_module) +{ + if (PyModule_AddObject(this_module, "_LibDBusConnection", + (PyObject *)&DBusPyLibDBusConnection_Type) < 0) + return FALSE; + + return TRUE; +} + +/* vim:set ft=c cino< sw=4 sts=4 et: */ -- cgit v1.2.1