[pygobject/gsoc2009: 83/160] Add gobject.enum_from_g_type



commit 8ea8a0c78107d3e1e1dff0815cde3f9624ee1cff
Author: Simon van der Linden <svdlinden src gnome org>
Date:   Fri Jul 31 12:08:44 2009 +0200

    Add gobject.enum_from_g_type
    
    Add gobject.enum_from_g_type to make it possible to get a Python type for a
    GEnum from Python. Otherwise, it wouldn't be possible since we'd need instances
    of the type to be created to fill __enum_values__ before being able to
    instanciate the type.

 gi/module.py            |   12 ++++++++++--
 gobject/gobjectmodule.c |   34 ++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 2 deletions(-)
---
diff --git a/gi/module.py b/gi/module.py
index 6bac1f1..e790706 100644
--- a/gi/module.py
+++ b/gi/module.py
@@ -84,6 +84,16 @@ class DynamicModule(object):
                 '__module__': info.getNamespace()
             }
             value = GObjectIntrospectionMeta(name, bases, dict_)
+        elif isinstance(info, EnumInfo):
+            type_ = info.getGType()
+            if type_.is_a(gobject.TYPE_ENUM):
+                value = gobject.enum_from_g_type(type_)
+            elif type_.is_a(gobject.TYPE_FLAGS):
+                value = gobject.flags_from_g_type(type_)
+            else:
+                raise TypeError, "Must be either a subtype of gobject.TYPE_ENUM, or gobject.TYPE_FLAGS"
+            value.__info__ = info
+            value.__module__ = info.getNamespace()
         elif isinstance(info, RegisteredTypeInfo):
             # Check if there is already a Python wrapper.
             gtype = info.getGType()
@@ -97,8 +107,6 @@ class DynamicModule(object):
                 bases = (parent,)
             elif isinstance(info, InterfaceInfo):
                 bases = (GInterface,)
-            elif isinstance(info, EnumInfo):
-                bases = (GEnum,)
             elif isinstance(info, BoxedInfo):
                 bases = (GBoxed,)
             else:
diff --git a/gobject/gobjectmodule.c b/gobject/gobjectmodule.c
index e9dd214..5fbc994 100644
--- a/gobject/gobjectmodule.c
+++ b/gobject/gobjectmodule.c
@@ -1985,6 +1985,38 @@ pyg__install_metaclass(PyObject *dummy, PyTypeObject *metaclass)
     return Py_None;
 }
 
+static PyObject *
+_wrap_pyg_enum_from_g_type(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+    static char *kwlist[] = { "type", NULL };
+    PyObject *py_g_type;
+    GType g_type;
+    PyObject *type;
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
+                                     "O!:gobject.enum_from_g_type",
+                                     kwlist, &PyGTypeWrapper_Type, &py_g_type)) {
+        return NULL;
+    }
+
+    g_type = pyg_type_from_object(py_g_type);
+
+    if (!g_type_is_a(g_type, G_TYPE_ENUM)) {
+        PyErr_SetString(PyExc_TypeError,
+            "gobject.enum_from_g_type() argument 0: Must be a subtype of gobject.TYPE_ENUM");
+        return NULL;
+    }
+
+    type = (PyObject *)g_type_get_qdata(g_type, pygenum_class_key);
+    if (type == NULL) {
+        type = pyg_enum_add(NULL, g_type_name(g_type), NULL, g_type);
+    } else {
+        Py_INCREF(type);
+    }
+
+    return type;
+}
+
 static PyMethodDef _gobject_functions[] = {
     { "type_name", pyg_type_name, METH_VARARGS },
     { "type_from_name", pyg_type_from_name, METH_VARARGS },
@@ -2018,6 +2050,8 @@ static PyMethodDef _gobject_functions[] = {
       (PyCFunction)pyg_remove_emission_hook, METH_VARARGS },
     { "_install_metaclass",
       (PyCFunction)pyg__install_metaclass, METH_O },
+    { "enum_from_g_type",
+      (PyCFunction)_wrap_pyg_enum_from_g_type, METH_VARARGS|METH_KEYWORDS },
 
     { NULL, NULL, 0 }
 };



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