pygobject r863 - in trunk: . examples/gio gio glib tests



Author: johan
Date: Sat Jul 26 10:46:49 2008
New Revision: 863
URL: http://svn.gnome.org/viewvc/pygobject?rev=863&view=rev

Log:
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.



Modified:
   trunk/ChangeLog
   trunk/examples/gio/directory-async.py
   trunk/gio/Makefile.am
   trunk/gio/giomodule.c
   trunk/glib/pyglib.c
   trunk/glib/pyglib.h
   trunk/tests/test_gio.py

Modified: trunk/examples/gio/directory-async.py
==============================================================================
--- trunk/examples/gio/directory-async.py	(original)
+++ trunk/examples/gio/directory-async.py	Sat Jul 26 10:46:49 2008
@@ -1,6 +1,6 @@
 import sys
 
-import gobject
+import glib
 import gio
 
 def next_files_done(enumerator, result):
@@ -11,7 +11,7 @@
 def enumerate_children_done(gfile, result):
     try:
         enumerator = gfile.enumerate_children_finish(result)
-    except gobject.GError, e:
+    except gio.Error, e:
         print 'ERROR:', e
         loop.quit()
         return
@@ -26,5 +26,5 @@
 gfile.enumerate_children_async(
     "standard::name", enumerate_children_done)
 
-loop = gobject.MainLoop()
+loop = glib.MainLoop()
 loop.run()

Modified: trunk/gio/Makefile.am
==============================================================================
--- trunk/gio/Makefile.am	(original)
+++ trunk/gio/Makefile.am	Sat Jul 26 10:46:49 2008
@@ -6,6 +6,7 @@
 	$(PYTHON_INCLUDES) 		\
 	$(PYGOBJECT_CFLAGS)		\
 	$(GIO_CFLAGS)			\
+	-I$(top_srcdir)/glib		\
 	-I$(top_srcdir)/gobject
 
 # defs files
@@ -49,7 +50,7 @@
 gio.c: $(GIO_DEFS) $(GIO_OVERRIDES)
 _gio_la_CFLAGS = $(GIO_CFLAGS)
 _gio_la_LDFLAGS = $(common_ldflags) -export-symbols-regex init_gio
-_gio_la_LIBADD = $(GIO_LIBS)
+_gio_la_LIBADD = $(GIO_LIBS) $(top_builddir)/glib/libpyglib-2.0.la
 _gio_la_SOURCES = \
 	giomodule.c \
 	pygio-utils.c \

Modified: trunk/gio/giomodule.c
==============================================================================
--- trunk/gio/giomodule.c	(original)
+++ trunk/gio/giomodule.c	Sat Jul 26 10:46:49 2008
@@ -23,6 +23,7 @@
 #  include "config.h"
 #endif
 #include <Python.h>
+#include <pyglib.h>
 #include <pygobject.h>
 
 #include <gio/gio.h>
@@ -43,7 +44,7 @@
 {
     PyObject *m, *d;
     PyObject *tuple;
-    
+    PyObject *e;
     /* perform any initialisation required by the library here */
 
     m = Py_InitModule("gio._gio", pygio_functions);
@@ -55,6 +56,9 @@
     pygio_add_constants(m, "G_IO_");
 
     PyModule_AddStringConstant(m, "ERROR", g_quark_to_string(G_IO_ERROR));
+    e = pyglib_register_exception_for_domain("gio.Error", G_IO_ERROR);
+    PyDict_SetItemString(d, "Error", e);
+    Py_DECREF(e);
 
     /* pygio version */
     tuple = Py_BuildValue ("(iii)",

Modified: trunk/glib/pyglib.c
==============================================================================
--- trunk/glib/pyglib.c	(original)
+++ trunk/glib/pyglib.c	Sat Jul 26 10:46:49 2008
@@ -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 @@
 pyglib_error_check(GError **error)
 {
     PyGILState_STATE state;
+    PyObject *exc_type;
     PyObject *exc_instance;
     PyObject *d;
 
@@ -225,9 +227,17 @@
 	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 @@
 }
 
 /**
+ * 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.
  *

Modified: trunk/glib/pyglib.h
==============================================================================
--- trunk/glib/pyglib.h	(original)
+++ trunk/glib/pyglib.h	Sat Jul 26 10:46:49 2008
@@ -37,6 +37,8 @@
 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,

Modified: trunk/tests/test_gio.py
==============================================================================
--- trunk/tests/test_gio.py	(original)
+++ trunk/tests/test_gio.py	Sat Jul 26 10:46:49 2008
@@ -103,18 +103,21 @@
             try:
                 try:
                     retval = gfile.mount_enclosing_volume_finish(result)
-                except glib.GError, e:
+                except gio.Error, e:
                     # If we run the tests too fast
-                    if (e.domain == gio.ERROR and
-                        e.code == gio.ERROR_ALREADY_MOUNTED):
+                    if e.code == gio.ERROR_ALREADY_MOUNTED:
                         print ('WARNING: testfile is already mounted, '
-                               'skipping test')
+                        'skipping test')
                         loop.quit()
                         return
                     raise
                 self.failUnless(retval)
             finally:
-                mount = gfile.find_enclosing_mount()
+                try:
+                    mount = gfile.find_enclosing_mount()
+                except gio.Error:
+                    loop.quit()
+                    return
                 mount.unmount(unmount_done)
 
         mount_operation = gio.MountOperation()
@@ -239,7 +242,7 @@
                 self.count += 1
                 if self.count == 1:
                     return
-                self.assertRaises(glib.GError, stream.read_finish, result)
+                self.assertRaises(gio.Error, stream.read_finish, result)
             finally:
                 loop.quit()
 
@@ -311,7 +314,7 @@
         def callback(stream, result):
             self.assertEquals(result.get_op_res_gssize(), 0)
             try:
-                self.assertRaises(glib.GError, stream.write_finish, result)
+                self.assertRaises(gio.Error, stream.write_finish, result)
             finally:
                 loop.quit()
 



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