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