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