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