[pygobject] [gi] Convert GErrors to GObject.GError exceptions, and throw them upon returning from calling the C



commit 25b69ae257a12b6dc97ed3f2f7ea54b166ddbba1
Author: Laszlo Pandy <lpandy src gnome org>
Date:   Wed Jan 19 17:45:11 2011 +0100

    [gi] Convert GErrors to GObject.GError exceptions, and throw them upon returning from calling the C function.
    
    This changes gi to make use of pyglib_error_check() which already exists in pyglib.
    
    The included tests make use of the other patch attached to this bug, to check that the right exception is thrown from the new function in GIMarshallingTests.
    two Gtk C functions.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=639834

 gi/Makefile.am   |    3 ++-
 gi/pygi-invoke.c |   12 +++---------
 tests/test_gi.py |   10 ++++++++++
 3 files changed, 15 insertions(+), 10 deletions(-)
---
diff --git a/gi/Makefile.am b/gi/Makefile.am
index 8b1639d..abd895e 100644
--- a/gi/Makefile.am
+++ b/gi/Makefile.am
@@ -25,7 +25,8 @@ _gi_la_LDFLAGS = \
 	-export-symbols-regex "init_gi|PyInit__gi"
 _gi_la_LIBADD = \
 	$(GI_LIBS) \
-	$(PYTHON_LIBS)
+	$(PYTHON_LIBS) \
+	$(top_builddir)/glib/libpyglib-2 0- PYTHON_BASENAME@.la
 _gi_la_SOURCES = \
 	pygi-repository.c \
 	pygi-repository.h \
diff --git a/gi/pygi-invoke.c b/gi/pygi-invoke.c
index a92c68e..e998df7 100644
--- a/gi/pygi-invoke.c
+++ b/gi/pygi-invoke.c
@@ -21,6 +21,7 @@
  * USA
  */
 
+#include <pyglib.h>
 #include "pygi-invoke.h"
 
 struct invocation_state
@@ -604,10 +605,7 @@ _invoke_function (struct invocation_state *state,
     pyg_end_allow_threads;
 
     if (!retval) {
-        g_assert (error != NULL);
-        /* TODO: raise the right error, out of the error domain. */
-        PyErr_SetString (PyExc_RuntimeError, error->message);
-        g_error_free (error);
+        pyglib_error_check(&error);
 
         /* TODO: release input arguments. */
 
@@ -619,11 +617,7 @@ _invoke_function (struct invocation_state *state,
 
         error = state->args[state->error_arg_pos]->v_pointer;
 
-        if (*error != NULL) {
-            /* TODO: raise the right error, out of the error domain, if applicable. */
-            PyErr_SetString (PyExc_Exception, (*error)->message);
-            g_error_free (*error);
-
+        if (pyglib_error_check(error)) {
             /* TODO: release input arguments. */
 
             return FALSE;
diff --git a/tests/test_gi.py b/tests/test_gi.py
index 531da51..3a61b31 100644
--- a/tests/test_gi.py
+++ b/tests/test_gi.py
@@ -1442,6 +1442,16 @@ class TestMultiOutputArgs(unittest.TestCase):
     def test_int_return_out(self):
         self.assertEquals((6, 7), GIMarshallingTests.int_return_out())
 
+class TestGErrorException(unittest.TestCase):
+    def test_gerror_exception(self):
+        self.assertRaises(GObject.GError, GIMarshallingTests.gerror)
+        try:
+            GIMarshallingTests.gerror()
+        except Exception, error:
+            self.assertEquals(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
+            self.assertEquals(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
+            self.assertEquals(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
+
 
 # Interface
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]