diff options
author | Johan Dahlin <johan@gnome.org> | 2008-07-20 12:06:24 +0000 |
---|---|---|
committer | Johan Dahlin <johan@src.gnome.org> | 2008-07-20 12:06:24 +0000 |
commit | 46a755b0216287884aab41654fa68a7541d25a79 (patch) | |
tree | 790ef97b06dbe62801a908101f8256586c398369 /glib/pyglib.c | |
parent | 6628e24baebc6dd40f9e76d10391ea5c06d3398a (diff) | |
download | pygobject-46a755b0216287884aab41654fa68a7541d25a79.tar.gz pygobject-46a755b0216287884aab41654fa68a7541d25a79.tar.xz pygobject-46a755b0216287884aab41654fa68a7541d25a79.zip |
Rename helper library to libpyglib-2.0. Move over
2008-07-20 Johan Dahlin <johan@gnome.org>
* glib/Makefile.am:
* glib/glibmodule.c (pyg_main_context_default):
* glib/pyglib.c (pyglib_gerror_exception_check):
* glib/pyglib.h:
Rename helper library to libpyglib-2.0.
Move over pyg_gerror_exception_check as well.
svn path=/trunk/; revision=840
Diffstat (limited to 'glib/pyglib.c')
-rw-r--r-- | glib/pyglib.c | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/glib/pyglib.c b/glib/pyglib.c index bdc3d67..57bc095 100644 --- a/glib/pyglib.c +++ b/glib/pyglib.c @@ -98,6 +98,9 @@ pyglib_enable_threads(void) return FALSE; } #else +/* Enable threading; note that the GIL must be held by the current + * thread when this function is called + */ gboolean pyglib_enable_threads(void) { @@ -178,3 +181,77 @@ pyglib_error_check(GError **error) return TRUE; } + +/** + * pyglib_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 gobject.GError, -2 otherwise. + */ +gboolean +pyglib_gerror_exception_check(GError **error) +{ + PyObject *type, *value, *traceback; + PyObject *py_message, *py_domain, *py_code; + const char *bad_gerror_message; + + 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 *) _PyGLib_API->gerror_exception)) { + PyErr_Restore(type, value, traceback); + PyErr_Print(); + return -2; + } + Py_DECREF(type); + Py_XDECREF(traceback); + + py_message = PyObject_GetAttrString(value, "message"); + if (!py_message || !PyString_Check(py_message)) { + bad_gerror_message = "gobject.GError instances must have a 'message' string attribute"; + goto bad_gerror; + } + + py_domain = PyObject_GetAttrString(value, "domain"); + if (!py_domain || !PyString_Check(py_domain)) { + bad_gerror_message = "gobject.GError instances must have a 'domain' string attribute"; + Py_DECREF(py_message); + goto bad_gerror; + } + + py_code = PyObject_GetAttrString(value, "code"); + if (!py_code || !PyInt_Check(py_code)) { + bad_gerror_message = "gobject.GError instances must have a 'code' int attribute"; + Py_DECREF(py_message); + Py_DECREF(py_domain); + goto bad_gerror; + } + + g_set_error(error, g_quark_from_string(PyString_AsString(py_domain)), + PyInt_AsLong(py_code), PyString_AsString(py_message)); + + Py_DECREF(py_message); + Py_DECREF(py_code); + Py_DECREF(py_domain); + return -1; + +bad_gerror: + Py_DECREF(value); + g_set_error(error, g_quark_from_static_string("pygobject"), 0, bad_gerror_message); + PyErr_SetString(PyExc_ValueError, bad_gerror_message); + PyErr_Print(); + return -2; +} |