1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
/* -*- Mode: C; c-basic-offset: 4 -*-
* vim: tabstop=4 shiftwidth=4 expandtab
*
* Copyright (C) 1998-2003 James Henstridge
* 2004-2008 Johan Dahlin
* Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>
* Copyright (C) 2014 Simon Feltman <sfeltman@gnome.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "pygi-basictype.h"
#include "pygi-error.h"
#include "pygi-type.h"
#include "pygi-util.h"
PyObject *PyGError = NULL;
/**
* pygi_error_marshal_to_py:
* @error: a pointer to the GError.
*
* Checks to see if @error has been set. If @error has been set, then a
* GLib.GError Python exception object is returned (but not raised).
* If not error is set returns Py_None.
*
* Returns: a GLib.GError Python exception object, or Py_None,
* or NULL and sets an error if creating the exception object fails.
*/
PyObject *
pygi_error_marshal_to_py (GError **error)
{
PyGILState_STATE state;
PyObject *exc_type;
PyObject *exc_instance;
const char *domain = NULL;
g_return_val_if_fail (error != NULL, NULL);
if (*error == NULL) Py_RETURN_NONE;
state = PyGILState_Ensure ();
exc_type = PyGError;
if ((*error)->domain) {
domain = g_quark_to_string ((*error)->domain);
}
exc_instance = PyObject_CallFunction (exc_type, "ssi", (*error)->message,
domain, (*error)->code);
PyGILState_Release (state);
return exc_instance;
}
/**
* pygi_error_check:
* @error: a pointer to the GError.
*
* Checks to see if the GError has been set. If the error has been
* set, then the glib.GError Python exception will be raised, and
* the GError cleared.
*
* Returns: True if an error was set.
*/
gboolean
pygi_error_check (GError **error)
{
PyGILState_STATE state;
PyObject *exc_instance;
g_return_val_if_fail (error != NULL, FALSE);
if (*error == NULL) return FALSE;
state = PyGILState_Ensure ();
exc_instance = pygi_error_marshal_to_py (error);
if (exc_instance != NULL) {
PyErr_SetObject (PyGError, exc_instance);
Py_DECREF (exc_instance);
} else {
PyErr_Print ();
PyErr_SetString (PyExc_RuntimeError, "Converting the GError failed");
}
g_clear_error (error);
PyGILState_Release (state);
return TRUE;
}
/**
* pygi_error_marshal_from_py:
* @pyerr: A Python exception instance.
* @error: a standard GLib GError ** output parameter
*
* Converts from a Python implemented GError into a GError.
*
* Returns: TRUE if the conversion was successful, otherwise a Python exception
* is set and FALSE is returned.
*/
gboolean
pygi_error_marshal_from_py (PyObject *pyerr, GError **error)
{
gint code;
gchar *message = NULL;
gchar *domain = NULL;
gboolean res = FALSE;
PyObject *py_message = NULL, *py_domain = NULL, *py_code = NULL;
if (PyObject_IsInstance (pyerr, PyGError) != 1) {
PyErr_Format (PyExc_TypeError, "Must be GLib.Error, not %s",
Py_TYPE (pyerr)->tp_name);
return FALSE;
}
py_message = PyObject_GetAttrString (pyerr, "message");
if (!py_message) {
PyErr_SetString (
PyExc_ValueError,
"GLib.Error instances must have a 'message' string attribute");
goto cleanup;
}
if (!pygi_utf8_from_py (py_message, &message)) goto cleanup;
py_domain = PyObject_GetAttrString (pyerr, "domain");
if (!py_domain) {
PyErr_SetString (
PyExc_ValueError,
"GLib.Error instances must have a 'domain' string attribute");
goto cleanup;
}
if (!pygi_utf8_from_py (py_domain, &domain)) goto cleanup;
py_code = PyObject_GetAttrString (pyerr, "code");
if (!py_code) {
PyErr_SetString (
PyExc_ValueError,
"GLib.Error instances must have a 'code' int attribute");
goto cleanup;
}
if (!pygi_gint_from_py (py_code, &code)) goto cleanup;
res = TRUE;
g_set_error_literal (error, g_quark_from_string (domain), code, message);
cleanup:
g_free (message);
g_free (domain);
Py_XDECREF (py_message);
Py_XDECREF (py_code);
Py_XDECREF (py_domain);
return res;
}
/**
* pygi_gerror_exception_check:
* @error: a standard GLib GError ** output parameter
*
* Checks to see if a GError exception has been raised, and if so
* translates the python exception to a standard GLib GError. If the
* raised exception is not a GError then PyErr_Print() is called.
*
* Returns: 0 if no exception has been raised, -1 if it is a
* valid glib.GError, -2 otherwise.
*/
gboolean
pygi_gerror_exception_check (GError **error)
{
int res = -1;
PyObject *type, *value, *traceback;
PyErr_Fetch (&type, &value, &traceback);
if (type == NULL) return 0;
PyErr_NormalizeException (&type, &value, &traceback);
if (value == NULL) {
PyErr_Restore (type, value, traceback);
PyErr_Print ();
return -2;
}
if (!value || !PyErr_GivenExceptionMatches (type, (PyObject *)PyGError)) {
PyErr_Restore (type, value, traceback);
PyErr_Print ();
return -2;
}
Py_DECREF (type);
Py_XDECREF (traceback);
if (!pygi_error_marshal_from_py (value, error)) {
PyErr_Print ();
res = -2;
}
Py_DECREF (value);
return res;
}
static PyObject *
pygerror_from_gvalue (const GValue *value)
{
GError *gerror = (GError *)g_value_get_boxed (value);
PyObject *pyerr = pygi_error_marshal_to_py (&gerror);
return pyerr;
}
static int
pygerror_to_gvalue (GValue *value, PyObject *pyerror)
{
GError *gerror = NULL;
if (pygi_error_marshal_from_py (pyerror, &gerror)) {
g_value_take_boxed (value, gerror);
return 0;
}
return -1;
}
/**
* Returns 0 on success, or -1 and sets an exception.
*/
int
pygi_error_register_types (PyObject *module)
{
PyObject *error_module = PyImport_ImportModule ("gi._error");
if (!error_module) {
return -1;
}
/* Stash a reference to the Python implemented gi._error.GError. */
PyGError = PyObject_GetAttrString (error_module, "GError");
Py_DECREF (error_module);
if (PyGError == NULL) return -1;
pyg_register_gtype_custom (G_TYPE_ERROR, pygerror_from_gvalue,
pygerror_to_gvalue);
return 0;
}
|