[pybank] Add preliminar support for registering interfaces



commit 82123ac5824b98924afa9875d35d0acdb1e23c90
Author: Tomeu Vizoso <tomeu sugarlabs org>
Date:   Thu May 14 10:20:40 2009 +0200

    Add preliminar support for registering interfaces
---
 bank/bank-info.c |   61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 bank/module.py   |    1 +
 2 files changed, 62 insertions(+), 0 deletions(-)

diff --git a/bank/bank-info.c b/bank/bank-info.c
index e3075ac..3d991d8 100644
--- a/bank/bank-info.c
+++ b/bank/bank-info.c
@@ -870,8 +870,69 @@ _wrap_g_interface_info_get_methods(PyGIBaseInfo *self)
     return retval;
 }
 
+static void
+initialize_interface (GTypeInterface *iface, PyTypeObject *pytype)
+{
+    GIRepository *repo = g_irepository_get_default();
+    GIBaseInfo *iface_info = g_irepository_find_by_gtype(repo, G_TYPE_FROM_INTERFACE(iface));
+    int length, i;
+    GTypeInterface *parent_iface = g_type_interface_peek_parent(iface);
+
+    // TODO: Implement this when g-i adds supports for vfunc offsets:
+    // http://bugzilla.gnome.org/show_bug.cgi?id=560281
+    /*
+    length = g_interface_info_get_n_methods((GIInterfaceInfo *) iface_info);
+
+    for (i = 0; i < length; i++) {
+        GIFunctionInfo *method = g_interface_info_get_method((GIInterfaceInfo *) iface_info, i);
+        const gchar *method_name = g_base_info_get_name((GIBaseInfo *) method);
+        gchar pymethod_name[250];
+        PyObject *py_method;
+        void *method_ptr = iface + i * sizeof(void*);
+
+        printf("%s\n", method_name);
+
+        g_snprintf(pymethod_name, sizeof(pymethod_name), "do_%s", pymethod_name);
+        py_method = PyObject_GetAttrString((PyObject *) pytype, pymethod_name);
+        if (py_method && !PyObject_TypeCheck(py_method, &PyCFunction_Type)) {
+            method_ptr = interface_method;
+        } else {
+            PyErr_Clear();
+            if (parent_iface) {
+                method_ptr = parent_iface + i * sizeof(void*);
+            }
+            Py_XDECREF(py_method);
+        }
+
+        g_base_info_unref((GIBaseInfo *) method);
+    }
+    */
+}
+
+static PyObject *
+_wrap_g_interface_info_register(PyGIBaseInfo *self, PyObject *args)
+{
+    PyObject *obj;
+    GType gtype;
+    GInterfaceInfo *info_struct = g_new0(GInterfaceInfo, 1);
+
+    if (!PyArg_ParseTuple(args, "O:TypeInfo.register", &obj))
+        return NULL;
+
+    info_struct->interface_init = (GInterfaceInitFunc) initialize_interface;
+    info_struct->interface_finalize = NULL;
+    info_struct->interface_data = NULL;
+
+    gtype = pyg_type_from_object(obj);
+    pyg_register_interface_info(gtype, info_struct);
+
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
 static PyMethodDef _PyGIInterfaceInfo_methods[] = {
     { "getMethods", (PyCFunction)_wrap_g_interface_info_get_methods, METH_NOARGS },
+    { "register", (PyCFunction)_wrap_g_interface_info_register, METH_VARARGS },
     { NULL, NULL, 0 }
 };
 
diff --git a/bank/module.py b/bank/module.py
index 10cad0c..d102872 100644
--- a/bank/module.py
+++ b/bank/module.py
@@ -184,6 +184,7 @@ class DynamicModule(object):
         if gtype is not None:
             klass.__gtype__ = gtype
             gtype.pytype = klass
+            interface_info.register(gtype)
         self.__dict__[name] = klass
 
         return klass



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