1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
4 * Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>
5 * Copyright (C) 2014 Simon Feltman <sfeltman@gnome.org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 #include <pyglib-python-compat.h>
25 #include "pygi-array.h"
26 #include "pygi-private.h"
27 #include "pygi-marshal-cleanup.h"
29 /* Needed for _pygi_marshal_cleanup_from_py_interface_struct_gvalue hack */
30 #include "pygi-struct-marshal.h"
37 gi_argument_from_py_ssize_t (GIArgument *arg_out,
42 case GI_TYPE_TAG_VOID:
43 case GI_TYPE_TAG_BOOLEAN:
46 case GI_TYPE_TAG_INT8:
47 if (size_in >= G_MININT8 && size_in <= G_MAXINT8) {
48 arg_out->v_int8 = size_in;
54 case GI_TYPE_TAG_UINT8:
55 if (size_in >= 0 && size_in <= G_MAXUINT8) {
56 arg_out->v_uint8 = size_in;
62 case GI_TYPE_TAG_INT16:
63 if (size_in >= G_MININT16 && size_in <= G_MAXINT16) {
64 arg_out->v_int16 = size_in;
70 case GI_TYPE_TAG_UINT16:
71 if (size_in >= 0 && size_in <= G_MAXUINT16) {
72 arg_out->v_uint16 = size_in;
78 /* Ranges assume two's complement */
79 case GI_TYPE_TAG_INT32:
80 if (size_in >= G_MININT32 && size_in <= G_MAXINT32) {
81 arg_out->v_int32 = size_in;
87 case GI_TYPE_TAG_UINT32:
88 if (size_in >= 0 && size_in <= G_MAXUINT32) {
89 arg_out->v_uint32 = size_in;
95 case GI_TYPE_TAG_INT64:
96 arg_out->v_int64 = size_in;
99 case GI_TYPE_TAG_UINT64:
101 arg_out->v_uint64 = size_in;
107 case GI_TYPE_TAG_FLOAT:
108 case GI_TYPE_TAG_DOUBLE:
109 case GI_TYPE_TAG_GTYPE:
110 case GI_TYPE_TAG_UTF8:
111 case GI_TYPE_TAG_FILENAME:
112 case GI_TYPE_TAG_ARRAY:
113 case GI_TYPE_TAG_INTERFACE:
114 case GI_TYPE_TAG_GLIST:
115 case GI_TYPE_TAG_GSLIST:
116 case GI_TYPE_TAG_GHASH:
117 case GI_TYPE_TAG_ERROR:
118 case GI_TYPE_TAG_UNICHAR:
124 PyErr_Format (PyExc_OverflowError,
125 "Unable to marshal C Py_ssize_t %zd to %s",
127 g_type_tag_to_string (type_tag));
131 PyErr_Format (PyExc_TypeError,
132 "Unable to marshal C Py_ssize_t %zd to %s",
134 g_type_tag_to_string (type_tag));
139 gi_argument_to_gsize (GIArgument *arg_in,
144 case GI_TYPE_TAG_INT8:
145 *gsize_out = arg_in->v_int8;
147 case GI_TYPE_TAG_UINT8:
148 *gsize_out = arg_in->v_uint8;
150 case GI_TYPE_TAG_INT16:
151 *gsize_out = arg_in->v_int16;
153 case GI_TYPE_TAG_UINT16:
154 *gsize_out = arg_in->v_uint16;
156 case GI_TYPE_TAG_INT32:
157 *gsize_out = arg_in->v_int32;
159 case GI_TYPE_TAG_UINT32:
160 *gsize_out = arg_in->v_uint32;
162 case GI_TYPE_TAG_INT64:
163 *gsize_out = arg_in->v_int64;
165 case GI_TYPE_TAG_UINT64:
166 *gsize_out = arg_in->v_uint64;
169 PyErr_Format (PyExc_TypeError,
170 "Unable to marshal %s to gsize",
171 g_type_tag_to_string (type_tag));
177 _pygi_marshal_from_py_array (PyGIInvokeState *state,
178 PyGICallableCache *callable_cache,
179 PyGIArgCache *arg_cache,
182 gpointer *cleanup_data)
184 PyGIMarshalFromPyFunc from_py_marshaller;
186 int success_count = 0;
189 gboolean is_ptr_array;
190 GArray *array_ = NULL;
191 PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache;
192 PyGIArgGArray *array_cache = (PyGIArgGArray *)arg_cache;
195 if (py_arg == Py_None) {
196 arg->v_pointer = NULL;
200 if (!PySequence_Check (py_arg)) {
201 PyErr_Format (PyExc_TypeError, "Must be sequence, not %s",
202 py_arg->ob_type->tp_name);
206 length = PySequence_Length (py_arg);
210 if (array_cache->fixed_size >= 0 &&
211 array_cache->fixed_size != length) {
212 PyErr_Format (PyExc_ValueError, "Must contain %zd items, not %zd",
213 array_cache->fixed_size, length);
218 item_size = array_cache->item_size;
219 is_ptr_array = (array_cache->array_type == GI_ARRAY_TYPE_PTR_ARRAY);
221 array_ = (GArray *)g_ptr_array_sized_new (length);
223 array_ = g_array_sized_new (array_cache->is_zero_terminated,
229 if (array_ == NULL) {
234 if (sequence_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8 &&
235 PYGLIB_PyBytes_Check (py_arg)) {
236 memcpy(array_->data, PYGLIB_PyBytes_AsString (py_arg), length);
237 array_->len = length;
238 if (array_cache->is_zero_terminated) {
239 /* If array_ has been created with zero_termination, space for the
240 * terminator is properly allocated, so we're not off-by-one here. */
241 array_->data[length] = '\0';
246 from_py_marshaller = sequence_cache->item_cache->from_py_marshaller;
247 for (i = 0, success_count = 0; i < length; i++) {
248 GIArgument item = {0};
249 gpointer item_cleanup_data = NULL;
250 PyObject *py_item = PySequence_GetItem (py_arg, i);
254 if (!from_py_marshaller ( state,
256 sequence_cache->item_cache,
259 &item_cleanup_data)) {
265 if (item_cleanup_data != NULL && item_cleanup_data != item.v_pointer) {
266 /* We only support one level of data discrepancy between an items
267 * data and its cleanup data. This is because we only track a single
268 * extra cleanup data pointer per-argument and cannot track the entire
269 * array of items differing data and cleanup_data.
270 * For example, this would fail if trying to marshal an array of
271 * callback closures marked with SCOPE call type where the cleanup data
272 * is different from the items v_pointer, likewise an array of arrays.
274 PyErr_SetString(PyExc_RuntimeError, "Cannot cleanup item data for array due to "
275 "the items data its cleanup data being different.");
279 /* FIXME: it is much more efficent to have seperate marshaller
280 * for ptr arrays than doing the evaluation
281 * and casting each loop iteration
284 g_ptr_array_add((GPtrArray *)array_, item.v_pointer);
285 } else if (sequence_cache->item_cache->is_pointer) {
286 /* if the item is a pointer, simply copy the pointer */
287 g_assert (item_size == sizeof (item.v_pointer));
288 g_array_insert_val (array_, i, item);
289 } else if (sequence_cache->item_cache->type_tag == GI_TYPE_TAG_INTERFACE) {
290 /* Special case handling of flat arrays of gvalue/boxed/struct */
291 PyGIInterfaceCache *item_iface_cache = (PyGIInterfaceCache *) sequence_cache->item_cache;
292 GIBaseInfo *base_info = (GIBaseInfo *) item_iface_cache->interface_info;
293 GIInfoType info_type = g_base_info_get_type (base_info);
296 case GI_INFO_TYPE_UNION:
297 case GI_INFO_TYPE_STRUCT:
299 PyGIArgCache *item_arg_cache = (PyGIArgCache *)item_iface_cache;
300 PyGIMarshalCleanupFunc from_py_cleanup = item_arg_cache->from_py_cleanup;
302 if (g_type_is_a (item_iface_cache->g_type, G_TYPE_VALUE)) {
303 /* Special case GValue flat arrays to properly init and copy the contents. */
304 GValue* dest = (GValue*) (array_->data + (i * item_size));
305 if (item.v_pointer != NULL) {
306 memset (dest, 0, item_size);
307 g_value_init (dest, G_VALUE_TYPE ((GValue*) item.v_pointer));
308 g_value_copy ((GValue*) item.v_pointer, dest);
310 /* Manually increment the length because we are manually setting the memory. */
314 /* Handles flat arrays of boxed or struct types. */
315 g_array_insert_vals (array_, i, item.v_pointer, 1);
318 /* Cleanup any memory left by the per-item marshaler because
319 * _pygi_marshal_cleanup_from_py_array will not know about this
320 * due to "item" being a temporarily marshaled value done on the stack.
323 from_py_cleanup (state, item_arg_cache, py_item, item_cleanup_data, TRUE);
328 g_array_insert_val (array_, i, item);
331 /* default value copy of a simple type */
332 g_array_insert_val (array_, i, item);
338 if (sequence_cache->item_cache->from_py_cleanup != NULL) {
340 PyGIMarshalCleanupFunc cleanup_func =
341 sequence_cache->item_cache->from_py_cleanup;
343 /* Only attempt per item cleanup on pointer items */
344 if (sequence_cache->item_cache->is_pointer) {
345 for(j = 0; j < success_count; j++) {
346 PyObject *py_item = PySequence_GetItem (py_arg, j);
348 sequence_cache->item_cache,
351 g_ptr_array_index ((GPtrArray *)array_, j) :
352 g_array_index (array_, gpointer, j),
360 g_ptr_array_free ( ( GPtrArray *)array_, TRUE);
362 g_array_free (array_, TRUE);
363 _PyGI_ERROR_PREFIX ("Item %i: ", i);
368 if (array_cache->len_arg_index >= 0) {
369 /* we have an child arg to handle */
370 PyGIArgCache *child_cache =
371 _pygi_callable_cache_get_arg (callable_cache, array_cache->len_arg_index);
373 if (!gi_argument_from_py_ssize_t (&state->arg_values[child_cache->c_arg_index],
375 child_cache->type_tag)) {
380 if (array_cache->array_type == GI_ARRAY_TYPE_C) {
381 /* In the case of GI_ARRAY_C, we give the data directly as the argument
382 * but keep the array_ wrapper as cleanup data so we don't have to find
385 arg->v_pointer = array_->data;
387 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING) {
388 g_array_free (array_, FALSE);
389 *cleanup_data = NULL;
391 *cleanup_data = array_;
394 arg->v_pointer = array_;
396 if (arg_cache->transfer == GI_TRANSFER_NOTHING) {
397 /* Free everything in cleanup. */
398 *cleanup_data = array_;
399 } else if (arg_cache->transfer == GI_TRANSFER_CONTAINER) {
400 /* Make a shallow copy so we can free the elements later in cleanup
401 * because it is possible invoke will free the list before our cleanup. */
402 *cleanup_data = is_ptr_array ?
403 (gpointer)g_ptr_array_ref ((GPtrArray *)array_) :
404 (gpointer)g_array_ref (array_);
405 } else { /* GI_TRANSFER_EVERYTHING */
406 /* No cleanup, everything is given to the callee. */
407 *cleanup_data = NULL;
415 _pygi_marshal_cleanup_from_py_array (PyGIInvokeState *state,
416 PyGIArgCache *arg_cache,
419 gboolean was_processed)
422 GArray *array_ = NULL;
423 GPtrArray *ptr_array_ = NULL;
424 PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache;
425 PyGIArgGArray *array_cache = (PyGIArgGArray *)arg_cache;
427 if (array_cache->array_type == GI_ARRAY_TYPE_PTR_ARRAY) {
428 ptr_array_ = (GPtrArray *) data;
430 array_ = (GArray *) data;
433 /* clean up items first */
434 if (sequence_cache->item_cache->from_py_cleanup != NULL) {
436 guint len = (array_ != NULL) ? array_->len : ptr_array_->len;
437 PyGIMarshalCleanupFunc cleanup_func =
438 sequence_cache->item_cache->from_py_cleanup;
440 for (i = 0; i < len; i++) {
442 PyObject *py_item = NULL;
444 /* case 1: GPtrArray */
445 if (ptr_array_ != NULL)
446 item = g_ptr_array_index (ptr_array_, i);
447 /* case 2: C array or GArray with object pointers */
448 else if (sequence_cache->item_cache->is_pointer)
449 item = g_array_index (array_, gpointer, i);
450 /* case 3: C array or GArray with simple types or structs */
452 item = array_->data + i * array_cache->item_size;
453 /* special-case hack: GValue array items do not get slice
454 * allocated in _pygi_marshal_from_py_array(), so we must
455 * not try to deallocate it as a slice and thus
456 * short-circuit cleanup_func. */
457 if (cleanup_func == pygi_arg_gvalue_from_py_cleanup) {
458 g_value_unset ((GValue*) item);
463 py_item = PySequence_GetItem (py_arg, i);
464 cleanup_func (state, sequence_cache->item_cache, py_item, item, TRUE);
465 Py_XDECREF (py_item);
469 /* Only free the array when we didn't transfer ownership */
470 if (array_cache->array_type == GI_ARRAY_TYPE_C) {
471 /* always free the GArray wrapper created in from_py marshaling and
472 * passed back as cleanup_data
474 g_array_free (array_, arg_cache->transfer == GI_TRANSFER_NOTHING);
477 g_array_unref (array_);
479 g_ptr_array_unref (ptr_array_);
488 _pygi_marshal_to_py_array (PyGIInvokeState *state,
489 PyGICallableCache *callable_cache,
490 PyGIArgCache *arg_cache,
494 PyObject *py_obj = NULL;
495 PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;
496 PyGIArgGArray *array_cache = (PyGIArgGArray *)arg_cache;
497 gsize processed_items = 0;
499 /* GArrays make it easier to iterate over arrays
500 * with different element sizes but requires that
501 * we allocate a GArray if the argument was a C array
503 if (array_cache->array_type == GI_ARRAY_TYPE_C) {
505 if (array_cache->fixed_size >= 0) {
506 g_assert(arg->v_pointer != NULL);
507 len = array_cache->fixed_size;
508 } else if (array_cache->is_zero_terminated) {
509 if (arg->v_pointer == NULL) {
511 } else if (seq_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8) {
512 len = strlen (arg->v_pointer);
514 len = g_strv_length ((gchar **)arg->v_pointer);
517 GIArgument *len_arg = &state->arg_values[array_cache->len_arg_index];
518 PyGIArgCache *arg_cache = _pygi_callable_cache_get_arg (callable_cache,
519 array_cache->len_arg_index);
521 if (!gi_argument_to_gsize (len_arg, &len, arg_cache->type_tag)) {
526 array_ = g_array_new (FALSE,
528 array_cache->item_size);
529 if (array_ == NULL) {
532 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING && arg->v_pointer != NULL)
533 g_free (arg->v_pointer);
538 if (array_->data != NULL)
539 g_free (array_->data);
540 array_->data = arg->v_pointer;
543 array_ = arg->v_pointer;
546 if (seq_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8) {
547 if (arg->v_pointer == NULL) {
548 py_obj = PYGLIB_PyBytes_FromString ("");
550 py_obj = PYGLIB_PyBytes_FromStringAndSize (array_->data, array_->len);
553 if (arg->v_pointer == NULL) {
554 py_obj = PyList_New (0);
559 PyGIMarshalToPyFunc item_to_py_marshaller;
560 PyGIArgCache *item_arg_cache;
562 py_obj = PyList_New (array_->len);
567 item_arg_cache = seq_cache->item_cache;
568 item_to_py_marshaller = item_arg_cache->to_py_marshaller;
570 item_size = g_array_get_element_size (array_);
572 for (i = 0; i < array_->len; i++) {
573 GIArgument item_arg = {0};
576 /* If we are receiving an array of pointers, simply assign the pointer
577 * and move on, letting the per-item marshaler deal with the
578 * various transfer modes and ref counts (e.g. g_variant_ref_sink).
580 if (array_cache->array_type == GI_ARRAY_TYPE_PTR_ARRAY) {
581 item_arg.v_pointer = g_ptr_array_index ( ( GPtrArray *)array_, i);
583 } else if (item_arg_cache->is_pointer) {
584 item_arg.v_pointer = g_array_index (array_, gpointer, i);
586 } else if (item_arg_cache->type_tag == GI_TYPE_TAG_INTERFACE) {
587 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *) item_arg_cache;
589 /* FIXME: This probably doesn't work with boxed types or gvalues.
590 * See fx. _pygi_marshal_from_py_array() */
591 switch (g_base_info_get_type (iface_cache->interface_info)) {
592 case GI_INFO_TYPE_STRUCT:
593 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING &&
594 !g_type_is_a (iface_cache->g_type, G_TYPE_BOXED)) {
595 /* array elements are structs */
596 gpointer *_struct = g_malloc (item_size);
597 memcpy (_struct, array_->data + i * item_size,
599 item_arg.v_pointer = _struct;
601 item_arg.v_pointer = array_->data + i * item_size;
605 item_arg.v_pointer = g_array_index (array_, gpointer, i);
609 memcpy (&item_arg, array_->data + i * item_size, item_size);
612 py_item = item_to_py_marshaller ( state,
617 if (py_item == NULL) {
620 if (array_cache->array_type == GI_ARRAY_TYPE_C)
621 g_array_unref (array_);
625 PyList_SET_ITEM (py_obj, i, py_item);
631 if (array_cache->array_type == GI_ARRAY_TYPE_C)
632 g_array_free (array_, FALSE);
637 if (array_cache->array_type == GI_ARRAY_TYPE_C) {
638 g_array_free (array_, arg_cache->transfer == GI_TRANSFER_EVERYTHING);
640 /* clean up unprocessed items */
641 if (seq_cache->item_cache->to_py_cleanup != NULL) {
643 PyGIMarshalCleanupFunc cleanup_func = seq_cache->item_cache->to_py_cleanup;
644 for (j = processed_items; j < array_->len; j++) {
646 seq_cache->item_cache,
648 g_array_index (array_, gpointer, j),
653 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
654 g_array_free (array_, TRUE);
661 _wrap_c_array (PyGIInvokeState *state,
662 PyGIArgGArray *array_cache,
668 if (array_cache->fixed_size >= 0) {
669 len = array_cache->fixed_size;
670 } else if (array_cache->is_zero_terminated) {
671 len = g_strv_length ((gchar **)data);
672 } else if (array_cache->len_arg_index >= 0) {
673 GIArgument *len_arg = &state->arg_values[array_cache->len_arg_index];
674 len = len_arg->v_long;
677 array_ = g_array_new (FALSE,
679 array_cache->item_size);
684 g_free (array_->data);
692 _pygi_marshal_cleanup_to_py_array (PyGIInvokeState *state,
693 PyGIArgCache *arg_cache,
696 gboolean was_processed)
698 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING ||
699 arg_cache->transfer == GI_TRANSFER_CONTAINER) {
700 GArray *array_ = NULL;
701 GPtrArray *ptr_array_ = NULL;
702 PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache;
703 PyGIArgGArray *array_cache = (PyGIArgGArray *)arg_cache;
705 /* If this isn't a garray create one to help process variable sized
707 if (array_cache->array_type == GI_ARRAY_TYPE_C) {
708 array_ = _wrap_c_array (state, array_cache, data);
713 } else if (array_cache->array_type == GI_ARRAY_TYPE_PTR_ARRAY) {
714 ptr_array_ = (GPtrArray *) data;
716 array_ = (GArray *) data;
719 if (sequence_cache->item_cache->to_py_cleanup != NULL) {
721 guint len = (array_ != NULL) ? array_->len : ptr_array_->len;
723 PyGIMarshalCleanupFunc cleanup_func = sequence_cache->item_cache->to_py_cleanup;
724 for (i = 0; i < len; i++) {
726 sequence_cache->item_cache,
728 (array_ != NULL) ? g_array_index (array_, gpointer, i) : g_ptr_array_index (ptr_array_, i),
734 g_array_free (array_, TRUE);
736 g_ptr_array_free (ptr_array_, TRUE);
741 _array_cache_free_func (PyGIArgGArray *cache)
744 pygi_arg_cache_free (((PyGISequenceCache *)cache)->item_cache);
745 g_slice_free (PyGIArgGArray, cache);
750 pygi_arg_garray_len_arg_setup (PyGIArgCache *arg_cache,
751 GITypeInfo *type_info,
752 PyGICallableCache *callable_cache,
753 PyGIDirection direction,
755 gssize *py_arg_index)
757 PyGIArgGArray *seq_cache = (PyGIArgGArray *)arg_cache;
759 /* attempt len_arg_index setup for the first time */
760 if (seq_cache->len_arg_index < 0) {
761 seq_cache->len_arg_index = g_type_info_get_array_length (type_info);
763 /* offset by self arg for methods and vfuncs */
764 if (seq_cache->len_arg_index >= 0 && callable_cache != NULL &&
765 (callable_cache->function_type == PYGI_FUNCTION_TYPE_METHOD ||
766 callable_cache->function_type == PYGI_FUNCTION_TYPE_VFUNC)) {
767 seq_cache->len_arg_index += 1;
771 if (seq_cache->len_arg_index >= 0) {
772 PyGIArgCache *child_cache = NULL;
774 child_cache = _pygi_callable_cache_get_arg (callable_cache,
775 seq_cache->len_arg_index);
776 if (child_cache == NULL) {
777 child_cache = pygi_arg_cache_alloc ();
779 /* If the "length" arg cache already exists (the length comes before
780 * the array in the argument list), remove it from the to_py_args list
781 * because it does not belong in "to python" return tuple. The length
782 * will implicitly be a part of the returned Python list.
784 if (direction & PYGI_DIRECTION_TO_PYTHON) {
785 callable_cache->to_py_args =
786 g_slist_remove (callable_cache->to_py_args, child_cache);
789 /* This is a case where the arg cache already exists and has been
790 * setup by another array argument sharing the same length argument.
791 * See: gi_marshalling_tests_multi_array_key_value_in
793 if (child_cache->meta_type == PYGI_META_ARG_TYPE_CHILD)
797 /* There is a length argument for this array, so increment the number
798 * of "to python" child arguments when applicable.
800 if (direction & PYGI_DIRECTION_TO_PYTHON)
801 callable_cache->n_to_py_child_args++;
803 child_cache->meta_type = PYGI_META_ARG_TYPE_CHILD;
804 child_cache->direction = direction;
805 child_cache->to_py_marshaller = NULL;
806 child_cache->from_py_marshaller = NULL;
808 /* ugly edge case code:
810 * When the length comes before the array parameter we need to update
811 * indexes of arguments after the index argument.
813 if (seq_cache->len_arg_index < arg_index && direction & PYGI_DIRECTION_FROM_PYTHON) {
815 (*py_arg_index) -= 1;
816 callable_cache->n_py_args -= 1;
818 for (i = seq_cache->len_arg_index + 1;
819 i < _pygi_callable_cache_args_len (callable_cache); i++) {
820 PyGIArgCache *update_cache = _pygi_callable_cache_get_arg (callable_cache, i);
821 if (update_cache == NULL)
824 update_cache->py_arg_index -= 1;
828 _pygi_callable_cache_set_arg (callable_cache, seq_cache->len_arg_index, child_cache);
836 pygi_arg_garray_setup (PyGIArgGArray *sc,
837 GITypeInfo *type_info,
838 GIArgInfo *arg_info, /* may be NULL for return arguments */
840 PyGIDirection direction)
842 GITypeInfo *item_type_info;
843 PyGIArgCache *arg_cache = (PyGIArgCache *)sc;
845 if (!pygi_arg_sequence_setup ((PyGISequenceCache *)sc,
853 ((PyGIArgCache *)sc)->destroy_notify = (GDestroyNotify)_array_cache_free_func;
854 sc->array_type = g_type_info_get_array_type (type_info);
855 sc->is_zero_terminated = g_type_info_is_zero_terminated (type_info);
856 sc->fixed_size = g_type_info_get_array_fixed_size (type_info);
857 sc->len_arg_index = -1; /* setup by pygi_arg_garray_len_arg_setup */
859 item_type_info = g_type_info_get_param_type (type_info, 0);
860 sc->item_size = _pygi_g_type_info_size (item_type_info);
861 g_base_info_unref ( (GIBaseInfo *)item_type_info);
863 if (direction & PYGI_DIRECTION_FROM_PYTHON) {
864 arg_cache->from_py_marshaller = _pygi_marshal_from_py_array;
865 arg_cache->from_py_cleanup = _pygi_marshal_cleanup_from_py_array;
868 if (direction & PYGI_DIRECTION_TO_PYTHON) {
869 arg_cache->to_py_marshaller = _pygi_marshal_to_py_array;
870 arg_cache->to_py_cleanup = _pygi_marshal_cleanup_to_py_array;
877 pygi_arg_garray_new_from_info (GITypeInfo *type_info,
880 PyGIDirection direction)
882 PyGIArgGArray *array_cache = g_slice_new0 (PyGIArgGArray);
883 if (array_cache == NULL)
886 if (!pygi_arg_garray_setup (array_cache,
891 pygi_arg_cache_free ( (PyGIArgCache *)array_cache);
895 return (PyGIArgCache *)array_cache;