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
;
35 GIInterfaceInfo
*interface_info
;
36 PyGIClosureCache
*closure_cache
;
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
;
46 _pygi_closure_assign_pyobj_to_retval (gpointer retval
,
48 PyGIArgCache
*arg_cache
)
53 switch (arg_cache
->type_tag
) {
54 case GI_TYPE_TAG_BOOLEAN
:
55 *((gboolean
*) retval
) = arg
->v_boolean
;
57 case GI_TYPE_TAG_INT8
:
58 *((gint8
*) retval
) = arg
->v_int8
;
60 case GI_TYPE_TAG_UINT8
:
61 *((guint8
*) retval
) = arg
->v_uint8
;
63 case GI_TYPE_TAG_INT16
:
64 *((gint16
*) retval
) = arg
->v_int16
;
66 case GI_TYPE_TAG_UINT16
:
67 *((guint16
*) retval
) = arg
->v_uint16
;
69 case GI_TYPE_TAG_INT32
:
70 *((gint32
*) retval
) = arg
->v_int32
;
72 case GI_TYPE_TAG_UINT32
:
73 *((guint32
*) retval
) = arg
->v_uint32
;
75 case GI_TYPE_TAG_INT64
:
76 *((gint64
*) retval
) = arg
->v_int64
;
78 case GI_TYPE_TAG_UINT64
:
79 *((guint64
*) retval
) = arg
->v_uint64
;
81 case GI_TYPE_TAG_FLOAT
:
82 *((gfloat
*) retval
) = arg
->v_float
;
84 case GI_TYPE_TAG_DOUBLE
:
85 *((gdouble
*) retval
) = arg
->v_double
;
87 case GI_TYPE_TAG_GTYPE
:
88 *((gsize
*) retval
) = arg
->v_size
;
90 case GI_TYPE_TAG_UNICHAR
:
91 *((guint32
*) retval
) = arg
->v_uint32
;
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
;
103 case GI_INFO_TYPE_FLAGS
:
104 *(guint
*) retval
= arg
->v_uint
;
107 *(gpointer
*) retval
= arg
->v_pointer
;
114 *(gpointer
*) retval
= arg
->v_pointer
;
120 _pygi_closure_assign_pyobj_to_out_argument (gpointer out_arg
,
122 PyGIArgCache
*arg_cache
)
127 switch (arg_cache
->type_tag
) {
128 case GI_TYPE_TAG_BOOLEAN
:
129 *((gboolean
*) out_arg
) = arg
->v_boolean
;
131 case GI_TYPE_TAG_INT8
:
132 *((gint8
*) out_arg
) = arg
->v_int8
;
134 case GI_TYPE_TAG_UINT8
:
135 *((guint8
*) out_arg
) = arg
->v_uint8
;
137 case GI_TYPE_TAG_INT16
:
138 *((gint16
*) out_arg
) = arg
->v_int16
;
140 case GI_TYPE_TAG_UINT16
:
141 *((guint16
*) out_arg
) = arg
->v_uint16
;
143 case GI_TYPE_TAG_INT32
:
144 *((gint32
*) out_arg
) = arg
->v_int32
;
146 case GI_TYPE_TAG_UINT32
:
147 *((guint32
*) out_arg
) = arg
->v_uint32
;
149 case GI_TYPE_TAG_INT64
:
150 *((gint64
*) out_arg
) = arg
->v_int64
;
152 case GI_TYPE_TAG_UINT64
:
153 *((guint64
*) out_arg
) = arg
->v_uint64
;
155 case GI_TYPE_TAG_FLOAT
:
156 *((gfloat
*) out_arg
) = arg
->v_float
;
158 case GI_TYPE_TAG_DOUBLE
:
159 *((gdouble
*) out_arg
) = arg
->v_double
;
161 case GI_TYPE_TAG_GTYPE
:
162 *((GType
*) out_arg
) = arg
->v_size
;
164 case GI_TYPE_TAG_UNICHAR
:
165 *((guint32
*) out_arg
) = arg
->v_uint32
;
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
;
177 case GI_INFO_TYPE_FLAGS
:
178 *(guint
*) out_arg
= arg
->v_uint
;
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
);
188 *((gpointer
*) out_arg
) = arg
->v_pointer
;
191 *((gpointer
*) out_arg
) = arg
->v_pointer
;
198 *((gpointer
*) out_arg
) = arg
->v_pointer
;
204 _pygi_closure_convert_ffi_arguments (PyGIInvokeArgState
*state
,
205 PyGICallableCache
*cache
,
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
)
220 state
[i
].arg_pointer
.v_pointer
= state
[i
].arg_value
.v_pointer
;
221 arg_pointer
= state
[i
].arg_value
.v_pointer
;
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
;
230 case GI_TYPE_TAG_INT8
:
231 state
[i
].arg_value
.v_int8
= * (gint8
*) arg_pointer
;
233 case GI_TYPE_TAG_UINT8
:
234 state
[i
].arg_value
.v_uint8
= * (guint8
*) arg_pointer
;
236 case GI_TYPE_TAG_INT16
:
237 state
[i
].arg_value
.v_int16
= * (gint16
*) arg_pointer
;
239 case GI_TYPE_TAG_UINT16
:
240 state
[i
].arg_value
.v_uint16
= * (guint16
*) arg_pointer
;
242 case GI_TYPE_TAG_INT32
:
243 state
[i
].arg_value
.v_int32
= * (gint32
*) arg_pointer
;
245 case GI_TYPE_TAG_UINT32
:
246 state
[i
].arg_value
.v_uint32
= * (guint32
*) arg_pointer
;
248 case GI_TYPE_TAG_INT64
:
249 state
[i
].arg_value
.v_int64
= * (gint64
*) arg_pointer
;
251 case GI_TYPE_TAG_UINT64
:
252 state
[i
].arg_value
.v_uint64
= * (guint64
*) arg_pointer
;
254 case GI_TYPE_TAG_FLOAT
:
255 state
[i
].arg_value
.v_float
= * (gfloat
*) arg_pointer
;
257 case GI_TYPE_TAG_DOUBLE
:
258 state
[i
].arg_value
.v_double
= * (gdouble
*) arg_pointer
;
260 case GI_TYPE_TAG_UTF8
:
261 state
[i
].arg_value
.v_string
= * (gchar
**) arg_pointer
;
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
;
276 state
[i
].arg_value
.v_pointer
= * (gpointer
*) arg_pointer
;
280 case GI_TYPE_TAG_UNICHAR
:
281 state
[i
].arg_value
.v_uint32
= * (guint32
*) arg_pointer
;
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
;
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;
299 gssize error_index
= _pygi_callable_cache_args_len (cache
);
301 state
[error_index
].arg_value
.v_pointer
= * (gpointer
*) args
[error_index
];
306 _invoke_state_init_from_cache (PyGIInvokeState
*state
,
307 PyGIClosureCache
*closure_cache
,
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 */
320 state
->py_in_args
= PyTuple_New (state
->n_py_in_args
);
321 if (state
->py_in_args
== NULL
) {
329 if (!_pygi_invoke_arg_state_init (state
)) {
333 state
->ffi_args
= NULL
;
335 _pygi_closure_convert_ffi_arguments (state
->args
, cache
, args
);
340 _invoke_state_clear (PyGIInvokeState
*state
)
342 _pygi_invoke_arg_state_free (state
);
343 Py_XDECREF (state
->py_in_args
);
347 _pygi_closure_convert_arguments (PyGIInvokeState
*state
,
348 PyGIClosureCache
*closure_cache
)
350 PyGICallableCache
*cache
= (PyGICallableCache
*) closure_cache
;
351 gssize n_in_args
= 0;
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
) {
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
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");
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
);
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.
394 } else if (arg_cache
->meta_type
!= PYGI_META_ARG_TYPE_PARENT
) {
397 gpointer cleanup_data
= NULL
;
399 value
= arg_cache
->to_py_marshaller (state
,
402 &state
->args
[i
].arg_value
,
404 state
->args
[i
].to_py_arg_cleanup_data
= cleanup_data
;
407 pygi_marshal_cleanup_args_to_py_parameter_fail (state
,
414 PyTuple_SET_ITEM (state
->py_in_args
, n_in_args
, value
);
419 if (_PyTuple_Resize (&state
->py_in_args
, n_in_args
) == -1)
426 _pygi_closure_set_out_arguments (PyGIInvokeState
*state
,
427 PyGICallableCache
*cache
,
432 gssize i_py_retval
= 0;
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
,
447 &state
->args
[0].arg_cleanup_data
);
450 pygi_marshal_cleanup_args_return_fail (state
,
455 _pygi_closure_assign_pyobj_to_retval (resp
, &state
->return_arg
,
456 cache
->return_cache
);
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
;
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
,
480 success
= arg_cache
->from_py_marshaller (state
,
484 &state
->args
[i
].arg_value
,
485 &state
->args
[i_py_retval
].arg_cleanup_data
);
488 pygi_marshal_cleanup_args_to_py_parameter_fail (state
,
494 _pygi_closure_assign_pyobj_to_out_argument (state
->args
[i
].arg_pointer
.v_pointer
,
495 &state
->args
[i
].arg_value
, arg_cache
);
505 _pygi_closure_clear_retvals (PyGIInvokeState
*state
,
506 PyGICallableCache
*cache
,
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
,
527 gssize error_index
= state
->n_args
- 1;
528 GError
**error
= (GError
**) state
->args
[error_index
].arg_value
.v_pointer
;
531 pygi_gerror_exception_check (error
);
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
);
548 _pygi_closure_handle (ffi_cif
*cif
,
553 PyGILState_STATE py_state
;
554 PyGICClosure
*closure
= data
;
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()) {
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
)
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
);
582 retval
= PyObject_CallObject ( (PyObject
*) closure
->function
, state
.py_in_args
);
584 if (retval
== NULL
) {
585 _pygi_closure_clear_retvals (&state
, closure
->cache
, result
);
589 pygi_marshal_cleanup_args_to_py_marshal_success (&state
, closure
->cache
);
590 success
= _pygi_closure_set_out_arguments (&state
, closure
->cache
, retval
, result
);
593 pygi_marshal_cleanup_args_from_py_marshal_success (&state
, closure
->cache
);
594 _pygi_closure_clear_retvals (&state
, closure
->cache
, result
);
601 if (PyErr_Occurred ())
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
:
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
);
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
);
650 _pygi_make_native_closure (GICallableInfo
* info
,
651 PyGIClosureCache
*cache
,
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
);
674 g_callable_info_prepare_closure (info
, &closure
->cif
, _pygi_closure_handle
,
676 closure
->closure
= fficlosure
;
678 /* Give the closure the information it needs to determine when
679 to free itself later */
680 closure
->scope
= scope
;
685 /* _pygi_destroy_notify_dummy:
687 * Dummy method used in the occasion when a method has a GDestroyNotify
688 * argument without user data.
691 _pygi_destroy_notify_dummy (gpointer data
) {
695 _pygi_marshal_from_py_interface_callback (PyGIInvokeState
*state
,
696 PyGICallableCache
*callable_cache
,
697 PyGIArgCache
*arg_cache
,
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
);
718 /* NULL out user_data if it was not supplied and the default arg placeholder
721 if (py_user_data
== _PyGIDefaultArgPlaceholder
) {
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
);
731 /* increment the ref borrowed from PyTuple_GetItem above */
732 Py_INCREF (py_user_data
);
737 if (py_arg
== Py_None
) {
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
);
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
);
778 if (user_data_cache
!= NULL
) {
779 state
->args
[destroy_cache
->c_arg_index
].arg_value
.v_pointer
= _pygi_invoke_closure_free
;
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",
787 if (PyErr_WarnEx(PyExc_RuntimeWarning
, msg
, 2)) {
789 _pygi_invoke_closure_free(closure
);
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
;
804 _pygi_marshal_to_py_interface_callback (PyGIInvokeState
*state
,
805 PyGICallableCache
*callable_cache
,
806 PyGIArgCache
*arg_cache
,
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
,
827 callback_cache
->scope
,
828 (GIFunctionInfo
*) callback_cache
->interface_info
,
833 _callback_cache_free_func (PyGICallbackCache
*cache
)
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
);
849 _pygi_marshal_cleanup_from_py_interface_callback (PyGIInvokeState
*state
,
850 PyGIArgCache
*arg_cache
,
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
);
863 pygi_arg_callback_setup_from_info (PyGICallbackCache
*arg_cache
,
864 GITypeInfo
*type_info
,
865 GIArgInfo
*arg_info
, /* may be null */
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
,
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
,
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
;
930 pygi_arg_callback_new_from_info (GITypeInfo
*type_info
,
931 GIArgInfo
*arg_info
, /* may be null */
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
)
944 res
= pygi_arg_callback_setup_from_info (callback_cache
,
952 return (PyGIArgCache
*)callback_cache
;
954 pygi_arg_cache_free ((PyGIArgCache
*)callback_cache
);