[pygobject] Consolidate GError related code into pygi-error
- From: Simon Feltman <sfeltman src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pygobject] Consolidate GError related code into pygi-error
- Date: Sun, 4 May 2014 07:24:31 +0000 (UTC)
commit 649895d83a90cd3a370da215a6f98a606b987419
Author: Simon Feltman <sfeltman src gnome org>
Date: Sun May 4 00:18:41 2014 -0700
Consolidate GError related code into pygi-error
Rename all pyglib_error_* functions to pygi_error_* and move them into
pygi-error.[h|c].
Register GError as part of the gi._gi module instead of gi._gi._glib.
Update all code to use new naming.
https://bugzilla.gnome.org/show_bug.cgi?id=712519
gi/_option.py | 6 +-
gi/gimodule.c | 4 +-
gi/glibmodule.c | 19 ----
gi/gobjectmodule.c | 54 +-----------
gi/overrides/GLib.py | 3 +-
gi/pygi-argument.c | 6 +-
gi/pygi-cache.c | 4 +-
gi/pygi-error.c | 222 +++++++++++++++++++++++++++++++++++++++++++++++-
gi/pygi-error.h | 21 ++++-
gi/pygi-invoke.c | 5 +-
gi/pyglib-private.h | 1 -
gi/pyglib.c | 199 -------------------------------------------
gi/pyglib.h | 5 -
gi/pygobject-private.h | 3 -
gi/pygoptioncontext.c | 3 +-
gi/pygoptiongroup.c | 3 +-
gi/pygspawn.c | 3 +-
17 files changed, 262 insertions(+), 299 deletions(-)
---
diff --git a/gi/_option.py b/gi/_option.py
index 422c53f..4ffeb1d 100644
--- a/gi/_option.py
+++ b/gi/_option.py
@@ -40,7 +40,7 @@ else:
_basestring = basestring
_bytes = str
-from gi._gi import _glib
+from gi._gi import _glib, GError
GLib = get_introspection_module('GLib')
OPTION_CONTEXT_ERROR_QUARK = GLib.quark_to_string(GLib.option_error_quark())
@@ -202,7 +202,7 @@ class OptionGroup(optparse.OptionGroup):
opt.process(option_name, option_value, self.values, parser)
except OptionValueError:
error = sys.exc_info()[1]
- gerror = _glib.GError(str(error))
+ gerror = GError(str(error))
gerror.domain = OPTION_CONTEXT_ERROR_QUARK
gerror.code = GLib.OptionError.BAD_VALUE
gerror.message = str(error)
@@ -347,7 +347,7 @@ class OptionParser(optparse.OptionParser):
try:
options, args = optparse.OptionParser.parse_args(
self, args, values)
- except _glib.GError:
+ except GError:
error = sys.exc_info()[1]
if error.domain != OPTION_CONTEXT_ERROR_QUARK:
raise
diff --git a/gi/gimodule.c b/gi/gimodule.c
index 25fc3d6..17e8587 100644
--- a/gi/gimodule.c
+++ b/gi/gimodule.c
@@ -27,6 +27,7 @@
#include "pygi-private.h"
#include "pygi.h"
#include "pyglib.h"
+#include "pygi-error.h"
#include <pyglib-python-compat.h>
@@ -584,7 +585,7 @@ pyg_channel_read(PyObject* self, PyObject *args, PyObject *kwargs)
status = g_io_channel_read_chars (iochannel, buf, buf_size, &single_read, &error);
Py_END_ALLOW_THREADS;
- if (pyglib_error_check(&error))
+ if (pygi_error_check (&error))
goto failure;
total_read += single_read;
@@ -664,6 +665,7 @@ PYGLIB_MODULE_START(_gi, "_gi")
PyModule_AddObject (module, "_gobject", _gobject_module);
PyModule_AddStringConstant(module, "__package__", "gi._gi");
+ pygi_error_register_types (module);
_pygi_repository_register_types (module);
_pygi_info_register_types (module);
_pygi_struct_register_types (module);
diff --git a/gi/glibmodule.c b/gi/glibmodule.c
index e859158..297805a 100644
--- a/gi/glibmodule.c
+++ b/gi/glibmodule.c
@@ -31,8 +31,6 @@
#include "pygoptiongroup.h"
#include "pygspawn.h"
-PyObject *PyGError = NULL;
-
/* ---------------- glib module functions -------------------- */
static PyMethodDef _glib_functions[] = {
@@ -48,27 +46,10 @@ static PyMethodDef _glib_functions[] = {
{ NULL, NULL, 0 }
};
-static void
-pyglib_register_error(PyObject *d)
-{
- PyObject *dict;
- dict = PyDict_New();
- /* This is a hack to work around the deprecation warning of
- * BaseException.message in Python 2.6+.
- * GError has also an "message" attribute.
- */
- PyDict_SetItemString(dict, "message", Py_None);
- PyGError = PyErr_NewException("gi._glib.GError", PyExc_RuntimeError, dict);
- Py_DECREF(dict);
-
- PyDict_SetItemString(d, "GError", PyGError);
-}
-
PYGLIB_MODULE_START(_glib, "_glib")
{
PyObject *d = PyModule_GetDict(module);
- pyglib_register_error(d);
pyglib_spawn_register_types(d);
pyglib_option_context_register_types(d);
pyglib_option_group_register_types(d);
diff --git a/gi/gobjectmodule.c b/gi/gobjectmodule.c
index a20d93a..458884b 100644
--- a/gi/gobjectmodule.c
+++ b/gi/gobjectmodule.c
@@ -37,6 +37,7 @@
#include "pygoptiongroup.h"
#include "pygi-value.h"
+#include "pygi-error.h"
static GHashTable *log_handlers = NULL;
static gboolean log_handlers_disabled = FALSE;
@@ -1834,55 +1835,6 @@ pyg_flags_add_constants(PyObject *module, GType flags_type,
}
/**
- * pyg_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 gobject.GError Python exception will be raised, and
- * the GError cleared.
- *
- * Returns: True if an error was set.
- *
- * Deprecated: Since 2.16, use pyglib_error_check instead.
- */
-gboolean
-pyg_error_check(GError **error)
-{
-#if 0
- if (PyErr_Warn(PyExc_DeprecationWarning,
- "pyg_error_check is deprecated, use "
- "pyglib_error_check instead"))
- return NULL;
-#endif
- return pyglib_error_check(error);
-}
-
-/**
- * pyg_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.
- *
- * Deprecated: Since 2.16, use pyglib_gerror_exception_check instead.
- */
-gboolean
-pyg_gerror_exception_check(GError **error)
-{
-#if 0
- if (PyErr_Warn(PyExc_DeprecationWarning,
- "pyg_gerror_exception_check is deprecated, use "
- "pyglib_gerror_exception_check instead"))
- return NULL;
-#endif
- return pyglib_gerror_exception_check(error);
-}
-
-/**
* pyg_parse_constructor_args: helper function for PyGObject constructors
* @obj_type: GType of the GObject, for parameter introspection
* @arg_names: %NULL-terminated array of constructor argument names
@@ -2055,7 +2007,7 @@ struct _PyGObject_Functions pygobject_api_functions = {
pyg_constant_strip_prefix,
- pyg_error_check,
+ pygi_error_check,
_pyg_set_thread_block_funcs,
(PyGThreadBlockFunc)0, /* block_threads */
@@ -2098,7 +2050,7 @@ struct _PyGObject_Functions pygobject_api_functions = {
NULL, /* previously type_register_custom */
- pyg_gerror_exception_check,
+ pygi_gerror_exception_check,
pyg_option_group_new,
pyg_type_from_object_strict,
diff --git a/gi/overrides/GLib.py b/gi/overrides/GLib.py
index 0e8f694..c2462e3 100644
--- a/gi/overrides/GLib.py
+++ b/gi/overrides/GLib.py
@@ -39,8 +39,7 @@ __all__.append('option')
# Types and functions still needed from static bindings
-from gi._gi import _glib
-GError = _glib.GError
+from gi._gi import _glib, GError
OptionContext = _glib.OptionContext
OptionGroup = _glib.OptionGroup
Pid = _glib.Pid
diff --git a/gi/pygi-argument.c b/gi/pygi-argument.c
index 4094c3d..23d7c89 100644
--- a/gi/pygi-argument.c
+++ b/gi/pygi-argument.c
@@ -33,7 +33,7 @@
#include "pygi-basictype.h"
#include "pygi-object.h"
#include "pygi-struct-marshal.h"
-
+#include "pygi-error.h"
static gboolean
gi_argument_to_gssize (GIArgument *arg_in,
@@ -1558,12 +1558,12 @@ _pygi_argument_to_object (GIArgument *arg,
GError *error = (GError *) arg->v_pointer;
if (error != NULL && transfer == GI_TRANSFER_NOTHING) {
/* If we have not been transferred the ownership we must copy
- * the error, because pyglib_error_check() is going to free it.
+ * the error, because pygi_error_check() is going to free it.
*/
error = g_error_copy (error);
}
- if (pyglib_error_check (&error)) {
+ if (pygi_error_check (&error)) {
PyObject *err_type;
PyObject *err_value;
PyObject *err_trace;
diff --git a/gi/pygi-cache.c b/gi/pygi-cache.c
index c8da66f..24bb1a4 100644
--- a/gi/pygi-cache.c
+++ b/gi/pygi-cache.c
@@ -695,7 +695,7 @@ _setup_invoker (GICallableInfo *callable_info,
&error)) {
return TRUE;
}
- if (!pyglib_error_check (&error)) {
+ if (!pygi_error_check (&error)) {
PyErr_Format (PyExc_RuntimeError,
"unknown error creating invoker for %s",
g_base_info_get_name ((GIBaseInfo *)callable_info));
@@ -707,7 +707,7 @@ _setup_invoker (GICallableInfo *callable_info,
(GIFunctionInfo *)callable_info,
invoker,
&error)) {
- if (!pyglib_error_check (&error)) {
+ if (!pygi_error_check (&error)) {
PyErr_Format (PyExc_RuntimeError,
"unknown error creating invoker for %s",
g_base_info_get_name ((GIBaseInfo *)callable_info));
diff --git a/gi/pygi-error.c b/gi/pygi-error.c
index 349bb7e..33275aa 100644
--- a/gi/pygi-error.c
+++ b/gi/pygi-error.c
@@ -1,6 +1,8 @@
/* -*- 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>
*
@@ -23,6 +25,207 @@
#include "pygi-error.h"
+static PyObject *PyGError = NULL;
+static PyObject *exception_table = NULL;
+
+/**
+ * pygi_error_marshal:
+ * @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).
+ *
+ * Returns: a GLib.GError Python exception object, or NULL.
+ */
+PyObject *
+pygi_error_marshal (GError **error)
+{
+ PyGILState_STATE state;
+ PyObject *exc_type;
+ PyObject *exc_instance;
+ PyObject *d;
+
+ g_return_val_if_fail(error != NULL, NULL);
+
+ if (*error == NULL)
+ return NULL;
+
+ state = pyglib_gil_state_ensure();
+
+ exc_type = PyGError;
+ if (exception_table != NULL)
+ {
+ PyObject *item;
+ item = PyDict_GetItem(exception_table, PYGLIB_PyLong_FromLong((*error)->domain));
+ if (item != NULL)
+ exc_type = item;
+ }
+
+ exc_instance = PyObject_CallFunction(exc_type, "z", (*error)->message);
+
+ if ((*error)->domain) {
+ PyObject_SetAttrString(exc_instance, "domain",
+ d=PYGLIB_PyUnicode_FromString(g_quark_to_string((*error)->domain)));
+ Py_DECREF(d);
+ }
+ else
+ PyObject_SetAttrString(exc_instance, "domain", Py_None);
+
+ PyObject_SetAttrString(exc_instance, "code",
+ d=PYGLIB_PyLong_FromLong((*error)->code));
+ Py_DECREF(d);
+
+ if ((*error)->message) {
+ PyObject_SetAttrString(exc_instance, "message",
+ d=PYGLIB_PyUnicode_FromString((*error)->message));
+ Py_DECREF(d);
+ } else {
+ PyObject_SetAttrString(exc_instance, "message", Py_None);
+ }
+
+ pyglib_gil_state_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 = pyglib_gil_state_ensure();
+
+ exc_instance = pygi_error_marshal (error);
+ PyErr_SetObject(PyGError, exc_instance);
+ Py_DECREF(exc_instance);
+ g_clear_error(error);
+
+ pyglib_gil_state_release(state);
+
+ return TRUE;
+}
+
+/**
+ * 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)
+{
+ 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 *) PyGError)) {
+ PyErr_Restore(type, value, traceback);
+ PyErr_Print();
+ return -2;
+ }
+ Py_DECREF(type);
+ Py_XDECREF(traceback);
+
+ py_message = PyObject_GetAttrString(value, "message");
+ if (!py_message || !PYGLIB_PyUnicode_Check(py_message)) {
+ bad_gerror_message = "gi._glib.GError instances must have a 'message' string attribute";
+ Py_XDECREF(py_message);
+ goto bad_gerror;
+ }
+
+ py_domain = PyObject_GetAttrString(value, "domain");
+ if (!py_domain || !PYGLIB_PyUnicode_Check(py_domain)) {
+ bad_gerror_message = "gi._glib.GError instances must have a 'domain' string attribute";
+ Py_DECREF(py_message);
+ Py_XDECREF(py_domain);
+ goto bad_gerror;
+ }
+
+ py_code = PyObject_GetAttrString(value, "code");
+ if (!py_code || !PYGLIB_PyLong_Check(py_code)) {
+ bad_gerror_message = "gi._glib.GError instances must have a 'code' int attribute";
+ Py_DECREF(py_message);
+ Py_DECREF(py_domain);
+ Py_XDECREF(py_code);
+ goto bad_gerror;
+ }
+
+ g_set_error(error, g_quark_from_string(PYGLIB_PyUnicode_AsString(py_domain)),
+ PYGLIB_PyLong_AsLong(py_code), "%s", PYGLIB_PyUnicode_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("pyglib"), 0, "%s", bad_gerror_message);
+ PyErr_SetString(PyExc_ValueError, bad_gerror_message);
+ PyErr_Print();
+ return -2;
+}
+
+/**
+ * pygi_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 pygi_error_check().
+ *
+ * Returns: the new exception
+ */
+PyObject *
+pygi_register_exception_for_domain (gchar *name,
+ gint error_domain)
+{
+ PyObject *exception;
+
+ exception = PyErr_NewException(name, PyGError, NULL);
+
+ if (exception_table == NULL)
+ exception_table = PyDict_New();
+
+ PyDict_SetItem(exception_table,
+ PYGLIB_PyLong_FromLong(error_domain),
+ exception);
+
+ return exception;
+}
+
static gboolean
_pygi_marshal_from_py_gerror (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
@@ -45,7 +248,7 @@ _pygi_marshal_to_py_gerror (PyGIInvokeState *state,
GError *error = arg->v_pointer;
PyObject *py_obj = NULL;
- py_obj = pyglib_error_marshal(&error);
+ py_obj = pygi_error_marshal (&error);
if (arg_cache->transfer == GI_TRANSFER_EVERYTHING && error != NULL) {
g_error_free (error);
@@ -107,3 +310,20 @@ pygi_arg_gerror_new_from_info (GITypeInfo *type_info,
return NULL;
}
}
+
+void
+pygi_error_register_types (PyObject *module)
+{
+ PyObject *dict;
+ dict = PyDict_New();
+ /* This is a hack to work around the deprecation warning of
+ * BaseException.message in Python 2.6+.
+ * GError has also an "message" attribute.
+ */
+ PyDict_SetItemString(dict, "message", Py_None);
+ PyGError = PyErr_NewException("GLib.GError", PyExc_RuntimeError, dict);
+ Py_DECREF(dict);
+
+ PyModule_AddObject (module, "GError", PyGError);
+}
+
diff --git a/gi/pygi-error.h b/gi/pygi-error.h
index fdeb32f..72d2be0 100644
--- a/gi/pygi-error.h
+++ b/gi/pygi-error.h
@@ -1,6 +1,8 @@
/* -*- Mode: C; c-basic-offset: 4 -*-
* vim: tabstop=4 shiftwidth=4 expandtab
*
+ * Copyright (C) 1998-2003 James Henstridge
+ * 2004-2008 Johan Dahlin
* Copyright (C) 2014 Simon Feltman <sfeltman gnome org>
*
* This library is free software; you can redistribute it and/or
@@ -25,10 +27,21 @@
G_BEGIN_DECLS
-PyGIArgCache *pygi_arg_gerror_new_from_info (GITypeInfo *type_info,
- GIArgInfo *arg_info, /* may be null */
- GITransfer transfer,
- PyGIDirection direction);
+gboolean pygi_error_check (GError **error);
+
+PyObject* pygi_error_marshal (GError **error);
+
+gboolean pygi_gerror_exception_check (GError **error);
+
+PyObject* pygi_register_exception_for_domain (gchar *name,
+ gint error_domain);
+
+PyGIArgCache* pygi_arg_gerror_new_from_info (GITypeInfo *type_info,
+ GIArgInfo *arg_info, /* may be null */
+ GITransfer transfer,
+ PyGIDirection direction);
+
+void pygi_error_register_types (PyObject *module);
G_END_DECLS
diff --git a/gi/pygi-invoke.c b/gi/pygi-invoke.c
index 4af2e72..eebf959 100644
--- a/gi/pygi-invoke.c
+++ b/gi/pygi-invoke.c
@@ -23,6 +23,7 @@
#include <pyglib.h>
#include "pygi-invoke.h"
#include "pygi-marshal-cleanup.h"
+#include "pygi-error.h"
static inline gboolean
_invoke_callable (PyGIInvokeState *state,
@@ -46,7 +47,7 @@ _invoke_callable (PyGIInvokeState *state,
* state->error allowing for easy checking here.
*/
if (state->error != NULL) {
- if (pyglib_error_check (&(state->error))) {
+ if (pygi_error_check (&(state->error))) {
/* even though we errored out, the call itself was successful,
so we assume the call processed all of the parameters */
pygi_marshal_cleanup_args_from_py_marshal_success (state, cache);
@@ -308,7 +309,7 @@ _invoke_state_init_from_callable_cache (GIBaseInfo *info,
state->function_ptr = g_vfunc_info_get_address ((GIVFuncInfo *)info,
state->implementor_gtype,
&error);
- if (pyglib_error_check (&error)) {
+ if (pygi_error_check (&error)) {
return FALSE;
}
}
diff --git a/gi/pyglib-private.h b/gi/pyglib-private.h
index 3569c6b..78dd489 100644
--- a/gi/pyglib-private.h
+++ b/gi/pyglib-private.h
@@ -31,7 +31,6 @@ G_BEGIN_DECLS
gboolean _pyglib_handler_marshal(gpointer user_data);
void _pyglib_destroy_notify(gpointer user_data);
-extern PyObject *PyGError;
extern PyObject *pyglib__glib_module_create (void);
G_END_DECLS
diff --git a/gi/pyglib.c b/gi/pyglib.c
index 6c52f31..8db9b17 100644
--- a/gi/pyglib.c
+++ b/gi/pyglib.c
@@ -28,205 +28,6 @@
#include "pygoptioncontext.h"
#include "pygoptiongroup.h"
-static PyObject *exception_table = NULL;
-
-/**
- * pyglib_error_marshal:
- * @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).
- *
- * Returns: a GLib.GError Python exception object, or NULL.
- */
-PyObject *
-pyglib_error_marshal (GError **error)
-{
- PyGILState_STATE state;
- PyObject *exc_type;
- PyObject *exc_instance;
- PyObject *d;
-
- g_return_val_if_fail(error != NULL, NULL);
-
- if (*error == NULL)
- return NULL;
-
- state = pyglib_gil_state_ensure();
-
- exc_type = PyGError;
- if (exception_table != NULL)
- {
- PyObject *item;
- item = PyDict_GetItem(exception_table, PYGLIB_PyLong_FromLong((*error)->domain));
- if (item != NULL)
- exc_type = item;
- }
-
- exc_instance = PyObject_CallFunction(exc_type, "z", (*error)->message);
-
- if ((*error)->domain) {
- PyObject_SetAttrString(exc_instance, "domain",
- d=PYGLIB_PyUnicode_FromString(g_quark_to_string((*error)->domain)));
- Py_DECREF(d);
- }
- else
- PyObject_SetAttrString(exc_instance, "domain", Py_None);
-
- PyObject_SetAttrString(exc_instance, "code",
- d=PYGLIB_PyLong_FromLong((*error)->code));
- Py_DECREF(d);
-
- if ((*error)->message) {
- PyObject_SetAttrString(exc_instance, "message",
- d=PYGLIB_PyUnicode_FromString((*error)->message));
- Py_DECREF(d);
- } else {
- PyObject_SetAttrString(exc_instance, "message", Py_None);
- }
-
- pyglib_gil_state_release(state);
-
- return exc_instance;
-}
-
-/**
- * pyglib_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
-pyglib_error_check(GError **error)
-{
- PyGILState_STATE state;
- PyObject *exc_instance;
-
- g_return_val_if_fail(error != NULL, FALSE);
- if (*error == NULL)
- return FALSE;
-
- state = pyglib_gil_state_ensure();
-
- exc_instance = pyglib_error_marshal (error);
- PyErr_SetObject(PyGError, exc_instance);
- Py_DECREF(exc_instance);
- g_clear_error(error);
-
- pyglib_gil_state_release(state);
-
- 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 glib.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 *) PyGError)) {
- PyErr_Restore(type, value, traceback);
- PyErr_Print();
- return -2;
- }
- Py_DECREF(type);
- Py_XDECREF(traceback);
-
- py_message = PyObject_GetAttrString(value, "message");
- if (!py_message || !PYGLIB_PyUnicode_Check(py_message)) {
- bad_gerror_message = "gi._glib.GError instances must have a 'message' string attribute";
- Py_XDECREF(py_message);
- goto bad_gerror;
- }
-
- py_domain = PyObject_GetAttrString(value, "domain");
- if (!py_domain || !PYGLIB_PyUnicode_Check(py_domain)) {
- bad_gerror_message = "gi._glib.GError instances must have a 'domain' string attribute";
- Py_DECREF(py_message);
- Py_XDECREF(py_domain);
- goto bad_gerror;
- }
-
- py_code = PyObject_GetAttrString(value, "code");
- if (!py_code || !PYGLIB_PyLong_Check(py_code)) {
- bad_gerror_message = "gi._glib.GError instances must have a 'code' int attribute";
- Py_DECREF(py_message);
- Py_DECREF(py_domain);
- Py_XDECREF(py_code);
- goto bad_gerror;
- }
-
- g_set_error(error, g_quark_from_string(PYGLIB_PyUnicode_AsString(py_domain)),
- PYGLIB_PyLong_AsLong(py_code), "%s", PYGLIB_PyUnicode_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("pyglib"), 0, "%s", bad_gerror_message);
- PyErr_SetString(PyExc_ValueError, bad_gerror_message);
- PyErr_Print();
- return -2;
-}
-
-/**
- * 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, PyGError, NULL);
-
- if (exception_table == NULL)
- exception_table = PyDict_New();
-
- PyDict_SetItem(exception_table,
- PYGLIB_PyLong_FromLong(error_domain),
- exception);
-
- return exception;
-}
/**
* pyg_option_group_transfer_group:
diff --git a/gi/pyglib.h b/gi/pyglib.h
index 544bada..00228dd 100644
--- a/gi/pyglib.h
+++ b/gi/pyglib.h
@@ -37,11 +37,6 @@ typedef void (*PyGLibThreadBlockFunc) (void);
# define pyglib_gil_state_release PyGILState_Release
#endif
-gboolean pyglib_error_check(GError **error);
-PyObject *pyglib_error_marshal (GError **error);
-gboolean pyglib_gerror_exception_check(GError **error);
-PyObject *pyglib_register_exception_for_domain(gchar *name,
- gint error_domain);
GOptionGroup * pyglib_option_group_transfer_group(PyObject *self);
/* Private: for gobject <-> glib interaction only. */
diff --git a/gi/pygobject-private.h b/gi/pygobject-private.h
index be565d6..b6242cd 100644
--- a/gi/pygobject-private.h
+++ b/gi/pygobject-private.h
@@ -53,7 +53,6 @@ extern GQuark pygobject_custom_key;
void pygobject_data_free (PyGObjectData *data);
void pyg_destroy_notify (gpointer user_data);
gboolean pyg_handler_marshal (gpointer user_data);
-gboolean pyg_error_check (GError **error);
int pygobject_constructv (PyGObject *self,
guint n_parameters,
GParameter *parameters);
@@ -62,8 +61,6 @@ PyObject *pyg_integer_richcompare(PyObject *v,
PyObject *w,
int op);
-gboolean pyg_gerror_exception_check(GError **error);
-
void pygobject_ref_float(PyGObject *self);
void pygobject_ref_sink(PyGObject *self);
diff --git a/gi/pygoptioncontext.c b/gi/pygoptioncontext.c
index f6bb223..ab439f0 100644
--- a/gi/pygoptioncontext.c
+++ b/gi/pygoptioncontext.c
@@ -25,6 +25,7 @@
#include <pyglib.h>
#include "pyglib-private.h"
#include "pygoptioncontext.h"
+#include "pygi-error.h"
PYGLIB_DEFINE_TYPE("gi._glib.OptionContext", PyGOptionContext_Type, PyGOptionContext)
@@ -138,7 +139,7 @@ pyg_option_context_parse(PyGOptionContext *self,
{
g_strfreev(argv_content);
g_strfreev(original);
- pyglib_error_check(&error);
+ pygi_error_check(&error);
return NULL;
}
diff --git a/gi/pygoptiongroup.c b/gi/pygoptiongroup.c
index 613232f..4c1664d 100644
--- a/gi/pygoptiongroup.c
+++ b/gi/pygoptiongroup.c
@@ -25,6 +25,7 @@
#include <pyglib.h>
#include "pyglib-private.h"
#include "pygoptiongroup.h"
+#include "pygi-error.h"
PYGLIB_DEFINE_TYPE("gi._glib.OptionGroup", PyGOptionGroup_Type, PyGOptionGroup)
@@ -148,7 +149,7 @@ arg_func(const gchar *option_name,
Py_DECREF(ret);
no_error = TRUE;
} else
- no_error = pyglib_gerror_exception_check(error) != -1;
+ no_error = pygi_gerror_exception_check(error) != -1;
pyglib_gil_state_release(state);
return no_error;
diff --git a/gi/pygspawn.c b/gi/pygspawn.c
index 8f3ff51..3851ad9 100644
--- a/gi/pygspawn.c
+++ b/gi/pygspawn.c
@@ -26,6 +26,7 @@
#include "pyglib-private.h"
#include "pygspawn.h"
+#include "pygi-error.h"
struct _PyGChildSetupData {
PyObject *func;
@@ -214,7 +215,7 @@ pyglib_spawn_async(PyObject *object, PyObject *args, PyObject *kwargs)
Py_XDECREF(callback_data->data);
g_slice_free(struct _PyGChildSetupData, callback_data);
}
- pyglib_error_check(&error);
+ pygi_error_check(&error);
return NULL;
}
g_free(argv);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]