1 /* Test fixtures for dbus-python, based on _dbus_glib_bindings.
2  *
3  * Copyright (C) 2007 Collabora Ltd. <http://www.collabora.co.uk/>
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person
8  * obtaining a copy of this software and associated documentation
9  * files (the "Software"), to deal in the Software without
10  * restriction, including without limitation the rights to use, copy,
11  * modify, merge, publish, distribute, sublicense, and/or sell copies
12  * of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  * DEALINGS IN THE SOFTWARE.
26  */
27 
28 #include <Python.h>
29 #include <dbus/dbus-python.h>
30 
31 #ifdef PY3
32 PyMODINIT_FUNC PyInit_dbus_py_test(void);
33 #else
34 PyMODINIT_FUNC initdbus_py_test(void);
35 #endif
36 
37 #if defined(__GNUC__)
38 #   if __GNUC__ >= 3
39 #       define UNUSED __attribute__((__unused__))
40 #   else
41 #       define UNUSED /*nothing*/
42 #   endif
43 #else
44 #   define UNUSED /*nothing*/
45 #endif
46 
47 static dbus_bool_t
dbus_py_test_set_up_conn(DBusConnection * conn UNUSED,void * data UNUSED)48 dbus_py_test_set_up_conn(DBusConnection *conn UNUSED, void *data UNUSED)
49 {
50     PyErr_SetString(PyExc_ValueError, "Dummy error from UnusableMainLoop");
51     return 0;
52 }
53 
54 static dbus_bool_t
dbus_py_test_set_up_srv(DBusServer * srv UNUSED,void * data UNUSED)55 dbus_py_test_set_up_srv(DBusServer *srv UNUSED, void *data UNUSED)
56 {
57     PyErr_SetString(PyExc_ValueError, "Dummy error from UnusableMainLoop");
58     return 0;
59 }
60 
61 static void
dbus_py_test_free(void * data UNUSED)62 dbus_py_test_free(void *data UNUSED)
63 {
64 }
65 
66 static PyObject *
dbus_test_native_mainloop(void)67 dbus_test_native_mainloop(void)
68 {
69     PyObject *loop = DBusPyNativeMainLoop_New4(dbus_py_test_set_up_conn,
70                                                dbus_py_test_set_up_srv,
71                                                dbus_py_test_free,
72                                                NULL);
73     return loop;
74 }
75 
76 static PyObject *
UnusableMainLoop(PyObject * always_null UNUSED,PyObject * args,PyObject * kwargs)77 UnusableMainLoop (PyObject *always_null UNUSED, PyObject *args, PyObject *kwargs)
78 {
79     PyObject *mainloop, *function, *result;
80     int set_as_default = 0;
81     static char *argnames[] = {"set_as_default", NULL};
82 
83     if (PyTuple_Size(args) != 0) {
84         PyErr_SetString(PyExc_TypeError, "UnusableMainLoop() takes no "
85                                          "positional arguments");
86         return NULL;
87     }
88     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", argnames,
89                                      &set_as_default)) {
90         return NULL;
91     }
92 
93     mainloop = dbus_test_native_mainloop();
94     if (mainloop && set_as_default) {
95         if (!_dbus_bindings_module) {
96             PyErr_SetString(PyExc_ImportError, "_dbus_bindings not imported");
97             Py_CLEAR(mainloop);
98             return NULL;
99         }
100         function = PyObject_GetAttrString(_dbus_bindings_module,
101                                           "set_default_main_loop");
102         if (!function) {
103             Py_CLEAR(mainloop);
104             return NULL;
105         }
106         result = PyObject_CallFunctionObjArgs(function, mainloop, NULL);
107         Py_CLEAR(function);
108         if (!result) {
109             Py_CLEAR(mainloop);
110             return NULL;
111         }
112     }
113     return mainloop;
114 }
115 
116 static PyMethodDef module_functions[] = {
117     {"UnusableMainLoop", (PyCFunction) (void (*)(void))UnusableMainLoop,
118      METH_VARARGS|METH_KEYWORDS, "Return a main loop that fails to attach"},
119     {NULL, NULL, 0, NULL}
120 };
121 
122 #ifdef PY3
123 PyMODINIT_FUNC
PyInit_dbus_py_test(void)124 PyInit_dbus_py_test(void)
125 {
126     static struct PyModuleDef moduledef = {
127         PyModuleDef_HEAD_INIT,
128         "dbus_py_test",         /* m_name */
129         NULL,                   /* m_doc */
130         -1,                     /* m_size */
131         module_functions,       /* m_methods */
132         NULL,                   /* m_reload */
133         NULL,                   /* m_traverse */
134         NULL,                   /* m_clear */
135         NULL                    /* m_free */
136     };
137     if (import_dbus_bindings("dbus_py_test") < 0)
138         return NULL;
139 
140     return PyModule_Create(&moduledef);
141 }
142 #else
143 PyMODINIT_FUNC
initdbus_py_test(void)144 initdbus_py_test(void)
145 {
146     PyObject *this_module;
147 
148     if (import_dbus_bindings("dbus_py_test") < 0) return;
149     this_module = Py_InitModule3 ("dbus_py_test", module_functions, "");
150     if (!this_module) return;
151 }
152 #endif
153 
154 /* vim:set ft=c cino< sw=4 sts=4 et: */
155