2 /* -*- Mode: C; c-basic-offset: 4 -*-
3 * vim: tabstop=4 shiftwidth=4 expandtab
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "pygi-value.h"
21 #include "pygi-struct.h"
22 #include "pyglib-python-compat.h"
23 #include "pygobject-object.h"
26 #include "pygpointer.h"
29 #include "pygparamspec.h"
32 _pygi_argument_from_g_value(const GValue *value,
33 GITypeInfo *type_info)
35 GIArgument arg = { 0, };
37 GITypeTag type_tag = g_type_info_get_tag (type_info);
39 /* For the long handling: long can be equivalent to
40 int32 or int64, depending on the architecture, but
41 gi doesn't tell us (and same for ulong)
44 case GI_TYPE_TAG_BOOLEAN:
45 arg.v_boolean = g_value_get_boolean (value);
47 case GI_TYPE_TAG_INT8:
48 arg.v_int8 = g_value_get_schar (value);
50 case GI_TYPE_TAG_INT16:
51 case GI_TYPE_TAG_INT32:
52 if (g_type_is_a (G_VALUE_TYPE (value), G_TYPE_LONG))
53 arg.v_int = g_value_get_long (value);
55 arg.v_int = g_value_get_int (value);
57 case GI_TYPE_TAG_INT64:
58 if (g_type_is_a (G_VALUE_TYPE (value), G_TYPE_LONG))
59 arg.v_int64 = g_value_get_long (value);
61 arg.v_int64 = g_value_get_int64 (value);
63 case GI_TYPE_TAG_UINT8:
64 arg.v_uint8 = g_value_get_uchar (value);
66 case GI_TYPE_TAG_UINT16:
67 case GI_TYPE_TAG_UINT32:
68 if (g_type_is_a (G_VALUE_TYPE (value), G_TYPE_ULONG))
69 arg.v_uint = g_value_get_ulong (value);
71 arg.v_uint = g_value_get_uint (value);
73 case GI_TYPE_TAG_UINT64:
74 if (g_type_is_a (G_VALUE_TYPE (value), G_TYPE_ULONG))
75 arg.v_uint64 = g_value_get_ulong (value);
77 arg.v_uint64 = g_value_get_uint64 (value);
79 case GI_TYPE_TAG_UNICHAR:
80 arg.v_uint32 = g_value_get_schar (value);
82 case GI_TYPE_TAG_FLOAT:
83 arg.v_float = g_value_get_float (value);
85 case GI_TYPE_TAG_DOUBLE:
86 arg.v_double = g_value_get_double (value);
88 case GI_TYPE_TAG_GTYPE:
89 arg.v_long = g_value_get_gtype (value);
91 case GI_TYPE_TAG_UTF8:
92 case GI_TYPE_TAG_FILENAME:
93 /* Callers are responsible for ensuring the GValue stays alive
94 * long enough for the string to be copied. */
95 arg.v_string = (char *)g_value_get_string (value);
97 case GI_TYPE_TAG_GLIST:
98 case GI_TYPE_TAG_GSLIST:
99 case GI_TYPE_TAG_ARRAY:
100 case GI_TYPE_TAG_GHASH:
101 if (G_VALUE_HOLDS_BOXED (value))
102 arg.v_pointer = g_value_get_boxed (value);
104 /* e. g. GSettings::change-event */
105 arg.v_pointer = g_value_get_pointer (value);
107 case GI_TYPE_TAG_INTERFACE:
110 GIInfoType info_type;
112 info = g_type_info_get_interface (type_info);
113 info_type = g_base_info_get_type (info);
115 g_base_info_unref (info);
118 case GI_INFO_TYPE_FLAGS:
119 arg.v_uint = g_value_get_flags (value);
121 case GI_INFO_TYPE_ENUM:
122 arg.v_int = g_value_get_enum (value);
124 case GI_INFO_TYPE_INTERFACE:
125 case GI_INFO_TYPE_OBJECT:
126 if (G_VALUE_HOLDS_PARAM (value))
127 arg.v_pointer = g_value_get_param (value);
129 arg.v_pointer = g_value_get_object (value);
131 case GI_INFO_TYPE_BOXED:
132 case GI_INFO_TYPE_STRUCT:
133 case GI_INFO_TYPE_UNION:
134 if (G_VALUE_HOLDS (value, G_TYPE_BOXED)) {
135 arg.v_pointer = g_value_get_boxed (value);
136 } else if (G_VALUE_HOLDS (value, G_TYPE_VARIANT)) {
137 arg.v_pointer = g_value_get_variant (value);
138 } else if (G_VALUE_HOLDS (value, G_TYPE_POINTER)) {
139 arg.v_pointer = g_value_get_pointer (value);
141 PyErr_Format (PyExc_NotImplementedError,
142 "Converting GValue's of type '%s' is not implemented.",
143 g_type_name (G_VALUE_TYPE (value)));
147 PyErr_Format (PyExc_NotImplementedError,
148 "Converting GValue's of type '%s' is not implemented.",
149 g_info_type_to_string (info_type));
154 case GI_TYPE_TAG_ERROR:
155 arg.v_pointer = g_value_get_boxed (value);
157 case GI_TYPE_TAG_VOID:
158 arg.v_pointer = g_value_get_pointer (value);
166 /* Ignore g_value_array deprecations. Although they are deprecated,
167 * we still need to support the marshaling of them in PyGObject.
169 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
172 pyg_value_array_from_pyobject(GValue *value,
174 const GParamSpecValueArray *pspec)
177 GValueArray *value_array;
180 len = PySequence_Length(obj);
186 if (pspec && pspec->fixed_n_elements > 0 && len != pspec->fixed_n_elements)
189 value_array = g_value_array_new(len);
191 for (i = 0; i < len; ++i) {
192 PyObject *item = PySequence_GetItem(obj, i);
194 GValue item_value = { 0, };
199 g_value_array_free(value_array);
203 if (pspec && pspec->element_spec)
204 type = G_PARAM_SPEC_VALUE_TYPE(pspec->element_spec);
205 else if (item == Py_None)
206 type = G_TYPE_POINTER; /* store None as NULL */
208 type = pyg_type_from_object((PyObject*)Py_TYPE(item));
211 g_value_array_free(value_array);
217 g_value_init(&item_value, type);
218 status = (pspec && pspec->element_spec)
219 ? pyg_param_gvalue_from_pyobject(&item_value, item, pspec->element_spec)
220 : pyg_value_from_pyobject(&item_value, item);
224 g_value_array_free(value_array);
225 g_value_unset(&item_value);
229 g_value_array_append(value_array, &item_value);
230 g_value_unset(&item_value);
233 g_value_take_boxed(value, value_array);
237 G_GNUC_END_IGNORE_DEPRECATIONS
240 pyg_array_from_pyobject(GValue *value,
247 len = PySequence_Length(obj);
253 array = g_array_new(FALSE, TRUE, sizeof(GValue));
255 for (i = 0; i < len; ++i) {
256 PyObject *item = PySequence_GetItem(obj, i);
258 GValue item_value = { 0, };
263 g_array_free(array, FALSE);
268 type = G_TYPE_POINTER; /* store None as NULL */
270 type = pyg_type_from_object((PyObject*)Py_TYPE(item));
273 g_array_free(array, FALSE);
279 g_value_init(&item_value, type);
280 status = pyg_value_from_pyobject(&item_value, item);
284 g_array_free(array, FALSE);
285 g_value_unset(&item_value);
289 g_array_append_val(array, item_value);
292 g_value_take_boxed(value, array);
297 * pyg_value_from_pyobject_with_error:
298 * @value: the GValue object to store the converted value in.
299 * @obj: the Python object to convert.
301 * This function converts a Python object and stores the result in a
302 * GValue. The GValue must be initialised in advance with
303 * g_value_init(). If the Python object can't be converted to the
304 * type of the GValue, then an error is returned.
306 * Returns: 0 on success, -1 on error.
309 pyg_value_from_pyobject_with_error(GValue *value, PyObject *obj)
312 GType value_type = G_VALUE_TYPE(value);
314 switch (G_TYPE_FUNDAMENTAL(value_type)) {
315 case G_TYPE_INTERFACE:
316 /* we only handle interface types that have a GObject prereq */
317 if (g_type_is_a(value_type, G_TYPE_OBJECT)) {
319 g_value_set_object(value, NULL);
321 if (!PyObject_TypeCheck(obj, &PyGObject_Type)) {
322 PyErr_SetString(PyExc_TypeError, "GObject is required");
325 if (!G_TYPE_CHECK_INSTANCE_TYPE(pygobject_get(obj),
327 PyErr_SetString(PyExc_TypeError, "Invalid GObject type for assignment");
330 g_value_set_object(value, pygobject_get(obj));
333 PyErr_SetString(PyExc_TypeError, "Unsupported conversion");
338 if (PYGLIB_PyLong_Check(obj)) {
340 val = PYGLIB_PyLong_AsLong(obj);
341 if (val >= -128 && val <= 127)
342 g_value_set_schar(value, (gchar) val);
346 #if PY_VERSION_HEX < 0x03000000
347 else if (PyString_Check(obj)) {
348 g_value_set_schar(value, PyString_AsString(obj)[0]);
351 else if (PyUnicode_Check(obj)) {
352 tmp = PyUnicode_AsUTF8String(obj);
353 g_value_set_schar(value, PYGLIB_PyBytes_AsString(tmp)[0]);
356 PyErr_SetString(PyExc_TypeError, "Cannot convert to TYPE_CHAR");
362 if (PYGLIB_PyLong_Check(obj)) {
364 val = PYGLIB_PyLong_AsLong(obj);
365 if (val >= 0 && val <= 255)
366 g_value_set_uchar(value, (guchar) val);
369 #if PY_VERSION_HEX < 0x03000000
370 } else if (PyString_Check(obj)) {
371 g_value_set_uchar(value, PyString_AsString(obj)[0]);
373 } else if (PyUnicode_Check(obj)) {
374 tmp = PyUnicode_AsUTF8String(obj);
375 g_value_set_uchar(value, PYGLIB_PyBytes_AsString(tmp)[0]);
383 g_value_set_boolean(value, PyObject_IsTrue(obj));
386 g_value_set_int(value, PYGLIB_PyLong_AsLong(obj));
390 if (PYGLIB_PyLong_Check(obj)) {
393 /* check that number is not negative */
394 if (PyLong_AsLongLong(obj) < 0)
397 val = PyLong_AsUnsignedLong(obj);
398 if (val <= G_MAXUINT)
399 g_value_set_uint(value, (guint) val);
403 g_value_set_uint(value, PyLong_AsUnsignedLong(obj));
408 g_value_set_long(value, PYGLIB_PyLong_AsLong(obj));
411 #if PY_VERSION_HEX < 0x03000000
412 if (PyInt_Check(obj)) {
415 val = PYGLIB_PyLong_AsLong(obj);
417 PyErr_SetString(PyExc_OverflowError, "negative value not allowed for uint64 property");
420 g_value_set_ulong(value, (gulong)val);
423 if (PyLong_Check(obj))
424 g_value_set_ulong(value, PyLong_AsUnsignedLong(obj));
429 g_value_set_int64(value, PyLong_AsLongLong(obj));
432 #if PY_VERSION_HEX < 0x03000000
433 if (PyInt_Check(obj)) {
434 long v = PyInt_AsLong(obj);
436 PyErr_SetString(PyExc_OverflowError, "negative value not allowed for uint64 property");
439 g_value_set_uint64(value, v);
442 if (PyLong_Check(obj))
443 g_value_set_uint64(value, PyLong_AsUnsignedLongLong(obj));
450 if (pyg_enum_get_value(G_VALUE_TYPE(value), obj, &val) < 0) {
453 g_value_set_enum(value, val);
459 if (pyg_flags_get_value(G_VALUE_TYPE(value), obj, &val) < 0) {
462 g_value_set_flags(value, val);
466 g_value_set_float(value, PyFloat_AsDouble(obj));
469 g_value_set_double(value, PyFloat_AsDouble(obj));
472 if (obj == Py_None) {
473 g_value_set_string(value, NULL);
475 PyObject* tmp_str = PyObject_Str(obj);
476 if (tmp_str == NULL) {
478 if (PyUnicode_Check(obj)) {
479 tmp = PyUnicode_AsUTF8String(obj);
480 g_value_set_string(value, PYGLIB_PyBytes_AsString(tmp));
483 PyErr_SetString(PyExc_TypeError, "Expected string");
487 #if PY_VERSION_HEX < 0x03000000
488 g_value_set_string(value, PyString_AsString(tmp_str));
490 tmp = PyUnicode_AsUTF8String(tmp_str);
491 g_value_set_string(value, PyBytes_AsString(tmp));
500 g_value_set_pointer(value, NULL);
501 else if (PyObject_TypeCheck(obj, &PyGPointer_Type) &&
502 G_VALUE_HOLDS(value, ((PyGPointer *)obj)->gtype))
503 g_value_set_pointer(value, pyg_pointer_get(obj, gpointer));
504 else if (PYGLIB_CPointer_Check(obj))
505 g_value_set_pointer(value, PYGLIB_CPointer_GetPointer(obj, NULL));
506 else if (G_VALUE_HOLDS_GTYPE (value))
507 g_value_set_gtype (value, pyg_type_from_object (obj));
509 PyErr_SetString(PyExc_TypeError, "Expected pointer");
515 gboolean holds_value_array;
517 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
518 holds_value_array = G_VALUE_HOLDS(value, G_TYPE_VALUE_ARRAY);
519 G_GNUC_END_IGNORE_DEPRECATIONS
522 g_value_set_boxed(value, NULL);
523 else if (G_VALUE_HOLDS(value, PY_TYPE_OBJECT))
524 g_value_set_boxed(value, obj);
525 else if (PyObject_TypeCheck(obj, &PyGBoxed_Type) &&
526 G_VALUE_HOLDS(value, ((PyGBoxed *)obj)->gtype))
527 g_value_set_boxed(value, pyg_boxed_get(obj, gpointer));
528 else if (G_VALUE_HOLDS(value, G_TYPE_VALUE)) {
532 type = pyg_type_from_object((PyObject*)Py_TYPE(obj));
533 if (G_UNLIKELY (! type)) {
536 n_value = g_new0 (GValue, 1);
537 g_value_init (n_value, type);
538 g_value_take_boxed (value, n_value);
539 return pyg_value_from_pyobject_with_error (n_value, obj);
541 else if (PySequence_Check(obj) && holds_value_array)
542 return pyg_value_array_from_pyobject(value, obj, NULL);
544 else if (PySequence_Check(obj) &&
545 G_VALUE_HOLDS(value, G_TYPE_ARRAY))
546 return pyg_array_from_pyobject(value, obj);
547 else if (PYGLIB_PyUnicode_Check(obj) &&
548 G_VALUE_HOLDS(value, G_TYPE_GSTRING)) {
552 if (PYGLIB_PyUnicode_AsStringAndSize(obj, &buffer, &len))
554 string = g_string_new_len(buffer, len);
555 g_value_set_boxed(value, string);
556 g_string_free (string, TRUE);
559 else if ((bm = pyg_type_lookup(G_VALUE_TYPE(value))) != NULL)
560 return bm->tovalue(value, obj);
561 else if (PYGLIB_CPointer_Check(obj))
562 g_value_set_boxed(value, PYGLIB_CPointer_GetPointer(obj, NULL));
564 PyErr_SetString(PyExc_TypeError, "Expected Boxed");
570 /* we need to support both the wrapped _gobject.GParamSpec and the GI
571 * GObject.ParamSpec */
572 if (G_IS_PARAM_SPEC (pygobject_get (obj)))
573 g_value_set_param(value, G_PARAM_SPEC (pygobject_get (obj)));
574 else if (pyg_param_spec_check (obj))
575 g_value_set_param(value, PYGLIB_CPointer_GetPointer(obj, NULL));
577 PyErr_SetString(PyExc_TypeError, "Expected ParamSpec");
582 if (obj == Py_None) {
583 g_value_set_object(value, NULL);
584 } else if (PyObject_TypeCheck(obj, &PyGObject_Type) &&
585 G_TYPE_CHECK_INSTANCE_TYPE(pygobject_get(obj),
586 G_VALUE_TYPE(value))) {
587 g_value_set_object(value, pygobject_get(obj));
589 PyErr_SetString(PyExc_TypeError, "Expected GObject");
596 g_value_set_variant(value, NULL);
597 else if (pyg_type_from_object_strict(obj, FALSE) == G_TYPE_VARIANT)
598 g_value_set_variant(value, pyg_boxed_get(obj, GVariant));
600 PyErr_SetString(PyExc_TypeError, "Expected Variant");
608 if ((bm = pyg_type_lookup(G_VALUE_TYPE(value))) != NULL) {
609 return bm->tovalue(value, obj);
611 PyErr_SetString(PyExc_TypeError, "Unknown value type");
618 /* If an error occurred, unset the GValue but don't clear the Python error. */
619 if (PyErr_Occurred()) {
620 g_value_unset(value);
628 * pyg_value_from_pyobject:
629 * @value: the GValue object to store the converted value in.
630 * @obj: the Python object to convert.
632 * Same basic function as pyg_value_from_pyobject_with_error but clears
633 * any Python errors before returning.
635 * Returns: 0 on success, -1 on error.
638 pyg_value_from_pyobject(GValue *value, PyObject *obj)
640 int res = pyg_value_from_pyobject_with_error (value, obj);
642 if (PyErr_Occurred()) {
650 * pygi_value_to_py_basic_type:
651 * @value: the GValue object.
653 * This function creates/returns a Python wrapper object that
654 * represents the GValue passed as an argument limited to supporting basic types
655 * like ints, bools, and strings.
657 * Returns: a PyObject representing the value.
660 pygi_value_to_py_basic_type (const GValue *value, GType fundamental)
662 switch (fundamental) {
664 return PYGLIB_PyLong_FromLong (g_value_get_schar (value));
667 return PYGLIB_PyLong_FromLong (g_value_get_uchar (value));
669 case G_TYPE_BOOLEAN: {
670 return PyBool_FromLong(g_value_get_boolean(value));
673 return PYGLIB_PyLong_FromLong(g_value_get_int(value));
676 /* in Python, the Int object is backed by a long. If a
677 long can hold the whole value of an unsigned int, use
678 an Int. Otherwise, use a Long object to avoid overflow.
679 This matches the ULongArg behavior in codegen/argtypes.h */
680 #if (G_MAXUINT <= G_MAXLONG)
681 return PYGLIB_PyLong_FromLong((glong) g_value_get_uint(value));
683 return PyLong_FromUnsignedLong((gulong) g_value_get_uint(value));
687 return PYGLIB_PyLong_FromLong(g_value_get_long(value));
690 gulong val = g_value_get_ulong(value);
692 if (val <= G_MAXLONG)
693 return PYGLIB_PyLong_FromLong((glong) val);
695 return PyLong_FromUnsignedLong(val);
699 gint64 val = g_value_get_int64(value);
701 if (G_MINLONG <= val && val <= G_MAXLONG)
702 return PYGLIB_PyLong_FromLong((glong) val);
704 return PyLong_FromLongLong(val);
708 guint64 val = g_value_get_uint64(value);
710 if (val <= G_MAXLONG)
711 return PYGLIB_PyLong_FromLong((glong) val);
713 return PyLong_FromUnsignedLongLong(val);
716 return pyg_enum_from_gtype(G_VALUE_TYPE(value), g_value_get_enum(value));
718 return pyg_flags_from_gtype(G_VALUE_TYPE(value), g_value_get_flags(value));
720 return PyFloat_FromDouble(g_value_get_float(value));
722 return PyFloat_FromDouble(g_value_get_double(value));
725 const gchar *str = g_value_get_string(value);
728 return PYGLIB_PyUnicode_FromString(str);
738 * pygi_value_to_py_structured_type:
739 * @value: the GValue object.
740 * @copy_boxed: true if boxed values should be copied.
742 * This function creates/returns a Python wrapper object that
743 * represents the GValue passed as an argument.
745 * Returns: a PyObject representing the value.
748 pygi_value_to_py_structured_type (const GValue *value, GType fundamental, gboolean copy_boxed)
750 switch (fundamental) {
751 case G_TYPE_INTERFACE:
752 if (g_type_is_a(G_VALUE_TYPE(value), G_TYPE_OBJECT))
753 return pygobject_new(g_value_get_object(value));
758 if (G_VALUE_HOLDS_GTYPE (value))
759 return pyg_type_wrapper_new (g_value_get_gtype (value));
761 return pyg_pointer_new(G_VALUE_TYPE(value),
762 g_value_get_pointer(value));
765 gboolean holds_value_array;
767 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
768 holds_value_array = G_VALUE_HOLDS(value, G_TYPE_VALUE_ARRAY);
769 G_GNUC_END_IGNORE_DEPRECATIONS
771 if (G_VALUE_HOLDS(value, PY_TYPE_OBJECT)) {
772 PyObject *ret = (PyObject *)g_value_dup_boxed(value);
778 } else if (G_VALUE_HOLDS(value, G_TYPE_VALUE)) {
779 GValue *n_value = g_value_get_boxed (value);
780 return pyg_value_as_pyobject(n_value, copy_boxed);
781 } else if (holds_value_array) {
782 GValueArray *array = (GValueArray *) g_value_get_boxed(value);
783 Py_ssize_t n_values = array ? array->n_values : 0;
784 PyObject *ret = PyList_New(n_values);
786 for (i = 0; i < n_values; ++i)
787 PyList_SET_ITEM(ret, i, pyg_value_as_pyobject
788 (array->values + i, copy_boxed));
790 } else if (G_VALUE_HOLDS(value, G_TYPE_GSTRING)) {
791 GString *string = (GString *) g_value_get_boxed(value);
792 PyObject *ret = PYGLIB_PyUnicode_FromStringAndSize(string->str, string->len);
795 bm = pyg_type_lookup(G_VALUE_TYPE(value));
797 return bm->fromvalue(value);
800 return pyg_boxed_new(G_VALUE_TYPE(value),
801 g_value_get_boxed(value), TRUE, TRUE);
803 return pyg_boxed_new(G_VALUE_TYPE(value),
804 g_value_get_boxed(value),FALSE,FALSE);
808 return pyg_param_spec_new(g_value_get_param(value));
810 return pygobject_new(g_value_get_object(value));
813 GVariant *v = g_value_get_variant(value);
818 return _pygi_struct_new_from_g_type (G_TYPE_VARIANT, g_variant_ref(v), FALSE);
823 if ((bm = pyg_type_lookup(G_VALUE_TYPE(value))))
824 return bm->fromvalue(value);
834 * pyg_value_as_pyobject:
835 * @value: the GValue object.
836 * @copy_boxed: true if boxed values should be copied.
838 * This function creates/returns a Python wrapper object that
839 * represents the GValue passed as an argument.
841 * Returns: a PyObject representing the value or %NULL and sets an exception.
844 pyg_value_as_pyobject (const GValue *value, gboolean copy_boxed)
847 const gchar *type_name;
848 GType fundamental = G_TYPE_FUNDAMENTAL (G_VALUE_TYPE (value));
850 /* HACK: special case char and uchar to return PyBytes intstead of integers
851 * in the general case. Property access will skip this by calling
852 * pygi_value_to_py_basic_type() directly.
853 * See: https://bugzilla.gnome.org/show_bug.cgi?id=733893 */
854 if (fundamental == G_TYPE_CHAR) {
855 gint8 val = g_value_get_schar(value);
856 return PYGLIB_PyUnicode_FromStringAndSize ((char *)&val, 1);
857 } else if (fundamental == G_TYPE_UCHAR) {
858 guint8 val = g_value_get_uchar(value);
859 return PYGLIB_PyBytes_FromStringAndSize ((char *)&val, 1);
862 pyobj = pygi_value_to_py_basic_type (value, fundamental);
867 pyobj = pygi_value_to_py_structured_type (value, fundamental, copy_boxed);
872 if (!PyErr_Occurred ()) {
873 type_name = g_type_name (G_VALUE_TYPE (value));
874 if (type_name == NULL) {
875 type_name = "(null)";
877 PyErr_Format (PyExc_TypeError, "unknown type %s", type_name);
886 pyg_param_gvalue_from_pyobject(GValue* value,
888 const GParamSpec* pspec)
890 if (G_IS_PARAM_SPEC_UNICHAR(pspec)) {
893 if (!pyg_pyobj_to_unichar_conv(py_obj, &u)) {
897 g_value_set_uint(value, u);
900 else if (G_IS_PARAM_SPEC_VALUE_ARRAY(pspec))
901 return pyg_value_array_from_pyobject(value, py_obj,
902 G_PARAM_SPEC_VALUE_ARRAY(pspec));
904 return pyg_value_from_pyobject(value, py_obj);
909 pyg_param_gvalue_as_pyobject(const GValue* gvalue,
911 const GParamSpec* pspec)
913 if (G_IS_PARAM_SPEC_UNICHAR(pspec)) {
915 Py_UNICODE uni_buffer[2] = { 0, 0 };
917 u = g_value_get_uint(gvalue);
919 return PyUnicode_FromUnicode(uni_buffer, 1);
922 return pyg_value_as_pyobject(gvalue, copy_boxed);
927 pyg_strv_from_gvalue(const GValue *value)
929 gchar **argv = (gchar **) g_value_get_boxed(value);
937 py_argv = PyList_New(argc);
938 for (i = 0; i < argc; ++i)
939 PyList_SET_ITEM(py_argv, i, PYGLIB_PyUnicode_FromString(argv[i]));
944 pyg_strv_to_gvalue(GValue *value, PyObject *obj)
949 if (!(PyTuple_Check (obj) || PyList_Check (obj)))
952 argc = PySequence_Length (obj);
953 argv = g_new (gchar *, argc + 1);
954 for (i = 0; i < argc; ++i) {
955 PyObject* item = PySequence_Fast_GET_ITEM (obj, i);
956 /* same as _pygi_marshal_from_py_utf8 */
957 if (PyUnicode_Check (item)) {
958 PyObject *pystr_obj = PyUnicode_AsUTF8String (item);
962 argv[i] = g_strdup (PYGLIB_PyBytes_AsString (pystr_obj));
963 Py_DECREF (pystr_obj);
965 #if PY_VERSION_HEX < 0x03000000
966 else if (PyString_Check (item)) {
967 argv[i] = g_strdup (PyString_AsString (item));
976 g_value_take_boxed (value, argv);
980 for (i = i - 1; i >= 0; i--) {