doc/tutorial.txt: @service and @method take dbus_interface=..., not interface=...
[dbus-python-phuang.git] / test / dbus_py_test.c
blob7ef63c4bc0e17e0d5363ce8652b439188194f12d
1 /* Test fixtures for dbus-python, based on _dbus_glib_bindings. */
3 #include <Python.h>
4 #include "dbus-python.h"
6 #if defined(__GNUC__)
7 # if __GNUC__ >= 3
8 # define UNUSED __attribute__((__unused__))
9 # else
10 # define UNUSED /*nothing*/
11 # endif
12 #else
13 # define UNUSED /*nothing*/
14 #endif
16 static dbus_bool_t
17 dbus_py_test_set_up_conn(DBusConnection *conn UNUSED, void *data UNUSED)
19 PyErr_SetString(PyExc_ValueError, "Dummy error from UnusableMainLoop");
20 return 0;
23 static dbus_bool_t
24 dbus_py_test_set_up_srv(DBusServer *srv UNUSED, void *data UNUSED)
26 PyErr_SetString(PyExc_ValueError, "Dummy error from UnusableMainLoop");
27 return 0;
30 static void
31 dbus_py_test_free(void *data UNUSED)
35 static PyObject *
36 dbus_test_native_mainloop(void)
38 PyObject *loop = DBusPyNativeMainLoop_New4(dbus_py_test_set_up_conn,
39 dbus_py_test_set_up_srv,
40 dbus_py_test_free,
41 NULL);
42 return loop;
45 static PyObject *
46 UnusableMainLoop (PyObject *always_null UNUSED, PyObject *args, PyObject *kwargs)
48 PyObject *mainloop, *function, *result;
49 int set_as_default = 0;
50 static char *argnames[] = {"set_as_default", NULL};
52 if (PyTuple_Size(args) != 0) {
53 PyErr_SetString(PyExc_TypeError, "UnusableMainLoop() takes no "
54 "positional arguments");
55 return NULL;
57 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", argnames,
58 &set_as_default)) {
59 return NULL;
62 mainloop = dbus_test_native_mainloop();
63 if (mainloop && set_as_default) {
64 if (!_dbus_bindings_module) {
65 PyErr_SetString(PyExc_ImportError, "_dbus_bindings not imported");
66 Py_DECREF(mainloop);
67 return NULL;
69 function = PyObject_GetAttrString(_dbus_bindings_module,
70 "set_default_main_loop");
71 if (!function) {
72 Py_DECREF(mainloop);
73 return NULL;
75 result = PyObject_CallFunctionObjArgs(function, mainloop, NULL);
76 Py_DECREF(function);
77 if (!result) {
78 Py_DECREF(mainloop);
79 return NULL;
82 return mainloop;
85 static PyMethodDef module_functions[] = {
86 {"UnusableMainLoop", (PyCFunction)UnusableMainLoop,
87 METH_VARARGS|METH_KEYWORDS, "Return a main loop that fails to attach"},
88 {NULL, NULL, 0, NULL}
91 PyMODINIT_FUNC
92 initdbus_py_test(void)
94 PyObject *this_module;
96 if (import_dbus_bindings("dbus_py_test") < 0) return;
97 this_module = Py_InitModule3 ("dbus_py_test", module_functions, "");
98 if (!this_module) return;
101 /* vim:set ft=c cino< sw=4 sts=4 et: */