summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Henstridge <james@daa.com.au>2002-12-24 09:11:44 +0000
committerJames Henstridge <jamesh@src.gnome.org>2002-12-24 09:11:44 +0000
commita833d630d6d8c7bd142dca4d1f60c209ec97e2b8 (patch)
tree2085a59c0455110a90daa3a54982afcb22313817
parenta93b4bd6c94a2134598109873964ffead2e46750 (diff)
downloadpygobject-a833d630d6d8c7bd142dca4d1f60c209ec97e2b8.tar.gz
pygobject-a833d630d6d8c7bd142dca4d1f60c209ec97e2b8.tar.xz
pygobject-a833d630d6d8c7bd142dca4d1f60c209ec97e2b8.zip
add abstract type check here too. (pygobject_init): make this code more
2002-12-24 James Henstridge <james@daa.com.au> * pygobject.c (pygobject_init): add abstract type check here too. (pygobject_init): make this code more similar to pyg_object_new, so that it is easier to fix bugs in the future. * gobjectmodule.c (pyg_object_new): handle case of no keyword arguments. (pyg_object_new): refuse to instantiate an abstract type.
-rw-r--r--gobject/gobjectmodule.c71
-rw-r--r--gobject/pygobject.c105
2 files changed, 84 insertions, 92 deletions
diff --git a/gobject/gobjectmodule.c b/gobject/gobjectmodule.c
index 959db46..da2aa72 100644
--- a/gobject/gobjectmodule.c
+++ b/gobject/gobjectmodule.c
@@ -1099,10 +1099,8 @@ pyg_object_new (PyGObject *self, PyObject *args, PyObject *kwargs)
GType type;
GObject *obj = NULL;
GObjectClass *class;
- PyObject *value;
- PyObject *key;
- int pos=0, num_params=0, i;
- GParameter *params;
+ int n_params = 0, i;
+ GParameter *params = NULL;
if (!PyArg_ParseTuple (args, "O:gobject.new", &pytype)) {
return NULL;
@@ -1110,48 +1108,55 @@ pyg_object_new (PyGObject *self, PyObject *args, PyObject *kwargs)
if ((type = pyg_type_from_object (pytype)) == 0)
return NULL;
-
+
+ if (G_TYPE_IS_ABSTRACT(type)) {
+ PyErr_Format(PyExc_TypeError, "cannot create instance of abstract "
+ "(non-instantiable) type `%s'", g_type_name(type));
+ return NULL;
+ }
+
if ((class = g_type_class_ref (type)) == NULL) {
PyErr_SetString(PyExc_TypeError,
"could not get a reference to type class");
return NULL;
}
- params = g_new0(GParameter, PyDict_Size(kwargs));
-
- while (kwargs && PyDict_Next (kwargs, &pos, &key, &value)) {
- GParamSpec *pspec;
- gchar *key_str = g_strdup(PyString_AsString (key));
- pspec = g_object_class_find_property (class, key_str);
- if (!pspec) {
- gchar buf[512];
-
- g_snprintf(buf, sizeof(buf),
- "gobject `%s' doesn't support property `%s'",
- g_type_name(type), key_str);
- PyErr_SetString(PyExc_TypeError, buf);
- goto cleanup;
+ if (kwargs) {
+ int 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 = PyString_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_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++;
}
- g_value_init(&params[num_params].value,
- G_PARAM_SPEC_VALUE_TYPE(pspec));
- if (pyg_value_from_pyobject(&params[num_params].value, value)) {
- gchar buf[512];
-
- g_snprintf(buf, sizeof(buf),
- "could not convert value for property `%s'", key_str);
- PyErr_SetString(PyExc_TypeError, buf);
- goto cleanup;
- }
- params[num_params].name = key_str;
- num_params++;
}
- obj = g_object_newv(type, num_params, params);
+ obj = g_object_newv(type, n_params, params);
if (!obj)
PyErr_SetString (PyExc_RuntimeError, "could not create object");
cleanup:
- for (i = 0; i < num_params; i++) {
+ for (i = 0; i < n_params; i++) {
g_free((gchar *) params[i].name);
g_value_unset(&params[i].value);
}
diff --git a/gobject/pygobject.c b/gobject/pygobject.c
index 9a8cb02..76bfb5d 100644
--- a/gobject/pygobject.c
+++ b/gobject/pygobject.c
@@ -339,25 +339,12 @@ pygobject_free(PyObject *op)
/* ---------------- PyGObject methods ----------------- */
-static void
-parameter_list_free (GParameter *parameters, guint n_parameters)
-{
- gint i;
- for (i = 0; i < n_parameters; i ++){
- g_free ((char *)parameters[i].name);
- g_value_unset (&(parameters[i].value));
- }
- g_free (parameters);
-}
-
static int
pygobject_init(PyGObject *self, PyObject *args, PyObject *kwargs)
{
GType object_type;
- guint n_parameters = 0;
- GParameter *parameters = NULL;
- PyObject *key, *item;
- gint pos = 0;
+ guint n_params = 0, i;
+ GParameter *params = NULL;
GObjectClass *class;
if (!PyArg_ParseTuple(args, ":GObject.__init__", &object_type))
@@ -367,63 +354,63 @@ pygobject_init(PyGObject *self, PyObject *args, PyObject *kwargs)
if (!object_type)
return -1;
+ if (G_TYPE_IS_ABSTRACT(object_type)) {
+ PyErr_Format(PyExc_TypeError, "cannot create instance of abstract "
+ "(non-instantiable) type `%s'", g_type_name(object_type));
+ return -1;
+ }
+
if ((class = g_type_class_ref (object_type)) == NULL) {
PyErr_SetString(PyExc_TypeError,
"could not get a reference to type class");
return -1;
}
+ if (kwargs) {
+ int pos = 0;
+ PyObject *key;
+ PyObject *value;
- while (kwargs && PyDict_Next(kwargs, &pos, &key, &item)) {
- gchar *param_name;
+ params = g_new0(GParameter, PyDict_Size(kwargs));
+ while (PyDict_Next (kwargs, &pos, &key, &value)) {
GParamSpec *pspec;
- GValue value = { 0, };
-
- param_name = PyString_AsString(key);
- pspec = g_object_class_find_property(class, param_name);
-
- if (pspec == NULL) {
- gchar buf[128];
-
- g_snprintf(buf, sizeof(buf),
- "Unknown parameter '%s' used in type initialization arguments",
- param_name);
- PyErr_SetString(PyExc_AttributeError, buf);
- parameter_list_free (parameters, n_parameters);
- g_type_class_unref(class);
- return -1;
- }
- g_value_init(&value, G_PARAM_SPEC_VALUE_TYPE(pspec));
- if (pyg_value_from_pyobject(&value, item) < 0) {
- PyErr_SetString(PyExc_TypeError,
- "could not convert argument to correct param type");
- parameter_list_free (parameters, n_parameters);
- g_type_class_unref(class);
- return -1;
+ gchar *key_str = PyString_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(object_type), key_str);
+ goto cleanup;
}
-
- n_parameters ++;
- if (parameters == NULL) {
- parameters = g_new (GParameter, 1);
- } else {
- parameters = g_renew (GParameter, parameters, n_parameters);
+ 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;
}
- parameters[n_parameters - 1].name = g_strdup (param_name);
- parameters[n_parameters - 1].value = value;
+ params[n_params].name = g_strdup(key_str);
+ n_params++;
}
-
- self->obj = g_object_newv(object_type, n_parameters, parameters);
- parameter_list_free (parameters, n_parameters);
-
- g_type_class_unref(class);
-
- if (!self->obj) {
- PyErr_SetString(PyExc_RuntimeError, "could not create object");
- return -1;
}
- pygobject_register_wrapper((PyObject *)self);
- return 0;
+ self->obj = g_object_newv(object_type, n_params, params);
+ if (self->obj)
+ pygobject_register_wrapper((PyObject *)self);
+ else
+ PyErr_SetString (PyExc_RuntimeError, "could not create object");
+
+ cleanup:
+ for (i = 0; i < n_params; i++) {
+ g_free((gchar *) params[i].name);
+ g_value_unset(&params[i].value);
+ }
+ g_free(params);
+ g_type_class_unref(class);
+
+ return (self->obj) ? 0 : -1;
}
static PyObject *