Blame test/dbus_py_test.c

Packit 130fc8
/* Test fixtures for dbus-python, based on _dbus_glib_bindings.
Packit 130fc8
 *
Packit 130fc8
 * Copyright (C) 2007 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 <Python.h>
Packit 130fc8
#include <dbus/dbus-python.h>
Packit 130fc8
Packit 130fc8
#ifdef PY3
Packit 130fc8
PyMODINIT_FUNC PyInit_dbus_py_test(void);
Packit 130fc8
#else
Packit 130fc8
PyMODINIT_FUNC initdbus_py_test(void);
Packit 130fc8
#endif
Packit 130fc8
Packit 130fc8
#if defined(__GNUC__)
Packit 130fc8
#   if __GNUC__ >= 3
Packit 130fc8
#       define UNUSED __attribute__((__unused__))
Packit 130fc8
#   else
Packit 130fc8
#       define UNUSED /*nothing*/
Packit 130fc8
#   endif
Packit 130fc8
#else
Packit 130fc8
#   define UNUSED /*nothing*/
Packit 130fc8
#endif
Packit 130fc8
Packit 130fc8
static dbus_bool_t
Packit 130fc8
dbus_py_test_set_up_conn(DBusConnection *conn UNUSED, void *data UNUSED)
Packit 130fc8
{
Packit 130fc8
    PyErr_SetString(PyExc_ValueError, "Dummy error from UnusableMainLoop");
Packit 130fc8
    return 0;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
static dbus_bool_t
Packit 130fc8
dbus_py_test_set_up_srv(DBusServer *srv UNUSED, void *data UNUSED)
Packit 130fc8
{
Packit 130fc8
    PyErr_SetString(PyExc_ValueError, "Dummy error from UnusableMainLoop");
Packit 130fc8
    return 0;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
static void
Packit 130fc8
dbus_py_test_free(void *data UNUSED)
Packit 130fc8
{
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
static PyObject *
Packit 130fc8
dbus_test_native_mainloop(void)
Packit 130fc8
{
Packit 130fc8
    PyObject *loop = DBusPyNativeMainLoop_New4(dbus_py_test_set_up_conn,
Packit 130fc8
                                               dbus_py_test_set_up_srv,
Packit 130fc8
                                               dbus_py_test_free,
Packit 130fc8
                                               NULL);
Packit 130fc8
    return loop;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
static PyObject *
Packit 130fc8
UnusableMainLoop (PyObject *always_null UNUSED, PyObject *args, PyObject *kwargs)
Packit 130fc8
{
Packit 130fc8
    PyObject *mainloop, *function, *result;
Packit 130fc8
    int set_as_default = 0;
Packit 130fc8
    static char *argnames[] = {"set_as_default", NULL};
Packit 130fc8
Packit 130fc8
    if (PyTuple_Size(args) != 0) {
Packit 130fc8
        PyErr_SetString(PyExc_TypeError, "UnusableMainLoop() takes no "
Packit 130fc8
                                         "positional arguments");
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", argnames,
Packit 130fc8
                                     &set_as_default)) {
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
Packit 130fc8
    mainloop = dbus_test_native_mainloop();
Packit 130fc8
    if (mainloop && set_as_default) {
Packit 130fc8
        if (!_dbus_bindings_module) {
Packit 130fc8
            PyErr_SetString(PyExc_ImportError, "_dbus_bindings not imported");
Packit 130fc8
            Py_CLEAR(mainloop);
Packit 130fc8
            return NULL;
Packit 130fc8
        }
Packit 130fc8
        function = PyObject_GetAttrString(_dbus_bindings_module,
Packit 130fc8
                                          "set_default_main_loop");
Packit 130fc8
        if (!function) {
Packit 130fc8
            Py_CLEAR(mainloop);
Packit 130fc8
            return NULL;
Packit 130fc8
        }
Packit 130fc8
        result = PyObject_CallFunctionObjArgs(function, mainloop, NULL);
Packit 130fc8
        Py_CLEAR(function);
Packit 130fc8
        if (!result) {
Packit 130fc8
            Py_CLEAR(mainloop);
Packit 130fc8
            return NULL;
Packit 130fc8
        }
Packit 130fc8
    }
Packit 130fc8
    return mainloop;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
static PyMethodDef module_functions[] = {
Packit 130fc8
    {"UnusableMainLoop", (PyCFunction)UnusableMainLoop,
Packit 130fc8
     METH_VARARGS|METH_KEYWORDS, "Return a main loop that fails to attach"},
Packit 130fc8
    {NULL, NULL, 0, NULL}
Packit 130fc8
};
Packit 130fc8
Packit 130fc8
#ifdef PY3
Packit 130fc8
PyMODINIT_FUNC
Packit 130fc8
PyInit_dbus_py_test(void)
Packit 130fc8
{
Packit 130fc8
    static struct PyModuleDef moduledef = {
Packit 130fc8
        PyModuleDef_HEAD_INIT,
Packit 130fc8
        "dbus_py_test",         /* m_name */
Packit 130fc8
        NULL,                   /* m_doc */
Packit 130fc8
        -1,                     /* m_size */
Packit 130fc8
        module_functions,       /* m_methods */
Packit 130fc8
        NULL,                   /* m_reload */
Packit 130fc8
        NULL,                   /* m_traverse */
Packit 130fc8
        NULL,                   /* m_clear */
Packit 130fc8
        NULL                    /* m_free */
Packit 130fc8
    };
Packit 130fc8
    if (import_dbus_bindings("dbus_py_test") < 0)
Packit 130fc8
        return NULL;
Packit 130fc8
Packit 130fc8
    return PyModule_Create(&moduledef);
Packit 130fc8
}
Packit 130fc8
#else
Packit 130fc8
PyMODINIT_FUNC
Packit 130fc8
initdbus_py_test(void)
Packit 130fc8
{
Packit 130fc8
    PyObject *this_module;
Packit 130fc8
Packit 130fc8
    if (import_dbus_bindings("dbus_py_test") < 0) return;
Packit 130fc8
    this_module = Py_InitModule3 ("dbus_py_test", module_functions, "");
Packit 130fc8
    if (!this_module) return;
Packit 130fc8
}
Packit 130fc8
#endif
Packit 130fc8
Packit 130fc8
/* vim:set ft=c cino< sw=4 sts=4 et: */