[pygobject/gsoc2009] Add support for constants in modules, objects and interfaces



commit 260837fc177222b3e58f742a649446dc05577972
Author: Simon van der Linden <svdlinden src gnome org>
Date:   Mon Aug 24 17:07:07 2009 +0200

    Add support for constants in modules, objects and interfaces

 gi/module.py      |    3 +
 gi/pygi-info.c    |  107 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 gi/pygi-private.h |    1 +
 gi/types.py       |    7 +++
 4 files changed, 116 insertions(+), 2 deletions(-)
---
diff --git a/gi/module.py b/gi/module.py
index ef0956a..f1ebe24 100644
--- a/gi/module.py
+++ b/gi/module.py
@@ -32,6 +32,7 @@ from ._gi import \
     EnumInfo, \
     ObjectInfo, \
     InterfaceInfo, \
+    ConstantInfo, \
     StructInfo
 from .types import GObjectMeta, StructMeta, Function
 
@@ -137,6 +138,8 @@ class DynamicModule(object):
 
         elif isinstance(info, FunctionInfo):
             value = Function(info)
+        elif isinstance(info, ConstantInfo):
+            value = info.get_value()
         else:
             raise NotImplementedError(info)
 
diff --git a/gi/pygi-info.c b/gi/pygi-info.c
index e668dbc..6f16bb6 100644
--- a/gi/pygi-info.c
+++ b/gi/pygi-info.c
@@ -185,8 +185,8 @@ _pygi_info_new (GIBaseInfo *info)
             type = &PyGIInterfaceInfo_Type;
             break;
         case GI_INFO_TYPE_CONSTANT:
-            PyErr_SetString(PyExc_NotImplementedError, "GIConstantInfo bindings not implemented");
-            return NULL;
+            type = &PyGIConstantInfo_Type;
+            break;
         case GI_INFO_TYPE_ERROR_DOMAIN:
             PyErr_SetString(PyExc_NotImplementedError, "GIErrorDomainInfo bindings not implemented");
             return NULL;
@@ -1581,11 +1581,48 @@ _wrap_g_object_info_get_interfaces (PyGIBaseInfo *self)
     return infos;
 }
 
+static PyObject *
+_wrap_g_object_info_get_constants (PyGIBaseInfo *self)
+{
+    gssize n_infos;
+    PyObject *infos;
+    gssize i;
+
+    n_infos = g_object_info_get_n_constants((GIObjectInfo *)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_object_info_get_constant((GIObjectInfo *)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 _PyGIObjectInfo_methods[] = {
     { "get_parent", (PyCFunction)_wrap_g_object_info_get_parent, METH_NOARGS },
     { "get_methods", (PyCFunction)_wrap_g_object_info_get_methods, METH_NOARGS },
     { "get_fields", (PyCFunction)_wrap_g_object_info_get_fields, METH_NOARGS },
     { "get_interfaces", (PyCFunction)_wrap_g_object_info_get_interfaces, METH_NOARGS },
+    { "get_constants", (PyCFunction)_wrap_g_object_info_get_constants, METH_NOARGS },
     { NULL, NULL, 0 }
 };
 
@@ -1629,11 +1666,76 @@ _wrap_g_interface_info_get_methods (PyGIBaseInfo *self)
     return infos;
 }
 
+static PyObject *
+_wrap_g_interface_info_get_constants (PyGIBaseInfo *self)
+{
+    gssize n_infos;
+    PyObject *infos;
+    gssize i;
+
+    n_infos = g_interface_info_get_n_constants((GIInterfaceInfo *)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_interface_info_get_constant((GIInterfaceInfo *)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 _PyGIInterfaceInfo_methods[] = {
     { "get_methods", (PyCFunction)_wrap_g_interface_info_get_methods, METH_NOARGS },
+    { "get_constants", (PyCFunction)_wrap_g_interface_info_get_constants, METH_NOARGS },
     { NULL, NULL, 0 }
 };
 
+/* GIConstantInfo */
+_PyGI_DEFINE_INFO_TYPE("ConstantInfo", GIConstantInfo, PyGIBaseInfo_Type);
+
+static PyObject *
+_wrap_g_constant_info_get_value (PyGIBaseInfo *self)
+{
+    GITypeInfo *type_info;
+    GArgument value;
+    PyObject *py_value;
+
+    if (g_constant_info_get_value((GIConstantInfo *)self->info, &value) < 0) {
+        PyErr_SetString(PyExc_RuntimeError, "unable to get value");
+        return NULL;
+    }
+
+    type_info = g_constant_info_get_type((GIConstantInfo *)self->info);
+
+    py_value = _pygi_argument_to_object(&value, type_info, GI_TRANSFER_NOTHING);
+
+    g_base_info_unref((GIBaseInfo *)type_info);
+
+    return py_value;
+}
+
+static PyMethodDef _PyGIConstantInfo_methods[] = {
+    { "get_value", (PyCFunction)_wrap_g_constant_info_get_value, METH_NOARGS },
+    { NULL, NULL, 0 }
+};
 
 /* GIValueInfo */
 _PyGI_DEFINE_INFO_TYPE("ValueInfo", GIValueInfo, PyGIBaseInfo_Type);
@@ -1954,6 +2056,7 @@ _pygi_info_register_types (PyObject *m)
     _PyGI_REGISTER_TYPE(m, PyGIEnumInfo_Type, "EnumInfo");
     _PyGI_REGISTER_TYPE(m, PyGIObjectInfo_Type, "ObjectInfo");
     _PyGI_REGISTER_TYPE(m, PyGIInterfaceInfo_Type, "InterfaceInfo");
+    _PyGI_REGISTER_TYPE(m, PyGIConstantInfo_Type, "ConstantInfo");
     _PyGI_REGISTER_TYPE(m, PyGIValueInfo_Type, "ValueInfo");
     _PyGI_REGISTER_TYPE(m, PyGIFieldInfo_Type, "FieldInfo");
 
diff --git a/gi/pygi-private.h b/gi/pygi-private.h
index 386e425..aa92311 100644
--- a/gi/pygi-private.h
+++ b/gi/pygi-private.h
@@ -35,6 +35,7 @@ extern PyTypeObject PyGIStructInfo_Type;
 extern PyTypeObject PyGIEnumInfo_Type;
 extern PyTypeObject PyGIObjectInfo_Type;
 extern PyTypeObject PyGIInterfaceInfo_Type;
+extern PyTypeObject PyGIConstantInfo_Type;
 extern PyTypeObject PyGIValueInfo_Type;
 extern PyTypeObject PyGIFieldInfo_Type;
 extern PyTypeObject PyGIUnresolvedInfo_Type;
diff --git a/gi/types.py b/gi/types.py
index 91bd269..3b84280 100644
--- a/gi/types.py
+++ b/gi/types.py
@@ -79,6 +79,12 @@ class MetaClassHelper(object):
             name = field_info.get_name().replace('-', '_')
             setattr(cls, name, property(field_info.get_value, field_info.set_value))
 
+    def _setup_constants(cls):
+        for constant_info in cls.__info__.get_constants():
+            name = constant_info.get_name()
+            value = constant_info.get_value()
+            setattr(cls, name, value)
+
 
 class GObjectMeta(gobject.GObjectMeta, MetaClassHelper):
 
@@ -90,6 +96,7 @@ class GObjectMeta(gobject.GObjectMeta, MetaClassHelper):
             return;
 
         cls._setup_methods()
+        cls._setup_constants()
 
         if (isinstance(cls.__info__, ObjectInfo)):
             cls._setup_fields()



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