02de46ee9afe601df5799488518a19ee669b10ee
[platform/upstream/python-gobject.git] / gi / pygi-invoke.c
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2  * vim: tabstop=4 shiftwidth=4 expandtab
3  *
4  * Copyright (C) 2005-2009 Johan Dahlin <johan@gnome.org>
5  * Copyright (C) 2011 John (J5) Palimier <johnp@redhat.com>
6  *
7  *   pygi-invoke.c: main invocation function
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #include <pyglib.h>
24 #include "pygi-invoke.h"
25 #include "pygi-marshal-cleanup.h"
26 #include "pygi-error.h"
27
28 static gboolean
29 _check_for_unexpected_kwargs (PyGICallableCache *cache,
30                               GHashTable  *arg_name_hash,
31                               PyObject    *py_kwargs)
32 {
33     PyObject *dict_key, *dict_value;
34     Py_ssize_t dict_iter_pos = 0;
35
36     while (PyDict_Next (py_kwargs, &dict_iter_pos, &dict_key, &dict_value)) {
37         PyObject *key;
38
39 #if PY_VERSION_HEX < 0x03000000
40         if (PyString_Check (dict_key)) {
41             Py_INCREF (dict_key);
42             key = dict_key;
43         } else
44 #endif
45         {
46             key = PyUnicode_AsUTF8String (dict_key);
47             if (key == NULL) {
48                 return FALSE;
49             }
50         }
51
52         /* Use extended lookup because it returns whether or not the key actually
53          * exists in the hash table. g_hash_table_lookup returns NULL for keys not
54          * found which maps to index 0 for our hash lookup.
55          */
56         if (!g_hash_table_lookup_extended (arg_name_hash, PyBytes_AsString(key), NULL, NULL)) {
57             char *full_name = pygi_callable_cache_get_full_name (cache);
58             PyErr_Format (PyExc_TypeError,
59                           "%.200s() got an unexpected keyword argument '%.400s'",
60                           full_name,
61                           PyBytes_AsString (key));
62             Py_DECREF (key);
63             g_free (full_name);
64             return FALSE;
65         }
66
67         Py_DECREF (key);
68     }
69     return TRUE;
70 }
71
72 /**
73  * _py_args_combine_and_check_length:
74  * @cache: PyGICallableCache
75  * @py_args: the tuple of positional arguments.
76  * @py_kwargs: the dict of keyword arguments to be merged with py_args.
77  *
78  * Returns: New value reference to the combined py_args and py_kwargs.
79  */
80 static PyObject *
81 _py_args_combine_and_check_length (PyGICallableCache *cache,
82                                    PyObject    *py_args,
83                                    PyObject    *py_kwargs)
84 {
85     PyObject *combined_py_args = NULL;
86     Py_ssize_t n_py_args, n_py_kwargs, i;
87     guint n_expected_args = cache->n_py_args;
88     GSList *l;
89
90     n_py_args = PyTuple_GET_SIZE (py_args);
91     if (py_kwargs == NULL)
92         n_py_kwargs = 0;
93     else
94         n_py_kwargs = PyDict_Size (py_kwargs);
95
96     /* Fast path, we already have the exact number of args and not kwargs. */
97     if (n_py_kwargs == 0 && n_py_args == n_expected_args && cache->user_data_varargs_index < 0) {
98         Py_INCREF (py_args);
99         return py_args;
100     }
101
102     if (cache->user_data_varargs_index < 0 && n_expected_args < n_py_args) {
103         char *full_name = pygi_callable_cache_get_full_name (cache);
104         PyErr_Format (PyExc_TypeError,
105                       "%.200s() takes exactly %d %sargument%s (%zd given)",
106                       full_name,
107                       n_expected_args,
108                       n_py_kwargs > 0 ? "non-keyword " : "",
109                       n_expected_args == 1 ? "" : "s",
110                       n_py_args);
111         g_free (full_name);
112         return NULL;
113     }
114
115     if (cache->user_data_varargs_index >= 0 && n_py_kwargs > 0 && n_expected_args < n_py_args) {
116         char *full_name = pygi_callable_cache_get_full_name (cache);
117         PyErr_Format (PyExc_TypeError,
118                       "%.200s() cannot use variable user data arguments with keyword arguments",
119                       full_name);
120         g_free (full_name);
121         return NULL;
122     }
123
124     if (n_py_kwargs > 0 && !_check_for_unexpected_kwargs (cache,
125                                                           cache->arg_name_hash,
126                                                           py_kwargs)) {
127         return NULL;
128     }
129
130     /* will hold arguments from both py_args and py_kwargs
131      * when they are combined into a single tuple */
132     combined_py_args = PyTuple_New (n_expected_args);
133
134     for (i = 0, l = cache->arg_name_list; i < n_expected_args && l; i++, l = l->next) {
135         PyObject *py_arg_item = NULL;
136         PyObject *kw_arg_item = NULL;
137         const gchar *arg_name = l->data;
138         int arg_cache_index = -1;
139         gboolean is_varargs_user_data = FALSE;
140
141         if (arg_name != NULL)
142             arg_cache_index = GPOINTER_TO_INT (g_hash_table_lookup (cache->arg_name_hash, arg_name));
143
144         is_varargs_user_data = cache->user_data_varargs_index >= 0 &&
145                                 arg_cache_index == cache->user_data_varargs_index;
146
147         if (n_py_kwargs > 0 && arg_name != NULL) {
148             /* NULL means this argument has no keyword name */
149             /* ex. the first argument to a method or constructor */
150             kw_arg_item = PyDict_GetItemString (py_kwargs, arg_name);
151         }
152
153         /* use a bounded retrieval of the original input */
154         if (i < n_py_args)
155             py_arg_item = PyTuple_GET_ITEM (py_args, i);
156
157         if (kw_arg_item == NULL && py_arg_item != NULL) {
158             if (is_varargs_user_data) {
159                 /* For tail end user_data varargs, pull a slice off and we are done. */
160                 PyObject *user_data = PyTuple_GetSlice (py_args, i, PY_SSIZE_T_MAX);
161                 PyTuple_SET_ITEM (combined_py_args, i, user_data);
162                 return combined_py_args;
163             } else {
164                 Py_INCREF (py_arg_item);
165                 PyTuple_SET_ITEM (combined_py_args, i, py_arg_item);
166             }
167         } else if (kw_arg_item != NULL && py_arg_item == NULL) {
168             if (is_varargs_user_data) {
169                 /* Special case where user_data is passed as a keyword argument (user_data=foo)
170                  * Wrap the value in a tuple to represent variable args for marshaling later on.
171                  */
172                 PyObject *user_data = Py_BuildValue("(O)", kw_arg_item, NULL);
173                 PyTuple_SET_ITEM (combined_py_args, i, user_data);
174             } else {
175                 Py_INCREF (kw_arg_item);
176                 PyTuple_SET_ITEM (combined_py_args, i, kw_arg_item);
177             }
178
179         } else if (kw_arg_item == NULL && py_arg_item == NULL) {
180             if (is_varargs_user_data) {
181                 /* For varargs user_data, pass an empty tuple when nothing is given. */
182                 PyTuple_SET_ITEM (combined_py_args, i, PyTuple_New (0));
183             } else if (arg_cache_index >= 0 && _pygi_callable_cache_get_arg (cache, arg_cache_index)->has_default) {
184                 /* If the argument supports a default, use a place holder in the
185                  * argument tuple, this will be checked later during marshaling.
186                  */
187                 Py_INCREF (_PyGIDefaultArgPlaceholder);
188                 PyTuple_SET_ITEM (combined_py_args, i, _PyGIDefaultArgPlaceholder);
189             } else {
190                 char *full_name = pygi_callable_cache_get_full_name (cache);
191                 PyErr_Format (PyExc_TypeError,
192                               "%.200s() takes exactly %d %sargument%s (%zd given)",
193                               full_name,
194                               n_expected_args,
195                               n_py_kwargs > 0 ? "non-keyword " : "",
196                               n_expected_args == 1 ? "" : "s",
197                               n_py_args);
198                 g_free (full_name);
199
200                 Py_DECREF (combined_py_args);
201                 return NULL;
202             }
203         } else if (kw_arg_item != NULL && py_arg_item != NULL) {
204             char *full_name = pygi_callable_cache_get_full_name (cache);
205             PyErr_Format (PyExc_TypeError,
206                           "%.200s() got multiple values for keyword argument '%.200s'",
207                           full_name,
208                           arg_name);
209
210             Py_DECREF (combined_py_args);
211             g_free (full_name);
212             return NULL;
213         }
214     }
215
216     return combined_py_args;
217 }
218
219 /* To reduce calls to g_slice_*() we (1) allocate all the memory depended on
220  * the argument count in one go and (2) keep one version per argument count
221  * around for faster reuse.
222  */
223
224 #define PyGI_INVOKE_ARG_STATE_SIZE(n)   (n * (sizeof (PyGIInvokeArgState) + sizeof (GIArgument *)))
225 #define PyGI_INVOKE_ARG_STATE_N_MAX     10
226 static gpointer free_arg_state[PyGI_INVOKE_ARG_STATE_N_MAX];
227
228 /**
229  * _pygi_invoke_arg_state_init:
230  * Sets PyGIInvokeState.args and PyGIInvokeState.ffi_args.
231  * On error returns FALSE and sets an exception.
232  */
233 gboolean
234 _pygi_invoke_arg_state_init (PyGIInvokeState *state) {
235
236     gpointer mem;
237
238     if (state->n_args < PyGI_INVOKE_ARG_STATE_N_MAX && (mem = free_arg_state[state->n_args]) != NULL) {
239         free_arg_state[state->n_args] = NULL;
240         memset (mem, 0, PyGI_INVOKE_ARG_STATE_SIZE (state->n_args));
241     } else {
242         mem = g_slice_alloc0 (PyGI_INVOKE_ARG_STATE_SIZE (state->n_args));
243     }
244
245     if (mem == NULL && state->n_args != 0) {
246         PyErr_NoMemory();
247         return FALSE;
248     }
249
250     if (mem != NULL) {
251         state->args = mem;
252         state->ffi_args = (gpointer)((gchar *)mem + state->n_args * sizeof (PyGIInvokeArgState));
253     }
254
255     return TRUE;
256 }
257
258 /**
259  * _pygi_invoke_arg_state_free:
260  * Frees PyGIInvokeState.args and PyGIInvokeState.ffi_args
261  */
262 void
263 _pygi_invoke_arg_state_free(PyGIInvokeState *state) {
264     if (state->n_args < PyGI_INVOKE_ARG_STATE_N_MAX && free_arg_state[state->n_args] == NULL) {
265         free_arg_state[state->n_args] = state->args;
266         return;
267     }
268
269     g_slice_free1 (PyGI_INVOKE_ARG_STATE_SIZE (state->n_args), state->args);
270 }
271
272 static gboolean
273 _invoke_state_init_from_cache (PyGIInvokeState *state,
274                                PyGIFunctionCache *function_cache,
275                                PyObject *py_args,
276                                PyObject *kwargs)
277 {
278     PyGICallableCache *cache = (PyGICallableCache *) function_cache;
279
280     state->n_args = _pygi_callable_cache_args_len (cache);
281
282     if (cache->throws) {
283         state->n_args++;
284     }
285
286     /* Copy the function pointer to the state for the normal case. For vfuncs,
287      * this has already been filled out based on the implementor's GType.
288      */
289     if (state->function_ptr == NULL)
290         state->function_ptr = function_cache->invoker.native_address;
291
292     state->py_in_args = _py_args_combine_and_check_length (cache,
293                                                            py_args,
294                                                            kwargs);
295
296     if (state->py_in_args == NULL) {
297         return FALSE;
298     }
299     state->n_py_in_args = PyTuple_Size (state->py_in_args);
300
301     if (!_pygi_invoke_arg_state_init (state)) {
302         return FALSE;
303     }
304
305     state->error = NULL;
306
307     if (cache->throws) {
308         gssize error_index = state->n_args - 1;
309         /* The ffi argument for GError needs to be a triple pointer. */
310         state->args[error_index].arg_pointer.v_pointer = &state->error;
311         state->ffi_args[error_index] = &(state->args[error_index].arg_pointer);
312     }
313
314     return TRUE;
315 }
316
317 static void
318 _invoke_state_clear (PyGIInvokeState *state, PyGIFunctionCache *function_cache)
319 {
320     _pygi_invoke_arg_state_free (state);
321     Py_XDECREF (state->py_in_args);
322 }
323
324 static gboolean
325 _caller_alloc (PyGIArgCache *arg_cache, GIArgument *arg)
326 {
327     if (arg_cache->type_tag == GI_TYPE_TAG_INTERFACE) {
328         PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
329
330         arg->v_pointer = NULL;
331         if (g_type_is_a (iface_cache->g_type, G_TYPE_BOXED)) {
332             arg->v_pointer =
333                 _pygi_boxed_alloc (iface_cache->interface_info, NULL);
334         } else if (iface_cache->g_type == G_TYPE_VALUE) {
335             arg->v_pointer = g_slice_new0 (GValue);
336         } else if (iface_cache->is_foreign) {
337             PyObject *foreign_struct =
338                 pygi_struct_foreign_convert_from_g_argument (
339                     iface_cache->interface_info,
340                     GI_TRANSFER_NOTHING,
341                     NULL);
342
343                 pygi_struct_foreign_convert_to_g_argument (foreign_struct,
344                                                            iface_cache->interface_info,
345                                                            GI_TRANSFER_EVERYTHING,
346                                                            arg);
347         } else {
348                 gssize size = g_struct_info_get_size(
349                     (GIStructInfo *)iface_cache->interface_info);
350                 arg->v_pointer = g_malloc0 (size);
351         }
352     } else if (arg_cache->type_tag == GI_TYPE_TAG_ARRAY) {
353         PyGIArgGArray *array_cache = (PyGIArgGArray *)arg_cache;
354
355         arg->v_pointer = g_array_new (TRUE, TRUE, array_cache->item_size);
356     } else {
357         return FALSE;
358     }
359
360     if (arg->v_pointer == NULL)
361         return FALSE;
362
363
364     return TRUE;
365 }
366
367 /* pygi_invoke_marshal_in_args:
368  *
369  * Fills out the state struct argument lists. arg_values will always hold
370  * actual values marshaled either to or from Python and C. arg_pointers will
371  * hold pointers (via v_pointer) to auxilary value storage. This will normally
372  * point to values stored in arg_values. In the case of caller allocated
373  * out args, arg_pointers[x].v_pointer will point to newly allocated memory.
374  * arg_pointers inserts a level of pointer indirection between arg_values
375  * and the argument list ffi receives when dealing with non-caller allocated
376  * out arguments.
377  *
378  * For example:
379  * [[
380  *  void callee (int *i, int j) { *i = 50 - j; }
381  *  void caller () {
382  *    int i = 0;
383  *    callee (&i, 8);
384  *  }
385  *
386  *  args[0] == &arg_pointers[0];
387  *  arg_pointers[0].v_pointer == &arg_values[0];
388  *  arg_values[0].v_int == 42;
389  *
390  *  args[1] == &arg_values[1];
391  *  arg_values[1].v_int == 8;
392  * ]]
393  *
394  */
395 static gboolean
396 _invoke_marshal_in_args (PyGIInvokeState *state, PyGIFunctionCache *function_cache)
397 {
398     PyGICallableCache *cache = (PyGICallableCache *) function_cache;
399     gssize i;
400
401     if (state->n_py_in_args > cache->n_py_args) {
402         char *full_name = pygi_callable_cache_get_full_name (cache);
403         PyErr_Format (PyExc_TypeError,
404                       "%s() takes exactly %zd argument(s) (%zd given)",
405                       full_name,
406                       cache->n_py_args,
407                       state->n_py_in_args);
408         g_free (full_name);
409         return FALSE;
410     }
411
412     for (i = 0; i < _pygi_callable_cache_args_len (cache); i++) {
413         GIArgument *c_arg = &state->args[i].arg_value;
414         PyGIArgCache *arg_cache = g_ptr_array_index (cache->args_cache, i);
415         PyObject *py_arg = NULL;
416
417         switch (arg_cache->direction) {
418             case PYGI_DIRECTION_FROM_PYTHON:
419                 /* The ffi argument points directly at memory in arg_values. */
420                 state->ffi_args[i] = c_arg;
421
422                 if (arg_cache->meta_type == PYGI_META_ARG_TYPE_CLOSURE) {
423                     state->ffi_args[i]->v_pointer = state->user_data;
424                     continue;
425                 } else if (arg_cache->meta_type != PYGI_META_ARG_TYPE_PARENT)
426                     continue;
427
428                 if (arg_cache->py_arg_index >= state->n_py_in_args) {
429                     char *full_name = pygi_callable_cache_get_full_name (cache);
430                     PyErr_Format (PyExc_TypeError,
431                                   "%s() takes exactly %zd argument(s) (%zd given)",
432                                    full_name,
433                                    cache->n_py_args,
434                                    state->n_py_in_args);
435                     g_free (full_name);
436
437                     /* clean up all of the args we have already marshalled,
438                      * since invoke will not be called
439                      */
440                     pygi_marshal_cleanup_args_from_py_parameter_fail (state,
441                                                                       cache,
442                                                                       i);
443                     return FALSE;
444                 }
445
446                 py_arg =
447                     PyTuple_GET_ITEM (state->py_in_args,
448                                       arg_cache->py_arg_index);
449
450                 break;
451             case PYGI_DIRECTION_BIDIRECTIONAL:
452                 if (arg_cache->meta_type != PYGI_META_ARG_TYPE_CHILD) {
453                     if (arg_cache->py_arg_index >= state->n_py_in_args) {
454                         char *full_name = pygi_callable_cache_get_full_name (cache);
455                         PyErr_Format (PyExc_TypeError,
456                                       "%s() takes exactly %zd argument(s) (%zd given)",
457                                        full_name,
458                                        cache->n_py_args,
459                                        state->n_py_in_args);
460                         g_free (full_name);
461                         pygi_marshal_cleanup_args_from_py_parameter_fail (state,
462                                                                           cache,
463                                                                           i);
464                         return FALSE;
465                     }
466
467                     py_arg =
468                         PyTuple_GET_ITEM (state->py_in_args,
469                                           arg_cache->py_arg_index);
470                 }
471                 /* Fall through */
472
473             case PYGI_DIRECTION_TO_PYTHON:
474                 /* arg_pointers always stores a pointer to the data to be marshaled "to python"
475                  * even in cases where arg_pointers is not being used as indirection between
476                  * ffi and arg_values. This gives a guarantee that out argument marshaling
477                  * (_invoke_marshal_out_args) can always rely on arg_pointers pointing to
478                  * the correct chunk of memory to marshal.
479                  */
480                 state->args[i].arg_pointer.v_pointer = c_arg;
481
482                 if (arg_cache->is_caller_allocates) {
483                     /* In the case of caller allocated out args, we don't use
484                      * an extra level of indirection and state->args will point
485                      * directly at the data to be marshaled. However, as noted
486                      * above, arg_pointers will also point to this caller allocated
487                      * chunk of memory used by out argument marshaling.
488                      */
489                     state->ffi_args[i] = c_arg;
490
491                     if (!_caller_alloc (arg_cache, c_arg)) {
492                         char *full_name = pygi_callable_cache_get_full_name (cache);
493                         PyErr_Format (PyExc_TypeError,
494                                       "Could not caller allocate argument %zd of callable %s",
495                                       i, full_name);
496                         g_free (full_name);
497                         pygi_marshal_cleanup_args_from_py_parameter_fail (state,
498                                                                           cache,
499                                                                           i);
500                         return FALSE;
501                     }
502                 } else {
503                     /* Non-caller allocated out args will use arg_pointers as an
504                      * extra level of indirection */
505                     state->ffi_args[i] = &state->args[i].arg_pointer;
506                 }
507
508                 break;
509         }
510
511         if (py_arg == _PyGIDefaultArgPlaceholder) {
512             *c_arg = arg_cache->default_value;
513         } else if (arg_cache->from_py_marshaller != NULL &&
514                    arg_cache->meta_type != PYGI_META_ARG_TYPE_CHILD) {
515             gboolean success;
516             gpointer cleanup_data = NULL;
517
518             if (!arg_cache->allow_none && py_arg == Py_None) {
519                 PyErr_Format (PyExc_TypeError,
520                               "Argument %zd does not allow None as a value",
521                               i);
522
523                  pygi_marshal_cleanup_args_from_py_parameter_fail (state,
524                                                                    cache,
525                                                                    i);
526                  return FALSE;
527             }
528             success = arg_cache->from_py_marshaller (state,
529                                                      cache,
530                                                      arg_cache,
531                                                      py_arg,
532                                                      c_arg,
533                                                      &cleanup_data);
534             state->args[i].arg_cleanup_data = cleanup_data;
535
536             if (!success) {
537                 pygi_marshal_cleanup_args_from_py_parameter_fail (state,
538                                                                   cache,
539                                                                   i);
540                 return FALSE;
541             }
542
543         }
544
545     }
546
547     return TRUE;
548 }
549
550 static PyObject *
551 _invoke_marshal_out_args (PyGIInvokeState *state, PyGIFunctionCache *function_cache)
552 {
553     PyGICallableCache *cache = (PyGICallableCache *) function_cache;
554     PyObject *py_out = NULL;
555     PyObject *py_return = NULL;
556     gssize n_out_args = cache->n_to_py_args - cache->n_to_py_child_args;
557
558     if (cache->return_cache) {
559         if (!cache->return_cache->is_skipped) {
560             py_return = cache->return_cache->to_py_marshaller ( state,
561                                                                 cache,
562                                                                 cache->return_cache,
563                                                                &state->return_arg);
564             if (py_return == NULL) {
565                 pygi_marshal_cleanup_args_return_fail (state,
566                                                        cache);
567                 return NULL;
568             }
569         } else {
570             if (cache->return_cache->transfer == GI_TRANSFER_EVERYTHING) {
571                 PyGIMarshalCleanupFunc to_py_cleanup =
572                     cache->return_cache->to_py_cleanup;
573
574                 if (to_py_cleanup != NULL)
575                     to_py_cleanup ( state,
576                                     cache->return_cache,
577                                     NULL,
578                                    &state->return_arg,
579                                     FALSE);
580             }
581         }
582     }
583
584     if (n_out_args == 0) {
585         if (cache->return_cache->is_skipped && state->error == NULL) {
586             /* we skip the return value and have no (out) arguments to return,
587              * so py_return should be NULL. But we must not return NULL,
588              * otherwise Python will expect an exception.
589              */
590             g_assert (py_return == NULL);
591             Py_INCREF(Py_None);
592             py_return = Py_None;
593         }
594
595         py_out = py_return;
596     } else if (!cache->has_return && n_out_args == 1) {
597         /* if we get here there is one out arg an no return */
598         PyGIArgCache *arg_cache = (PyGIArgCache *)cache->to_py_args->data;
599         py_out = arg_cache->to_py_marshaller (state,
600                                               cache,
601                                               arg_cache,
602                                               state->args[arg_cache->c_arg_index].arg_pointer.v_pointer);
603         if (py_out == NULL) {
604             pygi_marshal_cleanup_args_to_py_parameter_fail (state,
605                                                             cache,
606                                                             0);
607             return NULL;
608         }
609
610     } else {
611         /* return a tuple */
612         gssize py_arg_index = 0;
613         GSList *cache_item = cache->to_py_args;
614         gssize tuple_len = cache->has_return + n_out_args;
615
616         py_out = pygi_resulttuple_new (cache->resulttuple_type, tuple_len);
617
618         if (py_out == NULL) {
619             pygi_marshal_cleanup_args_to_py_parameter_fail (state,
620                                                             cache,
621                                                             py_arg_index);
622             return NULL;
623         }
624
625         if (cache->has_return) {
626             PyTuple_SET_ITEM (py_out, py_arg_index, py_return);
627             py_arg_index++;
628         }
629
630         for (; py_arg_index < tuple_len; py_arg_index++) {
631             PyGIArgCache *arg_cache = (PyGIArgCache *)cache_item->data;
632             PyObject *py_obj = arg_cache->to_py_marshaller (state,
633                                                             cache,
634                                                             arg_cache,
635                                                             state->args[arg_cache->c_arg_index].arg_pointer.v_pointer);
636
637             if (py_obj == NULL) {
638                 if (cache->has_return)
639                     py_arg_index--;
640
641                 pygi_marshal_cleanup_args_to_py_parameter_fail (state,
642                                                                 cache,
643                                                                 py_arg_index);
644                 Py_DECREF (py_out);
645                 return NULL;
646             }
647
648             PyTuple_SET_ITEM (py_out, py_arg_index, py_obj);
649             cache_item = cache_item->next;
650         }
651     }
652     return py_out;
653 }
654
655 PyObject *
656 pygi_invoke_c_callable (PyGIFunctionCache *function_cache,
657                         PyGIInvokeState *state,
658                         PyObject *py_args,
659                         PyObject *py_kwargs)
660 {
661     PyGICallableCache *cache = (PyGICallableCache *) function_cache;
662     GIFFIReturnValue ffi_return_value = {0};
663     PyObject *ret = NULL;
664
665     if (!_invoke_state_init_from_cache (state, function_cache,
666                                         py_args, py_kwargs))
667          goto err;
668
669     if (!_invoke_marshal_in_args (state, function_cache))
670          goto err;
671
672     Py_BEGIN_ALLOW_THREADS;
673
674         ffi_call (&function_cache->invoker.cif,
675                   state->function_ptr,
676                   (void *) &ffi_return_value,
677                   (void **) state->ffi_args);
678
679     Py_END_ALLOW_THREADS;
680
681     /* If the callable throws, the address of state->error will be bound into
682      * the state->args as the last value. When the callee sets an error using
683      * the state->args passed, it will have the side effect of setting
684      * state->error allowing for easy checking here.
685      */
686     if (state->error != NULL) {
687         if (pygi_error_check (&state->error)) {
688             /* even though we errored out, the call itself was successful,
689                so we assume the call processed all of the parameters */
690             pygi_marshal_cleanup_args_from_py_marshal_success (state, cache);
691             goto err;
692         }
693     }
694
695     if (cache->return_cache) {
696         gi_type_info_extract_ffi_return_value (cache->return_cache->type_info,
697                                                &ffi_return_value,
698                                                &state->return_arg);
699     }
700
701     ret = _invoke_marshal_out_args (state, function_cache);
702     pygi_marshal_cleanup_args_from_py_marshal_success (state, cache);
703
704     if (ret != NULL)
705         pygi_marshal_cleanup_args_to_py_marshal_success (state, cache);
706
707 err:
708     _invoke_state_clear (state, function_cache);
709     return ret;
710 }
711
712 PyObject *
713 pygi_callable_info_invoke (GIBaseInfo *info, PyObject *py_args,
714                            PyObject *kwargs, PyGICallableCache *cache,
715                            gpointer user_data)
716 {
717     return pygi_function_cache_invoke ((PyGIFunctionCache *) cache,
718                                        py_args, kwargs);
719 }
720
721 PyObject *
722 _wrap_g_callable_info_invoke (PyGIBaseInfo *self, PyObject *py_args,
723                               PyObject *kwargs)
724 {
725     if (self->cache == NULL) {
726         PyGIFunctionCache *function_cache;
727         GIInfoType type = g_base_info_get_type (self->info);
728
729         if (type == GI_INFO_TYPE_FUNCTION) {
730             GIFunctionInfoFlags flags;
731
732             flags = g_function_info_get_flags ( (GIFunctionInfo *)self->info);
733
734             if (flags & GI_FUNCTION_IS_CONSTRUCTOR) {
735                 function_cache = pygi_constructor_cache_new (self->info);
736             } else if (flags & GI_FUNCTION_IS_METHOD) {
737                 function_cache = pygi_method_cache_new (self->info);
738             } else {
739                 function_cache = pygi_function_cache_new (self->info);
740             }
741         } else if (type == GI_INFO_TYPE_VFUNC) {
742             function_cache = pygi_vfunc_cache_new (self->info);
743         } else if (type == GI_INFO_TYPE_CALLBACK) {
744             g_error ("Cannot invoke callback types");
745         } else {
746             function_cache = pygi_method_cache_new (self->info);
747         }
748
749         self->cache = (PyGICallableCache *)function_cache;
750         if (self->cache == NULL)
751             return NULL;
752     }
753
754     return pygi_callable_info_invoke (self->info, py_args, kwargs, self->cache, NULL);
755 }