Add wrapper for DBusServer.
[dbus-python-phuang.git] / test / dbus_py_test.c
blob35603cb39ee769c8bc2f211d4b497b44237fd1d9
1 /* Test fixtures for dbus-python, based on _dbus_glib_bindings.
3 * Copyright (C) 2007 Collabora Ltd. <http://www.collabora.co.uk/>
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use, copy,
9 * modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
26 #include <Python.h>
27 #include "dbus-python.h"
29 #if defined(__GNUC__)
30 # if __GNUC__ >= 3
31 # define UNUSED __attribute__((__unused__))
32 # else
33 # define UNUSED /*nothing*/
34 # endif
35 #else
36 # define UNUSED /*nothing*/
37 #endif
39 static dbus_bool_t
40 dbus_py_test_set_up_conn(DBusConnection *conn UNUSED, void *data UNUSED)
42 PyErr_SetString(PyExc_ValueError, "Dummy error from UnusableMainLoop");
43 return 0;
46 static dbus_bool_t
47 dbus_py_test_set_up_srv(DBusServer *srv UNUSED, void *data UNUSED)
49 PyErr_SetString(PyExc_ValueError, "Dummy error from UnusableMainLoop");
50 return 0;
53 static void
54 dbus_py_test_free(void *data UNUSED)
58 static PyObject *
59 dbus_test_native_mainloop(void)
61 PyObject *loop = DBusPyNativeMainLoop_New4(dbus_py_test_set_up_conn,
62 dbus_py_test_set_up_srv,
63 dbus_py_test_free,
64 NULL);
65 return loop;
68 static PyObject *
69 UnusableMainLoop (PyObject *always_null UNUSED, PyObject *args, PyObject *kwargs)
71 PyObject *mainloop, *function, *result;
72 int set_as_default = 0;
73 static char *argnames[] = {"set_as_default", NULL};
75 if (PyTuple_Size(args) != 0) {
76 PyErr_SetString(PyExc_TypeError, "UnusableMainLoop() takes no "
77 "positional arguments");
78 return NULL;
80 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", argnames,
81 &set_as_default)) {
82 return NULL;
85 mainloop = dbus_test_native_mainloop();
86 if (mainloop && set_as_default) {
87 if (!_dbus_bindings_module) {
88 PyErr_SetString(PyExc_ImportError, "_dbus_bindings not imported");
89 Py_DECREF(mainloop);
90 return NULL;
92 function = PyObject_GetAttrString(_dbus_bindings_module,
93 "set_default_main_loop");
94 if (!function) {
95 Py_DECREF(mainloop);
96 return NULL;
98 result = PyObject_CallFunctionObjArgs(function, mainloop, NULL);
99 Py_DECREF(function);
100 if (!result) {
101 Py_DECREF(mainloop);
102 return NULL;
105 return mainloop;
108 static PyMethodDef module_functions[] = {
109 {"UnusableMainLoop", (PyCFunction)UnusableMainLoop,
110 METH_VARARGS|METH_KEYWORDS, "Return a main loop that fails to attach"},
111 {NULL, NULL, 0, NULL}
114 PyMODINIT_FUNC
115 initdbus_py_test(void)
117 PyObject *this_module;
119 if (import_dbus_bindings("dbus_py_test") < 0) return;
120 this_module = Py_InitModule3 ("dbus_py_test", module_functions, "");
121 if (!this_module) return;
124 /* vim:set ft=c cino< sw=4 sts=4 et: */