summaryrefslogtreecommitdiffstats
path: root/glib
diff options
context:
space:
mode:
authorJohan Dahlin <johan@gnome.org>2008-07-26 10:46:49 +0000
committerJohan Dahlin <johan@src.gnome.org>2008-07-26 10:46:49 +0000
commita0a89623f6ab41d78dcb8a9602387aca6657225a (patch)
tree3b2793322e00e219bbd051d5a06dae1272eab99a /glib
parent31c7971dcad9d8e02cb59fe603bc6d83bf035666 (diff)
downloadpygobject-a0a89623f6ab41d78dcb8a9602387aca6657225a.tar.gz
pygobject-a0a89623f6ab41d78dcb8a9602387aca6657225a.tar.xz
pygobject-a0a89623f6ab41d78dcb8a9602387aca6657225a.zip
Add a new API for registering exceptions for a GError domain. Register a
2008-07-26 Johan Dahlin <johan@gnome.org> * examples/gio/directory-async.py: * gio/Makefile.am: * gio/giomodule.c (init_gio): * glib/pyglib.c (pyglib_error_check), (pyglib_register_exception_for_domain): * glib/pyglib.h: * tests/test_gio.py: Add a new API for registering exceptions for a GError domain. Register a new exception for G_IO_ERROR, update tests and examples to use the new exception. svn path=/trunk/; revision=863
Diffstat (limited to 'glib')
-rw-r--r--glib/pyglib.c45
-rw-r--r--glib/pyglib.h2
2 files changed, 44 insertions, 3 deletions
diff --git a/glib/pyglib.c b/glib/pyglib.c
index f101ec1..ac9a45b 100644
--- a/glib/pyglib.c
+++ b/glib/pyglib.c
@@ -33,6 +33,7 @@
static struct _PyGLib_Functions *_PyGLib_API;
static int pyglib_thread_state_tls_key;
+static PyObject *exception_table = NULL;
static PyTypeObject *_PyGMainContext_Type;
#define PyGMainContext_Type (*_PyGMainContext_Type)
@@ -216,6 +217,7 @@ gboolean
pyglib_error_check(GError **error)
{
PyGILState_STATE state;
+ PyObject *exc_type;
PyObject *exc_instance;
PyObject *d;
@@ -225,9 +227,17 @@ pyglib_error_check(GError **error)
return FALSE;
state = pyglib_gil_state_ensure();
-
- exc_instance = PyObject_CallFunction(_PyGLib_API->gerror_exception, "z",
- (*error)->message);
+
+ exc_type = _PyGLib_API->gerror_exception;
+ if (exception_table != NULL)
+ {
+ PyObject *item;
+ item = PyDict_GetItem(exception_table, PyInt_FromLong((*error)->domain));
+ if (item != NULL)
+ exc_type = item;
+ }
+
+ exc_instance = PyObject_CallFunction(exc_type, "z", (*error)->message);
PyObject_SetAttrString(exc_instance, "domain",
d=PyString_FromString(g_quark_to_string((*error)->domain)));
Py_DECREF(d);
@@ -328,6 +338,35 @@ bad_gerror:
}
/**
+ * pyglib_register_exception_for_domain:
+ * @name: name of the exception
+ * @error_domain: error domain
+ *
+ * Registers a new glib.GError exception subclass called #name for
+ * a specific #domain. This exception will be raised when a GError
+ * of the same domain is passed in to pyglib_error_check().
+ *
+ * Returns: the new exception
+ */
+PyObject *
+pyglib_register_exception_for_domain(gchar *name,
+ gint error_domain)
+{
+ PyObject *exception;
+
+ exception = PyErr_NewException(name, _PyGLib_API->gerror_exception, NULL);
+
+ if (exception_table == NULL)
+ exception_table = PyDict_New();
+
+ PyDict_SetItem(exception_table,
+ PyInt_FromLong(error_domain),
+ exception);
+
+ return exception;
+}
+
+/**
* pyglib_main_context_new:
* @context: a GMainContext.
*
diff --git a/glib/pyglib.h b/glib/pyglib.h
index dcc8f8a..384b60d 100644
--- a/glib/pyglib.h
+++ b/glib/pyglib.h
@@ -37,6 +37,8 @@ void pyglib_gil_state_release(PyGILState_STATE state);
gboolean pyglib_enable_threads(void);
gboolean pyglib_error_check(GError **error);
gboolean pyglib_gerror_exception_check(GError **error);
+PyObject *pyglib_register_exception_for_domain(gchar *name,
+ gint error_domain);
gboolean pyglib_threads_enabled(void);
PyObject * pyglib_main_context_new(GMainContext *context);
void pyglib_set_thread_block_funcs(PyGLibThreadBlockFunc block_threads_func,