NEWS: update
[dbus-python-phuang.git] / _dbus_bindings / exceptions.c
blobc5554ba2c62bd9182d14b97749f681eeacb1c263
1 /* D-Bus exception base classes.
3 * Copyright (C) 2006 Collabora Ltd.
5 * Licensed under the Academic Free License version 2.1
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "dbus_bindings-internal.h"
25 static PyObject *imported_dbus_exception = NULL;
27 static dbus_bool_t
28 import_exception(void)
30 PyObject *name;
31 PyObject *exceptions;
33 if (imported_dbus_exception != NULL) {
34 return TRUE;
37 name = PyString_FromString("dbus.exceptions");
38 if (name == NULL) {
39 return FALSE;
41 exceptions = PyImport_Import(name);
42 Py_DECREF(name);
43 if (exceptions == NULL) {
44 return FALSE;
46 imported_dbus_exception = PyObject_GetAttrString(exceptions,
47 "DBusException");
48 Py_DECREF(exceptions);
50 return (imported_dbus_exception != NULL);
53 PyObject *
54 DBusPyException_SetString(const char *msg)
56 if (imported_dbus_exception != NULL || import_exception()) {
57 PyErr_SetString(imported_dbus_exception, msg);
59 return NULL;
62 PyObject *
63 DBusPyException_ConsumeError(DBusError *error)
65 PyObject *exc_value = NULL;
67 if (imported_dbus_exception == NULL && !import_exception()) {
68 goto finally;
71 exc_value = PyObject_CallFunction(imported_dbus_exception,
72 "s",
73 error->message ? error->message
74 : "");
75 if (error->name) {
76 PyObject *name = PyString_FromString(error->name);
77 int ret;
79 if (!name)
80 goto finally;
81 ret = PyObject_SetAttrString(exc_value, "_dbus_error_name", name);
82 Py_DECREF(name);
83 if (ret < 0) {
84 goto finally;
88 PyErr_SetObject(imported_dbus_exception, exc_value);
90 finally:
91 Py_XDECREF(exc_value);
92 dbus_error_free(error);
93 return NULL;
96 /* vim:set ft=c cino< sw=4 sts=4 et: */