Imported Upstream version 3.19.91
[platform/upstream/python-gobject.git] / gi / pygi-value.c
index 2cf567d..9da87a5 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <Python.h>
 #include "pygi-value.h"
+#include "pygi-struct.h"
 #include "pyglib-python-compat.h"
 #include "pygobject-private.h"
 #include "pygtype.h"
@@ -775,9 +776,10 @@ pygi_value_to_py_structured_type (const GValue *value, GType fundamental, gboole
             return pyg_value_as_pyobject(n_value, copy_boxed);
         } else if (holds_value_array) {
             GValueArray *array = (GValueArray *) g_value_get_boxed(value);
-            PyObject *ret = PyList_New(array->n_values);
+            Py_ssize_t n_values = array ? array->n_values : 0;
+            PyObject *ret = PyList_New(n_values);
             int i;
-            for (i = 0; i < array->n_values; ++i)
+            for (i = 0; i < n_values; ++i)
                 PyList_SET_ITEM(ret, i, pyg_value_as_pyobject
                         (array->values + i, copy_boxed));
             return ret;
@@ -809,7 +811,7 @@ pygi_value_to_py_structured_type (const GValue *value, GType fundamental, gboole
             Py_INCREF(Py_None);
             return Py_None;
         }
-        return pyg_boxed_new(G_TYPE_VARIANT, g_variant_ref(v), FALSE, FALSE);
+        return _pygi_struct_new_from_g_type (G_TYPE_VARIANT, g_variant_ref(v), FALSE);
     }
     default:
     {
@@ -832,13 +834,13 @@ pygi_value_to_py_structured_type (const GValue *value, GType fundamental, gboole
  * This function creates/returns a Python wrapper object that
  * represents the GValue passed as an argument.
  *
- * Returns: a PyObject representing the value.
+ * Returns: a PyObject representing the value or %NULL and sets an exception.
  */
 PyObject *
 pyg_value_as_pyobject (const GValue *value, gboolean copy_boxed)
 {
-    gchar buf[128];
     PyObject *pyobj;
+    const gchar *type_name;
     GType fundamental = G_TYPE_FUNDAMENTAL (G_VALUE_TYPE (value));
 
     /* HACK: special case char and uchar to return PyBytes intstead of integers
@@ -863,10 +865,16 @@ pyg_value_as_pyobject (const GValue *value, gboolean copy_boxed)
         return pyobj;
     }
 
-    g_snprintf(buf, sizeof(buf), "unknown type %s",
-               g_type_name(G_VALUE_TYPE(value)));
-    PyErr_SetString(PyExc_TypeError, buf);
+    if (!PyErr_Occurred ()) {
+        type_name = g_type_name (G_VALUE_TYPE (value));
+        if (type_name == NULL) {
+            type_name = "(null)";
+        }
+        PyErr_Format (PyExc_TypeError, "unknown type %s", type_name);
+    }
+
     return NULL;
+
 }