diff options
| author | Jonathan Blandford <jrb@gnome.org> | 2002-06-24 03:41:28 +0000 |
|---|---|---|
| committer | Jonathan Blandford <jrb@src.gnome.org> | 2002-06-24 03:41:28 +0000 |
| commit | 3a1d9c9029779bdf632474d29db9a703194292b6 (patch) | |
| tree | 5cb540cfc21123003e32a52cabe501afa0794b89 /gobject | |
| parent | ee6e2f2bf962136029b000e3dc77abf13d217672 (diff) | |
| download | pygobject-3a1d9c9029779bdf632474d29db9a703194292b6.tar.gz pygobject-3a1d9c9029779bdf632474d29db9a703194292b6.tar.xz pygobject-3a1d9c9029779bdf632474d29db9a703194292b6.zip | |
Add GBoxed::copy
Sun Jun 23 11:10:30 2002 Jonathan Blandford <jrb@gnome.org>
* pygboxed.c: Add GBoxed::copy
* pygobject.c (pygobject_init): take kwargs so we can pass
construct-only arguments to our initialization function.
Diffstat (limited to 'gobject')
| -rw-r--r-- | gobject/pygboxed.c | 17 | ||||
| -rw-r--r-- | gobject/pygobject.c | 68 |
2 files changed, 83 insertions, 2 deletions
diff --git a/gobject/pygboxed.c b/gobject/pygboxed.c index 5b3f56d..a8f4a25 100644 --- a/gobject/pygboxed.c +++ b/gobject/pygboxed.c @@ -61,6 +61,21 @@ pyg_boxed_free(PyObject *op) PyObject_FREE(op); } +static PyObject * +pyg_boxed_copy(PyGBoxed *self) +{ + return pyg_boxed_new (self->gtype, self->boxed, TRUE, TRUE); +} + + + +static PyMethodDef pygboxed_methods[] = { + { "copy", (PyCFunction) pyg_boxed_copy, METH_NOARGS }, + { NULL, NULL, 0 } +}; + + + PyTypeObject PyGBoxed_Type = { PyObject_HEAD_INIT(NULL) 0, /* ob_size */ @@ -91,7 +106,7 @@ PyTypeObject PyGBoxed_Type = { 0, /* tp_weaklistoffset */ (getiterfunc)0, /* tp_iter */ (iternextfunc)0, /* tp_iternext */ - 0, /* tp_methods */ + pygboxed_methods, /* tp_methods */ 0, /* tp_members */ 0, /* tp_getset */ (PyTypeObject *)0, /* tp_base */ diff --git a/gobject/pygobject.c b/gobject/pygobject.c index 1c99deb..da8d17c 100644 --- a/gobject/pygobject.c +++ b/gobject/pygobject.c @@ -229,10 +229,27 @@ 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; + GObjectClass *class; + gint retval = 0; if (!PyArg_ParseTuple(args, ":GObject.__init__", &object_type)) return -1; @@ -241,7 +258,56 @@ pygobject_init(PyGObject *self, PyObject *args, PyObject *kwargs) if (!object_type) return -1; - self->obj = g_object_new(object_type, NULL); + if ((class = g_type_class_ref (object_type)) == NULL) { + PyErr_SetString(PyExc_TypeError, + "could not get a reference to type class"); + return -1; + } + + + while (kwargs && PyDict_Next(kwargs, &pos, &key, &item)) { + gchar *param_name; + 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; + } + + n_parameters ++; + if (parameters == NULL) { + parameters = g_new (GParameter, 1); + } else { + parameters = g_renew (GParameter, parameters, n_parameters); + } + parameters[n_parameters - 1].name = g_strdup (param_name); + parameters[n_parameters - 1].value = value; + } + + 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; |
