Imported Upstream version 3.21.91
[platform/upstream/python-gobject.git] / gi / pygi-struct-marshal.c
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2  * vim: tabstop=4 shiftwidth=4 expandtab
3  *
4  * Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>
5  * Copyright (C) 2014 Simon Feltman <sfeltman@gnome.org>
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include <Python.h>
22 #include <glib.h>
23 #include <pyglib-python-compat.h>
24
25 #include "pygi-struct-marshal.h"
26 #include "pygi-struct.h"
27 #include "pygi-foreign.h"
28 #include "pygi-value.h"
29 #include "pygi-type.h"
30 #include "pygi-boxed.h"
31 #include "pygi-info.h"
32 #include "pygpointer.h"
33 #include "pygboxed.h"
34 #include "pygtype.h"
35
36 /*
37  * _is_union_member - check to see if the py_arg is actually a member of the
38  * expected C union
39  */
40 static gboolean
41 _is_union_member (GIInterfaceInfo *interface_info, PyObject *py_arg) {
42     gint i;
43     gint n_fields;
44     GIUnionInfo *union_info;
45     GIInfoType info_type;
46     gboolean is_member = FALSE;
47
48     info_type = g_base_info_get_type (interface_info);
49
50     if (info_type != GI_INFO_TYPE_UNION)
51         return FALSE;
52
53     union_info = (GIUnionInfo *) interface_info;
54     n_fields = g_union_info_get_n_fields (union_info);
55
56     for (i = 0; i < n_fields; i++) {
57         GIFieldInfo *field_info;
58         GITypeInfo *field_type_info;
59
60         field_info = g_union_info_get_field (union_info, i);
61         field_type_info = g_field_info_get_type (field_info);
62
63         /* we can only check if the members are interfaces */
64         if (g_type_info_get_tag (field_type_info) == GI_TYPE_TAG_INTERFACE) {
65             GIInterfaceInfo *field_iface_info;
66             PyObject *py_type;
67
68             field_iface_info = g_type_info_get_interface (field_type_info);
69             py_type = _pygi_type_import_by_gi_info ((GIBaseInfo *) field_iface_info);
70
71             if (py_type != NULL && PyObject_IsInstance (py_arg, py_type)) {
72                 is_member = TRUE;
73             }
74
75             Py_XDECREF (py_type);
76             g_base_info_unref ( ( GIBaseInfo *) field_iface_info);
77         }
78
79         g_base_info_unref ( ( GIBaseInfo *) field_type_info);
80         g_base_info_unref ( ( GIBaseInfo *) field_info);
81
82         if (is_member)
83             break;
84     }
85
86     return is_member;
87 }
88
89
90 /*
91  * GValue from Python
92  */
93
94 /* pygi_arg_gvalue_from_py_marshal:
95  * py_arg: (in):
96  * arg: (out):
97  * transfer:
98  * copy_reference: TRUE if arg should use the pointer reference held by py_arg
99  *                 when it is already holding a GValue vs. copying the value.
100  */
101 gboolean
102 pygi_arg_gvalue_from_py_marshal (PyObject *py_arg,
103                                  GIArgument *arg,
104                                  GITransfer transfer,
105                                  gboolean copy_reference) {
106     GValue *value;
107     GType object_type;
108
109     object_type = pyg_type_from_object_strict ( (PyObject *) py_arg->ob_type, FALSE);
110     if (object_type == G_TYPE_INVALID) {
111         PyErr_SetString (PyExc_RuntimeError, "unable to retrieve object's GType");
112         return FALSE;
113     }
114
115     /* if already a gvalue, use that, else marshal into gvalue */
116     if (object_type == G_TYPE_VALUE) {
117         GValue *source_value = pyg_boxed_get (py_arg, GValue);
118         if (copy_reference) {
119             value = source_value;
120         } else {
121             value = g_slice_new0 (GValue);
122             g_value_init (value, G_VALUE_TYPE (source_value));
123             g_value_copy (source_value, value);
124         }
125     } else {
126         value = g_slice_new0 (GValue);
127         g_value_init (value, object_type);
128         if (pyg_value_from_pyobject (value, py_arg) < 0) {
129             g_slice_free (GValue, value);
130             PyErr_SetString (PyExc_RuntimeError, "PyObject conversion to GValue failed");
131             return FALSE;
132         }
133     }
134
135     arg->v_pointer = value;
136     return TRUE;
137 }
138
139 void
140 pygi_arg_gvalue_from_py_cleanup (PyGIInvokeState *state,
141                                  PyGIArgCache    *arg_cache,
142                                  PyObject        *py_arg,
143                                  gpointer         data,
144                                  gboolean         was_processed)
145 {
146     /* Note py_arg can be NULL for hash table which is a bug. */
147     if (was_processed && py_arg != NULL) {
148         GType py_object_type =
149             pyg_type_from_object_strict ( (PyObject *) py_arg->ob_type, FALSE);
150
151         /* When a GValue was not passed, it means the marshalers created a new
152          * one to pass in, clean this up.
153          */
154         if (py_object_type != G_TYPE_VALUE) {
155             g_value_unset ((GValue *) data);
156             g_slice_free (GValue, data);
157         }
158     }
159 }
160
161 /* pygi_arg_gclosure_from_py_marshal:
162  * py_arg: (in):
163  * arg: (out):
164  */
165 static gboolean
166 pygi_arg_gclosure_from_py_marshal (PyObject   *py_arg,
167                                    GIArgument *arg,
168                                    GITransfer  transfer)
169 {
170     GClosure *closure;
171     GType object_gtype = pyg_type_from_object_strict (py_arg, FALSE);
172
173     if ( !(PyCallable_Check(py_arg) ||
174            g_type_is_a (object_gtype, G_TYPE_CLOSURE))) {
175         PyErr_Format (PyExc_TypeError, "Must be callable, not %s",
176                       py_arg->ob_type->tp_name);
177         return FALSE;
178     }
179
180     if (g_type_is_a (object_gtype, G_TYPE_CLOSURE)) {
181         closure = (GClosure *)pyg_boxed_get (py_arg, void);
182         /* Make sure we own a ref which is held until cleanup. */
183         if (closure != NULL) {
184             g_closure_ref (closure);
185         }
186     } else {
187         closure = pyg_closure_new (py_arg, NULL, NULL);
188         g_closure_ref (closure);
189         g_closure_sink (closure);
190     }
191
192     if (closure == NULL) {
193         PyErr_SetString (PyExc_RuntimeError, "PyObject conversion to GClosure failed");
194         return FALSE;
195     }
196
197     /* Add an additional ref when transfering everything to the callee. */
198     if (transfer == GI_TRANSFER_EVERYTHING) {
199         g_closure_ref (closure);
200     }
201
202     arg->v_pointer = closure;
203     return TRUE;
204 }
205
206 static void
207 arg_gclosure_from_py_cleanup (PyGIInvokeState *state,
208                               PyGIArgCache    *arg_cache,
209                               PyObject        *py_arg,
210                               gpointer         cleanup_data,
211                               gboolean         was_processed)
212 {
213     if (cleanup_data != NULL) {
214         g_closure_unref (cleanup_data);
215     }
216 }
217
218 /* pygi_arg_struct_from_py_marshal:
219  *
220  * Dispatcher to various sub marshalers
221  */
222 gboolean
223 pygi_arg_struct_from_py_marshal (PyObject *py_arg,
224                                  GIArgument *arg,
225                                  const gchar *arg_name,
226                                  GIBaseInfo *interface_info,
227                                  GType g_type,
228                                  PyObject *py_type,
229                                  GITransfer transfer,
230                                  gboolean copy_reference,
231                                  gboolean is_foreign,
232                                  gboolean is_pointer)
233 {
234     gboolean is_union = FALSE;
235
236     if (py_arg == Py_None) {
237         arg->v_pointer = NULL;
238         return TRUE;
239     }
240
241     /* FIXME: handle this large if statement in the cache
242      *        and set the correct marshaller
243      */
244
245     if (g_type_is_a (g_type, G_TYPE_CLOSURE)) {
246         return pygi_arg_gclosure_from_py_marshal (py_arg, arg, transfer);
247     } else if (g_type_is_a (g_type, G_TYPE_VALUE)) {
248         return pygi_arg_gvalue_from_py_marshal(py_arg,
249                                                arg,
250                                                transfer,
251                                                copy_reference);
252     } else if (is_foreign) {
253         PyObject *success;
254         success = pygi_struct_foreign_convert_to_g_argument (py_arg,
255                                                              interface_info,
256                                                              transfer,
257                                                              arg);
258
259         return (success == Py_None);
260     } else if (!PyObject_IsInstance (py_arg, py_type)) {
261         /* first check to see if this is a member of the expected union */
262         is_union = _is_union_member (interface_info, py_arg);
263         if (!is_union) {
264             goto type_error;
265         }
266     }
267
268     if (g_type_is_a (g_type, G_TYPE_BOXED)) {
269         /* Additionally use pyg_type_from_object to pull the stashed __gtype__
270          * attribute off of the input argument for type checking. This is needed
271          * to work around type discrepancies in cases with aliased (typedef) types.
272          * e.g. GtkAllocation, GdkRectangle.
273          * See: https://bugzilla.gnomethere are .org/show_bug.cgi?id=707140
274          */
275         if (is_union || pyg_boxed_check (py_arg, g_type) ||
276                 g_type_is_a (pyg_type_from_object (py_arg), g_type)) {
277             arg->v_pointer = pyg_boxed_get (py_arg, void);
278             if (transfer == GI_TRANSFER_EVERYTHING) {
279                 arg->v_pointer = g_boxed_copy (g_type, arg->v_pointer);
280             }
281         } else {
282             goto type_error;
283         }
284
285     } else if (g_type_is_a (g_type, G_TYPE_POINTER) ||
286                g_type_is_a (g_type, G_TYPE_VARIANT) ||
287                g_type  == G_TYPE_NONE) {
288         g_warn_if_fail (g_type_is_a (g_type, G_TYPE_VARIANT) || !is_pointer || transfer == GI_TRANSFER_NOTHING);
289
290         if (g_type_is_a (g_type, G_TYPE_VARIANT) &&
291                 pyg_type_from_object (py_arg) != G_TYPE_VARIANT) {
292             PyErr_SetString (PyExc_TypeError, "expected GLib.Variant");
293             return FALSE;
294         }
295         arg->v_pointer = pyg_pointer_get (py_arg, void);
296         if (transfer == GI_TRANSFER_EVERYTHING) {
297             g_variant_ref ((GVariant *)arg->v_pointer);
298         }
299
300     } else {
301         PyErr_Format (PyExc_NotImplementedError,
302                       "structure type '%s' is not supported yet",
303                       g_type_name(g_type));
304         return FALSE;
305     }
306     return TRUE;
307
308 type_error:
309     {
310         gchar *type_name = _pygi_g_base_info_get_fullname (interface_info);
311         PyObject *module = PyObject_GetAttrString(py_arg, "__module__");
312
313         PyErr_Format (PyExc_TypeError, "argument %s: Expected %s, but got %s%s%s",
314                       arg_name ? arg_name : "self",
315                       type_name,
316                       module ? PYGLIB_PyUnicode_AsString(module) : "",
317                       module ? "." : "",
318                       py_arg->ob_type->tp_name);
319         if (module)
320             Py_DECREF (module);
321         g_free (type_name);
322         return FALSE;
323     }
324 }
325
326 static gboolean
327 arg_struct_from_py_marshal_adapter (PyGIInvokeState   *state,
328                                     PyGICallableCache *callable_cache,
329                                     PyGIArgCache      *arg_cache,
330                                     PyObject          *py_arg,
331                                     GIArgument        *arg,
332                                     gpointer          *cleanup_data)
333 {
334     PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
335
336     gboolean res =  pygi_arg_struct_from_py_marshal (py_arg,
337                                                      arg,
338                                                      arg_cache->arg_name,
339                                                      iface_cache->interface_info,
340                                                      iface_cache->g_type,
341                                                      iface_cache->py_type,
342                                                      arg_cache->transfer,
343                                                      TRUE, /*copy_reference*/
344                                                      iface_cache->is_foreign,
345                                                      arg_cache->is_pointer);
346
347     /* Assume struct marshaling is always a pointer and assign cleanup_data
348      * here rather than passing it further down the chain.
349      */
350     *cleanup_data = arg->v_pointer;
351     return res;
352 }
353
354 static void
355 arg_foreign_from_py_cleanup (PyGIInvokeState *state,
356                              PyGIArgCache    *arg_cache,
357                              PyObject        *py_arg,
358                              gpointer         data,
359                              gboolean         was_processed)
360 {
361     if (state->failed && was_processed) {
362         pygi_struct_foreign_release (
363             ( (PyGIInterfaceCache *)arg_cache)->interface_info,
364             data);
365     }
366 }
367
368
369 PyObject *
370 pygi_arg_struct_to_py_marshal (GIArgument *arg,
371                                GIInterfaceInfo *interface_info,
372                                GType g_type,
373                                PyObject *py_type,
374                                GITransfer transfer,
375                                gboolean is_allocated,
376                                gboolean is_foreign)
377 {
378     PyObject *py_obj = NULL;
379
380     if (arg->v_pointer == NULL) {
381         Py_RETURN_NONE;
382     }
383
384     if (g_type_is_a (g_type, G_TYPE_VALUE)) {
385         py_obj = pyg_value_as_pyobject (arg->v_pointer, FALSE);
386     } else if (is_foreign) {
387         py_obj = pygi_struct_foreign_convert_from_g_argument (interface_info,
388                                                               transfer,
389                                                               arg->v_pointer);
390     } else if (g_type_is_a (g_type, G_TYPE_BOXED)) {
391         if (py_type) {
392             /* Force a boxed copy if we are not transfered ownership and the
393              * memory is not caller allocated. */
394             py_obj = _pygi_boxed_new ((PyTypeObject *) py_type,
395                                       arg->v_pointer,
396                                       transfer == GI_TRANSFER_NOTHING && !is_allocated,
397                                       is_allocated ?
398                                               g_struct_info_get_size(interface_info) : 0);
399         }
400     } else if (g_type_is_a (g_type, G_TYPE_POINTER)) {
401         if (py_type == NULL ||
402                 !PyType_IsSubtype ((PyTypeObject *) py_type, &PyGIStruct_Type)) {
403             g_warn_if_fail (transfer == GI_TRANSFER_NOTHING);
404             py_obj = pyg_pointer_new (g_type, arg->v_pointer);
405         } else {
406             py_obj = _pygi_struct_new ( (PyTypeObject *) py_type,
407                                        arg->v_pointer,
408                                        transfer == GI_TRANSFER_EVERYTHING);
409         }
410     } else if (g_type_is_a (g_type, G_TYPE_VARIANT)) {
411         /* Note: sink the variant (add a ref) only if we are not transfered ownership.
412          * GLib.Variant overrides __del__ which will then call "g_variant_unref" for
413          * cleanup in either case. */
414         if (py_type) {
415             if (transfer == GI_TRANSFER_NOTHING) {
416                 g_variant_ref_sink (arg->v_pointer);
417             }
418             py_obj = _pygi_struct_new ((PyTypeObject *) py_type,
419                                        arg->v_pointer,
420                                        FALSE);
421         }
422     } else if (g_type == G_TYPE_NONE) {
423         if (py_type) {
424             py_obj = _pygi_struct_new ((PyTypeObject *) py_type,
425                                        arg->v_pointer,
426                                        transfer == GI_TRANSFER_EVERYTHING || is_allocated);
427         }
428     } else {
429         PyErr_Format (PyExc_NotImplementedError,
430                       "structure type '%s' is not supported yet",
431                       g_type_name (g_type));
432     }
433
434     return py_obj;
435 }
436
437 static PyObject *
438 arg_struct_to_py_marshal_adapter (PyGIInvokeState   *state,
439                                   PyGICallableCache *callable_cache,
440                                   PyGIArgCache      *arg_cache,
441                                   GIArgument        *arg)
442 {
443     PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
444
445     return pygi_arg_struct_to_py_marshal (arg,
446                                           iface_cache->interface_info,
447                                           iface_cache->g_type,
448                                           iface_cache->py_type,
449                                           arg_cache->transfer,
450                                           arg_cache->is_caller_allocates,
451                                           iface_cache->is_foreign);
452 }
453
454 static PyObject *
455 arg_boxed_to_py_marshal_pass_by_ref (PyGIInvokeState   *state,
456                                      PyGICallableCache *callable_cache,
457                                      PyGIArgCache      *arg_cache,
458                                      GIArgument        *arg)
459 {
460     PyObject *py_obj = NULL;
461     PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
462
463     if (arg->v_pointer == NULL) {
464         Py_RETURN_NONE;
465     }
466
467     if (g_type_is_a (iface_cache->g_type, G_TYPE_BOXED)) {
468         if (iface_cache->py_type) {
469             py_obj = _pygi_boxed_new ((PyTypeObject *) iface_cache->py_type,
470                                       arg->v_pointer,
471                                       FALSE, /* copy_boxed */
472                                       0);    /* slice_alloc */
473             ((PyGBoxed *)py_obj)->free_on_dealloc = FALSE;
474         }
475     } else {
476         PyErr_Format (PyExc_NotImplementedError,
477                       "expected boxed type but got %s",
478                       g_type_name (iface_cache->g_type));
479     }
480
481     return py_obj;
482 }
483
484 static void
485 arg_foreign_to_py_cleanup (PyGIInvokeState *state,
486                            PyGIArgCache    *arg_cache,
487                            PyObject        *dummy,
488                            gpointer         data,
489                            gboolean         was_processed)
490 {
491     if (!was_processed && arg_cache->transfer == GI_TRANSFER_EVERYTHING) {
492         pygi_struct_foreign_release (
493             ( (PyGIInterfaceCache *)arg_cache)->interface_info,
494             data);
495     }
496 }
497
498 static gboolean
499 arg_type_class_from_py_marshal (PyGIInvokeState   *state,
500                                 PyGICallableCache *callable_cache,
501                                 PyGIArgCache      *arg_cache,
502                                 PyObject          *py_arg,
503                                 GIArgument        *arg,
504                                 gpointer          *cleanup_data)
505 {
506     GType gtype = pyg_type_from_object (py_arg);
507
508     if (G_TYPE_IS_CLASSED (gtype)) {
509         arg->v_pointer = g_type_class_ref (gtype);
510         *cleanup_data = arg->v_pointer;
511         return TRUE;
512     } else {
513         PyErr_Format (PyExc_TypeError,
514                       "Unable to retrieve a GObject type class from \"%s\".",
515                       Py_TYPE(py_arg)->tp_name);
516         return FALSE;
517     }
518 }
519
520 static void
521 arg_type_class_from_py_cleanup (PyGIInvokeState *state,
522                                 PyGIArgCache    *arg_cache,
523                                 PyObject        *py_arg,
524                                 gpointer         data,
525                                 gboolean         was_processed)
526 {
527     if (was_processed) {
528         g_type_class_unref (data);
529     }
530 }
531
532 static void
533 arg_struct_from_py_setup (PyGIArgCache     *arg_cache,
534                           GIInterfaceInfo  *iface_info,
535                           GITransfer        transfer)
536 {
537     PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
538
539     if (g_struct_info_is_gtype_struct ((GIStructInfo*)iface_info)) {
540         arg_cache->from_py_marshaller = arg_type_class_from_py_marshal;
541         /* Since we always add a ref in the marshalling, only unref the
542          * GTypeClass when we don't transfer ownership. */
543         if (transfer == GI_TRANSFER_NOTHING) {
544             arg_cache->from_py_cleanup = arg_type_class_from_py_cleanup;
545         }
546
547     } else {
548         arg_cache->from_py_marshaller = arg_struct_from_py_marshal_adapter;
549
550         if (g_type_is_a (iface_cache->g_type, G_TYPE_CLOSURE)) {
551             arg_cache->from_py_cleanup = arg_gclosure_from_py_cleanup;
552
553         } else if (iface_cache->g_type == G_TYPE_VALUE) {
554             arg_cache->from_py_cleanup = pygi_arg_gvalue_from_py_cleanup;
555
556         } else if (iface_cache->is_foreign) {
557             arg_cache->from_py_cleanup = arg_foreign_from_py_cleanup;
558         }
559     }
560 }
561
562 static void
563 arg_struct_to_py_setup (PyGIArgCache     *arg_cache,
564                         GIInterfaceInfo  *iface_info,
565                         GITransfer        transfer,
566                         GIArgInfo        *arg_info)
567 {
568     PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
569
570     /* HACK to force GtkTreeModel:iter_next() and iter_previous() vfunc implementations
571      * to receive their Gtk.TreeIter argument as pass-by-reference. We create a new
572      * PyGIBoxed wrapper which does not copy the memory and also does not free it.
573      * This is needed to hack the noted vfunc implementations so they can continue
574      * working with bug https://bugzilla.gnome.org/show_bug.cgi?id=722899
575      * being fixed. This hack should be removed once GTK+ has fixed bug
576      * https://bugzilla.gnome.org/show_bug.cgi?id=734465
577      * and we've moved to a new major version.
578      */
579     if (arg_info && g_strcmp0 (iface_cache->type_name, "Gtk.TreeIter") == 0) {
580
581         /* GICallbackInfo */
582         GIBaseInfo *info = g_base_info_get_container (arg_info);
583         if (info && g_base_info_get_type (info) == GI_INFO_TYPE_CALLBACK &&
584                 (g_strcmp0 (g_base_info_get_name (info), "iter_next") == 0 ||
585                  g_strcmp0 (g_base_info_get_name (info), "iter_previous") == 0)) {
586
587             /* GITypeInfo */
588             info = g_base_info_get_container (info);
589             if (info && g_base_info_get_type (info) == GI_INFO_TYPE_TYPE &&
590                     g_type_info_get_tag ((GITypeInfo *)info) == GI_TYPE_TAG_INTERFACE) {
591
592                 /* GIFieldInfo */
593                 info = g_base_info_get_container (info);
594                 if (info && g_base_info_get_type (info) == GI_INFO_TYPE_FIELD) {
595
596                     /* GIStructInfo */
597                     info = g_base_info_get_container (info);
598                     if (info && g_base_info_get_type (info) == GI_INFO_TYPE_STRUCT &&
599                             g_strcmp0 (g_base_info_get_name (info), "TreeModelIface") == 0) {
600                         arg_cache->to_py_marshaller = arg_boxed_to_py_marshal_pass_by_ref;
601                     }
602                 }
603             }
604         }
605     }
606
607     if (arg_cache->to_py_marshaller == NULL) {
608         arg_cache->to_py_marshaller = arg_struct_to_py_marshal_adapter;
609     }
610
611     if (iface_cache->is_foreign)
612         arg_cache->to_py_cleanup = arg_foreign_to_py_cleanup;
613 }
614
615 PyGIArgCache *
616 pygi_arg_struct_new_from_info (GITypeInfo      *type_info,
617                                GIArgInfo       *arg_info,
618                                GITransfer       transfer,
619                                PyGIDirection    direction,
620                                GIInterfaceInfo *iface_info)
621 {
622     PyGIArgCache *cache = NULL;
623     PyGIInterfaceCache *iface_cache;
624
625     cache = pygi_arg_interface_new_from_info (type_info,
626                                               arg_info,
627                                               transfer,
628                                               direction,
629                                               iface_info);
630     if (cache == NULL)
631         return NULL;
632
633     iface_cache = (PyGIInterfaceCache *)cache;
634     iface_cache->is_foreign = (g_base_info_get_type ((GIBaseInfo *) iface_info) == GI_INFO_TYPE_STRUCT) &&
635                               (g_struct_info_is_foreign ((GIStructInfo*) iface_info));
636
637     if (direction & PYGI_DIRECTION_FROM_PYTHON) {
638         arg_struct_from_py_setup (cache, iface_info, transfer);
639     }
640
641     if (direction & PYGI_DIRECTION_TO_PYTHON) {
642         arg_struct_to_py_setup (cache, iface_info, transfer, arg_info);
643     }
644
645     return cache;
646 }