summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Ehresman <jpe@src.gnome.org>2004-07-05 16:45:58 +0000
committerJohn Ehresman <jpe@src.gnome.org>2004-07-05 16:45:58 +0000
commit53d3853168fabddfe97fe6295ba34d7407a9139a (patch)
tree2ab325bb05aaf046d047534789173acab0a3b9ef
parent15a51c629c459f5b69bd0f56a230f66bf7b631ec (diff)
downloadpygobject-53d3853168fabddfe97fe6295ba34d7407a9139a.tar.gz
pygobject-53d3853168fabddfe97fe6295ba34d7407a9139a.tar.xz
pygobject-53d3853168fabddfe97fe6295ba34d7407a9139a.zip
New conversion functions that use GParamSpec's declarations of new param
* pygtype.c (pyg_param_gvalue_from_pyobject, pyg_param_gvalue_as_pyobject): New conversion functions that use GParamSpec's * pygobject.h, pygobject-private.h: declarations of new param conversion functions * pygobject.c (pygobject_get_property, pygobject_set_property): Use param conversion functions * gobjectmodule.c (pyg_parse_constructor_args): reorder local variable declaration so it will compile with VC++ * tests/testconversion.py: tests for above
-rw-r--r--gobject/gobjectmodule.c10
-rw-r--r--gobject/pygobject-private.h6
-rw-r--r--gobject/pygobject.c4
-rw-r--r--gobject/pygobject.h9
-rw-r--r--gobject/pygtype.c39
-rw-r--r--tests/testconversion.py4
6 files changed, 65 insertions, 7 deletions
diff --git a/gobject/gobjectmodule.c b/gobject/gobjectmodule.c
index 5094311..563b7bd 100644
--- a/gobject/gobjectmodule.c
+++ b/gobject/gobjectmodule.c
@@ -2082,7 +2082,8 @@ pyg_parse_constructor_args(GType obj_type,
char **prop_names,
GParameter *params,
guint *nparams,
- PyObject **py_args)
+ PyObject **pos_args,
+ PyObject *kw_args)
{
guint arg_i, param_i;
GObjectClass *oclass;
@@ -2091,9 +2092,10 @@ pyg_parse_constructor_args(GType obj_type,
g_return_val_if_fail(oclass, FALSE);
for (param_i = arg_i = 0; arg_names[arg_i]; ++arg_i) {
+ GParamSpec *spec;
if (!py_args[arg_i])
continue;
- GParamSpec *spec = g_object_class_find_property(oclass, prop_names[arg_i]);
+ spec = g_object_class_find_property(oclass, prop_names[arg_i]);
params[param_i].name = prop_names[arg_i];
g_value_init(&params[param_i].value, spec->value_type);
if (pyg_value_from_pyobject(&params[param_i].value, py_args[arg_i]) == -1) {
@@ -2158,7 +2160,9 @@ struct _PyGObject_Functions pygobject_api_functions = {
pyg_param_spec_new,
pyg_param_spec_from_object,
pyg_pyobj_to_unichar_conv,
- pyg_parse_constructor_args
+ pyg_parse_constructor_args,
+ pyg_param_gvalue_as_pyobject,
+ pyg_param_gvalue_from_pyobject
};
DL_EXPORT(void)
diff --git a/gobject/pygobject-private.h b/gobject/pygobject-private.h
index 6c8bde1..38eff22 100644
--- a/gobject/pygobject-private.h
+++ b/gobject/pygobject-private.h
@@ -54,6 +54,12 @@ void pyg_register_boxed_custom(GType boxed_type,
tovaluefunc to_func);
int pyg_value_from_pyobject(GValue *value, PyObject *obj);
PyObject *pyg_value_as_pyobject(const GValue *value, gboolean copy_boxed);
+int pyg_param_gvalue_from_pyobject(GValue* value,
+ PyObject* py_obj,
+ const GParamSpec* pspec);
+PyObject *pyg_param_gvalue_as_pyobject(const GValue* gvalue,
+ gboolean copy_boxed,
+ const GParamSpec* pspec);
typedef struct _PyGClosure PyGClosure;
struct _PyGClosure {
diff --git a/gobject/pygobject.c b/gobject/pygobject.c
index b6ea6f8..6c1a6f0 100644
--- a/gobject/pygobject.c
+++ b/gobject/pygobject.c
@@ -669,7 +669,7 @@ pygobject_get_property(PyGObject *self, PyObject *args)
}
g_value_init(&value, G_PARAM_SPEC_VALUE_TYPE(pspec));
g_object_get_property(self->obj, param_name, &value);
- ret = pyg_value_as_pyobject(&value, TRUE);
+ ret = pyg_param_gvalue_as_pyobject(&value, TRUE, pspec);
g_value_unset(&value);
return ret;
}
@@ -698,7 +698,7 @@ pygobject_set_property(PyGObject *self, PyObject *args)
return NULL;
}
g_value_init(&value, G_PARAM_SPEC_VALUE_TYPE(pspec));
- if (pyg_value_from_pyobject(&value, pvalue) < 0) {
+ if (pyg_param_gvalue_from_pyobject(&value, pvalue, pspec) < 0) {
PyErr_SetString(PyExc_TypeError,
"could not convert argument to correct param type");
return NULL;
diff --git a/gobject/pygobject.h b/gobject/pygobject.h
index 70c1565..594e285 100644
--- a/gobject/pygobject.h
+++ b/gobject/pygobject.h
@@ -117,6 +117,13 @@ struct _PyGObject_Functions {
GParameter *params,
guint *nparams,
PyObject **py_args);
+ PyObject* (* param_gvalue_as_pyobject) (const GValue* gvalue,
+ gboolean copy_boxed,
+ const GParamSpec* pspec);
+ int (* gvalue_from_param_pyobject) (GValue* value,
+ PyObject* py_obj,
+ const GParamSpec* pspec);
+
};
#ifndef _INSIDE_PYGOBJECT_
@@ -159,6 +166,8 @@ struct _PyGObject_Functions *_PyGObject_API;
#define pyg_param_spec_from_object (_PyGObject_API->paramspec_get)
#define pyg_pyobj_to_unichar_conv (_PyGObject_API->pyobj_to_unichar_conv)
#define pyg_parse_constructor_args (_PyGObject_API->parse_constructor_args)
+#define pyg_param_gvalue_from_pyobject (_PyGObject_API->value_from_pyobject)
+#define pyg_param_gvalue_as_pyobject (_PyGObject_API->value_as_pyobject)
#define pyg_block_threads() G_STMT_START { \
if (_PyGObject_API->block_threads != NULL) \
(* _PyGObject_API->block_threads)(); \
diff --git a/gobject/pygtype.c b/gobject/pygtype.c
index 179182b..e28c84c 100644
--- a/gobject/pygtype.c
+++ b/gobject/pygtype.c
@@ -1205,3 +1205,42 @@ int pyg_pyobj_to_unichar_conv(PyObject* py_obj, void* ptr)
Py_XDECREF(tmp_uni);
return 0;
}
+
+
+int
+pyg_param_gvalue_from_pyobject(GValue* value,
+ PyObject* py_obj,
+ const GParamSpec* pspec)
+{
+ if (G_IS_PARAM_SPEC_UNICHAR(pspec)) {
+ gunichar u;
+
+ if (!pyg_pyobj_to_unichar_conv(py_obj, &u)) {
+ PyErr_Clear();
+ return -1;
+ }
+ g_value_set_uint(value, u);
+ return 0;
+ }
+ else {
+ return pyg_value_from_pyobject(value, py_obj);
+ }
+}
+
+PyObject*
+pyg_param_gvalue_as_pyobject(const GValue* gvalue,
+ gboolean copy_boxed,
+ const GParamSpec* pspec)
+{
+ if (G_IS_PARAM_SPEC_UNICHAR(pspec)) {
+ gunichar u;
+ Py_UNICODE uni_buffer[2] = { 0, 0 };
+
+ u = g_value_get_uint(gvalue);
+ uni_buffer[0] = u;
+ return PyUnicode_FromUnicode(uni_buffer, 1);
+ }
+ else {
+ return pyg_value_as_pyobject(gvalue, copy_boxed);
+ }
+}
diff --git a/tests/testconversion.py b/tests/testconversion.py
index 63d08f4..22debdc 100644
--- a/tests/testconversion.py
+++ b/tests/testconversion.py
@@ -24,8 +24,8 @@ class Tests(unittest.TestCase):
raise AssertionError('exception not raised on invalid value w/ set_invisible_char: %s'
% invalid_value)
- def failtestUnicharProperty(self):
- """ Test unichar values when used as arguments. """
+ def testUnicharProperty(self):
+ """ Test unichar values when used as properties. """
entry = gtk.Entry()
for valid_value in ['a', u'b', u'\ufff0', u'\ufff0'.encode()]: