[pygobject] Construct structs using default API constructor



commit 7c2f48bb6d67ec9a1ee5ac03a5aee34b54c6ebdd
Author: Tomeu Vizoso <tomeu vizoso collabora co uk>
Date:   Wed Jan 19 18:09:23 2011 +0100

    Construct structs using default API constructor
    
    If the struct has something that looks like a default constructor,
    use it instead of trying to directly allocate it, as it will fail
    if the struct fields are not exposed.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=627444

 gi/pygi-info.c |   37 +++++++++++++++++++++++++++++++++++++
 gi/types.py    |    7 +++++++
 2 files changed, 44 insertions(+), 0 deletions(-)
---
diff --git a/gi/pygi-info.c b/gi/pygi-info.c
index c753097..f5dd69f 100644
--- a/gi/pygi-info.c
+++ b/gi/pygi-info.c
@@ -234,8 +234,45 @@ out:
 /* CallableInfo */
 PYGLIB_DEFINE_TYPE ("gi.CallableInfo", PyGICallableInfo_Type, PyGIBaseInfo);
 
+static PyObject *
+_wrap_g_callable_info_get_arguments (PyGIBaseInfo *self)
+{
+    gssize n_infos;
+    PyObject *infos;
+    gssize i;
+
+    n_infos = g_callable_info_get_n_args ( (GICallableInfo *) self->info);
+
+    infos = PyTuple_New (n_infos);
+    if (infos == NULL) {
+        return NULL;
+    }
+
+    for (i = 0; i < n_infos; i++) {
+        GIBaseInfo *info;
+        PyObject *py_info;
+
+        info = (GIBaseInfo *) g_callable_info_get_arg ( (GICallableInfo *) self->info, i);
+        g_assert (info != NULL);
+
+        py_info = _pygi_info_new (info);
+
+        g_base_info_unref (info);
+
+        if (py_info == NULL) {
+            Py_CLEAR (infos);
+            break;
+        }
+
+        PyTuple_SET_ITEM (infos, i, py_info);
+    }
+
+    return infos;
+}
+
 static PyMethodDef _PyGICallableInfo_methods[] = {
     { "invoke", (PyCFunction) _wrap_g_callable_info_invoke, METH_VARARGS | METH_KEYWORDS },
+    { "get_arguments", (PyCFunction) _wrap_g_callable_info_get_arguments, METH_NOARGS },
     { NULL, NULL, 0 }
 };
 
diff --git a/gi/types.py b/gi/types.py
index ca302c7..446fa00 100644
--- a/gi/types.py
+++ b/gi/types.py
@@ -215,6 +215,13 @@ class StructMeta(type, MetaClassHelper):
         cls._setup_methods()
         cls._setup_constructors()
 
+        for method_info in cls.__info__.get_methods():
+            if method_info.is_constructor() and \
+                    method_info.get_name() == 'new' and \
+                    not method_info.get_arguments():
+                cls.__new__ = staticmethod(Constructor(method_info))
+                break
+
 class Enum(int):
     # Only subclasses of this type should be instantiated.
     # Each subclass requires an __info__ attribute,



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