functions: revert the function init order to make pylint happy again. See #217
[pygobject.git] / gi / pygi-closure.c
blobd878876208a4dae93d9a8aa2f3f670ad03b07c49
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
4 * pygi-closure.c: PyGI C Closure functions
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "pygi-closure.h"
21 #include "pygi-error.h"
22 #include "pygi-marshal-cleanup.h"
23 #include "pygi-invoke.h"
24 #include "pygi-ccallback.h"
25 #include "pygi-info.h"
27 extern PyObject *_PyGIDefaultArgPlaceholder;
29 typedef struct _PyGICallbackCache
31 PyGIArgCache arg_cache;
32 gssize user_data_index;
33 gssize destroy_notify_index;
34 GIScopeType scope;
35 GIInterfaceInfo *interface_info;
36 PyGIClosureCache *closure_cache;
37 } PyGICallbackCache;
39 /* This maintains a list of closures which can be free'd whenever
40 as they have been called. We will free them on the next
41 library function call.
43 static GSList* async_free_list;
45 static void
46 _pygi_closure_assign_pyobj_to_retval (gpointer retval,
47 GIArgument *arg,
48 PyGIArgCache *arg_cache)
50 if (retval == NULL)
51 return;
53 switch (arg_cache->type_tag) {
54 case GI_TYPE_TAG_BOOLEAN:
55 *((gboolean *) retval) = arg->v_boolean;
56 break;
57 case GI_TYPE_TAG_INT8:
58 *((gint8 *) retval) = arg->v_int8;
59 break;
60 case GI_TYPE_TAG_UINT8:
61 *((guint8 *) retval) = arg->v_uint8;
62 break;
63 case GI_TYPE_TAG_INT16:
64 *((gint16 *) retval) = arg->v_int16;
65 break;
66 case GI_TYPE_TAG_UINT16:
67 *((guint16 *) retval) = arg->v_uint16;
68 break;
69 case GI_TYPE_TAG_INT32:
70 *((gint32 *) retval) = arg->v_int32;
71 break;
72 case GI_TYPE_TAG_UINT32:
73 *((guint32 *) retval) = arg->v_uint32;
74 break;
75 case GI_TYPE_TAG_INT64:
76 *((gint64 *) retval) = arg->v_int64;
77 break;
78 case GI_TYPE_TAG_UINT64:
79 *((guint64 *) retval) = arg->v_uint64;
80 break;
81 case GI_TYPE_TAG_FLOAT:
82 *((gfloat *) retval) = arg->v_float;
83 break;
84 case GI_TYPE_TAG_DOUBLE:
85 *((gdouble *) retval) = arg->v_double;
86 break;
87 case GI_TYPE_TAG_GTYPE:
88 *((gsize *) retval) = arg->v_size;
89 break;
90 case GI_TYPE_TAG_UNICHAR:
91 *((guint32 *) retval) = arg->v_uint32;
92 break;
93 case GI_TYPE_TAG_INTERFACE:
95 GIBaseInfo *interface_info;
97 interface_info = ((PyGIInterfaceCache *) arg_cache)->interface_info;
99 switch (g_base_info_get_type (interface_info)) {
100 case GI_INFO_TYPE_ENUM:
101 *(gint *) retval = arg->v_int;
102 break;
103 case GI_INFO_TYPE_FLAGS:
104 *(guint *) retval = arg->v_uint;
105 break;
106 default:
107 *(gpointer *) retval = arg->v_pointer;
108 break;
111 break;
113 default:
114 *(gpointer *) retval = arg->v_pointer;
115 break;
119 static void
120 _pygi_closure_assign_pyobj_to_out_argument (gpointer out_arg,
121 GIArgument *arg,
122 PyGIArgCache *arg_cache)
124 if (out_arg == NULL)
125 return;
127 switch (arg_cache->type_tag) {
128 case GI_TYPE_TAG_BOOLEAN:
129 *((gboolean *) out_arg) = arg->v_boolean;
130 break;
131 case GI_TYPE_TAG_INT8:
132 *((gint8 *) out_arg) = arg->v_int8;
133 break;
134 case GI_TYPE_TAG_UINT8:
135 *((guint8 *) out_arg) = arg->v_uint8;
136 break;
137 case GI_TYPE_TAG_INT16:
138 *((gint16 *) out_arg) = arg->v_int16;
139 break;
140 case GI_TYPE_TAG_UINT16:
141 *((guint16 *) out_arg) = arg->v_uint16;
142 break;
143 case GI_TYPE_TAG_INT32:
144 *((gint32 *) out_arg) = arg->v_int32;
145 break;
146 case GI_TYPE_TAG_UINT32:
147 *((guint32 *) out_arg) = arg->v_uint32;
148 break;
149 case GI_TYPE_TAG_INT64:
150 *((gint64 *) out_arg) = arg->v_int64;
151 break;
152 case GI_TYPE_TAG_UINT64:
153 *((guint64 *) out_arg) = arg->v_uint64;
154 break;
155 case GI_TYPE_TAG_FLOAT:
156 *((gfloat *) out_arg) = arg->v_float;
157 break;
158 case GI_TYPE_TAG_DOUBLE:
159 *((gdouble *) out_arg) = arg->v_double;
160 break;
161 case GI_TYPE_TAG_GTYPE:
162 *((GType *) out_arg) = arg->v_size;
163 break;
164 case GI_TYPE_TAG_UNICHAR:
165 *((guint32 *) out_arg) = arg->v_uint32;
166 break;
167 case GI_TYPE_TAG_INTERFACE:
169 GIBaseInfo *interface_info;
171 interface_info = ((PyGIInterfaceCache *) arg_cache)->interface_info;
173 switch (g_base_info_get_type (interface_info)) {
174 case GI_INFO_TYPE_ENUM:
175 *(gint *) out_arg = arg->v_int;
176 break;
177 case GI_INFO_TYPE_FLAGS:
178 *(guint *) out_arg = arg->v_uint;
179 break;
180 case GI_INFO_TYPE_STRUCT:
181 if (!arg_cache->is_pointer) {
182 if (arg->v_pointer != NULL) {
183 gsize item_size = _pygi_g_type_info_size (arg_cache->type_info);
184 memcpy (out_arg, arg->v_pointer, item_size);
186 break;
188 *((gpointer *) out_arg) = arg->v_pointer;
189 break;
190 default:
191 *((gpointer *) out_arg) = arg->v_pointer;
192 break;
194 break;
197 default:
198 *((gpointer *) out_arg) = arg->v_pointer;
199 break;
203 static void
204 _pygi_closure_convert_ffi_arguments (PyGIInvokeArgState *state,
205 PyGICallableCache *cache,
206 void **args)
208 guint i;
210 for (i = 0; i < _pygi_callable_cache_args_len (cache); i++) {
211 PyGIArgCache *arg_cache = g_ptr_array_index (cache->args_cache, i);
212 gpointer arg_pointer;
214 if (arg_cache->direction & PYGI_DIRECTION_FROM_PYTHON) {
215 state[i].arg_value.v_pointer = * (gpointer *) args[i];
217 if (state[i].arg_value.v_pointer == NULL)
218 continue;
220 state[i].arg_pointer.v_pointer = state[i].arg_value.v_pointer;
221 arg_pointer = state[i].arg_value.v_pointer;
222 } else {
223 arg_pointer = args[i];
226 switch (arg_cache->type_tag) {
227 case GI_TYPE_TAG_BOOLEAN:
228 state[i].arg_value.v_boolean = * (gboolean *) arg_pointer;
229 break;
230 case GI_TYPE_TAG_INT8:
231 state[i].arg_value.v_int8 = * (gint8 *) arg_pointer;
232 break;
233 case GI_TYPE_TAG_UINT8:
234 state[i].arg_value.v_uint8 = * (guint8 *) arg_pointer;
235 break;
236 case GI_TYPE_TAG_INT16:
237 state[i].arg_value.v_int16 = * (gint16 *) arg_pointer;
238 break;
239 case GI_TYPE_TAG_UINT16:
240 state[i].arg_value.v_uint16 = * (guint16 *) arg_pointer;
241 break;
242 case GI_TYPE_TAG_INT32:
243 state[i].arg_value.v_int32 = * (gint32 *) arg_pointer;
244 break;
245 case GI_TYPE_TAG_UINT32:
246 state[i].arg_value.v_uint32 = * (guint32 *) arg_pointer;
247 break;
248 case GI_TYPE_TAG_INT64:
249 state[i].arg_value.v_int64 = * (gint64 *) arg_pointer;
250 break;
251 case GI_TYPE_TAG_UINT64:
252 state[i].arg_value.v_uint64 = * (guint64 *) arg_pointer;
253 break;
254 case GI_TYPE_TAG_FLOAT:
255 state[i].arg_value.v_float = * (gfloat *) arg_pointer;
256 break;
257 case GI_TYPE_TAG_DOUBLE:
258 state[i].arg_value.v_double = * (gdouble *) arg_pointer;
259 break;
260 case GI_TYPE_TAG_UTF8:
261 state[i].arg_value.v_string = * (gchar **) arg_pointer;
262 break;
263 case GI_TYPE_TAG_INTERFACE:
265 GIBaseInfo *interface;
266 GIInfoType interface_type;
268 interface = ((PyGIInterfaceCache *) arg_cache)->interface_info;
269 interface_type = g_base_info_get_type (interface);
271 if (interface_type == GI_INFO_TYPE_ENUM) {
272 state[i].arg_value.v_int = * (gint *) arg_pointer;
273 } else if (interface_type == GI_INFO_TYPE_FLAGS) {
274 state[i].arg_value.v_uint = * (guint *) arg_pointer;
275 } else {
276 state[i].arg_value.v_pointer = * (gpointer *) arg_pointer;
278 break;
280 case GI_TYPE_TAG_UNICHAR:
281 state[i].arg_value.v_uint32 = * (guint32 *) arg_pointer;
282 break;
283 case GI_TYPE_TAG_ERROR:
284 case GI_TYPE_TAG_GHASH:
285 case GI_TYPE_TAG_GLIST:
286 case GI_TYPE_TAG_GSLIST:
287 case GI_TYPE_TAG_ARRAY:
288 case GI_TYPE_TAG_VOID:
289 state[i].arg_value.v_pointer = * (gpointer *) arg_pointer;
290 break;
291 default:
292 g_warning ("Unhandled type tag %s",
293 g_type_tag_to_string (arg_cache->type_tag));
294 state[i].arg_value.v_pointer = 0;
298 if (cache->throws) {
299 gssize error_index = _pygi_callable_cache_args_len (cache);
301 state[error_index].arg_value.v_pointer = * (gpointer *) args[error_index];
305 static gboolean
306 _invoke_state_init_from_cache (PyGIInvokeState *state,
307 PyGIClosureCache *closure_cache,
308 void **args)
310 PyGICallableCache *cache = (PyGICallableCache *) closure_cache;
312 state->n_args = _pygi_callable_cache_args_len (cache);
313 state->n_py_in_args = state->n_args;
315 /* Increment after setting the number of Python input args */
316 if (cache->throws) {
317 state->n_args++;
320 state->py_in_args = PyTuple_New (state->n_py_in_args);
321 if (state->py_in_args == NULL) {
322 PyErr_NoMemory ();
323 return FALSE;
326 state->args = NULL;
327 state->error = NULL;
329 if (!_pygi_invoke_arg_state_init (state)) {
330 return FALSE;
333 state->ffi_args = NULL;
335 _pygi_closure_convert_ffi_arguments (state->args, cache, args);
336 return TRUE;
339 static void
340 _invoke_state_clear (PyGIInvokeState *state)
342 _pygi_invoke_arg_state_free (state);
343 Py_XDECREF (state->py_in_args);
346 static gboolean
347 _pygi_closure_convert_arguments (PyGIInvokeState *state,
348 PyGIClosureCache *closure_cache)
350 PyGICallableCache *cache = (PyGICallableCache *) closure_cache;
351 gssize n_in_args = 0;
352 gssize i;
354 for (i = 0; (gsize)i < _pygi_callable_cache_args_len (cache); i++) {
355 PyGIArgCache *arg_cache;
357 arg_cache = g_ptr_array_index (cache->args_cache, i);
359 if (arg_cache->direction & PYGI_DIRECTION_TO_PYTHON) {
360 PyObject *value;
362 if (cache->user_data_index == i) {
363 if (state->user_data == NULL) {
364 /* user_data can be NULL for connect functions which don't accept
365 * user_data or as the default for user_data in the middle of function
366 * arguments.
368 Py_INCREF (Py_None);
369 value = Py_None;
370 } else {
371 /* Extend the callbacks args with user_data as variable args. */
372 gssize j, user_data_len;
373 PyObject *py_user_data = state->user_data;
375 if (!PyTuple_Check (py_user_data)) {
376 PyErr_SetString (PyExc_TypeError, "expected tuple for callback user_data");
377 return FALSE;
380 user_data_len = PyTuple_Size (py_user_data);
381 _PyTuple_Resize (&state->py_in_args,
382 state->n_py_in_args + user_data_len - 1);
384 for (j = 0; j < user_data_len; j++, n_in_args++) {
385 value = PyTuple_GetItem (py_user_data, j);
386 Py_INCREF (value);
387 PyTuple_SET_ITEM (state->py_in_args, n_in_args, value);
389 /* We can assume user_data args are never going to be inout,
390 * so just continue here.
392 continue;
394 } else if (arg_cache->meta_type != PYGI_META_ARG_TYPE_PARENT) {
395 continue;
396 } else {
397 gpointer cleanup_data = NULL;
399 value = arg_cache->to_py_marshaller (state,
400 cache,
401 arg_cache,
402 &state->args[i].arg_value,
403 &cleanup_data);
404 state->args[i].to_py_arg_cleanup_data = cleanup_data;
406 if (value == NULL) {
407 pygi_marshal_cleanup_args_to_py_parameter_fail (state,
408 cache,
410 return FALSE;
414 PyTuple_SET_ITEM (state->py_in_args, n_in_args, value);
415 n_in_args++;
419 if (_PyTuple_Resize (&state->py_in_args, n_in_args) == -1)
420 return FALSE;
422 return TRUE;
425 static gboolean
426 _pygi_closure_set_out_arguments (PyGIInvokeState *state,
427 PyGICallableCache *cache,
428 PyObject *py_retval,
429 void *resp)
431 gssize i;
432 gssize i_py_retval = 0;
433 gboolean success;
435 if (cache->return_cache->type_tag != GI_TYPE_TAG_VOID) {
436 PyObject *item = py_retval;
438 if (PyTuple_Check (py_retval)) {
439 item = PyTuple_GET_ITEM (py_retval, 0);
442 success = cache->return_cache->from_py_marshaller (state,
443 cache,
444 cache->return_cache,
445 item,
446 &state->return_arg,
447 &state->args[0].arg_cleanup_data);
449 if (!success) {
450 pygi_marshal_cleanup_args_return_fail (state,
451 cache);
452 return FALSE;
455 _pygi_closure_assign_pyobj_to_retval (resp, &state->return_arg,
456 cache->return_cache);
457 i_py_retval++;
460 for (i = 0; (gsize)i < _pygi_callable_cache_args_len (cache); i++) {
461 PyGIArgCache *arg_cache = g_ptr_array_index (cache->args_cache, i);
463 if (arg_cache->direction & PYGI_DIRECTION_FROM_PYTHON) {
464 PyObject *item = py_retval;
466 if (arg_cache->type_tag == GI_TYPE_TAG_ERROR) {
467 * (GError **) state->args[i].arg_pointer.v_pointer = NULL;
468 continue;
471 if (PyTuple_Check (py_retval)) {
472 item = PyTuple_GET_ITEM (py_retval, i_py_retval);
473 } else if (i_py_retval != 0) {
474 pygi_marshal_cleanup_args_to_py_parameter_fail (state,
475 cache,
476 i_py_retval);
477 return FALSE;
480 success = arg_cache->from_py_marshaller (state,
481 cache,
482 arg_cache,
483 item,
484 &state->args[i].arg_value,
485 &state->args[i_py_retval].arg_cleanup_data);
487 if (!success) {
488 pygi_marshal_cleanup_args_to_py_parameter_fail (state,
489 cache,
490 i_py_retval);
491 return FALSE;
494 _pygi_closure_assign_pyobj_to_out_argument (state->args[i].arg_pointer.v_pointer,
495 &state->args[i].arg_value, arg_cache);
497 i_py_retval++;
501 return TRUE;
504 static void
505 _pygi_closure_clear_retvals (PyGIInvokeState *state,
506 PyGICallableCache *cache,
507 gpointer resp)
509 gsize i;
510 GIArgument arg = { 0, };
512 if (cache->return_cache->type_tag != GI_TYPE_TAG_VOID) {
513 _pygi_closure_assign_pyobj_to_retval (resp, &arg,
514 cache->return_cache);
517 for (i = 0; i < _pygi_callable_cache_args_len (cache); i++) {
518 PyGIArgCache *arg_cache = g_ptr_array_index (cache->args_cache, i);
520 if (arg_cache->direction & PYGI_DIRECTION_FROM_PYTHON) {
521 _pygi_closure_assign_pyobj_to_out_argument (state->args[i].arg_pointer.v_pointer,
522 &arg, arg_cache);
526 if (cache->throws) {
527 gssize error_index = state->n_args - 1;
528 GError **error = (GError **) state->args[error_index].arg_value.v_pointer;
530 if (error != NULL) {
531 pygi_gerror_exception_check (error);
536 static void
537 _pygi_invoke_closure_clear_py_data(PyGICClosure *invoke_closure)
539 PyGILState_STATE state = PyGILState_Ensure();
541 Py_CLEAR (invoke_closure->function);
542 Py_CLEAR (invoke_closure->user_data);
544 PyGILState_Release (state);
547 void
548 _pygi_closure_handle (ffi_cif *cif,
549 void *result,
550 void **args,
551 void *data)
553 PyGILState_STATE py_state;
554 PyGICClosure *closure = data;
555 PyObject *retval;
556 gboolean success;
557 PyGIInvokeState state = { 0, };
559 /* Ignore closures when Python is not initialized. This can happen in cases
560 * where calling Python implemented vfuncs can happen at shutdown time.
561 * See: https://bugzilla.gnome.org/show_bug.cgi?id=722562 */
562 if (!Py_IsInitialized()) {
563 return;
566 /* Lock the GIL as we are coming into this code without the lock and we
567 may be executing python code */
568 py_state = PyGILState_Ensure ();
570 if (closure->cache == NULL)
571 goto end;
573 state.user_data = closure->user_data;
575 _invoke_state_init_from_cache (&state, closure->cache, args);
577 if (!_pygi_closure_convert_arguments (&state, closure->cache)) {
578 _pygi_closure_clear_retvals (&state, closure->cache, result);
579 goto end;
582 retval = PyObject_CallObject ( (PyObject *) closure->function, state.py_in_args);
584 if (retval == NULL) {
585 _pygi_closure_clear_retvals (&state, closure->cache, result);
586 goto end;
589 pygi_marshal_cleanup_args_to_py_marshal_success (&state, closure->cache);
590 success = _pygi_closure_set_out_arguments (&state, closure->cache, retval, result);
592 if (!success) {
593 pygi_marshal_cleanup_args_from_py_marshal_success (&state, closure->cache);
594 _pygi_closure_clear_retvals (&state, closure->cache, result);
597 Py_DECREF (retval);
599 end:
601 if (PyErr_Occurred ())
602 PyErr_Print ();
604 /* Now that the closure has finished we can make a decision about how
605 to free it. Scope call gets free'd at the end of wrap_g_function_info_invoke.
606 Scope notified will be freed when the notify is called.
607 Scope async closures free only their python data now and the closure later
608 during the next creation of a closure. This minimizes potential ref leaks
609 at least in regards to the python objects.
610 (you can't free the closure you are currently using!)
612 switch (closure->scope) {
613 case GI_SCOPE_TYPE_CALL:
614 case GI_SCOPE_TYPE_NOTIFIED:
615 break;
616 case GI_SCOPE_TYPE_ASYNC:
617 /* Append this PyGICClosure to a list of closure that we will free
618 after we're done with this function invokation */
619 _pygi_invoke_closure_clear_py_data(closure);
620 async_free_list = g_slist_prepend (async_free_list, closure);
621 break;
622 default:
623 g_error ("Invalid scope reached inside %s. Possibly a bad annotation?",
624 g_base_info_get_name (closure->info));
627 _invoke_state_clear (&state);
628 PyGILState_Release (py_state);
631 void _pygi_invoke_closure_free (gpointer data)
633 PyGICClosure* invoke_closure = (PyGICClosure *) data;
635 g_callable_info_free_closure (invoke_closure->info,
636 invoke_closure->closure);
638 if (invoke_closure->info)
639 g_base_info_unref ( (GIBaseInfo*) invoke_closure->info);
641 invoke_closure->cache = NULL;
643 _pygi_invoke_closure_clear_py_data(invoke_closure);
645 g_slice_free (PyGICClosure, invoke_closure);
649 PyGICClosure*
650 _pygi_make_native_closure (GICallableInfo* info,
651 PyGIClosureCache *cache,
652 GIScopeType scope,
653 PyObject *py_function,
654 gpointer py_user_data)
656 PyGICClosure *closure;
657 ffi_closure *fficlosure;
659 /* Begin by cleaning up old async functions */
660 g_slist_free_full (async_free_list, (GDestroyNotify) _pygi_invoke_closure_free);
661 async_free_list = NULL;
663 /* Build the closure itself */
664 closure = g_slice_new0 (PyGICClosure);
665 closure->info = (GICallableInfo *) g_base_info_ref ( (GIBaseInfo *) info);
666 closure->function = py_function;
667 closure->user_data = py_user_data;
668 closure->cache = cache;
670 Py_INCREF (py_function);
671 Py_XINCREF (closure->user_data);
673 fficlosure =
674 g_callable_info_prepare_closure (info, &closure->cif, _pygi_closure_handle,
675 closure);
676 closure->closure = fficlosure;
678 /* Give the closure the information it needs to determine when
679 to free itself later */
680 closure->scope = scope;
682 return closure;
685 /* _pygi_destroy_notify_dummy:
687 * Dummy method used in the occasion when a method has a GDestroyNotify
688 * argument without user data.
690 static void
691 _pygi_destroy_notify_dummy (gpointer data) {
694 static gboolean
695 _pygi_marshal_from_py_interface_callback (PyGIInvokeState *state,
696 PyGICallableCache *callable_cache,
697 PyGIArgCache *arg_cache,
698 PyObject *py_arg,
699 GIArgument *arg,
700 gpointer *cleanup_data)
702 GICallableInfo *callable_info;
703 PyGICClosure *closure;
704 PyGIArgCache *user_data_cache = NULL;
705 PyGIArgCache *destroy_cache = NULL;
706 PyGICallbackCache *callback_cache;
707 PyObject *py_user_data = NULL;
709 callback_cache = (PyGICallbackCache *)arg_cache;
711 if (callback_cache->user_data_index > 0) {
712 user_data_cache = _pygi_callable_cache_get_arg (callable_cache, (guint)callback_cache->user_data_index);
713 if (user_data_cache->py_arg_index < state->n_py_in_args) {
714 /* py_user_data is a borrowed reference. */
715 py_user_data = PyTuple_GetItem (state->py_in_args, user_data_cache->py_arg_index);
716 if (!py_user_data)
717 return FALSE;
718 /* NULL out user_data if it was not supplied and the default arg placeholder
719 * was used instead.
721 if (py_user_data == _PyGIDefaultArgPlaceholder) {
722 py_user_data = NULL;
723 } else if (callable_cache->user_data_varargs_index < 0) {
724 /* For non-variable length user data, place the user data in a
725 * single item tuple which is concatenated to the callbacks arguments.
726 * This allows callback input arg marshaling to always expect a
727 * tuple for user data. Note the
729 py_user_data = Py_BuildValue("(O)", py_user_data, NULL);
730 } else {
731 /* increment the ref borrowed from PyTuple_GetItem above */
732 Py_INCREF (py_user_data);
737 if (py_arg == Py_None) {
738 return TRUE;
741 if (!PyCallable_Check (py_arg)) {
742 PyErr_Format (PyExc_TypeError,
743 "Callback needs to be a function or method not %s",
744 Py_TYPE (py_arg)->tp_name);
746 return FALSE;
749 callable_info = (GICallableInfo *)callback_cache->interface_info;
751 closure = _pygi_make_native_closure (
752 callable_info, callback_cache->closure_cache, callback_cache->scope,
753 py_arg, py_user_data);
754 arg->v_pointer = closure->closure;
756 /* always decref the user data as _pygi_make_native_closure adds its own ref */
757 Py_XDECREF (py_user_data);
759 /* The PyGICClosure instance is used as user data passed into the C function.
760 * The return trip to python will marshal this back and pull the python user data out.
762 if (user_data_cache != NULL) {
763 state->args[user_data_cache->c_arg_index].arg_value.v_pointer = closure;
766 /* Setup a GDestroyNotify callback if this method supports it along with
767 * a user data field. The user data field is a requirement in order
768 * free resources and ref counts associated with this arguments closure.
769 * In case a user data field is not available, show a warning giving
770 * explicit information and setup a dummy notification to avoid a crash
771 * later on in _pygi_destroy_notify_callback_closure.
773 if (callback_cache->destroy_notify_index > 0) {
774 destroy_cache = _pygi_callable_cache_get_arg (callable_cache, (guint)callback_cache->destroy_notify_index);
777 if (destroy_cache) {
778 if (user_data_cache != NULL) {
779 state->args[destroy_cache->c_arg_index].arg_value.v_pointer = _pygi_invoke_closure_free;
780 } else {
781 char *full_name = pygi_callable_cache_get_full_name (callable_cache);
782 gchar *msg = g_strdup_printf("Callables passed to %s will leak references because "
783 "the method does not support a user_data argument. "
784 "See: https://bugzilla.gnome.org/show_bug.cgi?id=685598",
785 full_name);
786 g_free (full_name);
787 if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 2)) {
788 g_free(msg);
789 _pygi_invoke_closure_free(closure);
790 return FALSE;
792 g_free(msg);
793 state->args[destroy_cache->c_arg_index].arg_value.v_pointer = _pygi_destroy_notify_dummy;
797 /* Use the PyGIClosure as data passed to cleanup for GI_SCOPE_TYPE_CALL. */
798 *cleanup_data = closure;
800 return TRUE;
803 static PyObject *
804 _pygi_marshal_to_py_interface_callback (PyGIInvokeState *state,
805 PyGICallableCache *callable_cache,
806 PyGIArgCache *arg_cache,
807 GIArgument *arg,
808 gpointer *arg_cleanup_data)
810 PyGICallbackCache *callback_cache = (PyGICallbackCache *) arg_cache;
811 gssize user_data_index;
812 gssize destroy_notify_index;
813 gpointer user_data = NULL;
814 GDestroyNotify destroy_notify = NULL;
816 user_data_index = callback_cache->user_data_index;
817 destroy_notify_index = callback_cache->destroy_notify_index;
819 if (user_data_index != -1)
820 user_data = state->args[user_data_index].arg_value.v_pointer;
822 if (destroy_notify_index != -1)
823 destroy_notify = state->args[destroy_notify_index].arg_value.v_pointer;
825 return _pygi_ccallback_new (arg->v_pointer,
826 user_data,
827 callback_cache->scope,
828 (GIFunctionInfo *) callback_cache->interface_info,
829 destroy_notify);
832 static void
833 _callback_cache_free_func (PyGICallbackCache *cache)
835 if (cache != NULL) {
836 if (cache->interface_info != NULL)
837 g_base_info_unref ( (GIBaseInfo *)cache->interface_info);
839 if (cache->closure_cache != NULL) {
840 pygi_callable_cache_free ((PyGICallableCache *) cache->closure_cache);
841 cache->closure_cache = NULL;
844 g_slice_free (PyGICallbackCache, cache);
848 static void
849 _pygi_marshal_cleanup_from_py_interface_callback (PyGIInvokeState *state,
850 PyGIArgCache *arg_cache,
851 PyObject *py_arg,
852 gpointer data,
853 gboolean was_processed)
855 PyGICallbackCache *callback_cache = (PyGICallbackCache *)arg_cache;
857 if (was_processed && callback_cache->scope == GI_SCOPE_TYPE_CALL) {
858 _pygi_invoke_closure_free (data);
862 static gboolean
863 pygi_arg_callback_setup_from_info (PyGICallbackCache *arg_cache,
864 GITypeInfo *type_info,
865 GIArgInfo *arg_info, /* may be null */
866 GITransfer transfer,
867 PyGIDirection direction,
868 GIInterfaceInfo *iface_info,
869 PyGICallableCache *callable_cache)
871 PyGIArgCache *cache = (PyGIArgCache *)arg_cache;
872 gssize child_offset = 0;
874 if (!pygi_arg_base_setup ((PyGIArgCache *)arg_cache,
875 type_info,
876 arg_info,
877 transfer,
878 direction)) {
879 return FALSE;
882 if (callable_cache != NULL)
883 child_offset = callable_cache->args_offset;
885 ( (PyGIArgCache *)arg_cache)->destroy_notify = (GDestroyNotify)_callback_cache_free_func;
887 arg_cache->user_data_index = g_arg_info_get_closure (arg_info);
888 if (arg_cache->user_data_index != -1)
889 arg_cache->user_data_index += child_offset;
891 arg_cache->destroy_notify_index = g_arg_info_get_destroy (arg_info);
892 if (arg_cache->destroy_notify_index != -1)
893 arg_cache->destroy_notify_index += child_offset;
895 if (arg_cache->user_data_index >= 0) {
896 PyGIArgCache *user_data_arg_cache = pygi_arg_cache_alloc ();
897 user_data_arg_cache->meta_type = PYGI_META_ARG_TYPE_CHILD_WITH_PYARG;
898 user_data_arg_cache->direction = direction;
899 user_data_arg_cache->has_default = TRUE; /* always allow user data with a NULL default. */
900 _pygi_callable_cache_set_arg (callable_cache, (guint)arg_cache->user_data_index,
901 user_data_arg_cache);
904 if (arg_cache->destroy_notify_index >= 0) {
905 PyGIArgCache *destroy_arg_cache = pygi_arg_cache_alloc ();
906 destroy_arg_cache->meta_type = PYGI_META_ARG_TYPE_CHILD;
907 destroy_arg_cache->direction = direction;
908 _pygi_callable_cache_set_arg (callable_cache, (guint)arg_cache->destroy_notify_index,
909 destroy_arg_cache);
912 arg_cache->scope = g_arg_info_get_scope (arg_info);
913 g_base_info_ref( (GIBaseInfo *)iface_info);
914 arg_cache->interface_info = iface_info;
916 if (direction & PYGI_DIRECTION_FROM_PYTHON) {
917 arg_cache->closure_cache = pygi_closure_cache_new (arg_cache->interface_info);
918 cache->from_py_marshaller = _pygi_marshal_from_py_interface_callback;
919 cache->from_py_cleanup = _pygi_marshal_cleanup_from_py_interface_callback;
922 if (direction & PYGI_DIRECTION_TO_PYTHON) {
923 cache->to_py_marshaller = _pygi_marshal_to_py_interface_callback;
926 return TRUE;
929 PyGIArgCache *
930 pygi_arg_callback_new_from_info (GITypeInfo *type_info,
931 GIArgInfo *arg_info, /* may be null */
932 GITransfer transfer,
933 PyGIDirection direction,
934 GIInterfaceInfo *iface_info,
935 PyGICallableCache *callable_cache)
937 gboolean res = FALSE;
938 PyGICallbackCache *callback_cache;
940 callback_cache = g_slice_new0 (PyGICallbackCache);
941 if (callback_cache == NULL)
942 return NULL;
944 res = pygi_arg_callback_setup_from_info (callback_cache,
945 type_info,
946 arg_info,
947 transfer,
948 direction,
949 iface_info,
950 callable_cache);
951 if (res) {
952 return (PyGIArgCache *)callback_cache;
953 } else {
954 pygi_arg_cache_free ((PyGIArgCache *)callback_cache);
955 return NULL;