d5a07349e7e498563138831964d8f395eb73b01a
[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 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 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_obj = Py_None;
297         Py_INCREF (py_obj);
298         return py_obj;
299      }
300
301     py_obj = PYGLIB_PyUnicode_FromString (arg->v_string);
302     return py_obj;
303 }
304
305 PyObject *
306 _pygi_marshal_to_py_filename (PyGIInvokeState   *state,
307                               PyGICallableCache *callable_cache,
308                               PyGIArgCache      *arg_cache,
309                               GIArgument        *arg)
310 {
311     gchar *string;
312     PyObject *py_obj = NULL;
313     GError *error = NULL;
314
315     if (arg->v_string == NULL) {
316         py_obj = Py_None;
317         Py_INCREF (py_obj);
318         return py_obj;
319     }
320
321     string = g_filename_to_utf8 (arg->v_string, -1, NULL, NULL, &error);
322     if (string == NULL) {
323         PyErr_SetString (PyExc_Exception, error->message);
324         /* TODO: Convert the error to an exception. */
325         return NULL;
326     }
327
328     py_obj = PYGLIB_PyUnicode_FromString (string);
329     g_free (string);
330
331     return py_obj;
332 }
333
334 PyObject *
335 _pygi_marshal_to_py_array (PyGIInvokeState   *state,
336                            PyGICallableCache *callable_cache,
337                            PyGIArgCache      *arg_cache,
338                            GIArgument        *arg)
339 {
340     GArray *array_;
341     PyObject *py_obj = NULL;
342     PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;
343     gsize processed_items = 0;
344
345      /* GArrays make it easier to iterate over arrays
346       * with different element sizes but requires that
347       * we allocate a GArray if the argument was a C array
348       */
349     if (seq_cache->array_type == GI_ARRAY_TYPE_C) {
350         gsize len;
351         if (seq_cache->fixed_size >= 0) {
352             g_assert(arg->v_pointer != NULL);
353             len = seq_cache->fixed_size;
354         } else if (seq_cache->is_zero_terminated) {
355             if (arg->v_pointer == NULL) {
356                 len = 0;
357             } else if (seq_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8) {
358                 len = strlen (arg->v_pointer);
359             } else {
360                 len = g_strv_length ((gchar **)arg->v_pointer);
361             }
362         } else {
363             GIArgument *len_arg = state->args[seq_cache->len_arg_index];
364
365             if (!gi_argument_to_gsize (len_arg,
366                                        &len,
367                                        callable_cache->args_cache[seq_cache->len_arg_index]->type_tag)) {
368                 return NULL;
369             }
370         }
371
372         array_ = g_array_new (FALSE,
373                               FALSE,
374                               seq_cache->item_size);
375         if (array_ == NULL) {
376             PyErr_NoMemory ();
377
378             if (arg_cache->transfer == GI_TRANSFER_EVERYTHING && arg->v_pointer != NULL)
379                 g_free (arg->v_pointer);
380
381             return NULL;
382         }
383
384         if (array_->data != NULL) 
385             g_free (array_->data);
386         array_->data = arg->v_pointer;
387         array_->len = len;
388     } else {
389         array_ = arg->v_pointer;
390     }
391
392     if (seq_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8) {
393         if (arg->v_pointer == NULL) {
394             py_obj = PYGLIB_PyBytes_FromString ("");
395         } else {
396             py_obj = PYGLIB_PyBytes_FromStringAndSize (array_->data, array_->len);
397         }
398     } else {
399         if (arg->v_pointer == NULL) {
400             py_obj = PyList_New (0);
401         } else {
402             int i;
403
404             gsize item_size;
405             PyGIMarshalToPyFunc item_to_py_marshaller;
406             PyGIArgCache *item_arg_cache;
407
408             py_obj = PyList_New (array_->len);
409             if (py_obj == NULL)
410                 goto err;
411
412
413             item_arg_cache = seq_cache->item_cache;
414             item_to_py_marshaller = item_arg_cache->to_py_marshaller;
415
416             item_size = g_array_get_element_size (array_);
417
418             for (i = 0; i < array_->len; i++) {
419                 GIArgument item_arg;
420                 PyObject *py_item;
421
422                 if (seq_cache->array_type == GI_ARRAY_TYPE_PTR_ARRAY) {
423                     item_arg.v_pointer = g_ptr_array_index ( ( GPtrArray *)array_, i);
424                 } else if (item_arg_cache->type_tag == GI_TYPE_TAG_INTERFACE) {
425                     PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *) item_arg_cache;
426                     gboolean is_gvariant = iface_cache->g_type == G_TYPE_VARIANT;
427
428                     // FIXME: This probably doesn't work with boxed types or gvalues. See fx. _pygi_marshal_from_py_array()
429                     switch (g_base_info_get_type (iface_cache->interface_info)) {
430                         case GI_INFO_TYPE_STRUCT:
431                             if (is_gvariant) {
432                               g_assert (item_size == sizeof (gpointer));
433                               if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
434                                 item_arg.v_pointer = g_variant_ref_sink (g_array_index (array_, gpointer, i));
435                               else
436                                 item_arg.v_pointer = g_array_index (array_, gpointer, i);
437                             } else if (arg_cache->transfer == GI_TRANSFER_EVERYTHING && !item_arg_cache->is_pointer) {
438                                 /* array elements are structs */
439                                 gpointer *_struct = g_malloc (item_size);
440                                 memcpy (_struct, array_->data + i * item_size,
441                                         item_size);
442                                 item_arg.v_pointer = _struct;
443                             } else if (item_arg_cache->is_pointer)
444                                 /* array elements are pointers to values */
445                                 item_arg.v_pointer = g_array_index (array_, gpointer, i);
446                             else
447                                 item_arg.v_pointer = array_->data + i * item_size;
448                             break;
449                         default:
450                             item_arg.v_pointer = g_array_index (array_, gpointer, i);
451                             break;
452                     }
453                 } else {
454                     memcpy (&item_arg, array_->data + i * item_size, item_size);
455                 }
456
457                 py_item = item_to_py_marshaller ( state,
458                                                 callable_cache,
459                                                 item_arg_cache,
460                                                 &item_arg);
461
462                 if (py_item == NULL) {
463                     Py_CLEAR (py_obj);
464
465                     if (seq_cache->array_type == GI_ARRAY_TYPE_C)
466                         g_array_unref (array_);
467
468                     goto err;
469                 }
470                 PyList_SET_ITEM (py_obj, i, py_item);
471                 processed_items++;
472             }
473         }
474     }
475
476     if (seq_cache->array_type == GI_ARRAY_TYPE_C)
477         g_array_free (array_, FALSE);
478
479     return py_obj;
480
481 err:
482     if (seq_cache->array_type == GI_ARRAY_TYPE_C) {
483         g_array_free (array_, arg_cache->transfer == GI_TRANSFER_EVERYTHING);
484     } else {
485         /* clean up unprocessed items */
486         if (seq_cache->item_cache->to_py_cleanup != NULL) {
487             int j;
488             PyGIMarshalCleanupFunc cleanup_func = seq_cache->item_cache->to_py_cleanup;
489             for (j = processed_items; j < array_->len; j++) {
490                 cleanup_func (state,
491                               seq_cache->item_cache,
492                               g_array_index (array_, gpointer, j),
493                               FALSE);
494             }
495         }
496
497         if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
498             g_array_free (array_, TRUE);
499     }
500
501     return NULL;
502 }
503
504 PyObject *
505 _pygi_marshal_to_py_glist (PyGIInvokeState   *state,
506                            PyGICallableCache *callable_cache,
507                            PyGIArgCache      *arg_cache,
508                            GIArgument        *arg)
509 {
510     GList *list_;
511     gsize length;
512     gsize i;
513
514     PyGIMarshalToPyFunc item_to_py_marshaller;
515     PyGIArgCache *item_arg_cache;
516     PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;
517
518     PyObject *py_obj = NULL;
519
520     list_ = arg->v_pointer;
521     length = g_list_length (list_);
522
523     py_obj = PyList_New (length);
524     if (py_obj == NULL)
525         return NULL;
526
527     item_arg_cache = seq_cache->item_cache;
528     item_to_py_marshaller = item_arg_cache->to_py_marshaller;
529
530     for (i = 0; list_ != NULL; list_ = g_list_next (list_), i++) {
531         GIArgument item_arg;
532         PyObject *py_item;
533
534         item_arg.v_pointer = list_->data;
535         _pygi_hash_pointer_to_arg (&item_arg, item_arg_cache->type_tag);
536         py_item = item_to_py_marshaller (state,
537                                          callable_cache,
538                                          item_arg_cache,
539                                          &item_arg);
540
541         if (py_item == NULL) {
542             Py_CLEAR (py_obj);
543             _PyGI_ERROR_PREFIX ("Item %zu: ", i);
544             return NULL;
545         }
546
547         PyList_SET_ITEM (py_obj, i, py_item);
548     }
549
550     return py_obj;
551 }
552
553 PyObject *
554 _pygi_marshal_to_py_gslist (PyGIInvokeState   *state,
555                             PyGICallableCache *callable_cache,
556                             PyGIArgCache      *arg_cache,
557                             GIArgument        *arg)
558 {
559     GSList *list_;
560     gsize length;
561     gsize i;
562
563     PyGIMarshalToPyFunc item_to_py_marshaller;
564     PyGIArgCache *item_arg_cache;
565     PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;
566
567     PyObject *py_obj = NULL;
568
569     list_ = arg->v_pointer;
570     length = g_slist_length (list_);
571
572     py_obj = PyList_New (length);
573     if (py_obj == NULL)
574         return NULL;
575
576     item_arg_cache = seq_cache->item_cache;
577     item_to_py_marshaller = item_arg_cache->to_py_marshaller;
578
579     for (i = 0; list_ != NULL; list_ = g_slist_next (list_), i++) {
580         GIArgument item_arg;
581         PyObject *py_item;
582
583         item_arg.v_pointer = list_->data;
584         _pygi_hash_pointer_to_arg (&item_arg, item_arg_cache->type_tag);
585         py_item = item_to_py_marshaller (state,
586                                         callable_cache,
587                                         item_arg_cache,
588                                         &item_arg);
589
590         if (py_item == NULL) {
591             Py_CLEAR (py_obj);
592             _PyGI_ERROR_PREFIX ("Item %zu: ", i);
593             return NULL;
594         }
595
596         PyList_SET_ITEM (py_obj, i, py_item);
597     }
598
599     return py_obj;
600 }
601
602 PyObject *
603 _pygi_marshal_to_py_ghash (PyGIInvokeState   *state,
604                            PyGICallableCache *callable_cache,
605                            PyGIArgCache      *arg_cache,
606                            GIArgument        *arg)
607 {
608     GHashTable *hash_;
609     GHashTableIter hash_table_iter;
610
611     PyGIMarshalToPyFunc key_to_py_marshaller;
612     PyGIMarshalToPyFunc value_to_py_marshaller;
613
614     PyGIArgCache *key_arg_cache;
615     PyGIArgCache *value_arg_cache;
616     PyGIHashCache *hash_cache = (PyGIHashCache *)arg_cache;
617
618     GIArgument key_arg;
619     GIArgument value_arg;
620
621     PyObject *py_obj = NULL;
622
623     hash_ = arg->v_pointer;
624
625     if (hash_ == NULL) {
626         py_obj = Py_None;
627         Py_INCREF (py_obj);
628         return py_obj;
629     }
630
631     py_obj = PyDict_New ();
632     if (py_obj == NULL)
633         return NULL;
634
635     key_arg_cache = hash_cache->key_cache;
636     key_to_py_marshaller = key_arg_cache->to_py_marshaller;
637
638     value_arg_cache = hash_cache->value_cache;
639     value_to_py_marshaller = value_arg_cache->to_py_marshaller;
640
641     g_hash_table_iter_init (&hash_table_iter, hash_);
642     while (g_hash_table_iter_next (&hash_table_iter,
643                                    &key_arg.v_pointer,
644                                    &value_arg.v_pointer)) {
645         PyObject *py_key;
646         PyObject *py_value;
647         int retval;
648
649
650         _pygi_hash_pointer_to_arg (&key_arg, hash_cache->key_cache->type_tag);
651         py_key = key_to_py_marshaller ( state,
652                                       callable_cache,
653                                       key_arg_cache,
654                                      &key_arg);
655
656         if (py_key == NULL) {
657             Py_CLEAR (py_obj);
658             return NULL;
659         }
660
661         _pygi_hash_pointer_to_arg (&value_arg, hash_cache->value_cache->type_tag);
662         py_value = value_to_py_marshaller ( state,
663                                           callable_cache,
664                                           value_arg_cache,
665                                          &value_arg);
666
667         if (py_value == NULL) {
668             Py_CLEAR (py_obj);
669             Py_DECREF(py_key);
670             return NULL;
671         }
672
673         retval = PyDict_SetItem (py_obj, py_key, py_value);
674
675         Py_DECREF (py_key);
676         Py_DECREF (py_value);
677
678         if (retval < 0) {
679             Py_CLEAR (py_obj);
680             return NULL;
681         }
682     }
683
684     return py_obj;
685 }
686
687 PyObject *
688 _pygi_marshal_to_py_gerror (PyGIInvokeState   *state,
689                             PyGICallableCache *callable_cache,
690                             PyGIArgCache      *arg_cache,
691                             GIArgument        *arg)
692 {
693     GError *error = arg->v_pointer;
694     PyObject *py_obj = NULL;
695
696     py_obj = pyglib_error_marshal(&error);
697
698     if (arg_cache->transfer == GI_TRANSFER_EVERYTHING && error != NULL) {
699         g_error_free (error);
700     }
701
702     if (py_obj != NULL) {
703         return py_obj;
704     } else {
705         Py_RETURN_NONE;
706     }
707 }
708
709 PyObject *
710 _pygi_marshal_to_py_interface_callback (PyGIInvokeState   *state,
711                                         PyGICallableCache *callable_cache,
712                                         PyGIArgCache      *arg_cache,
713                                         GIArgument        *arg)
714 {
715     PyObject *py_obj = NULL;
716
717     PyErr_Format (PyExc_NotImplementedError,
718                   "Marshalling a callback to PyObject is not supported");
719     return py_obj;
720 }
721
722 PyObject *
723 _pygi_marshal_to_py_interface_enum (PyGIInvokeState   *state,
724                                     PyGICallableCache *callable_cache,
725                                     PyGIArgCache      *arg_cache,
726                                     GIArgument        *arg)
727 {
728     PyObject *py_obj = NULL;
729     PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
730     GIBaseInfo *interface;
731     long c_long;
732
733     interface = g_type_info_get_interface (arg_cache->type_info);
734     g_assert (g_base_info_get_type (interface) == GI_INFO_TYPE_ENUM);
735
736     if (!gi_argument_to_c_long(arg, &c_long,
737                                g_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
738         return NULL;
739     }
740
741     if (iface_cache->g_type == G_TYPE_NONE) {
742         py_obj = PyObject_CallFunction (iface_cache->py_type, "l", c_long);
743     } else {
744         py_obj = pyg_enum_from_gtype (iface_cache->g_type, c_long);
745     }
746     return py_obj;
747 }
748
749 PyObject *
750 _pygi_marshal_to_py_interface_flags (PyGIInvokeState   *state,
751                                      PyGICallableCache *callable_cache,
752                                      PyGIArgCache      *arg_cache,
753                                      GIArgument        *arg)
754 {
755     PyObject *py_obj = NULL;
756     PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
757     GIBaseInfo *interface;
758     long c_long;
759
760     interface = g_type_info_get_interface (arg_cache->type_info);
761     g_assert (g_base_info_get_type (interface) == GI_INFO_TYPE_FLAGS);
762
763     if (!gi_argument_to_c_long(arg, &c_long,
764                                g_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
765         return NULL;
766     }
767
768     if (iface_cache->g_type == G_TYPE_NONE) {
769         /* An enum with a GType of None is an enum without GType */
770
771         PyObject *py_type = _pygi_type_import_by_gi_info (iface_cache->interface_info);
772         PyObject *py_args = NULL;
773
774         if (!py_type)
775             return NULL;
776
777         py_args = PyTuple_New (1);
778         if (PyTuple_SetItem (py_args, 0, PyLong_FromLong (c_long)) != 0) {
779             Py_DECREF (py_args);
780             Py_DECREF (py_type);
781             return NULL;
782         }
783
784         py_obj = PyObject_CallFunction (py_type, "l", c_long);
785
786         Py_DECREF (py_args);
787         Py_DECREF (py_type);
788     } else {
789         py_obj = pyg_flags_from_gtype (iface_cache->g_type, c_long);
790     }
791
792     return py_obj;
793 }
794
795 PyObject *
796 _pygi_marshal_to_py_interface_struct (PyGIInvokeState   *state,
797                                       PyGICallableCache *callable_cache,
798                                       PyGIArgCache      *arg_cache,
799                                       GIArgument        *arg)
800 {
801     PyObject *py_obj = NULL;
802     PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
803     GType type = iface_cache->g_type;
804
805     if (arg->v_pointer == NULL) {
806         py_obj = Py_None;
807         Py_INCREF (py_obj);
808         return py_obj;
809     }
810
811     if (g_type_is_a (type, G_TYPE_VALUE)) {
812         py_obj = pyg_value_as_pyobject (arg->v_pointer, FALSE);
813     } else if (iface_cache->is_foreign) {
814         py_obj = pygi_struct_foreign_convert_from_g_argument (iface_cache->interface_info,
815                                                               arg->v_pointer);
816     } else if (g_type_is_a (type, G_TYPE_BOXED)) {
817         py_obj = _pygi_boxed_new ( (PyTypeObject *)iface_cache->py_type, arg->v_pointer, 
818                                   arg_cache->transfer == GI_TRANSFER_EVERYTHING || arg_cache->is_caller_allocates);
819         if (arg_cache->is_caller_allocates)
820           ((PyGIBoxed *)py_obj)->slice_allocated = TRUE;
821     } else if (g_type_is_a (type, G_TYPE_POINTER)) {
822         if (iface_cache->py_type == NULL ||
823                 !PyType_IsSubtype ( (PyTypeObject *)iface_cache->py_type, &PyGIStruct_Type)) {
824             g_warn_if_fail(arg_cache->transfer == GI_TRANSFER_NOTHING);
825             py_obj = pyg_pointer_new (type, arg->v_pointer);
826         } else {
827             py_obj = _pygi_struct_new ( (PyTypeObject *)iface_cache->py_type, arg->v_pointer, 
828                                        arg_cache->transfer == GI_TRANSFER_EVERYTHING);
829         }
830     } else if (g_type_is_a (type, G_TYPE_VARIANT)) {
831          g_variant_ref_sink (arg->v_pointer);
832          py_obj = _pygi_struct_new ( (PyTypeObject *)iface_cache->py_type, arg->v_pointer, 
833                                     FALSE);
834     } else if (type == G_TYPE_NONE && iface_cache->is_foreign) {
835         py_obj = pygi_struct_foreign_convert_from_g_argument (iface_cache->interface_info, arg->v_pointer);
836     } else if (type == G_TYPE_NONE) {
837         py_obj = _pygi_struct_new ( (PyTypeObject *) iface_cache->py_type, arg->v_pointer, 
838                                    arg_cache->transfer == GI_TRANSFER_EVERYTHING);
839     } else {
840         PyErr_Format (PyExc_NotImplementedError,
841                       "structure type '%s' is not supported yet",
842                       g_type_name (type));
843     }
844
845     return py_obj;
846 }
847
848 PyObject *
849 _pygi_marshal_to_py_interface_interface (PyGIInvokeState   *state,
850                                          PyGICallableCache *callable_cache,
851                                          PyGIArgCache      *arg_cache,
852                                          GIArgument        *arg)
853 {
854     PyObject *py_obj = NULL;
855
856     PyErr_Format (PyExc_NotImplementedError,
857                   "Marshalling for this type is not implemented yet");
858     return py_obj;
859 }
860
861 PyObject *
862 _pygi_marshal_to_py_interface_boxed (PyGIInvokeState   *state,
863                                      PyGICallableCache *callable_cache,
864                                      PyGIArgCache      *arg_cache,
865                                      GIArgument        *arg)
866 {
867     PyObject *py_obj = NULL;
868
869     PyErr_Format (PyExc_NotImplementedError,
870                   "Marshalling for this type is not implemented yet");
871     return py_obj;
872 }
873
874 PyObject *
875 _pygi_marshal_to_py_interface_object (PyGIInvokeState   *state,
876                                       PyGICallableCache *callable_cache,
877                                       PyGIArgCache      *arg_cache,
878                                       GIArgument        *arg)
879 {
880     PyObject *py_obj;
881
882     if (arg->v_pointer == NULL) {
883         py_obj = Py_None;
884         Py_INCREF (py_obj);
885         return py_obj;
886     }
887
888     if (G_IS_PARAM_SPEC(arg->v_pointer))
889     {
890         py_obj = pyg_param_spec_new (arg->v_pointer);
891         if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
892                         g_param_spec_unref (arg->v_pointer);
893     }
894     else
895     {
896         py_obj = pygobject_new (arg->v_pointer);
897         if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
898                 g_object_unref (arg->v_pointer);
899     }
900
901     return py_obj;
902 }
903
904 PyObject *
905 _pygi_marshal_to_py_interface_union  (PyGIInvokeState   *state,
906                                       PyGICallableCache *callable_cache,
907                                       PyGIArgCache      *arg_cache,
908                                       GIArgument        *arg)
909 {
910     PyObject *py_obj = NULL;
911
912     PyErr_Format (PyExc_NotImplementedError,
913                   "Marshalling for this type is not implemented yet");
914     return py_obj;
915 }