[pygobject] Factor out parameter marshalling from construction functions.



commit 7d70105eb324ea4b6a58c2d3fb3f2dda36e7ab33
Author: Steve Frécinaux <code istique net>
Date:   Fri Jan 21 17:24:49 2011 +0100

    Factor out parameter marshalling from construction functions.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=640197

 gobject/gobjectmodule.c     |   35 ++-------------------
 gobject/pygobject-private.h |    5 +++
 gobject/pygobject.c         |   71 +++++++++++++++++++++++++-----------------
 3 files changed, 50 insertions(+), 61 deletions(-)
---
diff --git a/gobject/gobjectmodule.c b/gobject/gobjectmodule.c
index fb095f7..13cf0f4 100644
--- a/gobject/gobjectmodule.c
+++ b/gobject/gobjectmodule.c
@@ -1689,7 +1689,7 @@ pyg_object_new (PyGObject *self, PyObject *args, PyObject *kwargs)
     GType type;
     GObject *obj = NULL;
     GObjectClass *class;
-    int n_params = 0, i;
+    guint n_params = 0, i;
     GParameter *params = NULL;
 
     if (!PyArg_ParseTuple (args, "O:gobject.new", &pytype)) {
@@ -1711,37 +1711,8 @@ pyg_object_new (PyGObject *self, PyObject *args, PyObject *kwargs)
 	return NULL;
     }
 
-    if (kwargs) {
-	Py_ssize_t pos = 0;
-	PyObject *key;
-	PyObject *value;
-
-	params = g_new0(GParameter, PyDict_Size(kwargs));
-	while (PyDict_Next (kwargs, &pos, &key, &value)) {
-	    GParamSpec *pspec;
-	    const gchar *key_str = PYGLIB_PyUnicode_AsString (key);
-
-	    pspec = g_object_class_find_property (class, key_str);
-	    if (!pspec) {
-		PyErr_Format(PyExc_TypeError,
-			     "gobject `%s' doesn't support property `%s'",
-			     g_type_name(type), key_str);
-		goto cleanup;
-	    }
-	    g_value_init(&params[n_params].value,
-			 G_PARAM_SPEC_VALUE_TYPE(pspec));
-	    if (pyg_param_gvalue_from_pyobject(&params[n_params].value,
-					       value, pspec) < 0) {
-		PyErr_Format(PyExc_TypeError,
-			     "could not convert value for property `%s' from %s to %s",
-			     key_str, Py_TYPE(value)->tp_name,
-			     g_type_name(G_PARAM_SPEC_VALUE_TYPE(pspec)));
-		goto cleanup;
-	    }
-	    params[n_params].name = g_strdup(key_str);
-	    n_params++;
-	}
-    }
+    if (!pygobject_prepare_construct_properties (class, kwargs, &n_params, &params))
+        goto cleanup;
 
     obj = g_object_newv(type, n_params, params);
     if (!obj)
diff --git a/gobject/pygobject-private.h b/gobject/pygobject-private.h
index a928cb1..99c1894 100644
--- a/gobject/pygobject-private.h
+++ b/gobject/pygobject-private.h
@@ -232,5 +232,10 @@ pyg_object_peek_inst_data(GObject *obj)
             g_object_get_qdata(obj, pygobject_instance_data_key));
 }
 
+gboolean        pygobject_prepare_construct_properties  (GObjectClass *class,
+                                                         PyObject *kwargs,
+                                                         guint *n_params,
+                                                         GParameter **params);
+
 
 #endif
diff --git a/gobject/pygobject.c b/gobject/pygobject.c
index 262cf69..98897fc 100644
--- a/gobject/pygobject.c
+++ b/gobject/pygobject.c
@@ -1137,6 +1137,45 @@ pygobject_free(PyObject *op)
     PyObject_GC_Del(op);
 }
 
+gboolean
+pygobject_prepare_construct_properties(GObjectClass *class, PyObject *kwargs,
+                                       guint *n_params, GParameter **params)
+{
+    *n_params = 0;
+    *params = NULL;
+
+    if (kwargs) {
+        Py_ssize_t pos = 0;
+        PyObject *key;
+        PyObject *value;
+
+        *params = g_new0(GParameter, PyDict_Size(kwargs));
+        while (PyDict_Next(kwargs, &pos, &key, &value)) {
+            GParamSpec *pspec;
+            GParameter *param = &(*params)[*n_params];
+            const gchar *key_str = PYGLIB_PyUnicode_AsString(key);
+
+            pspec = g_object_class_find_property(class, key_str);
+            if (!pspec) {
+                PyErr_Format(PyExc_TypeError,
+                             "gobject `%s' doesn't support property `%s'",
+                             G_OBJECT_CLASS_NAME(class), key_str);
+                return FALSE;
+            }
+            g_value_init(&param->value, G_PARAM_SPEC_VALUE_TYPE(pspec));
+            if (pyg_param_gvalue_from_pyobject(&param->value, value, pspec) < 0) {
+                PyErr_Format(PyExc_TypeError,
+                             "could not convert value for property `%s' from %s to %s",
+                             key_str, Py_TYPE(value)->tp_name,
+                             g_type_name(G_PARAM_SPEC_VALUE_TYPE(pspec)));
+                return FALSE;
+            }
+            param->name = g_strdup(key_str);
+            ++(*n_params);
+        }
+    }
+    return TRUE;
+}
 
 /* ---------------- PyGObject methods ----------------- */
 
@@ -1167,35 +1206,9 @@ pygobject_init(PyGObject *self, PyObject *args, PyObject *kwargs)
 	return -1;
     }
 
-    if (kwargs) {
-	Py_ssize_t pos = 0;
-	PyObject *key;
-	PyObject *value;
-
-	params = g_new0(GParameter, PyDict_Size(kwargs));
-	while (PyDict_Next (kwargs, &pos, &key, &value)) {
-	    GParamSpec *pspec;
-	    gchar *key_str = PYGLIB_PyUnicode_AsString(key);
-
-	    pspec = g_object_class_find_property (class, key_str);
-	    if (!pspec) {
-		PyErr_Format(PyExc_TypeError,
-			     "object of type `%s' doesn't support property `%s'",
-			     g_type_name(object_type), key_str);
-		goto cleanup;
-	    }
-	    g_value_init(&params[n_params].value,
-			 G_PARAM_SPEC_VALUE_TYPE(pspec));
-	    if (pyg_value_from_pyobject(&params[n_params].value, value)) {
-		PyErr_Format(PyExc_TypeError,
-			     "could not convert value for property `%s'",
-			     key_str);
-		goto cleanup;
-	    }
-	    params[n_params].name = g_strdup(key_str);
-	    n_params++;
-	}
-    }
+    if (!pygobject_prepare_construct_properties (class, kwargs, &n_params, &params))
+        goto cleanup;
+
     if (pygobject_constructv(self, n_params, params))
 	PyErr_SetString(PyExc_RuntimeError, "could not create object");
 	   



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