|
Packit |
130fc8 |
/* Implementation of the _dbus_bindings Connection type, a Python wrapper
|
|
Packit |
130fc8 |
* for DBusConnection. See also conn-methods.c.
|
|
Packit |
130fc8 |
*
|
|
Packit |
130fc8 |
* Copyright (C) 2006-2008 Collabora Ltd. <http://www.collabora.co.uk/>
|
|
Packit |
130fc8 |
*
|
|
Packit |
130fc8 |
* Permission is hereby granted, free of charge, to any person
|
|
Packit |
130fc8 |
* obtaining a copy of this software and associated documentation
|
|
Packit |
130fc8 |
* files (the "Software"), to deal in the Software without
|
|
Packit |
130fc8 |
* restriction, including without limitation the rights to use, copy,
|
|
Packit |
130fc8 |
* modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
Packit |
130fc8 |
* of the Software, and to permit persons to whom the Software is
|
|
Packit |
130fc8 |
* furnished to do so, subject to the following conditions:
|
|
Packit |
130fc8 |
*
|
|
Packit |
130fc8 |
* The above copyright notice and this permission notice shall be
|
|
Packit |
130fc8 |
* included in all copies or substantial portions of the Software.
|
|
Packit |
130fc8 |
*
|
|
Packit |
130fc8 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
Packit |
130fc8 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
Packit |
130fc8 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
Packit |
130fc8 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
Packit |
130fc8 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
Packit |
130fc8 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
Packit |
130fc8 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
Packit |
130fc8 |
* DEALINGS IN THE SOFTWARE.
|
|
Packit |
130fc8 |
*/
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
#include "dbus_bindings-internal.h"
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
#include "conn-internal.h"
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
/* Connection definition ============================================ */
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
PyDoc_STRVAR(Connection_tp_doc,
|
|
Packit |
130fc8 |
"A D-Bus connection.\n"
|
|
Packit |
130fc8 |
"\n"
|
|
Packit |
130fc8 |
"::\n"
|
|
Packit |
130fc8 |
"\n"
|
|
Packit |
130fc8 |
" Connection(address, mainloop=None) -> Connection\n"
|
|
Packit |
130fc8 |
);
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
/* D-Bus Connection user data slot, containing an owned reference to either
|
|
Packit |
130fc8 |
* the Connection, or a weakref to the Connection.
|
|
Packit |
130fc8 |
*/
|
|
Packit |
130fc8 |
static dbus_int32_t _connection_python_slot;
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
/* C API for main-loop hooks ======================================== */
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
/* Return a borrowed reference to the DBusConnection which underlies this
|
|
Packit |
130fc8 |
* Connection. */
|
|
Packit |
130fc8 |
DBusConnection *
|
|
Packit |
130fc8 |
DBusPyConnection_BorrowDBusConnection(PyObject *self)
|
|
Packit |
130fc8 |
{
|
|
Packit |
130fc8 |
DBusConnection *dbc;
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
TRACE(self);
|
|
Packit |
130fc8 |
if (!DBusPyConnection_Check(self)) {
|
|
Packit |
130fc8 |
PyErr_SetString(PyExc_TypeError, "A dbus.Connection is required");
|
|
Packit |
130fc8 |
return NULL;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
dbc = ((Connection *)self)->conn;
|
|
Packit |
130fc8 |
if (!dbc) {
|
|
Packit |
130fc8 |
PyErr_SetString(PyExc_RuntimeError, "Connection is in an invalid "
|
|
Packit |
130fc8 |
"state: no DBusConnection");
|
|
Packit |
130fc8 |
return NULL;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
return dbc;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
/* Internal C API =================================================== */
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
/* Pass a message through a handler. */
|
|
Packit |
130fc8 |
DBusHandlerResult
|
|
Packit |
130fc8 |
DBusPyConnection_HandleMessage(Connection *conn,
|
|
Packit |
130fc8 |
PyObject *msg,
|
|
Packit |
130fc8 |
PyObject *callable)
|
|
Packit |
130fc8 |
{
|
|
Packit |
130fc8 |
PyObject *obj;
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
TRACE(conn);
|
|
Packit |
130fc8 |
obj = PyObject_CallFunctionObjArgs(callable, conn, msg,
|
|
Packit |
130fc8 |
NULL);
|
|
Packit |
130fc8 |
if (obj == Py_None) {
|
|
Packit |
130fc8 |
DBG("%p: OK, handler %p returned None", conn, callable);
|
|
Packit |
130fc8 |
Py_CLEAR(obj);
|
|
Packit |
130fc8 |
return DBUS_HANDLER_RESULT_HANDLED;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
else if (obj == Py_NotImplemented) {
|
|
Packit |
130fc8 |
DBG("%p: handler %p returned NotImplemented, continuing",
|
|
Packit |
130fc8 |
conn, callable);
|
|
Packit |
130fc8 |
Py_CLEAR(obj);
|
|
Packit |
130fc8 |
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
else if (!obj) {
|
|
Packit |
130fc8 |
if (PyErr_ExceptionMatches(PyExc_MemoryError)) {
|
|
Packit |
130fc8 |
DBG_EXC("%p: handler %p caused OOM", conn, callable);
|
|
Packit |
130fc8 |
PyErr_Clear();
|
|
Packit |
130fc8 |
return DBUS_HANDLER_RESULT_NEED_MEMORY;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
DBG_EXC("%p: handler %p raised exception", conn, callable);
|
|
Packit |
130fc8 |
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
else {
|
|
Packit |
130fc8 |
long i = PyLong_AsLong(obj);
|
|
Packit |
130fc8 |
DBG("%p: handler %p returned %ld", conn, callable, i);
|
|
Packit |
130fc8 |
Py_CLEAR(obj);
|
|
Packit |
130fc8 |
if (i == -1 && PyErr_Occurred()) {
|
|
Packit |
130fc8 |
PyErr_SetString(PyExc_TypeError, "Return from D-Bus message "
|
|
Packit |
130fc8 |
"handler callback should be None, "
|
|
Packit |
130fc8 |
"NotImplemented or integer");
|
|
Packit |
130fc8 |
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
else if (i == DBUS_HANDLER_RESULT_HANDLED ||
|
|
Packit |
130fc8 |
i == DBUS_HANDLER_RESULT_NOT_YET_HANDLED ||
|
|
Packit |
130fc8 |
i == DBUS_HANDLER_RESULT_NEED_MEMORY) {
|
|
Packit |
130fc8 |
return i;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
else {
|
|
Packit |
130fc8 |
PyErr_Format(PyExc_ValueError, "Integer return from "
|
|
Packit |
130fc8 |
"D-Bus message handler callback should "
|
|
Packit |
130fc8 |
"be a DBUS_HANDLER_RESULT_... constant, "
|
|
Packit |
130fc8 |
"not %d", (int)i);
|
|
Packit |
130fc8 |
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
/* On KeyError or if unregistration is in progress, return None. */
|
|
Packit |
130fc8 |
PyObject *
|
|
Packit |
130fc8 |
DBusPyConnection_GetObjectPathHandlers(PyObject *self, PyObject *path)
|
|
Packit |
130fc8 |
{
|
|
Packit |
130fc8 |
PyObject *callbacks;
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
TRACE(self);
|
|
Packit |
130fc8 |
callbacks = PyDict_GetItem(((Connection *)self)->object_paths, path);
|
|
Packit |
130fc8 |
if (!callbacks) {
|
|
Packit |
130fc8 |
if (PyErr_ExceptionMatches(PyExc_KeyError)) {
|
|
Packit |
130fc8 |
PyErr_Clear();
|
|
Packit |
130fc8 |
Py_RETURN_NONE;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
Py_INCREF(callbacks);
|
|
Packit |
130fc8 |
return callbacks;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
/* Return a new reference to a Python Connection or subclass corresponding
|
|
Packit |
130fc8 |
* to the DBusConnection conn. For use in callbacks.
|
|
Packit |
130fc8 |
*
|
|
Packit |
130fc8 |
* Raises AssertionError if the DBusConnection does not have a Connection.
|
|
Packit |
130fc8 |
*/
|
|
Packit |
130fc8 |
PyObject *
|
|
Packit |
130fc8 |
DBusPyConnection_ExistingFromDBusConnection(DBusConnection *conn)
|
|
Packit |
130fc8 |
{
|
|
Packit |
130fc8 |
PyObject *self, *ref;
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
Py_BEGIN_ALLOW_THREADS
|
|
Packit |
130fc8 |
ref = (PyObject *)dbus_connection_get_data(conn,
|
|
Packit |
130fc8 |
_connection_python_slot);
|
|
Packit |
130fc8 |
Py_END_ALLOW_THREADS
|
|
Packit |
130fc8 |
if (ref) {
|
|
Packit |
130fc8 |
DBG("(DBusConnection *)%p has weak reference at %p", conn, ref);
|
|
Packit |
130fc8 |
self = PyWeakref_GetObject(ref); /* still a borrowed ref */
|
|
Packit |
130fc8 |
if (self && self != Py_None && DBusPyConnection_Check(self)) {
|
|
Packit |
130fc8 |
DBG("(DBusConnection *)%p has weak reference at %p pointing to %p",
|
|
Packit |
130fc8 |
conn, ref, self);
|
|
Packit |
130fc8 |
TRACE(self);
|
|
Packit |
130fc8 |
Py_INCREF(self);
|
|
Packit |
130fc8 |
TRACE(self);
|
|
Packit |
130fc8 |
return self;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
PyErr_SetString(PyExc_AssertionError,
|
|
Packit |
130fc8 |
"D-Bus connection does not have a Connection "
|
|
Packit |
130fc8 |
"instance associated with it");
|
|
Packit |
130fc8 |
return NULL;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
/* Return a new reference to a Python Connection or subclass (given by cls)
|
|
Packit |
130fc8 |
* corresponding to the DBusConnection conn, which must have been newly
|
|
Packit |
130fc8 |
* created. For use by the Connection and Bus constructors.
|
|
Packit |
130fc8 |
*
|
|
Packit |
130fc8 |
* Raises AssertionError if the DBusConnection already has a Connection.
|
|
Packit |
130fc8 |
*/
|
|
Packit |
130fc8 |
static PyObject *
|
|
Packit |
130fc8 |
DBusPyConnection_NewConsumingDBusConnection(PyTypeObject *cls,
|
|
Packit |
130fc8 |
DBusConnection *conn,
|
|
Packit |
130fc8 |
PyObject *mainloop)
|
|
Packit |
130fc8 |
{
|
|
Packit |
130fc8 |
Connection *self = NULL;
|
|
Packit |
130fc8 |
PyObject *ref;
|
|
Packit |
130fc8 |
dbus_bool_t ok;
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
DBG("%s(cls=%p, conn=%p, mainloop=%p)", __func__, cls, conn, mainloop);
|
|
Packit |
130fc8 |
DBUS_PY_RAISE_VIA_NULL_IF_FAIL(conn);
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
Py_BEGIN_ALLOW_THREADS
|
|
Packit |
130fc8 |
ref = (PyObject *)dbus_connection_get_data(conn,
|
|
Packit |
130fc8 |
_connection_python_slot);
|
|
Packit |
130fc8 |
Py_END_ALLOW_THREADS
|
|
Packit |
130fc8 |
if (ref) {
|
|
Packit |
130fc8 |
self = (Connection *)PyWeakref_GetObject(ref);
|
|
Packit |
130fc8 |
ref = NULL;
|
|
Packit |
130fc8 |
if (self && (PyObject *)self != Py_None) {
|
|
Packit |
130fc8 |
self = NULL;
|
|
Packit |
130fc8 |
PyErr_SetString(PyExc_AssertionError,
|
|
Packit |
130fc8 |
"Newly created D-Bus connection already has a "
|
|
Packit |
130fc8 |
"Connection instance associated with it");
|
|
Packit |
130fc8 |
DBG("%s() fail - assertion failed, DBusPyConn has a DBusConn already", __func__);
|
|
Packit |
130fc8 |
DBG_WHEREAMI;
|
|
Packit |
130fc8 |
return NULL;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
ref = NULL;
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
/* Change mainloop from a borrowed reference to an owned reference */
|
|
Packit |
130fc8 |
if (!mainloop || mainloop == Py_None) {
|
|
Packit |
130fc8 |
mainloop = dbus_py_get_default_main_loop();
|
|
Packit |
130fc8 |
if (!mainloop)
|
|
Packit |
130fc8 |
goto err;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
else {
|
|
Packit |
130fc8 |
Py_INCREF(mainloop);
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
DBG("Constructing Connection from DBusConnection at %p", conn);
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
self = (Connection *)(cls->tp_alloc(cls, 0));
|
|
Packit |
130fc8 |
if (!self) goto err;
|
|
Packit |
130fc8 |
TRACE(self);
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
DBG_WHEREAMI;
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
self->has_mainloop = (mainloop != Py_None);
|
|
Packit |
130fc8 |
self->conn = NULL;
|
|
Packit |
130fc8 |
self->filters = PyList_New(0);
|
|
Packit |
130fc8 |
self->weaklist = NULL;
|
|
Packit |
130fc8 |
if (!self->filters) goto err;
|
|
Packit |
130fc8 |
self->object_paths = PyDict_New();
|
|
Packit |
130fc8 |
if (!self->object_paths) goto err;
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
ref = PyWeakref_NewRef((PyObject *)self, NULL);
|
|
Packit |
130fc8 |
if (!ref) goto err;
|
|
Packit |
130fc8 |
DBG("Created weak ref %p to (Connection *)%p for (DBusConnection *)%p",
|
|
Packit |
130fc8 |
ref, self, conn);
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
Py_BEGIN_ALLOW_THREADS
|
|
Packit |
130fc8 |
ok = dbus_connection_set_data(conn, _connection_python_slot,
|
|
Packit |
130fc8 |
(void *)ref,
|
|
Packit |
130fc8 |
(DBusFreeFunction)dbus_py_take_gil_and_xdecref);
|
|
Packit |
130fc8 |
Py_END_ALLOW_THREADS
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
if (ok) {
|
|
Packit |
130fc8 |
DBG("Attached weak ref %p ((Connection *)%p) to (DBusConnection *)%p",
|
|
Packit |
130fc8 |
ref, self, conn);
|
|
Packit |
130fc8 |
ref = NULL; /* don't DECREF it - the DBusConnection owns it now */
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
else {
|
|
Packit |
130fc8 |
DBG("Failed to attached weak ref %p ((Connection *)%p) to "
|
|
Packit |
130fc8 |
"(DBusConnection *)%p - will dispose of it", ref, self, conn);
|
|
Packit |
130fc8 |
PyErr_NoMemory();
|
|
Packit |
130fc8 |
goto err;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
DBUS_PY_RAISE_VIA_GOTO_IF_FAIL(conn, err);
|
|
Packit |
130fc8 |
self->conn = conn;
|
|
Packit |
130fc8 |
/* the DBusPyConnection will close it now */
|
|
Packit |
130fc8 |
conn = NULL;
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
if (self->has_mainloop
|
|
Packit |
130fc8 |
&& !dbus_py_set_up_connection((PyObject *)self, mainloop)) {
|
|
Packit |
130fc8 |
goto err;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
Py_CLEAR(mainloop);
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
DBG("%s() -> %p", __func__, self);
|
|
Packit |
130fc8 |
TRACE(self);
|
|
Packit |
130fc8 |
return (PyObject *)self;
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
err:
|
|
Packit |
130fc8 |
DBG("Failed to construct Connection from DBusConnection at %p", conn);
|
|
Packit |
130fc8 |
Py_CLEAR(mainloop);
|
|
Packit |
130fc8 |
Py_CLEAR(self);
|
|
Packit |
130fc8 |
Py_CLEAR(ref);
|
|
Packit |
130fc8 |
if (conn) {
|
|
Packit |
130fc8 |
Py_BEGIN_ALLOW_THREADS
|
|
Packit |
130fc8 |
dbus_connection_close(conn);
|
|
Packit |
130fc8 |
dbus_connection_unref(conn);
|
|
Packit |
130fc8 |
Py_END_ALLOW_THREADS
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
DBG("%s() fail", __func__);
|
|
Packit |
130fc8 |
DBG_WHEREAMI;
|
|
Packit |
130fc8 |
return NULL;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
/* Connection type-methods ========================================== */
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
/* Constructor */
|
|
Packit |
130fc8 |
static PyObject *
|
|
Packit |
130fc8 |
Connection_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
|
|
Packit |
130fc8 |
{
|
|
Packit |
130fc8 |
DBusConnection *conn;
|
|
Packit |
130fc8 |
PyObject *address_or_conn;
|
|
Packit |
130fc8 |
DBusError error;
|
|
Packit |
130fc8 |
PyObject *self, *mainloop = NULL;
|
|
Packit |
130fc8 |
static char *argnames[] = {"address", "mainloop", NULL};
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", argnames,
|
|
Packit |
130fc8 |
&address_or_conn, &mainloop)) {
|
|
Packit |
130fc8 |
return NULL;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
if (DBusPyLibDBusConnection_CheckExact(address_or_conn)) {
|
|
Packit |
130fc8 |
DBusPyLibDBusConnection *wrapper =
|
|
Packit |
130fc8 |
(DBusPyLibDBusConnection *) address_or_conn;
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
DBUS_PY_RAISE_VIA_NULL_IF_FAIL(wrapper->conn);
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
conn = dbus_connection_ref (wrapper->conn);
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
else if (PyBytes_Check(address_or_conn)) {
|
|
Packit |
130fc8 |
const char *address = PyBytes_AS_STRING(address_or_conn);
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
dbus_error_init(&error);
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
/* We always open a private connection (at the libdbus level). Sharing
|
|
Packit |
130fc8 |
* is done in Python, to keep things simple. */
|
|
Packit |
130fc8 |
Py_BEGIN_ALLOW_THREADS
|
|
Packit |
130fc8 |
conn = dbus_connection_open_private(address, &error);
|
|
Packit |
130fc8 |
Py_END_ALLOW_THREADS
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
if (!conn) {
|
|
Packit |
130fc8 |
DBusPyException_ConsumeError(&error);
|
|
Packit |
130fc8 |
return NULL;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
else if (PyUnicode_Check(address_or_conn)) {
|
|
Packit |
130fc8 |
PyObject *address_as_bytes = PyUnicode_AsUTF8String(address_or_conn);
|
|
Packit |
130fc8 |
const char *address;
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
if (!address_as_bytes)
|
|
Packit |
130fc8 |
return NULL;
|
|
Packit |
130fc8 |
address = PyBytes_AS_STRING(address_as_bytes);
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
dbus_error_init(&error);
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
/* We always open a private connection (at the libdbus level). Sharing
|
|
Packit |
130fc8 |
* is done in Python, to keep things simple. */
|
|
Packit |
130fc8 |
Py_BEGIN_ALLOW_THREADS
|
|
Packit |
130fc8 |
conn = dbus_connection_open_private(address, &error);
|
|
Packit |
130fc8 |
Py_END_ALLOW_THREADS
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
Py_CLEAR(address_as_bytes);
|
|
Packit |
130fc8 |
if (!conn) {
|
|
Packit |
130fc8 |
DBusPyException_ConsumeError(&error);
|
|
Packit |
130fc8 |
return NULL;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
else {
|
|
Packit |
130fc8 |
PyErr_SetString(PyExc_TypeError, "connection or str expected");
|
|
Packit |
130fc8 |
return NULL;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
self = DBusPyConnection_NewConsumingDBusConnection(cls, conn, mainloop);
|
|
Packit |
130fc8 |
TRACE(self);
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
return self;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
/* Post-construction: nothing to do (but don't chain up to object.__init__,
|
|
Packit |
130fc8 |
* which takes no arguments and does nothing) */
|
|
Packit |
130fc8 |
static int
|
|
Packit |
130fc8 |
Connection_tp_init(PyObject *self UNUSED, PyObject *args UNUSED,
|
|
Packit |
130fc8 |
PyObject *kwargs UNUSED)
|
|
Packit |
130fc8 |
{
|
|
Packit |
130fc8 |
return 0;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
/* Destructor */
|
|
Packit |
130fc8 |
static void Connection_tp_dealloc(Connection *self)
|
|
Packit |
130fc8 |
{
|
|
Packit |
130fc8 |
DBusConnection *conn = self->conn;
|
|
Packit |
130fc8 |
PyObject *et, *ev, *etb;
|
|
Packit |
130fc8 |
PyObject *filters = self->filters;
|
|
Packit |
130fc8 |
PyObject *object_paths = self->object_paths;
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
/* avoid clobbering any pending exception */
|
|
Packit |
130fc8 |
PyErr_Fetch(&et, &ev, &etb;;
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
if (self->weaklist) {
|
|
Packit |
130fc8 |
PyObject_ClearWeakRefs((PyObject *)self);
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
TRACE(self);
|
|
Packit |
130fc8 |
DBG("Deallocating Connection at %p (DBusConnection at %p)", self, conn);
|
|
Packit |
130fc8 |
DBG_WHEREAMI;
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
DBG("Connection at %p: deleting callbacks", self);
|
|
Packit |
130fc8 |
self->filters = NULL;
|
|
Packit |
130fc8 |
Py_CLEAR(filters);
|
|
Packit |
130fc8 |
self->object_paths = NULL;
|
|
Packit |
130fc8 |
Py_CLEAR(object_paths);
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
if (conn) {
|
|
Packit |
130fc8 |
/* Might trigger callbacks if we're unlucky... */
|
|
Packit |
130fc8 |
DBG("Connection at %p has a conn, closing it...", self);
|
|
Packit |
130fc8 |
Py_BEGIN_ALLOW_THREADS
|
|
Packit |
130fc8 |
dbus_connection_close(conn);
|
|
Packit |
130fc8 |
Py_END_ALLOW_THREADS
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
/* make sure to do this last to preserve the invariant that
|
|
Packit |
130fc8 |
* self->conn is always non-NULL for any referenced Connection
|
|
Packit |
130fc8 |
* (until the filters and object paths were freed, we might have been
|
|
Packit |
130fc8 |
* in a reference cycle!)
|
|
Packit |
130fc8 |
*/
|
|
Packit |
130fc8 |
DBG("Connection at %p: nulling self->conn", self);
|
|
Packit |
130fc8 |
self->conn = NULL;
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
if (conn) {
|
|
Packit |
130fc8 |
DBG("Connection at %p: unreffing conn", self);
|
|
Packit |
130fc8 |
dbus_connection_unref(conn);
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
DBG("Connection at %p: freeing self", self);
|
|
Packit |
130fc8 |
PyErr_Restore(et, ev, etb);
|
|
Packit |
130fc8 |
(Py_TYPE(self)->tp_free)((PyObject *)self);
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
/* Connection type object =========================================== */
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
PyTypeObject DBusPyConnection_Type = {
|
|
Packit |
130fc8 |
PyVarObject_HEAD_INIT(NULL, 0)
|
|
Packit |
130fc8 |
"_dbus_bindings.Connection", /*tp_name*/
|
|
Packit |
130fc8 |
sizeof(Connection), /*tp_basicsize*/
|
|
Packit |
130fc8 |
0, /*tp_itemsize*/
|
|
Packit |
130fc8 |
/* methods */
|
|
Packit |
130fc8 |
(destructor)Connection_tp_dealloc,
|
|
Packit |
130fc8 |
0, /*tp_print*/
|
|
Packit |
130fc8 |
0, /*tp_getattr*/
|
|
Packit |
130fc8 |
0, /*tp_setattr*/
|
|
Packit |
130fc8 |
0, /*tp_compare*/
|
|
Packit |
130fc8 |
0, /*tp_repr*/
|
|
Packit |
130fc8 |
0, /*tp_as_number*/
|
|
Packit |
130fc8 |
0, /*tp_as_sequence*/
|
|
Packit |
130fc8 |
0, /*tp_as_mapping*/
|
|
Packit |
130fc8 |
0, /*tp_hash*/
|
|
Packit |
130fc8 |
0, /*tp_call*/
|
|
Packit |
130fc8 |
0, /*tp_str*/
|
|
Packit |
130fc8 |
0, /*tp_getattro*/
|
|
Packit |
130fc8 |
0, /*tp_setattro*/
|
|
Packit |
130fc8 |
0, /*tp_as_buffer*/
|
|
Packit |
130fc8 |
#ifdef PY3
|
|
Packit |
130fc8 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
|
Packit |
130fc8 |
#else
|
|
Packit |
130fc8 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS | Py_TPFLAGS_BASETYPE,
|
|
Packit |
130fc8 |
#endif
|
|
Packit |
130fc8 |
Connection_tp_doc, /*tp_doc*/
|
|
Packit |
130fc8 |
0, /*tp_traverse*/
|
|
Packit |
130fc8 |
0, /*tp_clear*/
|
|
Packit |
130fc8 |
0, /*tp_richcompare*/
|
|
Packit |
130fc8 |
offsetof(Connection, weaklist), /*tp_weaklistoffset*/
|
|
Packit |
130fc8 |
0, /*tp_iter*/
|
|
Packit |
130fc8 |
0, /*tp_iternext*/
|
|
Packit |
130fc8 |
DBusPyConnection_tp_methods, /*tp_methods*/
|
|
Packit |
130fc8 |
0, /*tp_members*/
|
|
Packit |
130fc8 |
0, /*tp_getset*/
|
|
Packit |
130fc8 |
0, /*tp_base*/
|
|
Packit |
130fc8 |
0, /*tp_dict*/
|
|
Packit |
130fc8 |
0, /*tp_descr_get*/
|
|
Packit |
130fc8 |
0, /*tp_descr_set*/
|
|
Packit |
130fc8 |
0, /*tp_dictoffset*/
|
|
Packit |
130fc8 |
Connection_tp_init, /*tp_init*/
|
|
Packit |
130fc8 |
0, /*tp_alloc*/
|
|
Packit |
130fc8 |
Connection_tp_new, /*tp_new*/
|
|
Packit |
130fc8 |
0, /*tp_free*/
|
|
Packit |
130fc8 |
0, /*tp_is_gc*/
|
|
Packit |
130fc8 |
};
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
dbus_bool_t
|
|
Packit |
130fc8 |
dbus_py_init_conn_types(void)
|
|
Packit |
130fc8 |
{
|
|
Packit |
130fc8 |
/* Get a slot to store our weakref on DBus Connections */
|
|
Packit |
130fc8 |
_connection_python_slot = -1;
|
|
Packit |
130fc8 |
if (!dbus_connection_allocate_data_slot(&_connection_python_slot))
|
|
Packit |
130fc8 |
return FALSE;
|
|
Packit |
130fc8 |
if (PyType_Ready(&DBusPyConnection_Type) < 0)
|
|
Packit |
130fc8 |
return FALSE;
|
|
Packit |
130fc8 |
return TRUE;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
dbus_bool_t
|
|
Packit |
130fc8 |
dbus_py_insert_conn_types(PyObject *this_module)
|
|
Packit |
130fc8 |
{
|
|
Packit |
130fc8 |
/* PyModule_AddObject steals a ref */
|
|
Packit |
130fc8 |
Py_INCREF (&DBusPyConnection_Type);
|
|
Packit |
130fc8 |
if (PyModule_AddObject(this_module, "Connection",
|
|
Packit |
130fc8 |
(PyObject *)&DBusPyConnection_Type) < 0) return FALSE;
|
|
Packit |
130fc8 |
return TRUE;
|
|
Packit |
130fc8 |
}
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
/* vim:set ft=c cino< sw=4 sts=4 et: */
|