1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
4 * Copyright (C) 2005-2009 Johan Dahlin <johan@gnome.org>
6 * pygi-argument.c: GIArgument - PyObject conversion functions.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 #include "pygi-private.h"
23 #include "pygobject-private.h"
28 #include <pyglib-python-compat.h>
31 #include "pygi-value.h"
32 #include "pygi-basictype.h"
33 #include "pygi-object.h"
34 #include "pygi-struct-marshal.h"
35 #include "pygi-error.h"
38 pygi_argument_to_gssize (GIArgument
*arg_in
,
43 case GI_TYPE_TAG_INT8
:
44 *gssize_out
= arg_in
->v_int8
;
46 case GI_TYPE_TAG_UINT8
:
47 *gssize_out
= arg_in
->v_uint8
;
49 case GI_TYPE_TAG_INT16
:
50 *gssize_out
= arg_in
->v_int16
;
52 case GI_TYPE_TAG_UINT16
:
53 *gssize_out
= arg_in
->v_uint16
;
55 case GI_TYPE_TAG_INT32
:
56 *gssize_out
= arg_in
->v_int32
;
58 case GI_TYPE_TAG_UINT32
:
59 *gssize_out
= arg_in
->v_uint32
;
61 case GI_TYPE_TAG_INT64
:
62 *gssize_out
= arg_in
->v_int64
;
64 case GI_TYPE_TAG_UINT64
:
65 *gssize_out
= arg_in
->v_uint64
;
68 PyErr_Format (PyExc_TypeError
,
69 "Unable to marshal %s to gssize",
70 g_type_tag_to_string(type_tag
));
76 _pygi_hash_pointer_to_arg (GIArgument
*arg
,
80 case GI_TYPE_TAG_INT8
:
81 arg
->v_int8
= GPOINTER_TO_INT (arg
->v_pointer
);
83 case GI_TYPE_TAG_INT16
:
84 arg
->v_int16
= GPOINTER_TO_INT (arg
->v_pointer
);
86 case GI_TYPE_TAG_INT32
:
87 arg
->v_int32
= GPOINTER_TO_INT (arg
->v_pointer
);
89 case GI_TYPE_TAG_UINT8
:
90 arg
->v_uint8
= GPOINTER_TO_UINT (arg
->v_pointer
);
92 case GI_TYPE_TAG_UINT16
:
93 arg
->v_uint16
= GPOINTER_TO_UINT (arg
->v_pointer
);
95 case GI_TYPE_TAG_UINT32
:
96 arg
->v_uint32
= GPOINTER_TO_UINT (arg
->v_pointer
);
98 case GI_TYPE_TAG_UTF8
:
99 case GI_TYPE_TAG_FILENAME
:
100 case GI_TYPE_TAG_INTERFACE
:
103 g_critical ("Unsupported type %s", g_type_tag_to_string(type_tag
));
108 _pygi_arg_to_hash_pointer (const GIArgument
*arg
,
112 case GI_TYPE_TAG_INT8
:
113 return GINT_TO_POINTER (arg
->v_int8
);
114 case GI_TYPE_TAG_UINT8
:
115 return GINT_TO_POINTER (arg
->v_uint8
);
116 case GI_TYPE_TAG_INT16
:
117 return GINT_TO_POINTER (arg
->v_int16
);
118 case GI_TYPE_TAG_UINT16
:
119 return GINT_TO_POINTER (arg
->v_uint16
);
120 case GI_TYPE_TAG_INT32
:
121 return GINT_TO_POINTER (arg
->v_int32
);
122 case GI_TYPE_TAG_UINT32
:
123 return GINT_TO_POINTER (arg
->v_uint32
);
124 case GI_TYPE_TAG_UTF8
:
125 case GI_TYPE_TAG_FILENAME
:
126 case GI_TYPE_TAG_INTERFACE
:
127 return arg
->v_pointer
;
129 g_critical ("Unsupported type %s", g_type_tag_to_string(type_tag
));
130 return arg
->v_pointer
;
135 _pygi_g_type_tag_py_bounds (GITypeTag type_tag
,
140 case GI_TYPE_TAG_INT8
:
141 *lower
= PYGLIB_PyLong_FromLong (-128);
142 *upper
= PYGLIB_PyLong_FromLong (127);
144 case GI_TYPE_TAG_UINT8
:
145 *upper
= PYGLIB_PyLong_FromLong (255);
146 *lower
= PYGLIB_PyLong_FromLong (0);
148 case GI_TYPE_TAG_INT16
:
149 *lower
= PYGLIB_PyLong_FromLong (-32768);
150 *upper
= PYGLIB_PyLong_FromLong (32767);
152 case GI_TYPE_TAG_UINT16
:
153 *upper
= PYGLIB_PyLong_FromLong (65535);
154 *lower
= PYGLIB_PyLong_FromLong (0);
156 case GI_TYPE_TAG_INT32
:
157 *lower
= PYGLIB_PyLong_FromLong (G_MININT32
);
158 *upper
= PYGLIB_PyLong_FromLong (G_MAXINT32
);
160 case GI_TYPE_TAG_UINT32
:
161 /* Note: On 32-bit archs, this number doesn't fit in a long. */
162 *upper
= PyLong_FromLongLong (G_MAXUINT32
);
163 *lower
= PYGLIB_PyLong_FromLong (0);
165 case GI_TYPE_TAG_INT64
:
166 /* Note: On 32-bit archs, these numbers don't fit in a long. */
167 *lower
= PyLong_FromLongLong (G_MININT64
);
168 *upper
= PyLong_FromLongLong (G_MAXINT64
);
170 case GI_TYPE_TAG_UINT64
:
171 *upper
= PyLong_FromUnsignedLongLong (G_MAXUINT64
);
172 *lower
= PYGLIB_PyLong_FromLong (0);
174 case GI_TYPE_TAG_FLOAT
:
175 *upper
= PyFloat_FromDouble (G_MAXFLOAT
);
176 *lower
= PyFloat_FromDouble (-G_MAXFLOAT
);
178 case GI_TYPE_TAG_DOUBLE
:
179 *upper
= PyFloat_FromDouble (G_MAXDOUBLE
);
180 *lower
= PyFloat_FromDouble (-G_MAXDOUBLE
);
183 PyErr_SetString (PyExc_TypeError
, "Non-numeric type tag");
184 *lower
= *upper
= NULL
;
190 _pygi_g_registered_type_info_check_object (GIRegisteredTypeInfo
*info
,
191 gboolean is_instance
,
198 gchar
*type_name_expected
= NULL
;
199 GIInfoType interface_type
;
201 interface_type
= g_base_info_get_type (info
);
202 if ( (interface_type
== GI_INFO_TYPE_STRUCT
) &&
203 (g_struct_info_is_foreign ( (GIStructInfo
*) info
))) {
204 /* TODO: Could we check is the correct foreign type? */
208 g_type
= g_registered_type_info_get_g_type (info
);
209 if (g_type
!= G_TYPE_NONE
) {
210 py_type
= _pygi_type_get_from_g_type (g_type
);
212 py_type
= _pygi_type_import_by_gi_info ( (GIBaseInfo
*) info
);
215 if (py_type
== NULL
) {
219 g_assert (PyType_Check (py_type
));
222 retval
= PyObject_IsInstance (object
, py_type
);
224 type_name_expected
= _pygi_g_base_info_get_fullname (
225 (GIBaseInfo
*) info
);
228 if (!PyObject_Type (py_type
)) {
229 type_name_expected
= "type";
231 } else if (!PyType_IsSubtype ( (PyTypeObject
*) object
,
232 (PyTypeObject
*) py_type
)) {
233 type_name_expected
= _pygi_g_base_info_get_fullname (
234 (GIBaseInfo
*) info
);
244 PyTypeObject
*object_type
;
246 if (type_name_expected
== NULL
) {
250 object_type
= (PyTypeObject
*) PyObject_Type (object
);
251 if (object_type
== NULL
) {
255 PyErr_Format (PyExc_TypeError
, "Must be %s, not %s",
256 type_name_expected
, object_type
->tp_name
);
258 g_free (type_name_expected
);
265 _pygi_g_type_interface_check_object (GIBaseInfo
*info
,
269 GIInfoType info_type
;
271 info_type
= g_base_info_get_type (info
);
273 case GI_INFO_TYPE_CALLBACK
:
274 if (!PyCallable_Check (object
)) {
275 PyErr_Format (PyExc_TypeError
, "Must be callable, not %s",
276 object
->ob_type
->tp_name
);
280 case GI_INFO_TYPE_ENUM
:
282 if (PyNumber_Check (object
)) {
283 PyObject
*number
= PYGLIB_PyNumber_Long (object
);
287 glong value
= PYGLIB_PyLong_AsLong (number
);
289 for (i
= 0; i
< g_enum_info_get_n_values (info
); i
++) {
290 GIValueInfo
*value_info
= g_enum_info_get_value (info
, i
);
291 glong enum_value
= g_value_info_get_value (value_info
);
292 g_base_info_unref (value_info
);
293 if (value
== enum_value
) {
301 retval
= _pygi_g_registered_type_info_check_object (
302 (GIRegisteredTypeInfo
*) info
, TRUE
, object
);
304 case GI_INFO_TYPE_FLAGS
:
305 if (PyNumber_Check (object
)) {
306 /* Accept 0 as a valid flag value */
307 PyObject
*number
= PYGLIB_PyNumber_Long (object
);
311 long value
= PYGLIB_PyLong_AsLong (number
);
314 else if (value
== -1)
318 retval
= _pygi_g_registered_type_info_check_object (
319 (GIRegisteredTypeInfo
*) info
, TRUE
, object
);
321 case GI_INFO_TYPE_STRUCT
:
325 /* Handle special cases. */
326 type
= g_registered_type_info_get_g_type ( (GIRegisteredTypeInfo
*) info
);
327 if (g_type_is_a (type
, G_TYPE_CLOSURE
)) {
328 if (!(PyCallable_Check (object
) ||
329 pyg_type_from_object_strict (object
, FALSE
) == G_TYPE_CLOSURE
)) {
330 PyErr_Format (PyExc_TypeError
, "Must be callable, not %s",
331 object
->ob_type
->tp_name
);
335 } else if (g_type_is_a (type
, G_TYPE_VALUE
)) {
336 /* we can't check g_values because we don't have
337 * enough context so just pass them through */
343 case GI_INFO_TYPE_BOXED
:
344 case GI_INFO_TYPE_INTERFACE
:
345 case GI_INFO_TYPE_OBJECT
:
346 retval
= _pygi_g_registered_type_info_check_object ( (GIRegisteredTypeInfo
*) info
, TRUE
, object
);
348 case GI_INFO_TYPE_UNION
:
351 retval
= _pygi_g_registered_type_info_check_object ( (GIRegisteredTypeInfo
*) info
, TRUE
, object
);
353 /* If not the same type then check to see if the object's type
354 * is the same as one of the union's members
360 n_fields
= g_union_info_get_n_fields ( (GIUnionInfo
*) info
);
362 for (i
= 0; i
< n_fields
; i
++) {
364 GIFieldInfo
*field_info
;
365 GITypeInfo
*field_type_info
;
368 g_union_info_get_field ( (GIUnionInfo
*) info
, i
);
369 field_type_info
= g_field_info_get_type (field_info
);
371 member_retval
= _pygi_g_type_info_check_object(
376 g_base_info_unref ( ( GIBaseInfo
*) field_type_info
);
377 g_base_info_unref ( ( GIBaseInfo
*) field_info
);
379 if (member_retval
== 1) {
380 retval
= member_retval
;
388 g_assert_not_reached();
395 _pygi_g_type_info_check_object (GITypeInfo
*type_info
,
402 if (allow_none
&& object
== Py_None
) {
406 type_tag
= g_type_info_get_tag (type_info
);
409 case GI_TYPE_TAG_VOID
:
410 /* No check; VOID means undefined type */
412 case GI_TYPE_TAG_BOOLEAN
:
413 /* No check; every Python object has a truth value. */
415 case GI_TYPE_TAG_UINT8
:
416 case GI_TYPE_TAG_INT8
:
417 /* (U)INT8 types can be characters */
418 if (PYGLIB_PyBytes_Check(object
)) {
419 if (PYGLIB_PyBytes_Size(object
) != 1) {
420 PyErr_Format (PyExc_TypeError
, "Must be a single character");
427 case GI_TYPE_TAG_INT16
:
428 case GI_TYPE_TAG_UINT16
:
429 case GI_TYPE_TAG_INT32
:
430 case GI_TYPE_TAG_UINT32
:
431 case GI_TYPE_TAG_INT64
:
432 case GI_TYPE_TAG_UINT64
:
433 case GI_TYPE_TAG_FLOAT
:
434 case GI_TYPE_TAG_DOUBLE
:
436 PyObject
*number
, *lower
, *upper
;
438 if (!PyNumber_Check (object
)) {
439 PyErr_Format (PyExc_TypeError
, "Must be number, not %s",
440 object
->ob_type
->tp_name
);
445 if (type_tag
== GI_TYPE_TAG_FLOAT
|| type_tag
== GI_TYPE_TAG_DOUBLE
) {
446 number
= PyNumber_Float (object
);
448 number
= PYGLIB_PyNumber_Long (object
);
451 _pygi_g_type_tag_py_bounds (type_tag
, &lower
, &upper
);
453 if (lower
== NULL
|| upper
== NULL
|| number
== NULL
) {
455 goto check_number_release
;
459 if (PyObject_RichCompareBool (lower
, number
, Py_GT
)
460 || PyObject_RichCompareBool (upper
, number
, Py_LT
)) {
464 if (PyErr_Occurred()) {
466 goto check_number_release
;
469 lower_str
= PyObject_Str (lower
);
470 upper_str
= PyObject_Str (upper
);
471 if (lower_str
== NULL
|| upper_str
== NULL
) {
473 goto check_number_error_release
;
476 #if PY_VERSION_HEX < 0x03000000
477 PyErr_Format (PyExc_ValueError
, "Must range from %s to %s",
478 PyString_AS_STRING (lower_str
),
479 PyString_AS_STRING (upper_str
));
482 PyObject
*lower_pybytes_obj
;
483 PyObject
*upper_pybytes_obj
;
485 lower_pybytes_obj
= PyUnicode_AsUTF8String (lower_str
);
486 if (!lower_pybytes_obj
) {
490 upper_pybytes_obj
= PyUnicode_AsUTF8String (upper_str
);
491 if (!upper_pybytes_obj
) {
492 Py_DECREF(lower_pybytes_obj
);
496 PyErr_Format (PyExc_ValueError
, "Must range from %s to %s",
497 PyBytes_AsString (lower_pybytes_obj
),
498 PyBytes_AsString (upper_pybytes_obj
));
499 Py_DECREF (lower_pybytes_obj
);
500 Py_DECREF (upper_pybytes_obj
);
506 check_number_error_release
:
507 Py_XDECREF (lower_str
);
508 Py_XDECREF (upper_str
);
511 check_number_release
:
517 case GI_TYPE_TAG_GTYPE
:
519 if (pyg_type_from_object (object
) == 0) {
520 PyErr_Format (PyExc_TypeError
, "Must be gobject.GType, not %s",
521 object
->ob_type
->tp_name
);
526 case GI_TYPE_TAG_UNICHAR
:
529 if (PyUnicode_Check (object
)) {
530 size
= PyUnicode_GET_SIZE (object
);
531 #if PY_VERSION_HEX < 0x03000000
532 } else if (PyString_Check (object
)) {
533 PyObject
*pyuni
= PyUnicode_FromEncodedObject (object
, "UTF-8", "strict");
534 size
= PyUnicode_GET_SIZE (pyuni
);
538 PyErr_Format (PyExc_TypeError
, "Must be string, not %s",
539 object
->ob_type
->tp_name
);
545 PyErr_Format (PyExc_TypeError
, "Must be a one character string, not %" G_GINT64_FORMAT
" characters",
553 case GI_TYPE_TAG_UTF8
:
554 case GI_TYPE_TAG_FILENAME
:
555 if (!PYGLIB_PyBaseString_Check (object
) ) {
556 PyErr_Format (PyExc_TypeError
, "Must be string, not %s",
557 object
->ob_type
->tp_name
);
561 case GI_TYPE_TAG_ARRAY
:
565 GITypeInfo
*item_type_info
;
568 if (!PySequence_Check (object
)) {
569 PyErr_Format (PyExc_TypeError
, "Must be sequence, not %s",
570 object
->ob_type
->tp_name
);
575 length
= PySequence_Length (object
);
581 fixed_size
= g_type_info_get_array_fixed_size (type_info
);
582 if (fixed_size
>= 0 && length
!= fixed_size
) {
583 PyErr_Format (PyExc_ValueError
, "Must contain %zd items, not %zd",
589 item_type_info
= g_type_info_get_param_type (type_info
, 0);
590 g_assert (item_type_info
!= NULL
);
592 /* FIXME: This is insain. We really should only check the first
593 * object and perhaps have a debugging mode. Large arrays
594 * will cause apps to slow to a crawl.
596 for (i
= 0; i
< length
; i
++) {
599 item
= PySequence_GetItem (object
, i
);
605 retval
= _pygi_g_type_info_check_object (item_type_info
, item
, TRUE
);
613 _PyGI_ERROR_PREFIX ("Item %zd: ", i
);
618 g_base_info_unref ( (GIBaseInfo
*) item_type_info
);
622 case GI_TYPE_TAG_INTERFACE
:
626 info
= g_type_info_get_interface (type_info
);
627 g_assert (info
!= NULL
);
629 retval
= _pygi_g_type_interface_check_object(info
, object
);
631 g_base_info_unref (info
);
634 case GI_TYPE_TAG_GLIST
:
635 case GI_TYPE_TAG_GSLIST
:
638 GITypeInfo
*item_type_info
;
641 if (!PySequence_Check (object
)) {
642 PyErr_Format (PyExc_TypeError
, "Must be sequence, not %s",
643 object
->ob_type
->tp_name
);
648 length
= PySequence_Length (object
);
654 item_type_info
= g_type_info_get_param_type (type_info
, 0);
655 g_assert (item_type_info
!= NULL
);
657 for (i
= 0; i
< length
; i
++) {
660 item
= PySequence_GetItem (object
, i
);
666 retval
= _pygi_g_type_info_check_object (item_type_info
, item
, TRUE
);
674 _PyGI_ERROR_PREFIX ("Item %zd: ", i
);
679 g_base_info_unref ( (GIBaseInfo
*) item_type_info
);
682 case GI_TYPE_TAG_GHASH
:
687 GITypeInfo
*key_type_info
;
688 GITypeInfo
*value_type_info
;
691 keys
= PyMapping_Keys (object
);
693 PyErr_Format (PyExc_TypeError
, "Must be mapping, not %s",
694 object
->ob_type
->tp_name
);
699 length
= PyMapping_Length (object
);
706 values
= PyMapping_Values (object
);
707 if (values
== NULL
) {
713 key_type_info
= g_type_info_get_param_type (type_info
, 0);
714 g_assert (key_type_info
!= NULL
);
716 value_type_info
= g_type_info_get_param_type (type_info
, 1);
717 g_assert (value_type_info
!= NULL
);
719 for (i
= 0; i
< length
; i
++) {
723 key
= PyList_GET_ITEM (keys
, i
);
724 value
= PyList_GET_ITEM (values
, i
);
726 retval
= _pygi_g_type_info_check_object (key_type_info
, key
, TRUE
);
731 _PyGI_ERROR_PREFIX ("Key %zd :", i
);
735 retval
= _pygi_g_type_info_check_object (value_type_info
, value
, TRUE
);
740 _PyGI_ERROR_PREFIX ("Value %zd :", i
);
745 g_base_info_unref ( (GIBaseInfo
*) key_type_info
);
746 g_base_info_unref ( (GIBaseInfo
*) value_type_info
);
751 case GI_TYPE_TAG_ERROR
:
752 PyErr_SetString (PyExc_NotImplementedError
, "Error marshalling is not supported yet");
762 * _pygi_argument_array_length_marshal:
763 * @length_arg_index: Index of length argument in the callables args list.
764 * @user_data1: (type Array(GValue)): Array of GValue arguments to retrieve length
765 * @user_data2: (type GICallableInfo): Callable info to get the argument from.
767 * Generic marshalling policy for array length arguments in callables.
769 * Returns: The length of the array or -1 on failure.
772 _pygi_argument_array_length_marshal (gsize length_arg_index
,
776 GIArgInfo length_arg_info
;
777 GITypeInfo length_type_info
;
778 GIArgument length_arg
;
779 gssize array_len
= -1;
780 GValue
*values
= (GValue
*)user_data1
;
781 GICallableInfo
*callable_info
= (GICallableInfo
*)user_data2
;
783 g_callable_info_load_arg (callable_info
, length_arg_index
, &length_arg_info
);
784 g_arg_info_load_type (&length_arg_info
, &length_type_info
);
786 length_arg
= _pygi_argument_from_g_value (&(values
[length_arg_index
]),
788 if (!pygi_argument_to_gssize (&length_arg
,
789 g_type_info_get_tag (&length_type_info
),
798 * _pygi_argument_to_array
799 * @arg: The argument to convert
800 * @array_length_policy: Closure for marshalling the array length argument when needed.
801 * @user_data1: Generic user data passed to the array_length_policy.
802 * @user_data2: Generic user data passed to the array_length_policy.
803 * @type_info: The type info for @arg
804 * @out_free_array: A return location for a gboolean that indicates whether
805 * or not the wrapped GArray should be freed
807 * Make sure an array type argument is wrapped in a GArray.
809 * Note: This method can *not* be folded into _pygi_argument_to_object() because
810 * arrays are special in the sense that they might require access to @args in
811 * order to get the length.
813 * Returns: A GArray wrapping @arg. If @out_free_array has been set to TRUE then
814 * free the array with g_array_free() without freeing the data members.
815 * Otherwise don't free the array.
818 _pygi_argument_to_array (GIArgument
*arg
,
819 PyGIArgArrayLengthPolicy array_length_policy
,
822 GITypeInfo
*type_info
,
823 gboolean
*out_free_array
)
825 GITypeInfo
*item_type_info
;
826 gboolean is_zero_terminated
;
831 g_return_val_if_fail (g_type_info_get_tag (type_info
) == GI_TYPE_TAG_ARRAY
, NULL
);
833 if (arg
->v_pointer
== NULL
) {
837 switch (g_type_info_get_array_type (type_info
)) {
838 case GI_ARRAY_TYPE_C
:
839 is_zero_terminated
= g_type_info_is_zero_terminated (type_info
);
840 item_type_info
= g_type_info_get_param_type (type_info
, 0);
842 item_size
= _pygi_g_type_info_size (item_type_info
);
844 g_base_info_unref ( (GIBaseInfo
*) item_type_info
);
846 if (is_zero_terminated
) {
847 length
= g_strv_length (arg
->v_pointer
);
849 length
= g_type_info_get_array_fixed_size (type_info
);
853 if (G_UNLIKELY (array_length_policy
== NULL
)) {
854 g_critical ("Unable to determine array length for %p",
856 g_array
= g_array_new (is_zero_terminated
, FALSE
, item_size
);
857 *out_free_array
= TRUE
;
861 length_arg_pos
= g_type_info_get_array_length (type_info
);
862 g_assert (length_arg_pos
>= 0);
864 length
= array_length_policy (length_arg_pos
, user_data1
, user_data2
);
871 g_assert (length
>= 0);
873 g_array
= g_array_new (is_zero_terminated
, FALSE
, item_size
);
875 g_free (g_array
->data
);
876 g_array
->data
= arg
->v_pointer
;
877 g_array
->len
= length
;
878 *out_free_array
= TRUE
;
880 case GI_ARRAY_TYPE_ARRAY
:
881 case GI_ARRAY_TYPE_BYTE_ARRAY
:
882 /* Note: GByteArray is really just a GArray */
883 g_array
= arg
->v_pointer
;
884 *out_free_array
= FALSE
;
886 case GI_ARRAY_TYPE_PTR_ARRAY
:
888 GPtrArray
*ptr_array
= (GPtrArray
*) arg
->v_pointer
;
889 g_array
= g_array_sized_new (FALSE
, FALSE
,
892 g_array
->data
= (char*) ptr_array
->pdata
;
893 g_array
->len
= ptr_array
->len
;
894 *out_free_array
= TRUE
;
898 g_critical ("Unexpected array type %u",
899 g_type_info_get_array_type (type_info
));
908 _pygi_argument_from_object (PyObject
*object
,
909 GITypeInfo
*type_info
,
914 gpointer cleanup_data
= NULL
;
916 memset(&arg
, 0, sizeof(GIArgument
));
917 type_tag
= g_type_info_get_tag (type_info
);
919 /* Ignores cleanup data for now. */
920 if (_pygi_marshal_from_py_basic_type (object
, &arg
, type_tag
, transfer
, &cleanup_data
) ||
926 case GI_TYPE_TAG_ARRAY
:
929 gboolean is_zero_terminated
;
930 GITypeInfo
*item_type_info
;
933 GITransfer item_transfer
;
936 if (object
== Py_None
) {
937 arg
.v_pointer
= NULL
;
941 /* Note, strings are sequences, but we cannot accept them here */
942 if (!PySequence_Check (object
) ||
943 #if PY_VERSION_HEX < 0x03000000
944 PyString_Check (object
) ||
946 PyUnicode_Check (object
)) {
947 PyErr_SetString (PyExc_TypeError
, "expected sequence");
951 length
= PySequence_Length (object
);
956 is_zero_terminated
= g_type_info_is_zero_terminated (type_info
);
957 item_type_info
= g_type_info_get_param_type (type_info
, 0);
959 /* we handle arrays that are really strings specially, see below */
960 if (g_type_info_get_tag (item_type_info
) == GI_TYPE_TAG_UINT8
)
963 item_size
= sizeof (GIArgument
);
965 array
= g_array_sized_new (is_zero_terminated
, FALSE
, item_size
, length
);
967 g_base_info_unref ( (GIBaseInfo
*) item_type_info
);
972 if (g_type_info_get_tag (item_type_info
) == GI_TYPE_TAG_UINT8
&&
973 PYGLIB_PyBytes_Check(object
)) {
975 memcpy(array
->data
, PYGLIB_PyBytes_AsString(object
), length
);
981 item_transfer
= transfer
== GI_TRANSFER_CONTAINER
? GI_TRANSFER_NOTHING
: transfer
;
983 for (i
= 0; i
< length
; i
++) {
987 py_item
= PySequence_GetItem (object
, i
);
988 if (py_item
== NULL
) {
989 goto array_item_error
;
992 item
= _pygi_argument_from_object (py_item
, item_type_info
, item_transfer
);
996 if (PyErr_Occurred()) {
997 goto array_item_error
;
1000 g_array_insert_val (array
, i
, item
);
1004 /* Free everything we have converted so far. */
1005 _pygi_argument_release ( (GIArgument
*) &array
, type_info
,
1006 GI_TRANSFER_NOTHING
, GI_DIRECTION_IN
);
1009 _PyGI_ERROR_PREFIX ("Item %zd: ", i
);
1014 arg
.v_pointer
= array
;
1016 g_base_info_unref ( (GIBaseInfo
*) item_type_info
);
1019 case GI_TYPE_TAG_INTERFACE
:
1022 GIInfoType info_type
;
1024 info
= g_type_info_get_interface (type_info
);
1025 info_type
= g_base_info_get_type (info
);
1027 switch (info_type
) {
1028 case GI_INFO_TYPE_CALLBACK
:
1029 /* This should be handled in invoke() */
1030 g_assert_not_reached();
1032 case GI_INFO_TYPE_BOXED
:
1033 case GI_INFO_TYPE_STRUCT
:
1034 case GI_INFO_TYPE_UNION
:
1038 gboolean is_foreign
= (info_type
== GI_INFO_TYPE_STRUCT
) &&
1039 (g_struct_info_is_foreign ((GIStructInfo
*) info
));
1041 g_type
= g_registered_type_info_get_g_type ( (GIRegisteredTypeInfo
*) info
);
1042 py_type
= _pygi_type_import_by_gi_info ( (GIBaseInfo
*) info
);
1044 /* Note for G_TYPE_VALUE g_type:
1045 * This will currently leak the GValue that is allocated and
1046 * stashed in arg.v_pointer. Out argument marshaling for caller
1047 * allocated GValues already pass in memory for the GValue.
1048 * Further re-factoring is needed to fix this leak.
1049 * See: https://bugzilla.gnome.org/show_bug.cgi?id=693405
1051 pygi_arg_struct_from_py_marshal (object
,
1054 info
, /*interface_info*/
1058 FALSE
, /*copy_reference*/
1060 g_type_info_is_pointer (type_info
));
1062 Py_DECREF (py_type
);
1065 case GI_INFO_TYPE_ENUM
:
1066 case GI_INFO_TYPE_FLAGS
:
1070 int_
= PYGLIB_PyNumber_Long (object
);
1075 arg
.v_int
= PYGLIB_PyLong_AsLong (int_
);
1081 case GI_INFO_TYPE_INTERFACE
:
1082 case GI_INFO_TYPE_OBJECT
:
1083 /* An error within this call will result in a NULL arg */
1084 pygi_arg_gobject_out_arg_from_py (object
, &arg
, transfer
);
1088 g_assert_not_reached();
1090 g_base_info_unref (info
);
1093 case GI_TYPE_TAG_GLIST
:
1094 case GI_TYPE_TAG_GSLIST
:
1097 GITypeInfo
*item_type_info
;
1098 GSList
*list
= NULL
;
1099 GITransfer item_transfer
;
1102 if (object
== Py_None
) {
1103 arg
.v_pointer
= NULL
;
1107 length
= PySequence_Length (object
);
1112 item_type_info
= g_type_info_get_param_type (type_info
, 0);
1113 g_assert (item_type_info
!= NULL
);
1115 item_transfer
= transfer
== GI_TRANSFER_CONTAINER
? GI_TRANSFER_NOTHING
: transfer
;
1117 for (i
= length
- 1; i
>= 0; i
--) {
1121 py_item
= PySequence_GetItem (object
, i
);
1122 if (py_item
== NULL
) {
1123 goto list_item_error
;
1126 item
= _pygi_argument_from_object (py_item
, item_type_info
, item_transfer
);
1128 Py_DECREF (py_item
);
1130 if (PyErr_Occurred()) {
1131 goto list_item_error
;
1134 if (type_tag
== GI_TYPE_TAG_GLIST
) {
1135 list
= (GSList
*) g_list_prepend ( (GList
*) list
, item
.v_pointer
);
1137 list
= g_slist_prepend (list
, item
.v_pointer
);
1143 /* Free everything we have converted so far. */
1144 _pygi_argument_release ( (GIArgument
*) &list
, type_info
,
1145 GI_TRANSFER_NOTHING
, GI_DIRECTION_IN
);
1148 _PyGI_ERROR_PREFIX ("Item %zd: ", i
);
1152 arg
.v_pointer
= list
;
1154 g_base_info_unref ( (GIBaseInfo
*) item_type_info
);
1158 case GI_TYPE_TAG_GHASH
:
1163 GITypeInfo
*key_type_info
;
1164 GITypeInfo
*value_type_info
;
1165 GITypeTag key_type_tag
;
1166 GHashFunc hash_func
;
1167 GEqualFunc equal_func
;
1168 GHashTable
*hash_table
;
1169 GITransfer item_transfer
;
1173 if (object
== Py_None
) {
1174 arg
.v_pointer
= NULL
;
1178 length
= PyMapping_Length (object
);
1183 keys
= PyMapping_Keys (object
);
1188 values
= PyMapping_Values (object
);
1189 if (values
== NULL
) {
1194 key_type_info
= g_type_info_get_param_type (type_info
, 0);
1195 g_assert (key_type_info
!= NULL
);
1197 value_type_info
= g_type_info_get_param_type (type_info
, 1);
1198 g_assert (value_type_info
!= NULL
);
1200 key_type_tag
= g_type_info_get_tag (key_type_info
);
1202 switch (key_type_tag
) {
1203 case GI_TYPE_TAG_UTF8
:
1204 case GI_TYPE_TAG_FILENAME
:
1205 hash_func
= g_str_hash
;
1206 equal_func
= g_str_equal
;
1213 hash_table
= g_hash_table_new (hash_func
, equal_func
);
1214 if (hash_table
== NULL
) {
1216 goto hash_table_release
;
1219 item_transfer
= transfer
== GI_TRANSFER_CONTAINER
? GI_TRANSFER_NOTHING
: transfer
;
1221 for (i
= 0; i
< length
; i
++) {
1227 py_key
= PyList_GET_ITEM (keys
, i
);
1228 py_value
= PyList_GET_ITEM (values
, i
);
1230 key
= _pygi_argument_from_object (py_key
, key_type_info
, item_transfer
);
1231 if (PyErr_Occurred()) {
1232 goto hash_table_item_error
;
1235 value
= _pygi_argument_from_object (py_value
, value_type_info
, item_transfer
);
1236 if (PyErr_Occurred()) {
1237 _pygi_argument_release (&key
, type_info
, GI_TRANSFER_NOTHING
, GI_DIRECTION_IN
);
1238 goto hash_table_item_error
;
1241 g_hash_table_insert (hash_table
, key
.v_pointer
,
1242 _pygi_arg_to_hash_pointer (&value
, g_type_info_get_tag (value_type_info
)));
1245 hash_table_item_error
:
1246 /* Free everything we have converted so far. */
1247 _pygi_argument_release ( (GIArgument
*) &hash_table
, type_info
,
1248 GI_TRANSFER_NOTHING
, GI_DIRECTION_IN
);
1251 _PyGI_ERROR_PREFIX ("Item %zd: ", i
);
1255 arg
.v_pointer
= hash_table
;
1258 g_base_info_unref ( (GIBaseInfo
*) key_type_info
);
1259 g_base_info_unref ( (GIBaseInfo
*) value_type_info
);
1264 case GI_TYPE_TAG_ERROR
:
1265 PyErr_SetString (PyExc_NotImplementedError
, "error marshalling is not supported yet");
1269 g_assert_not_reached ();
1276 * _pygi_argument_to_object:
1277 * @arg: The argument to convert to an object.
1278 * @type_info: Type info for @arg
1281 * If the argument is of type array, it must be encoded in a GArray, by calling
1282 * _pygi_argument_to_array(). This logic can not be folded into this method
1283 * as determining array lengths may require access to method call arguments.
1285 * Returns: A PyObject representing @arg
1288 _pygi_argument_to_object (GIArgument
*arg
,
1289 GITypeInfo
*type_info
,
1290 GITransfer transfer
)
1293 PyObject
*object
= NULL
;
1295 type_tag
= g_type_info_get_tag (type_info
);
1296 object
= _pygi_marshal_to_py_basic_type (arg
, type_tag
, transfer
);
1301 case GI_TYPE_TAG_VOID
:
1303 if (g_type_info_is_pointer (type_info
)) {
1304 g_warn_if_fail (transfer
== GI_TRANSFER_NOTHING
);
1305 object
= PyLong_FromVoidPtr (arg
->v_pointer
);
1309 case GI_TYPE_TAG_ARRAY
:
1311 /* Arrays are assumed to be packed in a GArray */
1313 GITypeInfo
*item_type_info
;
1314 GITypeTag item_type_tag
;
1315 GITransfer item_transfer
;
1318 if (arg
->v_pointer
== NULL
)
1319 return PyList_New (0);
1321 item_type_info
= g_type_info_get_param_type (type_info
, 0);
1322 g_assert (item_type_info
!= NULL
);
1324 item_type_tag
= g_type_info_get_tag (item_type_info
);
1325 item_transfer
= transfer
== GI_TRANSFER_CONTAINER
? GI_TRANSFER_NOTHING
: transfer
;
1327 array
= arg
->v_pointer
;
1328 item_size
= g_array_get_element_size (array
);
1330 if (G_UNLIKELY (item_size
> sizeof(GIArgument
))) {
1331 g_critical ("Stack overflow protection. "
1332 "Can't copy array element into GIArgument.");
1333 return PyList_New (0);
1336 if (item_type_tag
== GI_TYPE_TAG_UINT8
) {
1337 /* Return as a byte array */
1338 object
= PYGLIB_PyBytes_FromStringAndSize (array
->data
, array
->len
);
1340 object
= PyList_New (array
->len
);
1341 if (object
== NULL
) {
1342 g_critical ("Failure to allocate array for %u items", array
->len
);
1343 g_base_info_unref ( (GIBaseInfo
*) item_type_info
);
1347 for (i
= 0; i
< array
->len
; i
++) {
1348 GIArgument item
= { 0 };
1351 memcpy (&item
, array
->data
+ i
* item_size
, item_size
);
1353 py_item
= _pygi_argument_to_object (&item
, item_type_info
, item_transfer
);
1354 if (py_item
== NULL
) {
1356 _PyGI_ERROR_PREFIX ("Item %zu: ", i
);
1360 PyList_SET_ITEM (object
, i
, py_item
);
1364 g_base_info_unref ( (GIBaseInfo
*) item_type_info
);
1367 case GI_TYPE_TAG_INTERFACE
:
1370 GIInfoType info_type
;
1372 info
= g_type_info_get_interface (type_info
);
1373 info_type
= g_base_info_get_type (info
);
1375 switch (info_type
) {
1376 case GI_INFO_TYPE_CALLBACK
:
1378 g_assert_not_reached();
1380 case GI_INFO_TYPE_BOXED
:
1381 case GI_INFO_TYPE_STRUCT
:
1382 case GI_INFO_TYPE_UNION
:
1385 GType g_type
= g_registered_type_info_get_g_type ( (GIRegisteredTypeInfo
*) info
);
1386 gboolean is_foreign
= (info_type
== GI_INFO_TYPE_STRUCT
) &&
1387 (g_struct_info_is_foreign ((GIStructInfo
*) info
));
1389 /* Special case variant and none to force loading from py module. */
1390 if (g_type
== G_TYPE_VARIANT
|| g_type
== G_TYPE_NONE
) {
1391 py_type
= _pygi_type_import_by_gi_info (info
);
1393 py_type
= _pygi_type_get_from_g_type (g_type
);
1396 object
= pygi_arg_struct_to_py_marshal (arg
,
1397 info
, /*interface_info*/
1401 FALSE
, /*is_allocated*/
1404 Py_XDECREF (py_type
);
1407 case GI_INFO_TYPE_ENUM
:
1408 case GI_INFO_TYPE_FLAGS
:
1412 type
= g_registered_type_info_get_g_type ( (GIRegisteredTypeInfo
*) info
);
1414 if (type
== G_TYPE_NONE
) {
1415 /* An enum with a GType of None is an enum without GType */
1416 PyObject
*py_type
= _pygi_type_import_by_gi_info (info
);
1417 PyObject
*py_args
= NULL
;
1422 py_args
= PyTuple_New (1);
1423 if (PyTuple_SetItem (py_args
, 0, PyLong_FromLong (arg
->v_int
)) != 0) {
1424 Py_DECREF (py_args
);
1425 Py_DECREF (py_type
);
1429 object
= PyObject_CallFunction (py_type
, "i", arg
->v_int
);
1431 Py_DECREF (py_args
);
1432 Py_DECREF (py_type
);
1434 } else if (info_type
== GI_INFO_TYPE_ENUM
) {
1435 object
= pyg_enum_from_gtype (type
, arg
->v_int
);
1437 object
= pyg_flags_from_gtype (type
, arg
->v_uint
);
1442 case GI_INFO_TYPE_INTERFACE
:
1443 case GI_INFO_TYPE_OBJECT
:
1444 object
= pygi_arg_gobject_to_py_called_from_c (arg
, transfer
);
1448 g_assert_not_reached();
1451 g_base_info_unref (info
);
1454 case GI_TYPE_TAG_GLIST
:
1455 case GI_TYPE_TAG_GSLIST
:
1459 GITypeInfo
*item_type_info
;
1460 GITransfer item_transfer
;
1463 list
= arg
->v_pointer
;
1464 length
= g_slist_length (list
);
1466 object
= PyList_New (length
);
1467 if (object
== NULL
) {
1471 item_type_info
= g_type_info_get_param_type (type_info
, 0);
1472 g_assert (item_type_info
!= NULL
);
1474 item_transfer
= transfer
== GI_TRANSFER_CONTAINER
? GI_TRANSFER_NOTHING
: transfer
;
1476 for (i
= 0; list
!= NULL
; list
= g_slist_next (list
), i
++) {
1480 item
.v_pointer
= list
->data
;
1482 py_item
= _pygi_argument_to_object (&item
, item_type_info
, item_transfer
);
1483 if (py_item
== NULL
) {
1485 _PyGI_ERROR_PREFIX ("Item %zu: ", i
);
1489 PyList_SET_ITEM (object
, i
, py_item
);
1492 g_base_info_unref ( (GIBaseInfo
*) item_type_info
);
1495 case GI_TYPE_TAG_GHASH
:
1497 GITypeInfo
*key_type_info
;
1498 GITypeInfo
*value_type_info
;
1499 GITransfer item_transfer
;
1500 GHashTableIter hash_table_iter
;
1504 if (arg
->v_pointer
== NULL
) {
1510 object
= PyDict_New();
1511 if (object
== NULL
) {
1515 key_type_info
= g_type_info_get_param_type (type_info
, 0);
1516 g_assert (key_type_info
!= NULL
);
1517 g_assert (g_type_info_get_tag (key_type_info
) != GI_TYPE_TAG_VOID
);
1519 value_type_info
= g_type_info_get_param_type (type_info
, 1);
1520 g_assert (value_type_info
!= NULL
);
1521 g_assert (g_type_info_get_tag (value_type_info
) != GI_TYPE_TAG_VOID
);
1523 item_transfer
= transfer
== GI_TRANSFER_CONTAINER
? GI_TRANSFER_NOTHING
: transfer
;
1525 g_hash_table_iter_init (&hash_table_iter
, (GHashTable
*) arg
->v_pointer
);
1526 while (g_hash_table_iter_next (&hash_table_iter
, &key
.v_pointer
, &value
.v_pointer
)) {
1531 py_key
= _pygi_argument_to_object (&key
, key_type_info
, item_transfer
);
1532 if (py_key
== NULL
) {
1536 _pygi_hash_pointer_to_arg (&value
, g_type_info_get_tag (value_type_info
));
1537 py_value
= _pygi_argument_to_object (&value
, value_type_info
, item_transfer
);
1538 if (py_value
== NULL
) {
1543 retval
= PyDict_SetItem (object
, py_key
, py_value
);
1546 Py_DECREF (py_value
);
1554 g_base_info_unref ( (GIBaseInfo
*) key_type_info
);
1555 g_base_info_unref ( (GIBaseInfo
*) value_type_info
);
1558 case GI_TYPE_TAG_ERROR
:
1560 GError
*error
= (GError
*) arg
->v_pointer
;
1561 if (error
!= NULL
&& transfer
== GI_TRANSFER_NOTHING
) {
1562 /* If we have not been transferred the ownership we must copy
1563 * the error, because pygi_error_check() is going to free it.
1565 error
= g_error_copy (error
);
1568 if (pygi_error_check (&error
)) {
1570 PyObject
*err_value
;
1571 PyObject
*err_trace
;
1572 PyErr_Fetch (&err_type
, &err_value
, &err_trace
);
1573 Py_XDECREF (err_type
);
1574 Py_XDECREF (err_trace
);
1585 g_assert_not_reached();
1593 _pygi_argument_release (GIArgument
*arg
,
1594 GITypeInfo
*type_info
,
1595 GITransfer transfer
,
1596 GIDirection direction
)
1599 gboolean is_out
= (direction
== GI_DIRECTION_OUT
|| direction
== GI_DIRECTION_INOUT
);
1601 type_tag
= g_type_info_get_tag (type_info
);
1604 case GI_TYPE_TAG_VOID
:
1605 /* Don't do anything, it's transparent to the C side */
1607 case GI_TYPE_TAG_BOOLEAN
:
1608 case GI_TYPE_TAG_INT8
:
1609 case GI_TYPE_TAG_UINT8
:
1610 case GI_TYPE_TAG_INT16
:
1611 case GI_TYPE_TAG_UINT16
:
1612 case GI_TYPE_TAG_INT32
:
1613 case GI_TYPE_TAG_UINT32
:
1614 case GI_TYPE_TAG_INT64
:
1615 case GI_TYPE_TAG_UINT64
:
1616 case GI_TYPE_TAG_FLOAT
:
1617 case GI_TYPE_TAG_DOUBLE
:
1618 case GI_TYPE_TAG_GTYPE
:
1619 case GI_TYPE_TAG_UNICHAR
:
1621 case GI_TYPE_TAG_FILENAME
:
1622 case GI_TYPE_TAG_UTF8
:
1623 /* With allow-none support the string could be NULL */
1624 if ((arg
->v_string
!= NULL
&&
1625 (direction
== GI_DIRECTION_IN
&& transfer
== GI_TRANSFER_NOTHING
))
1626 || (direction
== GI_DIRECTION_OUT
&& transfer
== GI_TRANSFER_EVERYTHING
)) {
1627 g_free (arg
->v_string
);
1630 case GI_TYPE_TAG_ARRAY
:
1635 if (arg
->v_pointer
== NULL
) {
1639 array
= arg
->v_pointer
;
1641 if ( (direction
== GI_DIRECTION_IN
&& transfer
!= GI_TRANSFER_EVERYTHING
)
1642 || (direction
== GI_DIRECTION_OUT
&& transfer
== GI_TRANSFER_EVERYTHING
)) {
1643 GITypeInfo
*item_type_info
;
1644 GITransfer item_transfer
;
1646 item_type_info
= g_type_info_get_param_type (type_info
, 0);
1648 item_transfer
= direction
== GI_DIRECTION_IN
? GI_TRANSFER_NOTHING
: GI_TRANSFER_EVERYTHING
;
1650 /* Free the items */
1651 for (i
= 0; i
< array
->len
; i
++) {
1653 item
= &_g_array_index (array
, GIArgument
, i
);
1654 _pygi_argument_release (item
, item_type_info
, item_transfer
, direction
);
1657 g_base_info_unref ( (GIBaseInfo
*) item_type_info
);
1660 if ( (direction
== GI_DIRECTION_IN
&& transfer
== GI_TRANSFER_NOTHING
)
1661 || (direction
== GI_DIRECTION_OUT
&& transfer
!= GI_TRANSFER_NOTHING
)) {
1662 g_array_free (array
, TRUE
);
1667 case GI_TYPE_TAG_INTERFACE
:
1670 GIInfoType info_type
;
1672 info
= g_type_info_get_interface (type_info
);
1673 info_type
= g_base_info_get_type (info
);
1675 switch (info_type
) {
1676 case GI_INFO_TYPE_CALLBACK
:
1679 case GI_INFO_TYPE_BOXED
:
1680 case GI_INFO_TYPE_STRUCT
:
1681 case GI_INFO_TYPE_UNION
:
1685 if (arg
->v_pointer
== NULL
) {
1689 type
= g_registered_type_info_get_g_type ( (GIRegisteredTypeInfo
*) info
);
1691 if (g_type_is_a (type
, G_TYPE_VALUE
)) {
1694 value
= arg
->v_pointer
;
1696 if ( (direction
== GI_DIRECTION_IN
&& transfer
!= GI_TRANSFER_EVERYTHING
)
1697 || (direction
== GI_DIRECTION_OUT
&& transfer
== GI_TRANSFER_EVERYTHING
)) {
1698 g_value_unset (value
);
1701 if ( (direction
== GI_DIRECTION_IN
&& transfer
== GI_TRANSFER_NOTHING
)
1702 || (direction
== GI_DIRECTION_OUT
&& transfer
!= GI_TRANSFER_NOTHING
)) {
1703 g_slice_free (GValue
, value
);
1705 } else if (g_type_is_a (type
, G_TYPE_CLOSURE
)) {
1706 if (direction
== GI_DIRECTION_IN
&& transfer
== GI_TRANSFER_NOTHING
) {
1707 g_closure_unref (arg
->v_pointer
);
1709 } else if (info_type
== GI_INFO_TYPE_STRUCT
&&
1710 g_struct_info_is_foreign ((GIStructInfo
*) info
)) {
1711 if (direction
== GI_DIRECTION_OUT
&& transfer
== GI_TRANSFER_EVERYTHING
) {
1712 pygi_struct_foreign_release (info
, arg
->v_pointer
);
1714 } else if (g_type_is_a (type
, G_TYPE_BOXED
)) {
1715 } else if (g_type_is_a (type
, G_TYPE_POINTER
) || type
== G_TYPE_NONE
) {
1716 g_warn_if_fail (!g_type_info_is_pointer (type_info
) || transfer
== GI_TRANSFER_NOTHING
);
1721 case GI_INFO_TYPE_ENUM
:
1722 case GI_INFO_TYPE_FLAGS
:
1724 case GI_INFO_TYPE_INTERFACE
:
1725 case GI_INFO_TYPE_OBJECT
:
1726 if (arg
->v_pointer
== NULL
) {
1729 if (is_out
&& transfer
== GI_TRANSFER_EVERYTHING
) {
1730 g_object_unref (arg
->v_pointer
);
1734 g_assert_not_reached();
1737 g_base_info_unref (info
);
1740 case GI_TYPE_TAG_GLIST
:
1741 case GI_TYPE_TAG_GSLIST
:
1745 if (arg
->v_pointer
== NULL
) {
1749 list
= arg
->v_pointer
;
1751 if ( (direction
== GI_DIRECTION_IN
&& transfer
!= GI_TRANSFER_EVERYTHING
)
1752 || (direction
== GI_DIRECTION_OUT
&& transfer
== GI_TRANSFER_EVERYTHING
)) {
1753 GITypeInfo
*item_type_info
;
1754 GITransfer item_transfer
;
1757 item_type_info
= g_type_info_get_param_type (type_info
, 0);
1758 g_assert (item_type_info
!= NULL
);
1760 item_transfer
= direction
== GI_DIRECTION_IN
? GI_TRANSFER_NOTHING
: GI_TRANSFER_EVERYTHING
;
1762 /* Free the items */
1763 for (item
= list
; item
!= NULL
; item
= g_slist_next (item
)) {
1764 _pygi_argument_release ( (GIArgument
*) &item
->data
, item_type_info
,
1765 item_transfer
, direction
);
1768 g_base_info_unref ( (GIBaseInfo
*) item_type_info
);
1771 if ( (direction
== GI_DIRECTION_IN
&& transfer
== GI_TRANSFER_NOTHING
)
1772 || (direction
== GI_DIRECTION_OUT
&& transfer
!= GI_TRANSFER_NOTHING
)) {
1773 if (type_tag
== GI_TYPE_TAG_GLIST
) {
1774 g_list_free ( (GList
*) list
);
1776 /* type_tag == GI_TYPE_TAG_GSLIST */
1777 g_slist_free (list
);
1783 case GI_TYPE_TAG_GHASH
:
1785 GHashTable
*hash_table
;
1787 if (arg
->v_pointer
== NULL
) {
1791 hash_table
= arg
->v_pointer
;
1793 if (direction
== GI_DIRECTION_IN
&& transfer
!= GI_TRANSFER_EVERYTHING
) {
1794 /* We created the table without a destroy function, so keys and
1795 * values need to be released. */
1796 GITypeInfo
*key_type_info
;
1797 GITypeInfo
*value_type_info
;
1798 GITransfer item_transfer
;
1799 GHashTableIter hash_table_iter
;
1803 key_type_info
= g_type_info_get_param_type (type_info
, 0);
1804 g_assert (key_type_info
!= NULL
);
1806 value_type_info
= g_type_info_get_param_type (type_info
, 1);
1807 g_assert (value_type_info
!= NULL
);
1809 if (direction
== GI_DIRECTION_IN
) {
1810 item_transfer
= GI_TRANSFER_NOTHING
;
1812 item_transfer
= GI_TRANSFER_EVERYTHING
;
1815 g_hash_table_iter_init (&hash_table_iter
, hash_table
);
1816 while (g_hash_table_iter_next (&hash_table_iter
, &key
, &value
)) {
1817 _pygi_argument_release ( (GIArgument
*) &key
, key_type_info
,
1818 item_transfer
, direction
);
1819 _pygi_argument_release ( (GIArgument
*) &value
, value_type_info
,
1820 item_transfer
, direction
);
1823 g_base_info_unref ( (GIBaseInfo
*) key_type_info
);
1824 g_base_info_unref ( (GIBaseInfo
*) value_type_info
);
1825 } else if (direction
== GI_DIRECTION_OUT
&& transfer
== GI_TRANSFER_CONTAINER
) {
1826 /* Be careful to avoid keys and values being freed if the
1827 * callee gave a destroy function. */
1828 g_hash_table_steal_all (hash_table
);
1831 if ( (direction
== GI_DIRECTION_IN
&& transfer
== GI_TRANSFER_NOTHING
)
1832 || (direction
== GI_DIRECTION_OUT
&& transfer
!= GI_TRANSFER_NOTHING
)) {
1833 g_hash_table_unref (hash_table
);
1838 case GI_TYPE_TAG_ERROR
:
1842 if (arg
->v_pointer
== NULL
) {
1846 error
= * (GError
**) arg
->v_pointer
;
1848 if (error
!= NULL
) {
1849 g_error_free (error
);
1852 g_slice_free (GError
*, arg
->v_pointer
);