Imported Upstream version 3.9.5
[platform/upstream/pygobject2.git] / gi / pygi-marshal-to-py.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>,  Red Hat, Inc.
5  *
6  *   pygi-marshal-from-py.c: functions for converting C types to PyObject
7  *
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.
12  *
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.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
21  * USA
22  */
23
24 #include "pygi-private.h"
25
26 #include <string.h>
27 #include <time.h>
28
29 #include <pyglib.h>
30 #include <pygobject.h>
31 #include <pyglib-python-compat.h>
32
33 #include "pygi-cache.h"
34 #include "pygi-marshal-cleanup.h"
35 #include "pygi-marshal-to-py.h"
36 #include "pygi-argument.h"
37
38 static gboolean
39 gi_argument_to_c_long (GIArgument *arg_in,
40                        long *c_long_out,
41                        GITypeTag type_tag)
42 {
43     switch (type_tag) {
44       case GI_TYPE_TAG_INT8:
45           *c_long_out = arg_in->v_int8;
46           return TRUE;
47       case GI_TYPE_TAG_UINT8:
48           *c_long_out = arg_in->v_uint8;
49           return TRUE;
50       case GI_TYPE_TAG_INT16:
51           *c_long_out = arg_in->v_int16;
52           return TRUE;
53       case GI_TYPE_TAG_UINT16:
54           *c_long_out = arg_in->v_uint16;
55           return TRUE;
56       case GI_TYPE_TAG_INT32:
57           *c_long_out = arg_in->v_int32;
58           return TRUE;
59       case GI_TYPE_TAG_UINT32:
60           *c_long_out = arg_in->v_uint32;
61           return TRUE;
62       case GI_TYPE_TAG_INT64:
63           *c_long_out = arg_in->v_int64;
64           return TRUE;
65       case GI_TYPE_TAG_UINT64:
66           *c_long_out = arg_in->v_uint64;
67           return TRUE;
68       default:
69           PyErr_Format (PyExc_TypeError,
70                         "Unable to marshal %s to C long",
71                         g_type_tag_to_string (type_tag));
72           return FALSE;
73     }
74 }
75
76 static gboolean
77 gi_argument_to_gsize (GIArgument *arg_in,
78                       gsize      *gsize_out,
79                       GITypeTag   type_tag)
80 {
81     switch (type_tag) {
82       case GI_TYPE_TAG_INT8:
83           *gsize_out = arg_in->v_int8;
84           return TRUE;
85       case GI_TYPE_TAG_UINT8:
86           *gsize_out = arg_in->v_uint8;
87           return TRUE;
88       case GI_TYPE_TAG_INT16:
89           *gsize_out = arg_in->v_int16;
90           return TRUE;
91       case GI_TYPE_TAG_UINT16:
92           *gsize_out = arg_in->v_uint16;
93           return TRUE;
94       case GI_TYPE_TAG_INT32:
95           *gsize_out = arg_in->v_int32;
96           return TRUE;
97       case GI_TYPE_TAG_UINT32:
98           *gsize_out = arg_in->v_uint32;
99           return TRUE;
100       case GI_TYPE_TAG_INT64:
101           *gsize_out = arg_in->v_int64;
102           return TRUE;
103       case GI_TYPE_TAG_UINT64:
104           *gsize_out = arg_in->v_uint64;
105           return TRUE;
106       default:
107           PyErr_Format (PyExc_TypeError,
108                         "Unable to marshal %s to gsize",
109                         g_type_tag_to_string (type_tag));
110           return FALSE;
111     }
112 }
113
114 PyObject *
115 _pygi_marshal_to_py_void (PyGIInvokeState   *state,
116                           PyGICallableCache *callable_cache,
117                           PyGIArgCache      *arg_cache,
118                           GIArgument        *arg)
119 {
120     PyObject *py_obj = NULL;
121     if (arg_cache->is_pointer) {
122         /* NOTE: This will change to interpret pointers as integer values
123          * by using the following:
124          * py_obj = PyLong_FromVoidPtr (arg->v_pointer);
125          * See: https://bugzilla.gnome.org/show_bug.cgi?id=688081
126          */
127         py_obj = arg->v_pointer;
128     } else {
129         py_obj = Py_None;
130     }
131
132     Py_XINCREF (py_obj);
133     return py_obj;
134 }
135
136 static PyObject *
137 _pygi_marshal_to_py_unichar (GIArgument *arg)
138 {
139     PyObject *py_obj = NULL;
140
141     /* Preserve the bidirectional mapping between 0 and "" */
142     if (arg->v_uint32 == 0) {
143         py_obj = PYGLIB_PyUnicode_FromString ("");
144     } else if (g_unichar_validate (arg->v_uint32)) {
145         gchar utf8[6];
146         gint bytes;
147
148         bytes = g_unichar_to_utf8 (arg->v_uint32, utf8);
149         py_obj = PYGLIB_PyUnicode_FromStringAndSize ((char*)utf8, bytes);
150     } else {
151         /* TODO: Convert the error to an exception. */
152         PyErr_Format (PyExc_TypeError,
153                       "Invalid unicode codepoint %" G_GUINT32_FORMAT,
154                       arg->v_uint32);
155     }
156
157     return py_obj;
158 }
159
160 static PyObject *
161 _pygi_marshal_to_py_utf8 (GIArgument *arg)
162 {
163     PyObject *py_obj = NULL;
164     if (arg->v_string == NULL) {
165         Py_RETURN_NONE;
166      }
167
168     py_obj = PYGLIB_PyUnicode_FromString (arg->v_string);
169     return py_obj;
170 }
171
172 static PyObject *
173 _pygi_marshal_to_py_filename (GIArgument *arg)
174 {
175     gchar *string = NULL;
176     PyObject *py_obj = NULL;
177     GError *error = NULL;
178
179     if (arg->v_string == NULL) {
180         Py_RETURN_NONE;
181     }
182
183     string = g_filename_to_utf8 (arg->v_string, -1, NULL, NULL, &error);
184     if (string == NULL) {
185         PyErr_SetString (PyExc_Exception, error->message);
186         /* TODO: Convert the error to an exception. */
187         return NULL;
188     }
189
190     py_obj = PYGLIB_PyUnicode_FromString (string);
191     g_free (string);
192
193     return py_obj;
194 }
195
196
197 /**
198  * _pygi_marshal_to_py_basic_type:
199  * @arg: The argument to convert to an object.
200  * @type_tag: Type tag for @arg
201  * @transfer: Transfer annotation
202  *
203  * Convert the given argument to a Python object. This function
204  * is restricted to simple types that only require the GITypeTag
205  * and GITransfer. For a more complete conversion routine, use:
206  * _pygi_argument_to_object.
207  *
208  * Returns: A PyObject representing @arg or NULL if it cannot convert
209  *          the argument.
210  */
211 PyObject *
212 _pygi_marshal_to_py_basic_type (GIArgument  *arg,
213                                  GITypeTag type_tag,
214                                  GITransfer transfer)
215 {
216     switch (type_tag) {
217         case GI_TYPE_TAG_BOOLEAN:
218             return PyBool_FromLong (arg->v_boolean);
219
220         case GI_TYPE_TAG_INT8:
221             return PYGLIB_PyLong_FromLong (arg->v_int8);
222
223         case GI_TYPE_TAG_UINT8:
224             return PYGLIB_PyLong_FromLong (arg->v_uint8);
225
226         case GI_TYPE_TAG_INT16:
227             return PYGLIB_PyLong_FromLong (arg->v_int16);
228
229         case GI_TYPE_TAG_UINT16:
230             return PYGLIB_PyLong_FromLong (arg->v_uint16);
231
232         case GI_TYPE_TAG_INT32:
233             return PYGLIB_PyLong_FromLong (arg->v_int32);
234
235         case GI_TYPE_TAG_UINT32:
236             return PyLong_FromLongLong (arg->v_uint32);
237
238         case GI_TYPE_TAG_INT64:
239             return PyLong_FromLongLong (arg->v_int64);
240
241         case GI_TYPE_TAG_UINT64:
242             return PyLong_FromUnsignedLongLong (arg->v_uint64);
243
244         case GI_TYPE_TAG_FLOAT:
245             return PyFloat_FromDouble (arg->v_float);
246
247         case GI_TYPE_TAG_DOUBLE:
248             return PyFloat_FromDouble (arg->v_double);
249
250         case GI_TYPE_TAG_GTYPE:
251             return pyg_type_wrapper_new ( (GType) arg->v_long);
252
253         case GI_TYPE_TAG_UNICHAR:
254             return _pygi_marshal_to_py_unichar (arg);
255
256         case GI_TYPE_TAG_UTF8:
257             return _pygi_marshal_to_py_utf8 (arg);
258
259         case GI_TYPE_TAG_FILENAME:
260             return _pygi_marshal_to_py_filename (arg);
261
262         default:
263             return NULL;
264     }
265     return NULL;
266 }
267
268 PyObject *
269 _pygi_marshal_to_py_basic_type_cache_adapter (PyGIInvokeState   *state,
270                                               PyGICallableCache *callable_cache,
271                                               PyGIArgCache      *arg_cache,
272                                               GIArgument        *arg)
273 {
274     return _pygi_marshal_to_py_basic_type (arg,
275                                             arg_cache->type_tag,
276                                             arg_cache->transfer);
277 }
278
279 PyObject *
280 _pygi_marshal_to_py_array (PyGIInvokeState   *state,
281                            PyGICallableCache *callable_cache,
282                            PyGIArgCache      *arg_cache,
283                            GIArgument        *arg)
284 {
285     GArray *array_;
286     PyObject *py_obj = NULL;
287     PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;
288     gsize processed_items = 0;
289
290      /* GArrays make it easier to iterate over arrays
291       * with different element sizes but requires that
292       * we allocate a GArray if the argument was a C array
293       */
294     if (seq_cache->array_type == GI_ARRAY_TYPE_C) {
295         gsize len;
296         if (seq_cache->fixed_size >= 0) {
297             g_assert(arg->v_pointer != NULL);
298             len = seq_cache->fixed_size;
299         } else if (seq_cache->is_zero_terminated) {
300             if (arg->v_pointer == NULL) {
301                 len = 0;
302             } else if (seq_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8) {
303                 len = strlen (arg->v_pointer);
304             } else {
305                 len = g_strv_length ((gchar **)arg->v_pointer);
306             }
307         } else {
308             GIArgument *len_arg = state->args[seq_cache->len_arg_index];
309
310             if (!gi_argument_to_gsize (len_arg,
311                                        &len,
312                                        callable_cache->args_cache[seq_cache->len_arg_index]->type_tag)) {
313                 return NULL;
314             }
315         }
316
317         array_ = g_array_new (FALSE,
318                               FALSE,
319                               seq_cache->item_size);
320         if (array_ == NULL) {
321             PyErr_NoMemory ();
322
323             if (arg_cache->transfer == GI_TRANSFER_EVERYTHING && arg->v_pointer != NULL)
324                 g_free (arg->v_pointer);
325
326             return NULL;
327         }
328
329         if (array_->data != NULL) 
330             g_free (array_->data);
331         array_->data = arg->v_pointer;
332         array_->len = len;
333     } else {
334         array_ = arg->v_pointer;
335     }
336
337     if (seq_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8) {
338         if (arg->v_pointer == NULL) {
339             py_obj = PYGLIB_PyBytes_FromString ("");
340         } else {
341             py_obj = PYGLIB_PyBytes_FromStringAndSize (array_->data, array_->len);
342         }
343     } else {
344         if (arg->v_pointer == NULL) {
345             py_obj = PyList_New (0);
346         } else {
347             int i;
348
349             gsize item_size;
350             PyGIMarshalToPyFunc item_to_py_marshaller;
351             PyGIArgCache *item_arg_cache;
352
353             py_obj = PyList_New (array_->len);
354             if (py_obj == NULL)
355                 goto err;
356
357
358             item_arg_cache = seq_cache->item_cache;
359             item_to_py_marshaller = item_arg_cache->to_py_marshaller;
360
361             item_size = g_array_get_element_size (array_);
362
363             for (i = 0; i < array_->len; i++) {
364                 GIArgument item_arg;
365                 PyObject *py_item;
366
367                 if (seq_cache->array_type == GI_ARRAY_TYPE_PTR_ARRAY) {
368                     item_arg.v_pointer = g_ptr_array_index ( ( GPtrArray *)array_, i);
369                 } else if (item_arg_cache->type_tag == GI_TYPE_TAG_INTERFACE) {
370                     PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *) item_arg_cache;
371                     gboolean is_gvariant = iface_cache->g_type == G_TYPE_VARIANT;
372
373                     // FIXME: This probably doesn't work with boxed types or gvalues. See fx. _pygi_marshal_from_py_array()
374                     switch (g_base_info_get_type (iface_cache->interface_info)) {
375                         case GI_INFO_TYPE_STRUCT:
376                             if (is_gvariant) {
377                               g_assert (item_size == sizeof (gpointer));
378                               if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
379                                 item_arg.v_pointer = g_variant_ref_sink (g_array_index (array_, gpointer, i));
380                               else
381                                 item_arg.v_pointer = g_array_index (array_, gpointer, i);
382                             } else if (arg_cache->transfer == GI_TRANSFER_EVERYTHING && !item_arg_cache->is_pointer &&
383                                        !g_type_is_a (iface_cache->g_type, G_TYPE_BOXED)) {
384                                 /* array elements are structs */
385                                 gpointer *_struct = g_malloc (item_size);
386                                 memcpy (_struct, array_->data + i * item_size,
387                                         item_size);
388                                 item_arg.v_pointer = _struct;
389                             } else if (item_arg_cache->is_pointer)
390                                 /* array elements are pointers to values */
391                                 item_arg.v_pointer = g_array_index (array_, gpointer, i);
392                             else
393                                 item_arg.v_pointer = array_->data + i * item_size;
394                             break;
395                         default:
396                             item_arg.v_pointer = g_array_index (array_, gpointer, i);
397                             break;
398                     }
399                 } else {
400                     memcpy (&item_arg, array_->data + i * item_size, item_size);
401                 }
402
403                 py_item = item_to_py_marshaller ( state,
404                                                 callable_cache,
405                                                 item_arg_cache,
406                                                 &item_arg);
407
408                 if (py_item == NULL) {
409                     Py_CLEAR (py_obj);
410
411                     if (seq_cache->array_type == GI_ARRAY_TYPE_C)
412                         g_array_unref (array_);
413
414                     goto err;
415                 }
416                 PyList_SET_ITEM (py_obj, i, py_item);
417                 processed_items++;
418             }
419         }
420     }
421
422     if (seq_cache->array_type == GI_ARRAY_TYPE_C)
423         g_array_free (array_, FALSE);
424
425     return py_obj;
426
427 err:
428     if (seq_cache->array_type == GI_ARRAY_TYPE_C) {
429         g_array_free (array_, arg_cache->transfer == GI_TRANSFER_EVERYTHING);
430     } else {
431         /* clean up unprocessed items */
432         if (seq_cache->item_cache->to_py_cleanup != NULL) {
433             int j;
434             PyGIMarshalCleanupFunc cleanup_func = seq_cache->item_cache->to_py_cleanup;
435             for (j = processed_items; j < array_->len; j++) {
436                 cleanup_func (state,
437                               seq_cache->item_cache,
438                               g_array_index (array_, gpointer, j),
439                               FALSE);
440             }
441         }
442
443         if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
444             g_array_free (array_, TRUE);
445     }
446
447     return NULL;
448 }
449
450 PyObject *
451 _pygi_marshal_to_py_glist (PyGIInvokeState   *state,
452                            PyGICallableCache *callable_cache,
453                            PyGIArgCache      *arg_cache,
454                            GIArgument        *arg)
455 {
456     GList *list_;
457     gsize length;
458     gsize i;
459
460     PyGIMarshalToPyFunc item_to_py_marshaller;
461     PyGIArgCache *item_arg_cache;
462     PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;
463
464     PyObject *py_obj = NULL;
465
466     list_ = arg->v_pointer;
467     length = g_list_length (list_);
468
469     py_obj = PyList_New (length);
470     if (py_obj == NULL)
471         return NULL;
472
473     item_arg_cache = seq_cache->item_cache;
474     item_to_py_marshaller = item_arg_cache->to_py_marshaller;
475
476     for (i = 0; list_ != NULL; list_ = g_list_next (list_), i++) {
477         GIArgument item_arg;
478         PyObject *py_item;
479
480         item_arg.v_pointer = list_->data;
481         _pygi_hash_pointer_to_arg (&item_arg, item_arg_cache->type_tag);
482         py_item = item_to_py_marshaller (state,
483                                          callable_cache,
484                                          item_arg_cache,
485                                          &item_arg);
486
487         if (py_item == NULL) {
488             Py_CLEAR (py_obj);
489             _PyGI_ERROR_PREFIX ("Item %zu: ", i);
490             return NULL;
491         }
492
493         PyList_SET_ITEM (py_obj, i, py_item);
494     }
495
496     return py_obj;
497 }
498
499 PyObject *
500 _pygi_marshal_to_py_gslist (PyGIInvokeState   *state,
501                             PyGICallableCache *callable_cache,
502                             PyGIArgCache      *arg_cache,
503                             GIArgument        *arg)
504 {
505     GSList *list_;
506     gsize length;
507     gsize i;
508
509     PyGIMarshalToPyFunc item_to_py_marshaller;
510     PyGIArgCache *item_arg_cache;
511     PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;
512
513     PyObject *py_obj = NULL;
514
515     list_ = arg->v_pointer;
516     length = g_slist_length (list_);
517
518     py_obj = PyList_New (length);
519     if (py_obj == NULL)
520         return NULL;
521
522     item_arg_cache = seq_cache->item_cache;
523     item_to_py_marshaller = item_arg_cache->to_py_marshaller;
524
525     for (i = 0; list_ != NULL; list_ = g_slist_next (list_), i++) {
526         GIArgument item_arg;
527         PyObject *py_item;
528
529         item_arg.v_pointer = list_->data;
530         _pygi_hash_pointer_to_arg (&item_arg, item_arg_cache->type_tag);
531         py_item = item_to_py_marshaller (state,
532                                         callable_cache,
533                                         item_arg_cache,
534                                         &item_arg);
535
536         if (py_item == NULL) {
537             Py_CLEAR (py_obj);
538             _PyGI_ERROR_PREFIX ("Item %zu: ", i);
539             return NULL;
540         }
541
542         PyList_SET_ITEM (py_obj, i, py_item);
543     }
544
545     return py_obj;
546 }
547
548 PyObject *
549 _pygi_marshal_to_py_ghash (PyGIInvokeState   *state,
550                            PyGICallableCache *callable_cache,
551                            PyGIArgCache      *arg_cache,
552                            GIArgument        *arg)
553 {
554     GHashTable *hash_;
555     GHashTableIter hash_table_iter;
556
557     PyGIMarshalToPyFunc key_to_py_marshaller;
558     PyGIMarshalToPyFunc value_to_py_marshaller;
559
560     PyGIArgCache *key_arg_cache;
561     PyGIArgCache *value_arg_cache;
562     PyGIHashCache *hash_cache = (PyGIHashCache *)arg_cache;
563
564     GIArgument key_arg;
565     GIArgument value_arg;
566
567     PyObject *py_obj = NULL;
568
569     hash_ = arg->v_pointer;
570
571     if (hash_ == NULL) {
572         py_obj = Py_None;
573         Py_INCREF (py_obj);
574         return py_obj;
575     }
576
577     py_obj = PyDict_New ();
578     if (py_obj == NULL)
579         return NULL;
580
581     key_arg_cache = hash_cache->key_cache;
582     key_to_py_marshaller = key_arg_cache->to_py_marshaller;
583
584     value_arg_cache = hash_cache->value_cache;
585     value_to_py_marshaller = value_arg_cache->to_py_marshaller;
586
587     g_hash_table_iter_init (&hash_table_iter, hash_);
588     while (g_hash_table_iter_next (&hash_table_iter,
589                                    &key_arg.v_pointer,
590                                    &value_arg.v_pointer)) {
591         PyObject *py_key;
592         PyObject *py_value;
593         int retval;
594
595
596         _pygi_hash_pointer_to_arg (&key_arg, hash_cache->key_cache->type_tag);
597         py_key = key_to_py_marshaller ( state,
598                                       callable_cache,
599                                       key_arg_cache,
600                                      &key_arg);
601
602         if (py_key == NULL) {
603             Py_CLEAR (py_obj);
604             return NULL;
605         }
606
607         _pygi_hash_pointer_to_arg (&value_arg, hash_cache->value_cache->type_tag);
608         py_value = value_to_py_marshaller ( state,
609                                           callable_cache,
610                                           value_arg_cache,
611                                          &value_arg);
612
613         if (py_value == NULL) {
614             Py_CLEAR (py_obj);
615             Py_DECREF(py_key);
616             return NULL;
617         }
618
619         retval = PyDict_SetItem (py_obj, py_key, py_value);
620
621         Py_DECREF (py_key);
622         Py_DECREF (py_value);
623
624         if (retval < 0) {
625             Py_CLEAR (py_obj);
626             return NULL;
627         }
628     }
629
630     return py_obj;
631 }
632
633 PyObject *
634 _pygi_marshal_to_py_gerror (PyGIInvokeState   *state,
635                             PyGICallableCache *callable_cache,
636                             PyGIArgCache      *arg_cache,
637                             GIArgument        *arg)
638 {
639     GError *error = arg->v_pointer;
640     PyObject *py_obj = NULL;
641
642     py_obj = pyglib_error_marshal(&error);
643
644     if (arg_cache->transfer == GI_TRANSFER_EVERYTHING && error != NULL) {
645         g_error_free (error);
646     }
647
648     if (py_obj != NULL) {
649         return py_obj;
650     } else {
651         Py_RETURN_NONE;
652     }
653 }
654
655 PyObject *
656 _pygi_marshal_to_py_interface_callback (PyGIInvokeState   *state,
657                                         PyGICallableCache *callable_cache,
658                                         PyGIArgCache      *arg_cache,
659                                         GIArgument        *arg)
660 {
661     PyObject *py_obj = NULL;
662
663     PyErr_Format (PyExc_NotImplementedError,
664                   "Marshalling a callback to PyObject is not supported");
665     return py_obj;
666 }
667
668 PyObject *
669 _pygi_marshal_to_py_interface_enum (PyGIInvokeState   *state,
670                                     PyGICallableCache *callable_cache,
671                                     PyGIArgCache      *arg_cache,
672                                     GIArgument        *arg)
673 {
674     PyObject *py_obj = NULL;
675     PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
676     GIBaseInfo *interface;
677     long c_long;
678
679     interface = g_type_info_get_interface (arg_cache->type_info);
680     g_assert (g_base_info_get_type (interface) == GI_INFO_TYPE_ENUM);
681
682     if (!gi_argument_to_c_long(arg, &c_long,
683                                g_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
684         return NULL;
685     }
686
687     if (iface_cache->g_type == G_TYPE_NONE) {
688         py_obj = PyObject_CallFunction (iface_cache->py_type, "l", c_long);
689     } else {
690         py_obj = pyg_enum_from_gtype (iface_cache->g_type, c_long);
691     }
692     g_base_info_unref (interface);
693     return py_obj;
694 }
695
696 PyObject *
697 _pygi_marshal_to_py_interface_flags (PyGIInvokeState   *state,
698                                      PyGICallableCache *callable_cache,
699                                      PyGIArgCache      *arg_cache,
700                                      GIArgument        *arg)
701 {
702     PyObject *py_obj = NULL;
703     PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
704     GIBaseInfo *interface;
705     long c_long;
706
707     interface = g_type_info_get_interface (arg_cache->type_info);
708     g_assert (g_base_info_get_type (interface) == GI_INFO_TYPE_FLAGS);
709
710     if (!gi_argument_to_c_long(arg, &c_long,
711                                g_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
712         g_base_info_unref (interface);
713         return NULL;
714     }
715
716     g_base_info_unref (interface);
717     if (iface_cache->g_type == G_TYPE_NONE) {
718         /* An enum with a GType of None is an enum without GType */
719
720         PyObject *py_type = _pygi_type_import_by_gi_info (iface_cache->interface_info);
721         PyObject *py_args = NULL;
722
723         if (!py_type)
724             return NULL;
725
726         py_args = PyTuple_New (1);
727         if (PyTuple_SetItem (py_args, 0, PyLong_FromLong (c_long)) != 0) {
728             Py_DECREF (py_args);
729             Py_DECREF (py_type);
730             return NULL;
731         }
732
733         py_obj = PyObject_CallFunction (py_type, "l", c_long);
734
735         Py_DECREF (py_args);
736         Py_DECREF (py_type);
737     } else {
738         py_obj = pyg_flags_from_gtype (iface_cache->g_type, c_long);
739     }
740
741     return py_obj;
742 }
743
744 PyObject *
745 _pygi_marshal_to_py_interface_struct_cache_adapter (PyGIInvokeState   *state,
746                                                     PyGICallableCache *callable_cache,
747                                                     PyGIArgCache      *arg_cache,
748                                                     GIArgument        *arg)
749 {
750     PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
751
752     return _pygi_marshal_to_py_interface_struct (arg,
753                                                  iface_cache->interface_info,
754                                                  iface_cache->g_type,
755                                                  iface_cache->py_type,
756                                                  arg_cache->transfer,
757                                                  arg_cache->is_caller_allocates,
758                                                  iface_cache->is_foreign);
759 }
760
761 PyObject *
762 _pygi_marshal_to_py_interface_interface (PyGIInvokeState   *state,
763                                          PyGICallableCache *callable_cache,
764                                          PyGIArgCache      *arg_cache,
765                                          GIArgument        *arg)
766 {
767     PyObject *py_obj = NULL;
768
769     PyErr_Format (PyExc_NotImplementedError,
770                   "Marshalling for this type is not implemented yet");
771     return py_obj;
772 }
773
774 PyObject *
775 _pygi_marshal_to_py_interface_boxed (PyGIInvokeState   *state,
776                                      PyGICallableCache *callable_cache,
777                                      PyGIArgCache      *arg_cache,
778                                      GIArgument        *arg)
779 {
780     PyObject *py_obj = NULL;
781
782     PyErr_Format (PyExc_NotImplementedError,
783                   "Marshalling for this type is not implemented yet");
784     return py_obj;
785 }
786
787 PyObject *
788 _pygi_marshal_to_py_interface_object_cache_adapter (PyGIInvokeState   *state,
789                                                     PyGICallableCache *callable_cache,
790                                                     PyGIArgCache      *arg_cache,
791                                                     GIArgument        *arg)
792 {
793     return _pygi_marshal_to_py_object(arg, arg_cache->transfer);
794 }
795
796 PyObject *
797 _pygi_marshal_to_py_interface_union  (PyGIInvokeState   *state,
798                                       PyGICallableCache *callable_cache,
799                                       PyGIArgCache      *arg_cache,
800                                       GIArgument        *arg)
801 {
802     PyObject *py_obj = NULL;
803
804     PyErr_Format (PyExc_NotImplementedError,
805                   "Marshalling for this type is not implemented yet");
806     return py_obj;
807 }
808
809 PyObject *
810 _pygi_marshal_to_py_object (GIArgument *arg, GITransfer transfer) {
811     PyObject *pyobj;
812
813     if (arg->v_pointer == NULL) {
814         pyobj = Py_None;
815         Py_INCREF (pyobj);
816
817     } else if (G_IS_PARAM_SPEC(arg->v_pointer)) {
818         pyobj = pyg_param_spec_new (arg->v_pointer);
819         if (transfer == GI_TRANSFER_EVERYTHING)
820             g_param_spec_unref (arg->v_pointer);
821
822     } else {
823          pyobj = pygobject_new_full (arg->v_pointer,
824                                      /*steal=*/ transfer == GI_TRANSFER_EVERYTHING,
825                                      /*type=*/  NULL);
826     }
827
828     return pyobj;
829 }
830
831 PyObject *
832 _pygi_marshal_to_py_interface_struct (GIArgument *arg,
833                                       GIInterfaceInfo *interface_info,
834                                       GType g_type,
835                                       PyObject *py_type,
836                                       GITransfer transfer,
837                                       gboolean is_allocated,
838                                       gboolean is_foreign)
839 {
840     PyObject *py_obj = NULL;
841
842     if (arg->v_pointer == NULL) {
843         Py_RETURN_NONE;
844     }
845
846     if (g_type_is_a (g_type, G_TYPE_VALUE)) {
847         py_obj = pyg_value_as_pyobject (arg->v_pointer, FALSE);
848     } else if (is_foreign) {
849         py_obj = pygi_struct_foreign_convert_from_g_argument (interface_info,
850                                                               arg->v_pointer);
851     } else if (g_type_is_a (g_type, G_TYPE_BOXED)) {
852         if (py_type) {
853             py_obj = _pygi_boxed_new ((PyTypeObject *) py_type,
854                                       arg->v_pointer,
855                                       transfer == GI_TRANSFER_EVERYTHING || is_allocated,
856                                       is_allocated ?
857                                               g_struct_info_get_size(interface_info) : 0);
858         }
859     } else if (g_type_is_a (g_type, G_TYPE_POINTER)) {
860         if (py_type == NULL ||
861                 !PyType_IsSubtype ((PyTypeObject *) py_type, &PyGIStruct_Type)) {
862             g_warn_if_fail (transfer == GI_TRANSFER_NOTHING);
863             py_obj = pyg_pointer_new (g_type, arg->v_pointer);
864         } else {
865             py_obj = _pygi_struct_new ( (PyTypeObject *) py_type,
866                                        arg->v_pointer,
867                                        transfer == GI_TRANSFER_EVERYTHING);
868         }
869     } else if (g_type_is_a (g_type, G_TYPE_VARIANT)) {
870         /* Note we do not use transfer for the structs free_on_dealloc because
871          * GLib.Variant overrides __del__ to call "g_variant_unref". */
872         if (py_type) {
873             g_variant_ref_sink (arg->v_pointer);
874             py_obj = _pygi_struct_new ((PyTypeObject *) py_type,
875                                        arg->v_pointer,
876                                        FALSE);
877         }
878     } else if (g_type == G_TYPE_NONE) {
879         if (py_type) {
880             py_obj = _pygi_struct_new ((PyTypeObject *) py_type,
881                                        arg->v_pointer,
882                                        transfer == GI_TRANSFER_EVERYTHING);
883         }
884     } else {
885         PyErr_Format (PyExc_NotImplementedError,
886                       "structure type '%s' is not supported yet",
887                       g_type_name (g_type));
888     }
889
890     return py_obj;
891 }