~jderose/ubuntu/precise/dbus-python/oneiric-backport

« back to all changes in this revision

Viewing changes to .pc/since-0.84.0.patch/_dbus_bindings/mainloop.c

  • Committer: Package Import Robot
  • Author(s): Barry Warsaw
  • Date: 2012-01-12 14:47:33 UTC
  • Revision ID: package-import@ubuntu.com-20120112144733-xtfbmgw30h0j40d2
Tags: 0.84.0-2ubuntu1
* debian/patches:
  - since-0.84.0.patch: Upstream unreleased changes from git tag
    dbus-python-0.84.0 to HEAD.  This is a precursor to the following.
  - python3-support.patch: Upstream unreleased changes from git
    `python3` branch for supporting Python 3. (LP: #893091)
* debian/rules: Enable the test suite.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Implementation of main-loop integration for dbus-python.
 
2
 *
 
3
 * Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/>
 
4
 * Copyright (C) 2008 Huang Peng <phuang@redhat.com>
 
5
 *
 
6
 * Permission is hereby granted, free of charge, to any person
 
7
 * obtaining a copy of this software and associated documentation
 
8
 * files (the "Software"), to deal in the Software without
 
9
 * restriction, including without limitation the rights to use, copy,
 
10
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 
11
 * of the Software, and to permit persons to whom the Software is
 
12
 * furnished to do so, subject to the following conditions:
 
13
 *
 
14
 * The above copyright notice and this permission notice shall be
 
15
 * included in all copies or substantial portions of the Software.
 
16
 *
 
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
18
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
20
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 
21
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 
22
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
24
 * DEALINGS IN THE SOFTWARE.
 
25
 */
 
26
 
 
27
#include "config.h"
 
28
 
 
29
#include "dbus_bindings-internal.h"
 
30
 
 
31
/* Native mainloop wrapper ========================================= */
 
32
 
 
33
PyDoc_STRVAR(NativeMainLoop_tp_doc,
 
34
"Object representing D-Bus main loop integration done in native code.\n"
 
35
"Cannot be instantiated directly.\n"
 
36
);
 
37
 
 
38
static PyTypeObject NativeMainLoop_Type;
 
39
 
 
40
DEFINE_CHECK(NativeMainLoop)
 
41
 
 
42
typedef struct {
 
43
    PyObject_HEAD
 
44
    /* Called with the GIL held, should set a Python exception on error */
 
45
    dbus_bool_t (*set_up_connection_cb)(DBusConnection *, void *);
 
46
    dbus_bool_t (*set_up_server_cb)(DBusServer *, void *);
 
47
    /* Called in a destructor. Must not touch the exception state (use
 
48
     * PyErr_Fetch and PyErr_Restore if necessary). */
 
49
    void (*free_cb)(void *);
 
50
    void *data;
 
51
} NativeMainLoop;
 
52
 
 
53
static void NativeMainLoop_tp_dealloc(NativeMainLoop *self)
 
54
{
 
55
    if (self->data && self->free_cb) {
 
56
        (self->free_cb)(self->data);
 
57
    }
 
58
    PyObject_Del((PyObject *)self);
 
59
}
 
60
 
 
61
static PyTypeObject NativeMainLoop_Type = {
 
62
    PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
 
63
    0,
 
64
    "dbus.mainloop.NativeMainLoop",
 
65
    sizeof(NativeMainLoop),
 
66
    0,
 
67
    (destructor)NativeMainLoop_tp_dealloc,  /* tp_dealloc */
 
68
    0,                                      /* tp_print */
 
69
    0,                                      /* tp_getattr */
 
70
    0,                                      /* tp_setattr */
 
71
    0,                                      /* tp_compare */
 
72
    0,                                      /* tp_repr */
 
73
    0,                                      /* tp_as_number */
 
74
    0,                                      /* tp_as_sequence */
 
75
    0,                                      /* tp_as_mapping */
 
76
    0,                                      /* tp_hash */
 
77
    0,                                      /* tp_call */
 
78
    0,                                      /* tp_str */
 
79
    0,                                      /* tp_getattro */
 
80
    0,                                      /* tp_setattro */
 
81
    0,                                      /* tp_as_buffer */
 
82
    Py_TPFLAGS_DEFAULT,                     /* tp_flags */
 
83
    NativeMainLoop_tp_doc,                  /* tp_doc */
 
84
    0,                                      /* tp_traverse */
 
85
    0,                                      /* tp_clear */
 
86
    0,                                      /* tp_richcompare */
 
87
    0,                                      /* tp_weaklistoffset */
 
88
    0,                                      /* tp_iter */
 
89
    0,                                      /* tp_iternext */
 
90
    0,                                      /* tp_methods */
 
91
    0,                                      /* tp_members */
 
92
    0,                                      /* tp_getset */
 
93
    0,                                      /* tp_base */
 
94
    0,                                      /* tp_dict */
 
95
    0,                                      /* tp_descr_get */
 
96
    0,                                      /* tp_descr_set */
 
97
    0,                                      /* tp_dictoffset */
 
98
    0,                                      /* tp_init */
 
99
    0,                                      /* tp_alloc */
 
100
    /* deliberately not callable! */
 
101
    0,                                      /* tp_new */
 
102
};
 
103
 
 
104
/* Internal C API for Connection, Bus, Server ======================= */
 
105
 
 
106
dbus_bool_t
 
107
dbus_py_check_mainloop_sanity(PyObject *mainloop)
 
108
{
 
109
    if (NativeMainLoop_Check(mainloop)) {
 
110
        return TRUE;
 
111
    }
 
112
    PyErr_SetString(PyExc_TypeError,
 
113
                    "A dbus.mainloop.NativeMainLoop instance is required");
 
114
    return FALSE;
 
115
}
 
116
 
 
117
dbus_bool_t
 
118
dbus_py_set_up_connection(PyObject *conn, PyObject *mainloop)
 
119
{
 
120
    if (NativeMainLoop_Check(mainloop)) {
 
121
        /* Native mainloops are allowed to do arbitrary strange things */
 
122
        NativeMainLoop *nml = (NativeMainLoop *)mainloop;
 
123
        DBusConnection *dbc = DBusPyConnection_BorrowDBusConnection(conn);
 
124
 
 
125
        if (!dbc) {
 
126
            return FALSE;
 
127
        }
 
128
        return (nml->set_up_connection_cb)(dbc, nml->data);
 
129
    }
 
130
    PyErr_SetString(PyExc_TypeError,
 
131
                    "A dbus.mainloop.NativeMainLoop instance is required");
 
132
    return FALSE;
 
133
}
 
134
 
 
135
dbus_bool_t
 
136
dbus_py_set_up_server(PyObject *server, PyObject *mainloop)
 
137
{
 
138
    if (NativeMainLoop_Check(mainloop)) {
 
139
        /* Native mainloops are allowed to do arbitrary strange things */
 
140
        NativeMainLoop *nml = (NativeMainLoop *)mainloop;
 
141
        DBusServer *dbs = DBusPyServer_BorrowDBusServer(server);
 
142
 
 
143
        if (!dbs) {
 
144
            return FALSE;
 
145
        }
 
146
        return (nml->set_up_server_cb)(dbs, nml->data);
 
147
    }
 
148
    PyErr_SetString(PyExc_TypeError,
 
149
                    "A dbus.mainloop.NativeMainLoop instance is required");
 
150
    return FALSE;
 
151
}
 
152
 
 
153
/* C API ============================================================ */
 
154
 
 
155
PyObject *
 
156
DBusPyNativeMainLoop_New4(dbus_bool_t (*conn_cb)(DBusConnection *, void *),
 
157
                          dbus_bool_t (*server_cb)(DBusServer *, void *),
 
158
                          void (*free_cb)(void *),
 
159
                          void *data)
 
160
{
 
161
    NativeMainLoop *self = PyObject_New(NativeMainLoop, &NativeMainLoop_Type);
 
162
    if (self) {
 
163
        self->data = data;
 
164
        self->free_cb = free_cb;
 
165
        self->set_up_connection_cb = conn_cb;
 
166
        self->set_up_server_cb = server_cb;
 
167
    }
 
168
    return (PyObject *)self;
 
169
}
 
170
 
 
171
/* Null mainloop implementation ===================================== */
 
172
 
 
173
static dbus_bool_t
 
174
noop_main_loop_cb(void *conn_or_server UNUSED, void *data UNUSED)
 
175
{
 
176
    return TRUE;
 
177
}
 
178
 
 
179
#define noop_conn_cb ((dbus_bool_t (*)(DBusConnection *, void *))(noop_main_loop_cb))
 
180
#define noop_server_cb ((dbus_bool_t (*)(DBusServer *, void *))(noop_main_loop_cb))
 
181
 
 
182
/* Initialization =================================================== */
 
183
 
 
184
dbus_bool_t
 
185
dbus_py_init_mainloop(void)
 
186
{
 
187
    if (PyType_Ready (&NativeMainLoop_Type) < 0) return 0;
 
188
 
 
189
    return 1;
 
190
}
 
191
 
 
192
dbus_bool_t
 
193
dbus_py_insert_mainloop_types(PyObject *this_module)
 
194
{
 
195
    PyObject *null_main_loop = DBusPyNativeMainLoop_New4(noop_conn_cb,
 
196
                                                         noop_server_cb,
 
197
                                                         NULL,
 
198
                                                         NULL);
 
199
    if (!null_main_loop) return 0;
 
200
 
 
201
    /* PyModule_AddObject steals a ref */
 
202
    Py_INCREF (&NativeMainLoop_Type);
 
203
    if (PyModule_AddObject (this_module, "NativeMainLoop",
 
204
                            (PyObject *)&NativeMainLoop_Type) < 0) return 0;
 
205
    if (PyModule_AddObject (this_module, "NULL_MAIN_LOOP",
 
206
                            null_main_loop) < 0) return 0;
 
207
    return 1;
 
208
}
 
209
 
 
210
/* vim:set ft=c cino< sw=4 sts=4 et: */