Blame _dbus_bindings/exceptions.c

Packit 130fc8
/* D-Bus exception base classes.
Packit 130fc8
 *
Packit 130fc8
 * Copyright (C) 2006 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 "dbus_bindings-internal.h"
Packit 130fc8
Packit 130fc8
static PyObject *imported_dbus_exception = NULL;
Packit 130fc8
Packit 130fc8
static dbus_bool_t
Packit 130fc8
import_exception(void)
Packit 130fc8
{
Packit 130fc8
    PyObject *name;
Packit 130fc8
    PyObject *exceptions;
Packit 130fc8
Packit 130fc8
    if (imported_dbus_exception != NULL) {
Packit 130fc8
        return TRUE;
Packit 130fc8
    }
Packit 130fc8
Packit 130fc8
    name = NATIVESTR_FROMSTR("dbus.exceptions");
Packit 130fc8
    if (name == NULL) {
Packit 130fc8
        return FALSE;
Packit 130fc8
    }
Packit 130fc8
    exceptions = PyImport_Import(name);
Packit 130fc8
    Py_CLEAR(name);
Packit 130fc8
    if (exceptions == NULL) {
Packit 130fc8
        return FALSE;
Packit 130fc8
    }
Packit 130fc8
    imported_dbus_exception = PyObject_GetAttrString(exceptions,
Packit 130fc8
                                                     "DBusException");
Packit 130fc8
    Py_CLEAR(exceptions);
Packit 130fc8
Packit 130fc8
    return (imported_dbus_exception != NULL);
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
PyObject *
Packit 130fc8
DBusPyException_SetString(const char *msg)
Packit 130fc8
{
Packit 130fc8
    if (imported_dbus_exception != NULL || import_exception()) {
Packit 130fc8
        PyErr_SetString(imported_dbus_exception, msg);
Packit 130fc8
    }
Packit 130fc8
    return NULL;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
PyObject *
Packit 130fc8
DBusPyException_ConsumeError(DBusError *error)
Packit 130fc8
{
Packit 130fc8
    PyObject *exc_value = NULL;
Packit 130fc8
Packit 130fc8
    if (imported_dbus_exception == NULL && !import_exception()) {
Packit 130fc8
        goto finally;
Packit 130fc8
    }
Packit 130fc8
Packit 130fc8
    exc_value = PyObject_CallFunction(imported_dbus_exception,
Packit 130fc8
                                      "s",
Packit 130fc8
                                      error->message ? error->message
Packit 130fc8
                                                     : "");
Packit 130fc8
Packit 130fc8
    if (!exc_value) {
Packit 130fc8
        goto finally;
Packit 130fc8
    }
Packit 130fc8
Packit 130fc8
    if (error->name) {
Packit 130fc8
        PyObject *name = NATIVESTR_FROMSTR(error->name);
Packit 130fc8
        int ret;
Packit 130fc8
Packit 130fc8
        if (!name)
Packit 130fc8
            goto finally;
Packit 130fc8
        ret = PyObject_SetAttrString(exc_value, "_dbus_error_name", name);
Packit 130fc8
        Py_CLEAR(name);
Packit 130fc8
        if (ret < 0) {
Packit 130fc8
            goto finally;
Packit 130fc8
        }
Packit 130fc8
    }
Packit 130fc8
Packit 130fc8
    PyErr_SetObject(imported_dbus_exception, exc_value);
Packit 130fc8
Packit 130fc8
finally:
Packit 130fc8
    Py_CLEAR(exc_value);
Packit 130fc8
    dbus_error_free(error);
Packit 130fc8
    return NULL;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
/* vim:set ft=c cino< sw=4 sts=4 et: */