17b283f4a84bfeb17f7c89d629ddb182252d3f3f
[platform/upstream/pygobject2.git] / gi / pygi-closure.c
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2  * vim: tabstop=4 shiftwidth=4 expandtab
3  *
4  *   pygi-closure.c: PyGI C Closure functions
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
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"
26
27 extern PyObject *_PyGIDefaultArgPlaceholder;
28
29 typedef struct _PyGICallbackCache
30 {
31     PyGIArgCache arg_cache;
32     gssize user_data_index;
33     gssize destroy_notify_index;
34     GIScopeType scope;
35     GIInterfaceInfo *interface_info;
36 } PyGICallbackCache;
37
38 /* This maintains a list of closures which can be free'd whenever
39    as they have been called.  We will free them on the next
40    library function call.
41  */
42 static GSList* async_free_list;
43
44 static void
45 _pygi_closure_assign_pyobj_to_retval (gpointer retval,
46                                       GIArgument *arg,
47                                       PyGIArgCache *arg_cache)
48 {
49     if (retval == NULL)
50         return;
51
52     switch (arg_cache->type_tag) {
53         case GI_TYPE_TAG_BOOLEAN:
54            *((ffi_sarg *) retval) = arg->v_boolean;
55            break;
56         case GI_TYPE_TAG_INT8:
57            *((ffi_sarg *) retval) = arg->v_int8;
58            break;
59         case GI_TYPE_TAG_UINT8:
60            *((ffi_arg *) retval) = arg->v_uint8;
61            break;
62         case GI_TYPE_TAG_INT16:
63            *((ffi_sarg *) retval) = arg->v_int16;
64            break;
65         case GI_TYPE_TAG_UINT16:
66            *((ffi_arg *) retval) = arg->v_uint16;
67            break;
68         case GI_TYPE_TAG_INT32:
69            *((ffi_sarg *) retval) = arg->v_int32;
70            break;
71         case GI_TYPE_TAG_UINT32:
72            *((ffi_arg *) retval) = arg->v_uint32;
73            break;
74         case GI_TYPE_TAG_INT64:
75            *((ffi_sarg *) retval) = arg->v_int64;
76            break;
77         case GI_TYPE_TAG_UINT64:
78            *((ffi_arg *) retval) = arg->v_uint64;
79            break;
80         case GI_TYPE_TAG_FLOAT:
81            *((gfloat *) retval) = arg->v_float;
82            break;
83         case GI_TYPE_TAG_DOUBLE:
84            *((gdouble *) retval) = arg->v_double;
85            break;
86         case GI_TYPE_TAG_GTYPE:
87            *((ffi_arg *) retval) = arg->v_ulong;
88            break;
89         case GI_TYPE_TAG_UNICHAR:
90             *((ffi_arg *) retval) = arg->v_uint32;
91             break;
92         case GI_TYPE_TAG_INTERFACE:
93             {
94                 GIBaseInfo *interface_info;
95
96                 interface_info = ((PyGIInterfaceCache *) arg_cache)->interface_info;
97
98                 switch (g_base_info_get_type (interface_info)) {
99                 case GI_INFO_TYPE_ENUM:
100                     *(ffi_sarg *) retval = arg->v_int;
101                     break;
102                 case GI_INFO_TYPE_FLAGS:
103                     *(ffi_arg *) retval = arg->v_uint;
104                     break;
105                 default:
106                     *(ffi_arg *) retval = (ffi_arg) arg->v_pointer;
107                     break;
108                 }
109
110                 break;
111             }
112         default:
113             *(ffi_arg *) retval = (ffi_arg) arg->v_pointer;
114             break;
115       }
116 }
117
118 static void
119 _pygi_closure_assign_pyobj_to_out_argument (gpointer out_arg,
120                                             GIArgument *arg,
121                                             PyGIArgCache *arg_cache)
122 {
123     if (out_arg == NULL)
124         return;
125
126     switch (arg_cache->type_tag) {
127         case GI_TYPE_TAG_BOOLEAN:
128            *((gboolean *) out_arg) = arg->v_boolean;
129            break;
130         case GI_TYPE_TAG_INT8:
131            *((gint8 *) out_arg) = arg->v_int8;
132            break;
133         case GI_TYPE_TAG_UINT8:
134            *((guint8 *) out_arg) = arg->v_uint8;
135            break;
136         case GI_TYPE_TAG_INT16:
137            *((gint16 *) out_arg) = arg->v_int16;
138            break;
139         case GI_TYPE_TAG_UINT16:
140            *((guint16 *) out_arg) = arg->v_uint16;
141            break;
142         case GI_TYPE_TAG_INT32:
143            *((gint32 *) out_arg) = arg->v_int32;
144            break;
145         case GI_TYPE_TAG_UINT32:
146            *((guint32 *) out_arg) = arg->v_uint32;
147            break;
148         case GI_TYPE_TAG_INT64:
149            *((gint64 *) out_arg) = arg->v_int64;
150            break;
151         case GI_TYPE_TAG_UINT64:
152            *((glong *) out_arg) = arg->v_uint64;
153            break;
154         case GI_TYPE_TAG_FLOAT:
155            *((gfloat *) out_arg) = arg->v_float;
156            break;
157         case GI_TYPE_TAG_DOUBLE:
158            *((gdouble *) out_arg) = arg->v_double;
159            break;
160         case GI_TYPE_TAG_GTYPE:
161            *((gulong *) out_arg) = arg->v_ulong;
162            break;
163         case GI_TYPE_TAG_UNICHAR:
164             *((guint32 *) out_arg) = arg->v_uint32;
165             break;
166         case GI_TYPE_TAG_INTERFACE:
167         {
168            GIBaseInfo *interface_info;
169
170            interface_info = ((PyGIInterfaceCache *) arg_cache)->interface_info;
171
172            switch (g_base_info_get_type (interface_info)) {
173            case GI_INFO_TYPE_ENUM:
174                *(gint *) out_arg = arg->v_int;
175                break;
176            case GI_INFO_TYPE_FLAGS:
177                *(guint *) out_arg = arg->v_uint;
178                break;
179            case GI_INFO_TYPE_STRUCT:
180                if (!arg_cache->is_pointer) {
181                    if (arg->v_pointer != NULL) {
182                        gsize item_size = _pygi_g_type_info_size (arg_cache->type_info);
183                        memcpy (out_arg, arg->v_pointer, item_size);
184                    }
185                    break;
186                }
187
188            /* Fall through if pointer */
189            default:
190                *((gpointer *) out_arg) = arg->v_pointer;
191                break;
192            }
193            break;
194         }
195
196         default:
197            *((gpointer *) out_arg) = arg->v_pointer;
198            break;
199       }
200 }
201
202 static void
203 _pygi_closure_convert_ffi_arguments (PyGIInvokeArgState *state,
204                                      PyGICallableCache *cache,
205                                      void **args)
206 {
207     guint i;
208
209     for (i = 0; i < _pygi_callable_cache_args_len (cache); i++) {
210         PyGIArgCache *arg_cache = g_ptr_array_index (cache->args_cache, i);
211
212         if (arg_cache->direction & PYGI_DIRECTION_FROM_PYTHON) {
213             state[i].arg_value.v_pointer = * (gpointer *) args[i];
214
215             if (state[i].arg_value.v_pointer == NULL)
216                 continue;
217
218             state[i].arg_pointer.v_pointer = state[i].arg_value.v_pointer;
219             state[i].arg_value = *(GIArgument *) state[i].arg_value.v_pointer;
220             continue;
221         }
222
223         switch (arg_cache->type_tag) {
224             case GI_TYPE_TAG_BOOLEAN:
225                 state[i].arg_value.v_boolean = * (gboolean *) args[i];
226                 break;
227             case GI_TYPE_TAG_INT8:
228                 state[i].arg_value.v_int8 = * (gint8 *) args[i];
229                 break;
230             case GI_TYPE_TAG_UINT8:
231                 state[i].arg_value.v_uint8 = * (guint8 *) args[i];
232                 break;
233             case GI_TYPE_TAG_INT16:
234                 state[i].arg_value.v_int16 = * (gint16 *) args[i];
235                 break;
236             case GI_TYPE_TAG_UINT16:
237                 state[i].arg_value.v_uint16 = * (guint16 *) args[i];
238                 break;
239             case GI_TYPE_TAG_INT32:
240                 state[i].arg_value.v_int32 = * (gint32 *) args[i];
241                 break;
242             case GI_TYPE_TAG_UINT32:
243                 state[i].arg_value.v_uint32 = * (guint32 *) args[i];
244                 break;
245             case GI_TYPE_TAG_INT64:
246                 state[i].arg_value.v_int64 = * (glong *) args[i];
247                 break;
248             case GI_TYPE_TAG_UINT64:
249                 state[i].arg_value.v_uint64 = * (glong *) args[i];
250                 break;
251             case GI_TYPE_TAG_FLOAT:
252                 state[i].arg_value.v_float = * (gfloat *) args[i];
253                 break;
254             case GI_TYPE_TAG_DOUBLE:
255                 state[i].arg_value.v_double = * (gdouble *) args[i];
256                 break;
257             case GI_TYPE_TAG_UTF8:
258                 state[i].arg_value.v_string = * (gchar **) args[i];
259                 break;
260             case GI_TYPE_TAG_INTERFACE:
261             {
262                 GIBaseInfo *interface;
263                 GIInfoType interface_type;
264
265                 interface = ((PyGIInterfaceCache *) arg_cache)->interface_info;
266                 interface_type = g_base_info_get_type (interface);
267
268                 if (interface_type == GI_INFO_TYPE_ENUM) {
269                     state[i].arg_value.v_int = * (gint *) args[i];
270                 } else if (interface_type == GI_INFO_TYPE_FLAGS) {
271                     state[i].arg_value.v_uint = * (guint *) args[i];
272                 } else {
273                     state[i].arg_value.v_pointer = * (gpointer *) args[i];
274                 }
275                 break;
276             }
277             case GI_TYPE_TAG_UNICHAR:
278                 state[i].arg_value.v_uint32 = * (guint32 *) args[i];
279                 break;
280             case GI_TYPE_TAG_ERROR:
281             case GI_TYPE_TAG_GHASH:
282             case GI_TYPE_TAG_GLIST:
283             case GI_TYPE_TAG_GSLIST:
284             case GI_TYPE_TAG_ARRAY:
285             case GI_TYPE_TAG_VOID:
286                 state[i].arg_value.v_pointer = * (gpointer *) args[i];
287                 break;
288             default:
289                 g_warning ("Unhandled type tag %s",
290                            g_type_tag_to_string (arg_cache->type_tag));
291                 state[i].arg_value.v_pointer = 0;
292         }
293     }
294
295     if (cache->throws) {
296         gssize error_index = _pygi_callable_cache_args_len (cache);
297
298         state[error_index].arg_value.v_pointer = * (gpointer *) args[error_index];
299     }
300 }
301
302 static gboolean
303 _invoke_state_init_from_cache (PyGIInvokeState *state,
304                                PyGIClosureCache *closure_cache,
305                                void **args)
306 {
307     PyGICallableCache *cache = (PyGICallableCache *) closure_cache;
308
309     state->n_args = _pygi_callable_cache_args_len (cache);
310     state->n_py_in_args = state->n_args;
311
312     /* Increment after setting the number of Python input args */
313     if (cache->throws) {
314         state->n_args++;
315     }
316
317     state->py_in_args = PyTuple_New (state->n_py_in_args);
318     if (state->py_in_args == NULL) {
319         PyErr_NoMemory ();
320         return FALSE;
321     }
322
323     state->args = NULL;
324     state->error = NULL;
325
326     if (!_pygi_invoke_arg_state_init (state)) {
327         return FALSE;
328     }
329
330     state->ffi_args = NULL;
331
332     _pygi_closure_convert_ffi_arguments (state->args, cache, args);
333     return TRUE;
334 }
335
336 static void
337 _invoke_state_clear (PyGIInvokeState *state)
338 {
339     _pygi_invoke_arg_state_free (state);
340     Py_XDECREF (state->py_in_args);
341 }
342
343 static gboolean
344 _pygi_closure_convert_arguments (PyGIInvokeState *state,
345                                  PyGIClosureCache *closure_cache)
346 {
347     PyGICallableCache *cache = (PyGICallableCache *) closure_cache;
348     gssize n_in_args = 0;
349     gssize i;
350
351     for (i = 0; (gsize)i < _pygi_callable_cache_args_len (cache); i++) {
352         PyGIArgCache *arg_cache;
353
354         arg_cache = g_ptr_array_index (cache->args_cache, i);
355
356         if (arg_cache->direction & PYGI_DIRECTION_TO_PYTHON) {
357             PyObject *value;
358
359             if (cache->user_data_index == i) {
360                 if (state->user_data == NULL) {
361                     /* user_data can be NULL for connect functions which don't accept
362                      * user_data or as the default for user_data in the middle of function
363                      * arguments.
364                      */
365                     Py_INCREF (Py_None);
366                     value = Py_None;
367                 } else {
368                     /* Extend the callbacks args with user_data as variable args. */
369                     gssize j, user_data_len;
370                     PyObject *py_user_data = state->user_data;
371
372                     if (!PyTuple_Check (py_user_data)) {
373                         PyErr_SetString (PyExc_TypeError, "expected tuple for callback user_data");
374                         return FALSE;
375                     }
376
377                     user_data_len = PyTuple_Size (py_user_data);
378                     _PyTuple_Resize (&state->py_in_args,
379                                      state->n_py_in_args + user_data_len - 1);
380
381                     for (j = 0; j < user_data_len; j++, n_in_args++) {
382                         value = PyTuple_GetItem (py_user_data, j);
383                         Py_INCREF (value);
384                         PyTuple_SET_ITEM (state->py_in_args, n_in_args, value);
385                     }
386                     /* We can assume user_data args are never going to be inout,
387                      * so just continue here.
388                      */
389                     continue;
390                 }
391             } else if (arg_cache->meta_type != PYGI_META_ARG_TYPE_PARENT) {
392                 continue;
393             } else {
394                 value = arg_cache->to_py_marshaller (state,
395                                                      cache,
396                                                      arg_cache,
397                                                      &state->args[i].arg_value);
398
399                 if (value == NULL) {
400                     pygi_marshal_cleanup_args_to_py_parameter_fail (state,
401                                                                     cache,
402                                                                     i);
403                     return FALSE;
404                 }
405             }
406
407             PyTuple_SET_ITEM (state->py_in_args, n_in_args, value);
408             n_in_args++;
409         }
410     }
411
412     if (_PyTuple_Resize (&state->py_in_args, n_in_args) == -1)
413         return FALSE;
414
415     return TRUE;
416 }
417
418 static gboolean
419 _pygi_closure_set_out_arguments (PyGIInvokeState *state,
420                                  PyGICallableCache *cache,
421                                  PyObject *py_retval,
422                                  void *resp)
423 {
424     gssize i;
425     gssize i_py_retval = 0;
426     gboolean success;
427
428     if (cache->return_cache->type_tag != GI_TYPE_TAG_VOID) {
429         PyObject *item = py_retval;
430
431         if (PyTuple_Check (py_retval)) {
432             item = PyTuple_GET_ITEM (py_retval, 0);
433         }
434
435         success = cache->return_cache->from_py_marshaller (state,
436                                                            cache,
437                                                            cache->return_cache,
438                                                            item,
439                                                            &state->return_arg,
440                                                            &state->args[0].arg_cleanup_data);
441
442         if (!success) {
443             pygi_marshal_cleanup_args_return_fail (state,
444                                                    cache);
445             return FALSE;
446         }
447
448         _pygi_closure_assign_pyobj_to_retval (resp, &state->return_arg,
449                                               cache->return_cache);
450         i_py_retval++;
451     }
452
453     for (i = 0; (gsize)i < _pygi_callable_cache_args_len (cache); i++) {
454         PyGIArgCache *arg_cache = g_ptr_array_index (cache->args_cache, i);
455
456         if (arg_cache->direction & PYGI_DIRECTION_FROM_PYTHON) {
457             PyObject *item = py_retval;
458
459             if (arg_cache->type_tag == GI_TYPE_TAG_ERROR) {
460                 * (GError **) state->args[i].arg_pointer.v_pointer = NULL;
461                 continue;
462             }
463
464             if (PyTuple_Check (py_retval)) {
465                 item = PyTuple_GET_ITEM (py_retval, i_py_retval);
466             } else if (i_py_retval != 0) {
467                 pygi_marshal_cleanup_args_to_py_parameter_fail (state,
468                                                                 cache,
469                                                                 i_py_retval);
470                 return FALSE;
471             }
472
473             success = arg_cache->from_py_marshaller (state,
474                                                      cache,
475                                                      arg_cache,
476                                                      item,
477                                                      &state->args[i].arg_value,
478                                                      &state->args[i_py_retval].arg_cleanup_data);
479
480             if (!success) {
481                 pygi_marshal_cleanup_args_to_py_parameter_fail (state,
482                                                                 cache,
483                                                                 i_py_retval);
484                 return FALSE;
485             }
486
487             _pygi_closure_assign_pyobj_to_out_argument (state->args[i].arg_pointer.v_pointer,
488                                                         &state->args[i].arg_value, arg_cache);
489
490             i_py_retval++;
491         }
492     }
493
494     return TRUE;
495 }
496
497 static void
498 _pygi_closure_clear_retvals (PyGIInvokeState *state,
499                              PyGICallableCache *cache,
500                              gpointer resp)
501 {
502     gsize i;
503     GIArgument arg = { 0, };
504
505     if (cache->return_cache->type_tag != GI_TYPE_TAG_VOID) {
506         _pygi_closure_assign_pyobj_to_retval (resp, &arg,
507                                               cache->return_cache);
508     }
509
510     for (i = 0; i < _pygi_callable_cache_args_len (cache); i++) {
511         PyGIArgCache *arg_cache = g_ptr_array_index (cache->args_cache, i);
512
513         if (arg_cache->direction & PYGI_DIRECTION_FROM_PYTHON) {
514             _pygi_closure_assign_pyobj_to_out_argument (state->args[i].arg_pointer.v_pointer,
515                                                         &arg, arg_cache);
516         }
517     }
518
519     if (cache->throws) {
520         gssize error_index = state->n_args - 1;
521         GError **error = (GError **) state->args[error_index].arg_value.v_pointer;
522
523         if (error != NULL) {
524             pygi_gerror_exception_check (error);
525         }
526     }
527 }
528
529 static void
530 _pygi_invoke_closure_clear_py_data(PyGICClosure *invoke_closure)
531 {
532     PyGILState_STATE state = PyGILState_Ensure();
533
534     Py_CLEAR (invoke_closure->function);
535     Py_CLEAR (invoke_closure->user_data);
536
537     PyGILState_Release (state);
538 }
539
540 void
541 _pygi_closure_handle (ffi_cif *cif,
542                       void    *result,
543                       void   **args,
544                       void    *data)
545 {
546     PyGILState_STATE py_state;
547     PyGICClosure *closure = data;
548     PyObject *retval;
549     gboolean success;
550     PyGIInvokeState state = { 0, };
551
552     /* Ignore closures when Python is not initialized. This can happen in cases
553      * where calling Python implemented vfuncs can happen at shutdown time.
554      * See: https://bugzilla.gnome.org/show_bug.cgi?id=722562 */
555     if (!Py_IsInitialized()) {
556         return;
557     }
558
559     /* Lock the GIL as we are coming into this code without the lock and we
560       may be executing python code */
561     py_state = PyGILState_Ensure ();
562
563     if (closure->cache == NULL) {
564         closure->cache = pygi_closure_cache_new ((GICallableInfo *) closure->info);
565
566         if (closure->cache == NULL)
567             goto end;
568     }
569
570     state.user_data = closure->user_data;
571
572     _invoke_state_init_from_cache (&state, closure->cache, args);
573
574     if (!_pygi_closure_convert_arguments (&state, closure->cache)) {
575         _pygi_closure_clear_retvals (&state, closure->cache, result);
576         goto end;
577     }
578
579     retval = PyObject_CallObject ( (PyObject *) closure->function, state.py_in_args);
580
581     if (retval == NULL) {
582         _pygi_closure_clear_retvals (&state, closure->cache, result);
583         goto end;
584     }
585
586     pygi_marshal_cleanup_args_to_py_marshal_success (&state, closure->cache);
587     success = _pygi_closure_set_out_arguments (&state, closure->cache, retval, result);
588
589     if (!success) {
590         pygi_marshal_cleanup_args_from_py_marshal_success (&state, closure->cache);
591         _pygi_closure_clear_retvals (&state, closure->cache, result);
592     }
593
594     Py_DECREF (retval);
595
596 end:
597
598     if (PyErr_Occurred ())
599         PyErr_Print ();
600
601     /* Now that the closure has finished we can make a decision about how
602        to free it.  Scope call gets free'd at the end of wrap_g_function_info_invoke.
603        Scope notified will be freed when the notify is called.
604        Scope async closures free only their python data now and the closure later
605        during the next creation of a closure. This minimizes potential ref leaks
606        at least in regards to the python objects.
607        (you can't free the closure you are currently using!)
608     */
609     switch (closure->scope) {
610         case GI_SCOPE_TYPE_CALL:
611         case GI_SCOPE_TYPE_NOTIFIED:
612             break;
613         case GI_SCOPE_TYPE_ASYNC:
614             /* Append this PyGICClosure to a list of closure that we will free
615                after we're done with this function invokation */
616             _pygi_invoke_closure_clear_py_data(closure);
617             async_free_list = g_slist_prepend (async_free_list, closure);
618             break;
619         default:
620             g_error ("Invalid scope reached inside %s.  Possibly a bad annotation?",
621                      g_base_info_get_name (closure->info));
622     }
623
624     _invoke_state_clear (&state);
625     PyGILState_Release (py_state);
626 }
627
628 void _pygi_invoke_closure_free (gpointer data)
629 {
630     PyGICClosure* invoke_closure = (PyGICClosure *) data;
631
632     g_callable_info_free_closure (invoke_closure->info,
633                                   invoke_closure->closure);
634
635     if (invoke_closure->info)
636         g_base_info_unref ( (GIBaseInfo*) invoke_closure->info);
637
638     if (invoke_closure->cache != NULL)
639         pygi_callable_cache_free ((PyGICallableCache *) invoke_closure->cache);
640
641     _pygi_invoke_closure_clear_py_data(invoke_closure);
642
643     g_slice_free (PyGICClosure, invoke_closure);
644 }
645
646
647 PyGICClosure*
648 _pygi_make_native_closure (GICallableInfo* info,
649                            GIScopeType scope,
650                            PyObject *py_function,
651                            gpointer py_user_data)
652 {
653     PyGICClosure *closure;
654     ffi_closure *fficlosure;
655
656     /* Begin by cleaning up old async functions */
657     g_slist_free_full (async_free_list, (GDestroyNotify) _pygi_invoke_closure_free);
658     async_free_list = NULL;
659
660     /* Build the closure itself */
661     closure = g_slice_new0 (PyGICClosure);
662     closure->info = (GICallableInfo *) g_base_info_ref ( (GIBaseInfo *) info);
663     closure->function = py_function;
664     closure->user_data = py_user_data;
665
666     Py_INCREF (py_function);
667     Py_XINCREF (closure->user_data);
668
669     fficlosure =
670         g_callable_info_prepare_closure (info, &closure->cif, _pygi_closure_handle,
671                                          closure);
672     closure->closure = fficlosure;
673
674     /* Give the closure the information it needs to determine when
675        to free itself later */
676     closure->scope = scope;
677
678     return closure;
679 }
680
681 /* _pygi_destroy_notify_dummy:
682  *
683  * Dummy method used in the occasion when a method has a GDestroyNotify
684  * argument without user data.
685  */
686 static void
687 _pygi_destroy_notify_dummy (gpointer data) {
688 }
689
690 static gboolean
691 _pygi_marshal_from_py_interface_callback (PyGIInvokeState   *state,
692                                           PyGICallableCache *callable_cache,
693                                           PyGIArgCache      *arg_cache,
694                                           PyObject          *py_arg,
695                                           GIArgument        *arg,
696                                           gpointer          *cleanup_data)
697 {
698     GICallableInfo *callable_info;
699     PyGICClosure *closure;
700     PyGIArgCache *user_data_cache = NULL;
701     PyGIArgCache *destroy_cache = NULL;
702     PyGICallbackCache *callback_cache;
703     PyObject *py_user_data = NULL;
704
705     callback_cache = (PyGICallbackCache *)arg_cache;
706
707     if (callback_cache->user_data_index > 0) {
708         user_data_cache = _pygi_callable_cache_get_arg (callable_cache, callback_cache->user_data_index);
709         if (user_data_cache->py_arg_index < state->n_py_in_args) {
710             /* py_user_data is a borrowed reference. */
711             py_user_data = PyTuple_GetItem (state->py_in_args, user_data_cache->py_arg_index);
712             if (!py_user_data)
713                 return FALSE;
714             /* NULL out user_data if it was not supplied and the default arg placeholder
715              * was used instead.
716              */
717             if (py_user_data == _PyGIDefaultArgPlaceholder) {
718                 py_user_data = NULL;
719             } else if (callable_cache->user_data_varargs_index < 0) {
720                 /* For non-variable length user data, place the user data in a
721                  * single item tuple which is concatenated to the callbacks arguments.
722                  * This allows callback input arg marshaling to always expect a
723                  * tuple for user data. Note the
724                  */
725                 py_user_data = Py_BuildValue("(O)", py_user_data, NULL);
726             } else {
727                 /* increment the ref borrowed from PyTuple_GetItem above */
728                 Py_INCREF (py_user_data);
729             }
730         }
731     }
732
733     if (py_arg == Py_None) {
734         return TRUE;
735     }
736
737     if (!PyCallable_Check (py_arg)) {
738         PyErr_Format (PyExc_TypeError,
739                       "Callback needs to be a function or method not %s",
740                       py_arg->ob_type->tp_name);
741
742         return FALSE;
743     }
744
745     callable_info = (GICallableInfo *)callback_cache->interface_info;
746
747     closure = _pygi_make_native_closure (callable_info, callback_cache->scope, py_arg, py_user_data);
748     arg->v_pointer = closure->closure;
749
750     /* always decref the user data as _pygi_make_native_closure adds its own ref */
751     Py_XDECREF (py_user_data);
752
753     /* The PyGICClosure instance is used as user data passed into the C function.
754      * The return trip to python will marshal this back and pull the python user data out.
755      */
756     if (user_data_cache != NULL) {
757         state->args[user_data_cache->c_arg_index].arg_value.v_pointer = closure;
758     }
759
760     /* Setup a GDestroyNotify callback if this method supports it along with
761      * a user data field. The user data field is a requirement in order
762      * free resources and ref counts associated with this arguments closure.
763      * In case a user data field is not available, show a warning giving
764      * explicit information and setup a dummy notification to avoid a crash
765      * later on in _pygi_destroy_notify_callback_closure.
766      */
767     if (callback_cache->destroy_notify_index > 0) {
768         destroy_cache = _pygi_callable_cache_get_arg (callable_cache, callback_cache->destroy_notify_index);
769     }
770
771     if (destroy_cache) {
772         if (user_data_cache != NULL) {
773             state->args[destroy_cache->c_arg_index].arg_value.v_pointer = _pygi_invoke_closure_free;
774         } else {
775             char *full_name = pygi_callable_cache_get_full_name (callable_cache);
776             gchar *msg = g_strdup_printf("Callables passed to %s will leak references because "
777                                          "the method does not support a user_data argument. "
778                                          "See: https://bugzilla.gnome.org/show_bug.cgi?id=685598",
779                                          full_name);
780             g_free (full_name);
781             if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 2)) {
782                 g_free(msg);
783                 _pygi_invoke_closure_free(closure);
784                 return FALSE;
785             }
786             g_free(msg);
787             state->args[destroy_cache->c_arg_index].arg_value.v_pointer = _pygi_destroy_notify_dummy;
788         }
789     }
790
791     /* Use the PyGIClosure as data passed to cleanup for GI_SCOPE_TYPE_CALL. */
792     *cleanup_data = closure;
793
794     return TRUE;
795 }
796
797 static PyObject *
798 _pygi_marshal_to_py_interface_callback (PyGIInvokeState   *state,
799                                         PyGICallableCache *callable_cache,
800                                         PyGIArgCache      *arg_cache,
801                                         GIArgument        *arg)
802 {
803     PyGICallbackCache *callback_cache = (PyGICallbackCache *) arg_cache;
804     gssize user_data_index;
805     gssize destroy_notify_index;
806     gpointer user_data = NULL;
807     GDestroyNotify destroy_notify = NULL;
808
809     user_data_index = callback_cache->user_data_index;
810     destroy_notify_index = callback_cache->destroy_notify_index;
811
812     if (user_data_index != -1)
813         user_data = state->args[user_data_index].arg_value.v_pointer;
814
815     if (destroy_notify_index != -1)
816         destroy_notify = state->args[destroy_notify_index].arg_value.v_pointer;
817
818     return _pygi_ccallback_new (arg->v_pointer,
819                                 user_data,
820                                 callback_cache->scope,
821                                 (GIFunctionInfo *) callback_cache->interface_info,
822                                 destroy_notify);
823 }
824
825 static void
826 _callback_cache_free_func (PyGICallbackCache *cache)
827 {
828     if (cache != NULL) {
829         if (cache->interface_info != NULL)
830             g_base_info_unref ( (GIBaseInfo *)cache->interface_info);
831
832         g_slice_free (PyGICallbackCache, cache);
833     }
834 }
835
836 static void
837 _pygi_marshal_cleanup_from_py_interface_callback (PyGIInvokeState *state,
838                                                   PyGIArgCache    *arg_cache,
839                                                   PyObject        *py_arg,
840                                                   gpointer         data,
841                                                   gboolean         was_processed)
842 {
843     PyGICallbackCache *callback_cache = (PyGICallbackCache *)arg_cache;
844
845     if (was_processed && callback_cache->scope == GI_SCOPE_TYPE_CALL) {
846         _pygi_invoke_closure_free (data);
847     }
848 }
849
850 static gboolean
851 pygi_arg_callback_setup_from_info (PyGICallbackCache  *arg_cache,
852                                    GITypeInfo         *type_info,
853                                    GIArgInfo          *arg_info,   /* may be null */
854                                    GITransfer          transfer,
855                                    PyGIDirection       direction,
856                                    GIInterfaceInfo    *iface_info,
857                                    PyGICallableCache  *callable_cache)
858 {
859     PyGIArgCache *cache = (PyGIArgCache *)arg_cache;
860     gssize child_offset = 0;
861
862     if (!pygi_arg_base_setup ((PyGIArgCache *)arg_cache,
863                               type_info,
864                               arg_info,
865                               transfer,
866                               direction)) {
867         return FALSE;
868     }
869
870     if (callable_cache != NULL)
871         child_offset = callable_cache->args_offset;
872
873     ( (PyGIArgCache *)arg_cache)->destroy_notify = (GDestroyNotify)_callback_cache_free_func;
874
875     arg_cache->user_data_index = g_arg_info_get_closure (arg_info);
876     if (arg_cache->user_data_index != -1)
877         arg_cache->user_data_index += child_offset;
878
879     arg_cache->destroy_notify_index = g_arg_info_get_destroy (arg_info);
880     if (arg_cache->destroy_notify_index != -1)
881         arg_cache->destroy_notify_index += child_offset;
882
883     if (arg_cache->user_data_index >= 0) {
884         PyGIArgCache *user_data_arg_cache = pygi_arg_cache_alloc ();
885         user_data_arg_cache->meta_type = PYGI_META_ARG_TYPE_CHILD_WITH_PYARG;
886         user_data_arg_cache->direction = direction;
887         user_data_arg_cache->has_default = TRUE; /* always allow user data with a NULL default. */
888         _pygi_callable_cache_set_arg (callable_cache, arg_cache->user_data_index,
889                                       user_data_arg_cache);
890     }
891
892     if (arg_cache->destroy_notify_index >= 0) {
893         PyGIArgCache *destroy_arg_cache = pygi_arg_cache_alloc ();
894         destroy_arg_cache->meta_type = PYGI_META_ARG_TYPE_CHILD;
895         destroy_arg_cache->direction = direction;
896         _pygi_callable_cache_set_arg (callable_cache, arg_cache->destroy_notify_index,
897                                       destroy_arg_cache);
898     }
899
900     arg_cache->scope = g_arg_info_get_scope (arg_info);
901     g_base_info_ref( (GIBaseInfo *)iface_info);
902     arg_cache->interface_info = iface_info;
903
904     if (direction & PYGI_DIRECTION_FROM_PYTHON) {
905         cache->from_py_marshaller = _pygi_marshal_from_py_interface_callback;
906         cache->from_py_cleanup = _pygi_marshal_cleanup_from_py_interface_callback;
907     }
908
909     if (direction & PYGI_DIRECTION_TO_PYTHON) {
910         cache->to_py_marshaller = _pygi_marshal_to_py_interface_callback;
911     }
912
913     return TRUE;
914 }
915
916 PyGIArgCache *
917 pygi_arg_callback_new_from_info  (GITypeInfo        *type_info,
918                                   GIArgInfo         *arg_info,   /* may be null */
919                                   GITransfer         transfer,
920                                   PyGIDirection      direction,
921                                   GIInterfaceInfo   *iface_info,
922                                   PyGICallableCache *callable_cache)
923 {
924     gboolean res = FALSE;
925     PyGICallbackCache *callback_cache;
926
927     callback_cache = g_slice_new0 (PyGICallbackCache);
928     if (callback_cache == NULL)
929         return NULL;
930
931     res = pygi_arg_callback_setup_from_info (callback_cache,
932                                              type_info,
933                                              arg_info,
934                                              transfer,
935                                              direction,
936                                              iface_info,
937                                              callable_cache);
938     if (res) {
939         return (PyGIArgCache *)callback_cache;
940     } else {
941         pygi_arg_cache_free ((PyGIArgCache *)callback_cache);
942         return NULL;
943     }
944 }