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