[pygobject] Break pyg_value_as_pyobject into two functions



commit 8f4b06f700ed79df32774fad8e2a2a922bfbfbe5
Author: Simon Feltman <sfeltman src gnome org>
Date:   Fri Aug 8 16:31:01 2014 -0700

    Break pyg_value_as_pyobject into two functions
    
    Add pygi_value_to_py_basic_type() which is limited to handling basic
    types that don't need introspection information when marshalling to Python.
    Add pygi_value_to_py_structured_type() for marshalling of structured data
    which can eventually accept GI type hints.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=726999

 gi/pygi-value.c |   74 ++++++++++++++++++++++++++++++++++++++++++++++---------
 gi/pygi-value.h |    6 ++++
 2 files changed, 68 insertions(+), 12 deletions(-)
---
diff --git a/gi/pygi-value.c b/gi/pygi-value.c
index e09c839..c83ef5c 100644
--- a/gi/pygi-value.c
+++ b/gi/pygi-value.c
@@ -643,26 +643,19 @@ pyg_value_from_pyobject(GValue *value, PyObject *obj)
 }
 
 /**
- * pyg_value_as_pyobject:
+ * pygi_value_to_py_basic_type:
  * @value: the GValue object.
- * @copy_boxed: true if boxed values should be copied.
  *
  * This function creates/returns a Python wrapper object that
- * represents the GValue passed as an argument.
+ * represents the GValue passed as an argument limited to supporting basic types
+ * like ints, bools, and strings.
  *
  * Returns: a PyObject representing the value.
  */
 PyObject *
-pyg_value_as_pyobject(const GValue *value, gboolean copy_boxed)
+pygi_value_to_py_basic_type (const GValue *value, GType fundamental)
 {
-    gchar buf[128];
-
-    switch (G_TYPE_FUNDAMENTAL(G_VALUE_TYPE(value))) {
-    case G_TYPE_INTERFACE:
-        if (g_type_is_a(G_VALUE_TYPE(value), G_TYPE_OBJECT))
-            return pygobject_new(g_value_get_object(value));
-        else
-            break;
+    switch (fundamental) {
     case G_TYPE_CHAR: {
         gint8 val = g_value_get_schar(value);
         return PYGLIB_PyUnicode_FromStringAndSize((char *)&val, 1);
@@ -734,6 +727,31 @@ pyg_value_as_pyobject(const GValue *value, gboolean copy_boxed)
         Py_INCREF(Py_None);
         return Py_None;
     }
+    default:
+        return NULL;
+    }
+}
+
+/**
+ * pygi_value_to_py_structured_type:
+ * @value: the GValue object.
+ * @copy_boxed: true if boxed values should be copied.
+ *
+ * This function creates/returns a Python wrapper object that
+ * represents the GValue passed as an argument.
+ *
+ * Returns: a PyObject representing the value.
+ */
+PyObject *
+pygi_value_to_py_structured_type (const GValue *value, GType fundamental, gboolean copy_boxed)
+{
+    switch (fundamental) {
+    case G_TYPE_INTERFACE:
+        if (g_type_is_a(G_VALUE_TYPE(value), G_TYPE_OBJECT))
+            return pygobject_new(g_value_get_object(value));
+        else
+            break;
+
     case G_TYPE_POINTER:
         if (G_VALUE_HOLDS_GTYPE (value))
             return pyg_type_wrapper_new (g_value_get_gtype (value));
@@ -804,6 +822,38 @@ pyg_value_as_pyobject(const GValue *value, gboolean copy_boxed)
         break;
     }
     }
+
+    return NULL;
+}
+
+
+/**
+ * pyg_value_as_pyobject:
+ * @value: the GValue object.
+ * @copy_boxed: true if boxed values should be copied.
+ *
+ * This function creates/returns a Python wrapper object that
+ * represents the GValue passed as an argument.
+ *
+ * Returns: a PyObject representing the value.
+ */
+PyObject *
+pyg_value_as_pyobject (const GValue *value, gboolean copy_boxed)
+{
+    gchar buf[128];
+    PyObject *pyobj;
+    GType fundamental = G_TYPE_FUNDAMENTAL (G_VALUE_TYPE (value));
+
+    pyobj = pygi_value_to_py_basic_type (value, fundamental);
+    if (pyobj) {
+        return pyobj;
+    }
+
+    pyobj = pygi_value_to_py_structured_type (value, fundamental, copy_boxed);
+    if (pyobj) {
+        return pyobj;
+    }
+
     g_snprintf(buf, sizeof(buf), "unknown type %s",
                g_type_name(G_VALUE_TYPE(value)));
     PyErr_SetString(PyExc_TypeError, buf);
diff --git a/gi/pygi-value.h b/gi/pygi-value.h
index 544da3c..ce2e902 100644
--- a/gi/pygi-value.h
+++ b/gi/pygi-value.h
@@ -39,6 +39,12 @@ PyObject *pyg_param_gvalue_as_pyobject(const GValue* gvalue,
 PyObject *pyg_strv_from_gvalue(const GValue *value);
 int       pyg_strv_to_gvalue(GValue *value, PyObject *obj);
 
+PyObject *pygi_value_to_py_basic_type      (const GValue *value,
+                                            GType fundamental);
+PyObject *pygi_value_to_py_structured_type (const GValue *value,
+                                            GType fundamental,
+                                            gboolean copy_boxed);
+
 G_END_DECLS
 
 #endif /* __PYGI_VALUE_H__ */


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