Imported Upstream version 3.3.1
[platform/upstream/pygobject2.git] / gi / pygi-marshal-from-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 to convert PyObjects to C types.
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 #include <pygobject.h>
29 #include <pyglib-python-compat.h>
30
31 #include "pygi-cache.h"
32 #include "pygi-marshal-cleanup.h"
33 #include "pygi-marshal-from-py.h"
34
35 /*
36  * _is_union_member - check to see if the py_arg is actually a member of the
37  * expected C union
38  */
39 static gboolean
40 _is_union_member (PyGIInterfaceCache *iface_cache, PyObject *py_arg) {
41     gint i;
42     gint n_fields;
43     GIUnionInfo *union_info;
44     GIInfoType info_type;
45     gboolean is_member = FALSE;
46
47     info_type = g_base_info_get_type (iface_cache->interface_info);
48
49     if (info_type != GI_INFO_TYPE_UNION)
50         return FALSE;
51
52     union_info = (GIUnionInfo *) iface_cache->interface_info;
53     n_fields = g_union_info_get_n_fields (union_info);
54
55     for (i = 0; i < n_fields; i++) {
56         GIFieldInfo *field_info;
57         GITypeInfo *field_type_info;
58
59         field_info = g_union_info_get_field (union_info, i);
60         field_type_info = g_field_info_get_type (field_info);
61
62         /* we can only check if the members are interfaces */
63         if (g_type_info_get_tag (field_type_info) == GI_TYPE_TAG_INTERFACE) {
64             GIInterfaceInfo *field_iface_info;
65             PyObject *py_type;
66
67             field_iface_info = g_type_info_get_interface (field_type_info);
68             py_type = _pygi_type_import_by_gi_info ((GIBaseInfo *) field_iface_info);
69
70             if (py_type != NULL && PyObject_IsInstance (py_arg, py_type)) {
71                 is_member = TRUE;
72             }
73
74             Py_XDECREF (py_type);
75             g_base_info_unref ( ( GIBaseInfo *) field_iface_info);
76         }
77
78         g_base_info_unref ( ( GIBaseInfo *) field_type_info);
79         g_base_info_unref ( ( GIBaseInfo *) field_info);
80
81         if (is_member)
82             break;
83     }
84
85     return is_member;
86 }
87
88 gboolean
89 _pygi_marshal_from_py_void (PyGIInvokeState   *state,
90                             PyGICallableCache *callable_cache,
91                             PyGIArgCache      *arg_cache,
92                             PyObject          *py_arg,
93                             GIArgument        *arg)
94 {
95     g_warn_if_fail (arg_cache->transfer == GI_TRANSFER_NOTHING);
96
97     arg->v_pointer = py_arg;
98
99     return TRUE;
100 }
101
102 gboolean
103 _pygi_marshal_from_py_boolean (PyGIInvokeState   *state,
104                                PyGICallableCache *callable_cache,
105                                PyGIArgCache      *arg_cache,
106                                PyObject          *py_arg,
107                                GIArgument        *arg)
108 {
109     arg->v_boolean = PyObject_IsTrue (py_arg);
110
111     return TRUE;
112 }
113
114 gboolean
115 _pygi_marshal_from_py_int8 (PyGIInvokeState   *state,
116                             PyGICallableCache *callable_cache,
117                             PyGIArgCache      *arg_cache,
118                             PyObject          *py_arg,
119                             GIArgument        *arg)
120 {
121     PyObject *py_long;
122     long long_;
123
124     if (!PyNumber_Check (py_arg)) {
125         PyErr_Format (PyExc_TypeError, "Must be number, not %s",
126                       py_arg->ob_type->tp_name);
127         return FALSE;
128     }
129
130     py_long = PYGLIB_PyNumber_Long (py_arg);
131     if (!py_long)
132         return FALSE;
133
134     long_ = PYGLIB_PyLong_AsLong (py_long);
135     Py_DECREF (py_long);
136
137     if (PyErr_Occurred ()) {
138         PyErr_Clear ();
139         PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, -128, 127);
140         return FALSE;
141     }
142
143     if (long_ < -128 || long_ > 127) {
144         PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, -128, 127);
145         return FALSE;
146     }
147
148     arg->v_int8 = long_;
149
150     return TRUE;
151 }
152
153 gboolean
154 _pygi_marshal_from_py_uint8 (PyGIInvokeState   *state,
155                              PyGICallableCache *callable_cache,
156                              PyGIArgCache      *arg_cache,
157                              PyObject          *py_arg,
158                              GIArgument        *arg)
159 {
160     unsigned long long_;
161
162     if (PYGLIB_PyBytes_Check (py_arg)) {
163
164         if (PYGLIB_PyBytes_Size (py_arg) != 1) {
165             PyErr_Format (PyExc_TypeError, "Must be a single character");
166             return FALSE;
167         }
168
169         long_ = (unsigned char)(PYGLIB_PyBytes_AsString (py_arg)[0]);
170
171     } else if (PyNumber_Check (py_arg)) {
172         PyObject *py_long;
173         py_long = PYGLIB_PyNumber_Long (py_arg);
174         if (!py_long)
175             return FALSE;
176
177         long_ = PYGLIB_PyLong_AsLong (py_long);
178         Py_DECREF (py_long);
179
180         if (PyErr_Occurred ()) {
181             PyErr_Clear();
182
183             PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, 0, 255);
184             return FALSE;
185         }
186     } else {
187         PyErr_Format (PyExc_TypeError, "Must be number or single byte string, not %s",
188                       py_arg->ob_type->tp_name);
189         return FALSE;
190     }
191
192     if (long_ < 0 || long_ > 255) {
193         PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, 0, 255);
194         return FALSE;
195     }
196
197     arg->v_uint8 = long_;
198
199     return TRUE;
200 }
201
202 gboolean
203 _pygi_marshal_from_py_int16 (PyGIInvokeState   *state,
204                              PyGICallableCache *callable_cache,
205                              PyGIArgCache      *arg_cache,
206                              PyObject          *py_arg,
207                              GIArgument        *arg)
208 {
209     PyObject *py_long;
210     long long_;
211
212     if (!PyNumber_Check (py_arg)) {
213         PyErr_Format (PyExc_TypeError, "Must be number, not %s",
214                       py_arg->ob_type->tp_name);
215         return FALSE;
216     }
217
218     py_long = PYGLIB_PyNumber_Long (py_arg);
219     if (!py_long)
220         return FALSE;
221
222     long_ = PYGLIB_PyLong_AsLong (py_long);
223     Py_DECREF (py_long);
224
225     if (PyErr_Occurred ()) {
226         PyErr_Clear ();
227         PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, -32768, 32767);
228         return FALSE;
229     }
230
231     if (long_ < -32768 || long_ > 32767) {
232         PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, -32768, 32767);
233         return FALSE;
234     }
235
236     arg->v_int16 = long_;
237
238     return TRUE;
239 }
240
241 gboolean
242 _pygi_marshal_from_py_uint16 (PyGIInvokeState   *state,
243                               PyGICallableCache *callable_cache,
244                               PyGIArgCache      *arg_cache,
245                               PyObject          *py_arg,
246                               GIArgument        *arg)
247 {
248     PyObject *py_long;
249     long long_;
250
251     if (!PyNumber_Check (py_arg)) {
252         PyErr_Format (PyExc_TypeError, "Must be number, not %s",
253                       py_arg->ob_type->tp_name);
254         return FALSE;
255     }
256
257     py_long = PYGLIB_PyNumber_Long (py_arg);
258     if (!py_long)
259         return FALSE;
260
261     long_ = PYGLIB_PyLong_AsLong (py_long);
262     Py_DECREF (py_long);
263
264     if (PyErr_Occurred ()) {
265         PyErr_Clear ();
266         PyErr_Format (PyExc_ValueError, "%li not in range %d to %d", long_, 0, 65535);
267         return FALSE;
268     }
269
270     if (long_ < 0 || long_ > 65535) {
271         PyErr_Format (PyExc_ValueError, "%li not in range %d to %d", long_, 0, 65535);
272         return FALSE;
273     }
274
275     arg->v_uint16 = long_;
276
277     return TRUE;
278 }
279
280 gboolean
281 _pygi_marshal_from_py_int32 (PyGIInvokeState   *state,
282                              PyGICallableCache *callable_cache,
283                              PyGIArgCache      *arg_cache,
284                              PyObject          *py_arg,
285                              GIArgument        *arg)
286 {
287     PyObject *py_long;
288     long long_;
289
290     if (!PyNumber_Check (py_arg)) {
291         PyErr_Format (PyExc_TypeError, "Must be number, not %s",
292                       py_arg->ob_type->tp_name);
293         return FALSE;
294     }
295
296     py_long = PYGLIB_PyNumber_Long (py_arg);
297     if (!py_long)
298         return FALSE;
299
300     long_ = PYGLIB_PyLong_AsLong (py_long);
301     Py_DECREF (py_long);
302
303     if (PyErr_Occurred ()) {
304         PyErr_Clear();
305         PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, G_MININT32, G_MAXINT32);
306         return FALSE;
307     }
308
309     if (long_ < G_MININT32 || long_ > G_MAXINT32) {
310         PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, G_MININT32, G_MAXINT32);
311         return FALSE;
312     }
313
314     arg->v_int32 = long_;
315
316     return TRUE;
317 }
318
319 gboolean
320 _pygi_marshal_from_py_uint32 (PyGIInvokeState   *state,
321                               PyGICallableCache *callable_cache,
322                               PyGIArgCache      *arg_cache,
323                               PyObject          *py_arg,
324                               GIArgument        *arg)
325 {
326     PyObject *py_long;
327     long long long_;
328
329     if (!PyNumber_Check (py_arg)) {
330         PyErr_Format (PyExc_TypeError, "Must be number, not %s",
331                       py_arg->ob_type->tp_name);
332         return FALSE;
333     }
334
335     py_long = PYGLIB_PyNumber_Long (py_arg);
336     if (!py_long)
337         return FALSE;
338
339 #if PY_VERSION_HEX < 0x03000000
340     if (PyInt_Check (py_long))
341         long_ = PyInt_AsLong (py_long);
342     else
343 #endif
344         long_ = PyLong_AsLongLong (py_long);
345
346     Py_DECREF (py_long);
347
348     if (PyErr_Occurred ()) {
349         PyErr_Clear ();
350         PyErr_Format (PyExc_ValueError, "%lli not in range %i to %u", long_, 0, G_MAXUINT32);
351         return FALSE;
352     }
353
354     if (long_ < 0 || long_ > G_MAXUINT32) {
355         PyErr_Format (PyExc_ValueError, "%lli not in range %i to %u", long_, 0, G_MAXUINT32);
356         return FALSE;
357     }
358
359     arg->v_uint32 = long_;
360
361     return TRUE;
362 }
363
364 gboolean
365 _pygi_marshal_from_py_int64 (PyGIInvokeState   *state,
366                              PyGICallableCache *callable_cache,
367                              PyGIArgCache      *arg_cache,
368                              PyObject          *py_arg,
369                              GIArgument        *arg)
370 {
371     PyObject *py_long;
372     long long long_;
373
374     if (!PyNumber_Check (py_arg)) {
375         PyErr_Format (PyExc_TypeError, "Must be number, not %s",
376                       py_arg->ob_type->tp_name);
377         return FALSE;
378     }
379
380     py_long = PYGLIB_PyNumber_Long (py_arg);
381     if (!py_long)
382         return FALSE;
383
384 #if PY_VERSION_HEX < 0x03000000
385     if (PyInt_Check (py_long))
386         long_ = PyInt_AS_LONG (py_long);
387     else
388 #endif
389         long_ = PyLong_AsLongLong (py_long);
390
391     Py_DECREF (py_long);
392
393     if (PyErr_Occurred ()) {
394         /* OverflowError occured but range errors should be returned as ValueError */
395         char *long_str;
396         PyObject *py_str;
397
398         PyErr_Clear ();
399
400         py_str = PyObject_Str (py_long);
401
402         if (PyUnicode_Check (py_str)) {
403             PyObject *py_bytes = PyUnicode_AsUTF8String (py_str);
404             if (py_bytes == NULL)
405                 return FALSE;
406
407             long_str = g_strdup (PYGLIB_PyBytes_AsString (py_bytes));
408             if (long_str == NULL) {
409                 PyErr_NoMemory ();
410                 return FALSE;
411             }
412
413             Py_DECREF (py_bytes);
414         } else {
415             long_str = g_strdup (PYGLIB_PyBytes_AsString(py_str));
416         }
417
418         Py_DECREF (py_str);
419         PyErr_Format (PyExc_ValueError, "%s not in range %ld to %ld",
420                       long_str, G_MININT64, G_MAXINT64);
421
422         g_free (long_str);
423         return FALSE;
424     }
425
426     if (long_ < G_MININT64 || long_ > G_MAXINT64) {
427         PyErr_Format (PyExc_ValueError, "%lld not in range %ld to %ld", long_, G_MININT64, G_MAXINT64);
428         return FALSE;
429     }
430
431     arg->v_int64 = long_;
432
433     return TRUE;
434 }
435
436 gboolean
437 _pygi_marshal_from_py_uint64 (PyGIInvokeState   *state,
438                               PyGICallableCache *callable_cache,
439                               PyGIArgCache      *arg_cache,
440                               PyObject          *py_arg,
441                               GIArgument        *arg)
442 {
443     PyObject *py_long;
444     guint64 ulong_;
445
446     if (!PyNumber_Check (py_arg)) {
447         PyErr_Format (PyExc_TypeError, "Must be number, not %s",
448                       py_arg->ob_type->tp_name);
449         return FALSE;
450     }
451
452     py_long = PYGLIB_PyNumber_Long (py_arg);
453     if (!py_long)
454         return FALSE;
455
456 #if PY_VERSION_HEX < 0x03000000
457     if (PyInt_Check (py_long)) {
458         long long_ = PyInt_AsLong (py_long);
459         if (long_ < 0) {
460             PyErr_Format (PyExc_ValueError, "%ld not in range %d to %lu",
461                           long_, 0, G_MAXUINT64);
462             return FALSE;
463         }
464         ulong_ = long_;
465     } else
466 #endif
467         ulong_ = PyLong_AsUnsignedLongLong (py_long);
468
469     Py_DECREF (py_long);
470
471     if (PyErr_Occurred ()) {
472         /* OverflowError occured but range errors should be returned as ValueError */
473         char *long_str;
474         PyObject *py_str;
475
476         PyErr_Clear ();
477
478         py_str = PyObject_Str (py_long);
479
480         if (PyUnicode_Check (py_str)) {
481             PyObject *py_bytes = PyUnicode_AsUTF8String (py_str);
482             if (py_bytes == NULL)
483                 return FALSE;
484
485             long_str = g_strdup (PYGLIB_PyBytes_AsString (py_bytes));
486             if (long_str == NULL) {
487                 PyErr_NoMemory ();
488                 return FALSE;
489             }
490
491             Py_DECREF (py_bytes);
492         } else {
493             long_str = g_strdup (PYGLIB_PyBytes_AsString (py_str));
494         }
495
496         Py_DECREF (py_str);
497
498         PyErr_Format (PyExc_ValueError, "%s not in range %d to %lu",
499                       long_str, 0, G_MAXUINT64);
500
501         g_free (long_str);
502         return FALSE;
503     }
504
505     if (ulong_ > G_MAXUINT64) {
506         PyErr_Format (PyExc_ValueError, "%lu not in range %d to %lu", ulong_, 0, G_MAXUINT64);
507         return FALSE;
508     }
509
510     arg->v_uint64 = ulong_;
511
512     return TRUE;
513 }
514
515 gboolean
516 _pygi_marshal_from_py_float (PyGIInvokeState   *state,
517                              PyGICallableCache *callable_cache,
518                              PyGIArgCache      *arg_cache,
519                              PyObject          *py_arg,
520                              GIArgument        *arg)
521 {
522     PyObject *py_float;
523     double double_;
524
525     if (!PyNumber_Check (py_arg)) {
526         PyErr_Format (PyExc_TypeError, "Must be number, not %s",
527                       py_arg->ob_type->tp_name);
528         return FALSE;
529     }
530
531     py_float = PyNumber_Float (py_arg);
532     if (!py_float)
533         return FALSE;
534
535     double_ = PyFloat_AsDouble (py_float);
536     Py_DECREF (py_float);
537
538     if (PyErr_Occurred ()) {
539         PyErr_Clear ();
540         PyErr_Format (PyExc_ValueError, "%f not in range %f to %f", double_, -G_MAXFLOAT, G_MAXFLOAT);
541         return FALSE;
542     }
543
544     if (double_ < -G_MAXFLOAT || double_ > G_MAXFLOAT) {
545         PyErr_Format (PyExc_ValueError, "%f not in range %f to %f", double_, -G_MAXFLOAT, G_MAXFLOAT);
546         return FALSE;
547     }
548
549     arg->v_float = double_;
550
551     return TRUE;
552 }
553
554 gboolean
555 _pygi_marshal_from_py_double (PyGIInvokeState   *state,
556                               PyGICallableCache *callable_cache,
557                               PyGIArgCache      *arg_cache,
558                               PyObject          *py_arg,
559                               GIArgument        *arg)
560 {
561     PyObject *py_float;
562     double double_;
563
564     if (!PyNumber_Check (py_arg)) {
565         PyErr_Format (PyExc_TypeError, "Must be number, not %s",
566                       py_arg->ob_type->tp_name);
567         return FALSE;
568     }
569
570     py_float = PyNumber_Float (py_arg);
571     if (!py_float)
572         return FALSE;
573
574     double_ = PyFloat_AsDouble (py_float);
575     Py_DECREF (py_float);
576
577     if (PyErr_Occurred ()) {
578         PyErr_Clear ();
579         PyErr_Format (PyExc_ValueError, "%f not in range %f to %f", double_, -G_MAXDOUBLE, G_MAXDOUBLE);
580         return FALSE;
581     }
582
583     if (double_ < -G_MAXDOUBLE || double_ > G_MAXDOUBLE) {
584         PyErr_Format (PyExc_ValueError, "%f not in range %f to %f", double_, -G_MAXDOUBLE, G_MAXDOUBLE);
585         return FALSE;
586     }
587
588     arg->v_double = double_;
589
590     return TRUE;
591 }
592
593 gboolean
594 _pygi_marshal_from_py_unichar (PyGIInvokeState   *state,
595                                PyGICallableCache *callable_cache,
596                                PyGIArgCache      *arg_cache,
597                                PyObject          *py_arg,
598                                GIArgument        *arg)
599 {
600     Py_ssize_t size;
601     gchar *string_;
602
603     if (PyUnicode_Check (py_arg)) {
604        PyObject *py_bytes;
605
606        size = PyUnicode_GET_SIZE (py_arg);
607        py_bytes = PyUnicode_AsUTF8String (py_arg);
608        string_ = strdup(PYGLIB_PyBytes_AsString (py_bytes));
609        Py_DECREF (py_bytes);
610
611 #if PY_VERSION_HEX < 0x03000000
612     } else if (PyString_Check (py_arg)) {
613        PyObject *pyuni = PyUnicode_FromEncodedObject (py_arg, "UTF-8", "strict");
614        if (!pyuni)
615            return FALSE;
616
617        size = PyUnicode_GET_SIZE (pyuni);
618        string_ = g_strdup (PyString_AsString(py_arg));
619        Py_DECREF (pyuni);
620 #endif
621     } else {
622        PyErr_Format (PyExc_TypeError, "Must be string, not %s",
623                      py_arg->ob_type->tp_name);
624        return FALSE;
625     }
626
627     if (size != 1) {
628        PyErr_Format (PyExc_TypeError, "Must be a one character string, not %ld characters",
629                      size);
630        g_free (string_);
631        return FALSE;
632     }
633
634     arg->v_uint32 = g_utf8_get_char (string_);
635     g_free (string_);
636
637     return TRUE;
638 }
639 gboolean
640 _pygi_marshal_from_py_gtype (PyGIInvokeState   *state,
641                              PyGICallableCache *callable_cache,
642                              PyGIArgCache      *arg_cache,
643                              PyObject          *py_arg,
644                              GIArgument        *arg)
645 {
646     long type_ = pyg_type_from_object (py_arg);
647
648     if (type_ == 0) {
649         PyErr_Format (PyExc_TypeError, "Must be gobject.GType, not %s",
650                       py_arg->ob_type->tp_name);
651         return FALSE;
652     }
653
654     arg->v_long = type_;
655     return TRUE;
656 }
657 gboolean
658 _pygi_marshal_from_py_utf8 (PyGIInvokeState   *state,
659                             PyGICallableCache *callable_cache,
660                             PyGIArgCache      *arg_cache,
661                             PyObject          *py_arg,
662                             GIArgument        *arg)
663 {
664     gchar *string_;
665
666     if (py_arg == Py_None) {
667         arg->v_pointer = NULL;
668         return TRUE;
669     }
670
671     if (PyUnicode_Check (py_arg)) {
672         PyObject *pystr_obj = PyUnicode_AsUTF8String (py_arg);
673         if (!pystr_obj)
674             return FALSE;
675
676         string_ = g_strdup (PYGLIB_PyBytes_AsString (pystr_obj));
677         Py_DECREF (pystr_obj);
678     }
679 #if PY_VERSION_HEX < 0x03000000
680     else if (PyString_Check (py_arg)) {
681         string_ = g_strdup (PyString_AsString (py_arg));
682     }
683 #endif
684     else {
685         PyErr_Format (PyExc_TypeError, "Must be string, not %s",
686                       py_arg->ob_type->tp_name);
687         return FALSE;
688     }
689
690     arg->v_string = string_;
691     return TRUE;
692 }
693
694 gboolean
695 _pygi_marshal_from_py_filename (PyGIInvokeState   *state,
696                                 PyGICallableCache *callable_cache,
697                                 PyGIArgCache      *arg_cache,
698                                 PyObject          *py_arg,
699                                 GIArgument        *arg)
700 {
701     gchar *string_;
702     GError *error = NULL;
703
704     if (PyUnicode_Check (py_arg)) {
705         PyObject *pystr_obj = PyUnicode_AsUTF8String (py_arg);
706         if (!pystr_obj)
707             return FALSE;
708
709         string_ = g_strdup (PYGLIB_PyBytes_AsString (pystr_obj));
710         Py_DECREF (pystr_obj);
711     }
712 #if PY_VERSION_HEX < 0x03000000
713     else if (PyString_Check (py_arg)) {
714         string_ = g_strdup (PyString_AsString (py_arg));
715     }
716 #endif
717     else {
718         PyErr_Format (PyExc_TypeError, "Must be string, not %s",
719                       py_arg->ob_type->tp_name);
720         return FALSE;
721     }
722
723     arg->v_string = g_filename_from_utf8 (string_, -1, NULL, NULL, &error);
724     g_free (string_);
725
726     if (arg->v_string == NULL) {
727         PyErr_SetString (PyExc_Exception, error->message);
728         g_error_free (error);
729         /* TODO: Convert the error to an exception. */
730         return FALSE;
731     }
732
733     return TRUE;
734 }
735
736 gboolean
737 _pygi_marshal_from_py_array (PyGIInvokeState   *state,
738                              PyGICallableCache *callable_cache,
739                              PyGIArgCache      *arg_cache,
740                              PyObject          *py_arg,
741                              GIArgument        *arg)
742 {
743     PyGIMarshalFromPyFunc from_py_marshaller;
744     int i;
745     Py_ssize_t length;
746     gssize item_size;
747     gboolean is_ptr_array;
748     GArray *array_ = NULL;
749     PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache;
750
751
752     if (py_arg == Py_None) {
753         arg->v_pointer = NULL;
754         return TRUE;
755     }
756
757     if (!PySequence_Check (py_arg)) {
758         PyErr_Format (PyExc_TypeError, "Must be sequence, not %s",
759                       py_arg->ob_type->tp_name);
760         return FALSE;
761     }
762
763     length = PySequence_Length (py_arg);
764     if (length < 0)
765         return FALSE;
766
767     if (sequence_cache->fixed_size >= 0 &&
768         sequence_cache->fixed_size != length) {
769         PyErr_Format (PyExc_ValueError, "Must contain %zd items, not %zd",
770                       sequence_cache->fixed_size, length);
771
772         return FALSE;
773     }
774
775     item_size = sequence_cache->item_size;
776     is_ptr_array = (sequence_cache->array_type == GI_ARRAY_TYPE_PTR_ARRAY);
777     if (is_ptr_array) {
778         array_ = (GArray *)g_ptr_array_new ();
779     } else {
780         array_ = g_array_sized_new (sequence_cache->is_zero_terminated,
781                                     FALSE,
782                                     item_size,
783                                     length);
784     }
785
786     if (array_ == NULL) {
787         PyErr_NoMemory ();
788         return FALSE;
789     }
790
791     if (sequence_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8 &&
792         PYGLIB_PyBytes_Check (py_arg)) {
793         memcpy(array_->data, PYGLIB_PyBytes_AsString (py_arg), length);
794         if (sequence_cache->is_zero_terminated) {
795             /* If array_ has been created with zero_termination, space for the
796              * terminator is properly allocated, so we're not off-by-one here. */
797             array_->data[length] = '\0';
798         }
799         goto array_success;
800     }
801
802     from_py_marshaller = sequence_cache->item_cache->from_py_marshaller;
803     for (i = 0; i < length; i++) {
804         GIArgument item;
805         PyObject *py_item = PySequence_GetItem (py_arg, i);
806         if (py_item == NULL)
807             goto err;
808
809         if (!from_py_marshaller ( state,
810                                   callable_cache,
811                                   sequence_cache->item_cache,
812                                   py_item,
813                                  &item))
814             goto err;
815
816         /* FIXME: it is much more efficent to have seperate marshaller
817          *        for ptr arrays than doing the evaluation
818          *        and casting each loop iteration
819          */
820         if (is_ptr_array) {
821             g_ptr_array_add((GPtrArray *)array_, item.v_pointer);
822         } else if (sequence_cache->item_cache->type_tag == GI_TYPE_TAG_INTERFACE) {
823             PyGIInterfaceCache *item_iface_cache = (PyGIInterfaceCache *) sequence_cache->item_cache;
824             GIBaseInfo *base_info = (GIBaseInfo *) item_iface_cache->interface_info;
825             GIInfoType info_type = g_base_info_get_type (base_info);
826
827             switch (info_type) {
828                 case GI_INFO_TYPE_UNION:
829                 case GI_INFO_TYPE_STRUCT:
830                 {
831                     PyGIArgCache *item_arg_cache = (PyGIArgCache *)item_iface_cache;
832                     PyGIMarshalCleanupFunc from_py_cleanup = item_arg_cache->from_py_cleanup;
833                     gboolean is_boxed = g_type_is_a (item_iface_cache->g_type, G_TYPE_BOXED);
834                     gboolean is_gvalue = item_iface_cache->g_type == G_TYPE_VALUE;
835                     gboolean is_gvariant = item_iface_cache->g_type == G_TYPE_VARIANT;
836                     
837                     if (is_gvariant) {
838                         /* Item size will always be that of a pointer,
839                          * since GVariants are opaque hence always passed by ref */
840                         g_assert (item_size == sizeof (item.v_pointer));
841                         g_array_insert_val (array_, i, item.v_pointer);
842                     } else if (is_gvalue) {
843                         GValue* dest = (GValue*) (array_->data + (i * item_size));
844                         memset (dest, 0, item_size);
845                         if (item.v_pointer != NULL) {
846                             g_value_init (dest, G_VALUE_TYPE ((GValue*) item.v_pointer));
847                             g_value_copy ((GValue*) item.v_pointer, dest);
848                         }
849
850                         if (from_py_cleanup) {
851                             from_py_cleanup (state, item_arg_cache, item.v_pointer, TRUE);
852                             /* we freed the original copy already, the new one is a 
853                              * struct in an array. _pygi_marshal_cleanup_from_py_array()
854                              * must not free it again */
855                             item_arg_cache->from_py_cleanup = NULL;
856                         }
857                     } else if (!is_boxed) {
858                         /* HACK: Gdk.Atom is merely an integer wrapped in a pointer,
859                          * so we must not dereference it; just copy the pointer
860                          * value, and don't attempt to free it. TODO: find out
861                          * if there are other data types with similar behaviour
862                          * and generalize. */
863                         if (g_strcmp0 (item_iface_cache->type_name, "Gdk.Atom") == 0) {
864                             g_assert (item_size == sizeof (item.v_pointer));
865                             memcpy (array_->data + (i * item_size), &item.v_pointer, item_size);
866                         } else {
867                             memcpy (array_->data + (i * item_size), item.v_pointer, item_size);
868
869                             if (from_py_cleanup)
870                                 from_py_cleanup (state, item_arg_cache, item.v_pointer, TRUE);
871                         }
872                     } else {
873                         g_array_insert_val (array_, i, item);
874                     }
875                     break;
876                 }
877                 default:
878                     g_array_insert_val (array_, i, item);
879             }
880         } else {
881             g_array_insert_val (array_, i, item);
882         }
883         continue;
884 err:
885         if (sequence_cache->item_cache->from_py_cleanup != NULL) {
886             gsize j;
887             PyGIMarshalCleanupFunc cleanup_func =
888                 sequence_cache->item_cache->from_py_cleanup;
889
890             for(j = 0; j < i; j++) {
891                 cleanup_func (state,
892                               sequence_cache->item_cache,
893                               g_array_index (array_, gpointer, j),
894                               TRUE);
895             }
896         }
897
898         if (is_ptr_array)
899             g_ptr_array_free ( ( GPtrArray *)array_, TRUE);
900         else
901             g_array_free (array_, TRUE);
902         _PyGI_ERROR_PREFIX ("Item %i: ", i);
903         return FALSE;
904     }
905
906 array_success:
907     if (sequence_cache->len_arg_index >= 0) {
908         /* we have an child arg to handle */
909         PyGIArgCache *child_cache =
910             callable_cache->args_cache[sequence_cache->len_arg_index];
911
912         if (child_cache->direction == PYGI_DIRECTION_BIDIRECTIONAL) {
913             gint *len_arg = (gint *)state->in_args[child_cache->c_arg_index].v_pointer;
914             /* if we are not setup yet just set the in arg */
915             if (len_arg == NULL)
916                 state->in_args[child_cache->c_arg_index].v_long = length;
917             else
918                 *len_arg = length;
919         } else {
920             state->in_args[child_cache->c_arg_index].v_long = length;
921         }
922     }
923
924     if (sequence_cache->array_type == GI_ARRAY_TYPE_C) {
925         arg->v_pointer = array_->data;
926         g_array_free (array_, FALSE);
927     } else {
928         arg->v_pointer = array_;
929     }
930
931     return TRUE;
932 }
933
934 gboolean
935 _pygi_marshal_from_py_glist (PyGIInvokeState   *state,
936                              PyGICallableCache *callable_cache,
937                              PyGIArgCache      *arg_cache,
938                              PyObject          *py_arg,
939                              GIArgument        *arg)
940 {
941     PyGIMarshalFromPyFunc from_py_marshaller;
942     int i;
943     Py_ssize_t length;
944     GList *list_ = NULL;
945     PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache;
946
947
948     if (py_arg == Py_None) {
949         arg->v_pointer = NULL;
950         return TRUE;
951     }
952
953     if (!PySequence_Check (py_arg)) {
954         PyErr_Format (PyExc_TypeError, "Must be sequence, not %s",
955                       py_arg->ob_type->tp_name);
956         return FALSE;
957     }
958
959     length = PySequence_Length (py_arg);
960     if (length < 0)
961         return FALSE;
962
963     if (sequence_cache->fixed_size >= 0 &&
964         sequence_cache->fixed_size != length) {
965         PyErr_Format (PyExc_ValueError, "Must contain %zd items, not %zd",
966                       sequence_cache->fixed_size, length);
967
968         return FALSE;
969     }
970
971     from_py_marshaller = sequence_cache->item_cache->from_py_marshaller;
972     for (i = 0; i < length; i++) {
973         GIArgument item;
974         PyObject *py_item = PySequence_GetItem (py_arg, i);
975         if (py_item == NULL)
976             goto err;
977
978         if (!from_py_marshaller ( state,
979                                   callable_cache,
980                                   sequence_cache->item_cache,
981                                   py_item,
982                                  &item))
983             goto err;
984
985         list_ = g_list_prepend (list_, item.v_pointer);
986         continue;
987 err:
988         /* FIXME: clean up list
989         if (sequence_cache->item_cache->from_py_cleanup != NULL) {
990             PyGIMarshalCleanupFunc cleanup = sequence_cache->item_cache->from_py_cleanup;
991         }
992         */
993         g_list_free (list_);
994         _PyGI_ERROR_PREFIX ("Item %i: ", i);
995         return FALSE;
996     }
997
998     arg->v_pointer = g_list_reverse (list_);
999     return TRUE;
1000 }
1001
1002 gboolean
1003 _pygi_marshal_from_py_gslist (PyGIInvokeState   *state,
1004                               PyGICallableCache *callable_cache,
1005                               PyGIArgCache      *arg_cache,
1006                               PyObject          *py_arg,
1007                               GIArgument        *arg)
1008 {
1009     PyGIMarshalFromPyFunc from_py_marshaller;
1010     int i;
1011     Py_ssize_t length;
1012     GSList *list_ = NULL;
1013     PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache;
1014
1015     if (py_arg == Py_None) {
1016         arg->v_pointer = NULL;
1017         return TRUE;
1018     }
1019
1020     if (!PySequence_Check (py_arg)) {
1021         PyErr_Format (PyExc_TypeError, "Must be sequence, not %s",
1022                       py_arg->ob_type->tp_name);
1023         return FALSE;
1024     }
1025
1026     length = PySequence_Length (py_arg);
1027     if (length < 0)
1028         return FALSE;
1029
1030     if (sequence_cache->fixed_size >= 0 &&
1031         sequence_cache->fixed_size != length) {
1032         PyErr_Format (PyExc_ValueError, "Must contain %zd items, not %zd",
1033                       sequence_cache->fixed_size, length);
1034
1035         return FALSE;
1036     }
1037
1038     from_py_marshaller = sequence_cache->item_cache->from_py_marshaller;
1039     for (i = 0; i < length; i++) {
1040         GIArgument item;
1041         PyObject *py_item = PySequence_GetItem (py_arg, i);
1042         if (py_item == NULL)
1043             goto err;
1044
1045         if (!from_py_marshaller ( state,
1046                              callable_cache,
1047                              sequence_cache->item_cache,
1048                              py_item,
1049                             &item))
1050             goto err;
1051
1052         list_ = g_slist_prepend (list_, item.v_pointer);
1053         continue;
1054 err:
1055         /* FIXME: Clean up list
1056         if (sequence_cache->item_cache->from_py_cleanup != NULL) {
1057             PyGIMarshalCleanupFunc cleanup = sequence_cache->item_cache->from_py_cleanup;
1058         }
1059         */
1060
1061         g_slist_free (list_);
1062         _PyGI_ERROR_PREFIX ("Item %i: ", i);
1063         return FALSE;
1064     }
1065
1066     arg->v_pointer = g_slist_reverse (list_);
1067     return TRUE;
1068 }
1069
1070 static gpointer
1071 _pygi_arg_to_hash_pointer (const GIArgument *arg,
1072                            GITypeTag        type_tag)
1073 {
1074     switch (type_tag) {
1075         case GI_TYPE_TAG_INT32:
1076             return GINT_TO_POINTER(arg->v_int32);
1077         case GI_TYPE_TAG_UTF8:
1078         case GI_TYPE_TAG_FILENAME:
1079         case GI_TYPE_TAG_INTERFACE:
1080             return arg->v_pointer;
1081         default:
1082             g_critical("Unsupported type %s", g_type_tag_to_string(type_tag));
1083             return arg->v_pointer;
1084     }
1085 }
1086
1087 gboolean
1088 _pygi_marshal_from_py_ghash (PyGIInvokeState   *state,
1089                              PyGICallableCache *callable_cache,
1090                              PyGIArgCache      *arg_cache,
1091                              PyObject          *py_arg,
1092                              GIArgument        *arg)
1093 {
1094     PyGIMarshalFromPyFunc key_from_py_marshaller;
1095     PyGIMarshalFromPyFunc value_from_py_marshaller;
1096
1097     int i;
1098     Py_ssize_t length;
1099     PyObject *py_keys, *py_values;
1100
1101     GHashFunc hash_func;
1102     GEqualFunc equal_func;
1103
1104     GHashTable *hash_ = NULL;
1105     PyGIHashCache *hash_cache = (PyGIHashCache *)arg_cache;
1106
1107     if (py_arg == Py_None) {
1108         arg->v_pointer = NULL;
1109         return TRUE;
1110     }
1111
1112     py_keys = PyMapping_Keys (py_arg);
1113     if (py_keys == NULL) {
1114         PyErr_Format (PyExc_TypeError, "Must be mapping, not %s",
1115                       py_arg->ob_type->tp_name);
1116         return FALSE;
1117     }
1118
1119     length = PyMapping_Length (py_arg);
1120     if (length < 0) {
1121         Py_DECREF (py_keys);
1122         return FALSE;
1123     }
1124
1125     py_values = PyMapping_Values (py_arg);
1126     if (py_values == NULL) {
1127         Py_DECREF (py_keys);
1128         return FALSE;
1129     }
1130
1131     key_from_py_marshaller = hash_cache->key_cache->from_py_marshaller;
1132     value_from_py_marshaller = hash_cache->value_cache->from_py_marshaller;
1133
1134     switch (hash_cache->key_cache->type_tag) {
1135         case GI_TYPE_TAG_UTF8:
1136         case GI_TYPE_TAG_FILENAME:
1137             hash_func = g_str_hash;
1138             equal_func = g_str_equal;
1139             break;
1140         default:
1141             hash_func = NULL;
1142             equal_func = NULL;
1143     }
1144
1145     hash_ = g_hash_table_new (hash_func, equal_func);
1146     if (hash_ == NULL) {
1147         PyErr_NoMemory ();
1148         Py_DECREF (py_keys);
1149         Py_DECREF (py_values);
1150         return FALSE;
1151     }
1152
1153     for (i = 0; i < length; i++) {
1154         GIArgument key, value;
1155         PyObject *py_key = PyList_GET_ITEM (py_keys, i);
1156         PyObject *py_value = PyList_GET_ITEM (py_values, i);
1157         if (py_key == NULL || py_value == NULL)
1158             goto err;
1159
1160         if (!key_from_py_marshaller ( state,
1161                                       callable_cache,
1162                                       hash_cache->key_cache,
1163                                       py_key,
1164                                      &key))
1165             goto err;
1166
1167         if (!value_from_py_marshaller ( state,
1168                                         callable_cache,
1169                                         hash_cache->value_cache,
1170                                         py_value,
1171                                        &value))
1172             goto err;
1173
1174         g_hash_table_insert (hash_,
1175                              _pygi_arg_to_hash_pointer (&key, hash_cache->key_cache->type_tag),
1176                              _pygi_arg_to_hash_pointer (&value, hash_cache->value_cache->type_tag));
1177         continue;
1178 err:
1179         /* FIXME: cleanup hash keys and values */
1180         Py_XDECREF (py_key);
1181         Py_XDECREF (py_value);
1182         Py_DECREF (py_keys);
1183         Py_DECREF (py_values);
1184         g_hash_table_unref (hash_);
1185         _PyGI_ERROR_PREFIX ("Item %i: ", i);
1186         return FALSE;
1187     }
1188
1189     arg->v_pointer = hash_;
1190     return TRUE;
1191 }
1192
1193 gboolean
1194 _pygi_marshal_from_py_gerror (PyGIInvokeState   *state,
1195                               PyGICallableCache *callable_cache,
1196                               PyGIArgCache      *arg_cache,
1197                               PyObject          *py_arg,
1198                               GIArgument        *arg)
1199 {
1200     PyErr_Format (PyExc_NotImplementedError,
1201                   "Marshalling for GErrors is not implemented");
1202     return FALSE;
1203 }
1204
1205 gboolean
1206 _pygi_marshal_from_py_interface_callback (PyGIInvokeState   *state,
1207                                           PyGICallableCache *callable_cache,
1208                                           PyGIArgCache      *arg_cache,
1209                                           PyObject          *py_arg,
1210                                           GIArgument        *arg)
1211 {
1212     GICallableInfo *callable_info;
1213     PyGICClosure *closure;
1214     PyGIArgCache *user_data_cache = NULL;
1215     PyGIArgCache *destroy_cache = NULL;
1216     PyGICallbackCache *callback_cache;
1217     PyObject *py_user_data = NULL;
1218
1219     callback_cache = (PyGICallbackCache *)arg_cache;
1220
1221     if (callback_cache->user_data_index > 0) {
1222         user_data_cache = callable_cache->args_cache[callback_cache->user_data_index];
1223         if (user_data_cache->py_arg_index < state->n_py_in_args) {
1224             py_user_data = PyTuple_GetItem (state->py_in_args, user_data_cache->py_arg_index);
1225             if (!py_user_data)
1226                 return FALSE;
1227         } else {
1228             py_user_data = Py_None;
1229             Py_INCREF (Py_None);
1230         }
1231     }
1232
1233     if (py_arg == Py_None && !(py_user_data == Py_None || py_user_data == NULL)) {
1234         Py_DECREF (py_user_data);
1235         PyErr_Format (PyExc_TypeError,
1236                       "When passing None for a callback userdata must also be None");
1237
1238         return FALSE;
1239     }
1240
1241     if (py_arg == Py_None) {
1242         Py_XDECREF (py_user_data);
1243         return TRUE;
1244     }
1245
1246     if (!PyCallable_Check (py_arg)) {
1247         Py_XDECREF (py_user_data);
1248         PyErr_Format (PyExc_TypeError,
1249                       "Callback needs to be a function or method not %s",
1250                       py_arg->ob_type->tp_name);
1251
1252         return FALSE;
1253     }
1254
1255     if (callback_cache->destroy_notify_index > 0)
1256         destroy_cache = callable_cache->args_cache[callback_cache->destroy_notify_index];
1257
1258     callable_info = (GICallableInfo *)callback_cache->interface_info;
1259
1260     closure = _pygi_make_native_closure (callable_info, callback_cache->scope, py_arg, py_user_data);
1261     arg->v_pointer = closure->closure;
1262     if (user_data_cache != NULL) {
1263         state->in_args[user_data_cache->c_arg_index].v_pointer = closure;
1264     }
1265
1266     if (destroy_cache) {
1267         PyGICClosure *destroy_notify = _pygi_destroy_notify_create ();
1268         state->in_args[destroy_cache->c_arg_index].v_pointer = destroy_notify->closure;
1269     }
1270
1271     return TRUE;
1272 }
1273
1274 gboolean
1275 _pygi_marshal_from_py_interface_enum (PyGIInvokeState   *state,
1276                                       PyGICallableCache *callable_cache,
1277                                       PyGIArgCache      *arg_cache,
1278                                       PyObject          *py_arg,
1279                                       GIArgument        *arg)
1280 {
1281     PyObject *int_;
1282     gint is_instance;
1283     PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
1284
1285     is_instance = PyObject_IsInstance (py_arg, iface_cache->py_type);
1286
1287     int_ = PYGLIB_PyNumber_Long (py_arg);
1288     if (int_ == NULL) {
1289         PyErr_Clear();
1290         goto err;
1291     }
1292
1293     arg->v_long = PYGLIB_PyLong_AsLong (int_);
1294     Py_DECREF (int_);
1295
1296     /* If this is not an instance of the Enum type that we want
1297      * we need to check if the value is equivilant to one of the
1298      * Enum's memebers */
1299     if (!is_instance) {
1300         int i;
1301         gboolean is_found = FALSE;
1302
1303         for (i = 0; i < g_enum_info_get_n_values (iface_cache->interface_info); i++) {
1304             GIValueInfo *value_info =
1305                 g_enum_info_get_value (iface_cache->interface_info, i);
1306             glong enum_value = g_value_info_get_value (value_info);
1307             g_base_info_unref ( (GIBaseInfo *)value_info);
1308             if (arg->v_long == enum_value) {
1309                 is_found = TRUE;
1310                 break;
1311             }
1312         }
1313
1314         if (!is_found)
1315             goto err;
1316     }
1317
1318     return TRUE;
1319
1320 err:
1321     PyErr_Format (PyExc_TypeError, "Expected a %s, but got %s",
1322                   iface_cache->type_name, py_arg->ob_type->tp_name);
1323     return FALSE;
1324 }
1325
1326 gboolean
1327 _pygi_marshal_from_py_interface_flags (PyGIInvokeState   *state,
1328                                        PyGICallableCache *callable_cache,
1329                                        PyGIArgCache      *arg_cache,
1330                                        PyObject          *py_arg,
1331                                        GIArgument        *arg)
1332 {
1333     PyObject *int_;
1334     gint is_instance;
1335     PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
1336
1337     is_instance = PyObject_IsInstance (py_arg, iface_cache->py_type);
1338
1339     int_ = PYGLIB_PyNumber_Long (py_arg);
1340     if (int_ == NULL) {
1341         PyErr_Clear ();
1342         goto err;
1343     }
1344
1345     arg->v_long = PYGLIB_PyLong_AsLong (int_);
1346     Py_DECREF (int_);
1347
1348     /* only 0 or argument of type Flag is allowed */
1349     if (!is_instance && arg->v_long != 0)
1350         goto err;
1351
1352     return TRUE;
1353
1354 err:
1355     PyErr_Format (PyExc_TypeError, "Expected a %s, but got %s",
1356                   iface_cache->type_name, py_arg->ob_type->tp_name);
1357     return FALSE;
1358
1359 }
1360
1361 gboolean
1362 _pygi_marshal_from_py_interface_struct (PyGIInvokeState   *state,
1363                                         PyGICallableCache *callable_cache,
1364                                         PyGIArgCache      *arg_cache,
1365                                         PyObject          *py_arg,
1366                                         GIArgument        *arg)
1367 {
1368     PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
1369
1370     if (py_arg == Py_None) {
1371         arg->v_pointer = NULL;
1372         return TRUE;
1373     }
1374
1375     /* FIXME: handle this large if statement in the cache
1376      *        and set the correct marshaller
1377      */
1378
1379     if (iface_cache->g_type == G_TYPE_CLOSURE) {
1380         GClosure *closure;
1381         GType object_gtype = pyg_type_from_object_strict (py_arg, FALSE);
1382
1383         if ( !(PyCallable_Check(py_arg) || 
1384                g_type_is_a (object_gtype, G_TYPE_CLOSURE))) {
1385             PyErr_Format (PyExc_TypeError, "Must be callable, not %s",
1386                           py_arg->ob_type->tp_name);
1387             return FALSE;
1388         }
1389
1390         if (g_type_is_a (object_gtype, G_TYPE_CLOSURE))
1391             closure = (GClosure *)pyg_boxed_get (py_arg, void);
1392         else
1393             closure = pyg_closure_new (py_arg, NULL, NULL);
1394
1395         if (closure == NULL) {
1396             PyErr_SetString (PyExc_RuntimeError, "PyObject conversion to GClosure failed");
1397             return FALSE;
1398         }
1399
1400         arg->v_pointer = closure;
1401         return TRUE;
1402     } else if (iface_cache->g_type == G_TYPE_VALUE) {
1403         GValue *value;
1404         GType object_type;
1405
1406         object_type = pyg_type_from_object_strict ( (PyObject *) py_arg->ob_type, FALSE);
1407         if (object_type == G_TYPE_INVALID) {
1408             PyErr_SetString (PyExc_RuntimeError, "unable to retrieve object's GType");
1409             return FALSE;
1410         }
1411
1412         /* if already a gvalue, use that, else marshal into gvalue */
1413         if (object_type == G_TYPE_VALUE) {
1414             value = (GValue *)( (PyGObject *)py_arg)->obj;
1415         } else {
1416             value = g_slice_new0 (GValue);
1417             g_value_init (value, object_type);
1418             if (pyg_value_from_pyobject (value, py_arg) < 0) {
1419                 g_slice_free (GValue, value);
1420                 PyErr_SetString (PyExc_RuntimeError, "PyObject conversion to GValue failed");
1421                 return FALSE;
1422             }
1423         }
1424
1425         arg->v_pointer = value;
1426         return TRUE;
1427     } else if (iface_cache->is_foreign) {
1428         PyObject *success;
1429         success = pygi_struct_foreign_convert_to_g_argument (py_arg,
1430                                                              iface_cache->interface_info,
1431                                                              arg_cache->transfer,
1432                                                              arg);
1433
1434         return (success == Py_None);
1435     } else if (!PyObject_IsInstance (py_arg, iface_cache->py_type)) {
1436         /* first check to see if this is a member of the expected union */
1437         if (!_is_union_member (iface_cache, py_arg)) {
1438             if (!PyErr_Occurred())
1439                 PyErr_Format (PyExc_TypeError, "Expected %s, but got %s",
1440                               iface_cache->type_name,
1441                               iface_cache->py_type->ob_type->tp_name);
1442
1443             return FALSE;
1444         }
1445     }
1446
1447     if (g_type_is_a (iface_cache->g_type, G_TYPE_BOXED)) {
1448         arg->v_pointer = pyg_boxed_get (py_arg, void);
1449         if (arg_cache->transfer == GI_TRANSFER_EVERYTHING) {
1450             arg->v_pointer = g_boxed_copy (iface_cache->g_type, arg->v_pointer);
1451         }
1452     } else if (g_type_is_a (iface_cache->g_type, G_TYPE_POINTER) ||
1453                    g_type_is_a (iface_cache->g_type, G_TYPE_VARIANT) ||
1454                        iface_cache->g_type  == G_TYPE_NONE) {
1455         arg->v_pointer = pyg_pointer_get (py_arg, void);
1456     } else {
1457         PyErr_Format (PyExc_NotImplementedError,
1458                       "structure type '%s' is not supported yet",
1459                       g_type_name(iface_cache->g_type));
1460         return FALSE;
1461     }
1462     return TRUE;
1463 }
1464
1465 gboolean
1466 _pygi_marshal_from_py_interface_boxed (PyGIInvokeState   *state,
1467                                        PyGICallableCache *callable_cache,
1468                                        PyGIArgCache      *arg_cache,
1469                                        PyObject          *py_arg,
1470                                        GIArgument        *arg)
1471 {
1472     PyErr_Format (PyExc_NotImplementedError,
1473                   "Marshalling for this type is not implemented yet");
1474     return FALSE;
1475 }
1476
1477 gboolean
1478 _pygi_marshal_from_py_interface_object (PyGIInvokeState   *state,
1479                                         PyGICallableCache *callable_cache,
1480                                         PyGIArgCache      *arg_cache,
1481                                         PyObject          *py_arg,
1482                                         GIArgument        *arg)
1483 {
1484     if (py_arg == Py_None) {
1485         arg->v_pointer = NULL;
1486         return TRUE;
1487     }
1488
1489     if (!PyObject_IsInstance (py_arg, ( (PyGIInterfaceCache *)arg_cache)->py_type)) {
1490         PyErr_Format (PyExc_TypeError, "Expected %s, but got %s",
1491                       ( (PyGIInterfaceCache *)arg_cache)->type_name,
1492                       ( (PyGIInterfaceCache *)arg_cache)->py_type->ob_type->tp_name);
1493         return FALSE;
1494     }
1495
1496     arg->v_pointer = pygobject_get(py_arg);
1497     if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
1498         g_object_ref (arg->v_pointer);
1499
1500     return TRUE;
1501 }
1502
1503 gboolean
1504 _pygi_marshal_from_py_interface_union (PyGIInvokeState   *state,
1505                                        PyGICallableCache *callable_cache,
1506                                        PyGIArgCache      *arg_cache,
1507                                        PyObject          *py_arg,
1508                                        GIArgument        *arg)
1509 {
1510     PyErr_Format(PyExc_NotImplementedError,
1511                  "Marshalling for this type is not implemented yet");
1512     return FALSE;
1513 }
1514
1515 gboolean _pygi_marshal_from_py_interface_instance (PyGIInvokeState   *state,
1516                                                    PyGICallableCache *callable_cache,
1517                                                    PyGIArgCache      *arg_cache,
1518                                                    PyObject          *py_arg,
1519                                                    GIArgument        *arg)
1520 {
1521     GIInfoType info_type;
1522     PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
1523
1524     info_type = g_base_info_get_type (iface_cache->interface_info);
1525     switch (info_type) {
1526         case GI_INFO_TYPE_UNION:
1527         case GI_INFO_TYPE_STRUCT:
1528         {
1529             GType type = iface_cache->g_type;
1530
1531             if (!PyObject_IsInstance (py_arg, iface_cache->py_type)) {
1532                 /* wait, we might be a member of a union so manually check */
1533                 if (!_is_union_member (iface_cache, py_arg)) {
1534                     if (!PyErr_Occurred())
1535                         PyErr_Format (PyExc_TypeError,
1536                                       "Expected a %s, but got %s",
1537                                       iface_cache->type_name,
1538                                       py_arg->ob_type->tp_name);
1539
1540                     return FALSE;
1541                 }
1542             }
1543
1544             if (g_type_is_a (type, G_TYPE_BOXED)) {
1545                 arg->v_pointer = pyg_boxed_get (py_arg, void);
1546             } else if (g_type_is_a (type, G_TYPE_POINTER) ||
1547                            g_type_is_a (type, G_TYPE_VARIANT) ||
1548                                type == G_TYPE_NONE) {
1549                 arg->v_pointer = pyg_pointer_get (py_arg, void);
1550             } else {
1551                  PyErr_Format (PyExc_TypeError, "unable to convert an instance of '%s'", g_type_name (type));
1552                  return FALSE;
1553             }
1554
1555             break;
1556         }
1557         case GI_INFO_TYPE_OBJECT:
1558         case GI_INFO_TYPE_INTERFACE:
1559             arg->v_pointer = pygobject_get (py_arg);
1560             if (arg->v_pointer != NULL) {
1561                 GType obj_type = G_OBJECT_TYPE (( GObject *)arg->v_pointer);
1562                 GType expected_type = iface_cache->g_type;
1563
1564                 if (!g_type_is_a (obj_type, expected_type)) {
1565                     PyErr_Format (PyExc_TypeError, "Expected a %s, but got %s",
1566                                   iface_cache->type_name,
1567                                   py_arg->ob_type->tp_name);
1568                     return FALSE;
1569                 }
1570             }
1571             break;
1572         default:
1573             /* Other types don't have methods. */
1574             g_assert_not_reached ();
1575    }
1576
1577    return TRUE;
1578 }