[pygobject] Some error handling/reporting fixes.



commit 7e29227b6f58cfcc96118a4af83658ca1a6fa1f4
Author: Christoph Reiter <creiter src gnome org>
Date:   Sat Jul 4 22:09:46 2015 +0200

    Some error handling/reporting fixes.
    
    * Check in pyg_boxed_new() if the passed type is an actual subclass
    * Don't replace existing exceptions in pyg_value_as_pyobject()
    * Print an error in pyg_closure_marshal() in case marshalling
      an argument failed.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=751956

 gi/pygboxed.c   |    8 +++++++-
 gi/pygi-value.c |   15 ++++++++++-----
 gi/pygtype.c    |   11 ++++++++++-
 3 files changed, 27 insertions(+), 7 deletions(-)
---
diff --git a/gi/pygboxed.c b/gi/pygboxed.c
index 30ab423..cdb766c 100644
--- a/gi/pygboxed.c
+++ b/gi/pygboxed.c
@@ -189,7 +189,7 @@ pyg_register_boxed(PyObject *dict, const gchar *class_name,
  * wrapper will be freed when the wrapper is deallocated.  If
  * @copy_boxed is True, then @own_ref must also be True.
  *
- * Returns: the boxed wrapper.
+ * Returns: the boxed wrapper or %NULL and sets an exception.
  */
 PyObject *
 pyg_boxed_new(GType boxed_type, gpointer boxed, gboolean copy_boxed,
@@ -218,6 +218,12 @@ pyg_boxed_new(GType boxed_type, gpointer boxed, gboolean copy_boxed,
     if (!tp)
        tp = (PyTypeObject *)&PyGBoxed_Type; /* fallback */
 
+    if (!PyType_IsSubtype (tp, &PyGBoxed_Type)) {
+        PyErr_Format (PyExc_RuntimeError, "%s isn't a GBoxed", tp->tp_name);
+        pyglib_gil_state_release (state);
+        return NULL;
+    }
+
     self = (PyGBoxed *)tp->tp_alloc(tp, 0);
 
     if (self == NULL) {
diff --git a/gi/pygi-value.c b/gi/pygi-value.c
index 413e14a..b155a68 100644
--- a/gi/pygi-value.c
+++ b/gi/pygi-value.c
@@ -834,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
@@ -865,9 +865,14 @@ 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;
 }
 
diff --git a/gi/pygtype.c b/gi/pygtype.c
index 985e969..a3784c8 100644
--- a/gi/pygtype.c
+++ b/gi/pygtype.c
@@ -704,7 +704,16 @@ pyg_closure_marshal(GClosure *closure,
 
            /* error condition */
            if (!item) {
-               goto out;
+            if (!PyErr_Occurred ())
+                PyErr_SetString (PyExc_TypeError,
+                                 "can't convert parameter to desired type");
+
+            if (pc->exception_handler)
+                pc->exception_handler (return_value, n_param_values, param_values);
+            else
+                PyErr_Print();
+
+            goto out;
            }
            PyTuple_SetItem(params, i, item);
        }


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