Debian Codegraph @

gi/pygi-closure.c

raw
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-argument.h"
22 #include "pygi-async.h"
23 #include "pygi-ccallback.h"
24 #include "pygi-error.h"
25 #include "pygi-invoke.h"
26 #include "pygi-marshal-cleanup.h"
27
28 /* This maintains a list of closures which can be free'd whenever
29 as they have been called. We will free them on the next
30 library function call.
31 */
32 static GSList *async_free_list;
33
34 static void
35 _pygi_closure_assign_pyobj_to_retval (gpointer retval, GIArgument *arg,
36 PyGIArgCache *arg_cache)
37 {
38 if (retval == NULL) return;
39
40 switch (arg_cache->type_tag) {
41 case GI_TYPE_TAG_BOOLEAN:
42 *((ffi_sarg *)retval) = arg->v_boolean;
43 break;
44 case GI_TYPE_TAG_INT8:
45 *((ffi_sarg *)retval) = arg->v_int8;
46 break;
47 case GI_TYPE_TAG_UINT8:
48 *((ffi_arg *)retval) = arg->v_uint8;
49 break;
50 case GI_TYPE_TAG_INT16:
51 *((ffi_sarg *)retval) = arg->v_int16;
52 break;
53 case GI_TYPE_TAG_UINT16:
54 *((ffi_arg *)retval) = arg->v_uint16;
55 break;
56 case GI_TYPE_TAG_INT32:
57 *((ffi_sarg *)retval) = arg->v_int32;
58 break;
59 case GI_TYPE_TAG_UINT32:
60 *((ffi_arg *)retval) = arg->v_uint32;
61 break;
62 case GI_TYPE_TAG_INT64:
63 *((ffi_sarg *)retval) = (ffi_sarg)arg->v_int64;
64 break;
65 case GI_TYPE_TAG_UINT64:
66 *((ffi_arg *)retval) = (ffi_arg)arg->v_uint64;
67 break;
68 case GI_TYPE_TAG_FLOAT:
69 *((gfloat *)retval) = arg->v_float;
70 break;
71 case GI_TYPE_TAG_DOUBLE:
72 *((gdouble *)retval) = arg->v_double;
73 break;
74 case GI_TYPE_TAG_GTYPE:
75 *((ffi_arg *)retval) = arg->v_size;
76 break;
77 case GI_TYPE_TAG_UNICHAR:
78 *((ffi_arg *)retval) = arg->v_uint32;
79 break;
80 case GI_TYPE_TAG_INTERFACE: {
81 GIRegisteredTypeInfo *interface_info;
82
83 interface_info = ((PyGIInterfaceCache *)arg_cache)->interface_info;
84
85 if (GI_IS_FLAGS_INFO (interface_info)) {
86 /* Check flags before enums: flags are a subtype of enum. */
87 *(ffi_arg *)retval = arg->v_uint;
88 } else if (GI_IS_ENUM_INFO (interface_info)) {
89 *(ffi_sarg *)retval = arg->v_int;
90 } else {
91 *(ffi_arg *)retval = (ffi_arg)arg->v_pointer;
92 }
93
94 break;
95 }
96 case GI_TYPE_TAG_VOID:
97 case GI_TYPE_TAG_UTF8:
98 case GI_TYPE_TAG_FILENAME:
99 case GI_TYPE_TAG_ARRAY:
100 case GI_TYPE_TAG_GLIST:
101 case GI_TYPE_TAG_GSLIST:
102 case GI_TYPE_TAG_GHASH:
103 case GI_TYPE_TAG_ERROR:
104 *(ffi_arg *)retval = (ffi_arg)arg->v_pointer;
105 break;
106 default:
107 g_assert_not_reached ();
108 }
109 }
110
111 static void
112 _pygi_closure_assign_pyobj_to_out_argument (gpointer out_arg, GIArgument *arg,
113 PyGIArgCache *arg_cache)
114 {
115 if (out_arg == NULL) return;
116
117 switch (arg_cache->type_tag) {
118 case GI_TYPE_TAG_BOOLEAN:
119 *((gboolean *)out_arg) = arg->v_boolean;
120 break;
121 case GI_TYPE_TAG_INT8:
122 *((gint8 *)out_arg) = arg->v_int8;
123 break;
124 case GI_TYPE_TAG_UINT8:
125 *((guint8 *)out_arg) = arg->v_uint8;
126 break;
127 case GI_TYPE_TAG_INT16:
128 *((gint16 *)out_arg) = arg->v_int16;
129 break;
130 case GI_TYPE_TAG_UINT16:
131 *((guint16 *)out_arg) = arg->v_uint16;
132 break;
133 case GI_TYPE_TAG_INT32:
134 *((gint32 *)out_arg) = arg->v_int32;
135 break;
136 case GI_TYPE_TAG_UINT32:
137 *((guint32 *)out_arg) = arg->v_uint32;
138 break;
139 case GI_TYPE_TAG_INT64:
140 *((gint64 *)out_arg) = arg->v_int64;
141 break;
142 case GI_TYPE_TAG_UINT64:
143 *((guint64 *)out_arg) = arg->v_uint64;
144 break;
145 case GI_TYPE_TAG_FLOAT:
146 *((gfloat *)out_arg) = arg->v_float;
147 break;
148 case GI_TYPE_TAG_DOUBLE:
149 *((gdouble *)out_arg) = arg->v_double;
150 break;
151 case GI_TYPE_TAG_GTYPE:
152 *((GType *)out_arg) = arg->v_size;
153 break;
154 case GI_TYPE_TAG_UNICHAR:
155 *((guint32 *)out_arg) = arg->v_uint32;
156 break;
157 case GI_TYPE_TAG_INTERFACE: {
158 GIRegisteredTypeInfo *interface_info;
159
160 interface_info = ((PyGIInterfaceCache *)arg_cache)->interface_info;
161
162 if (GI_IS_FLAGS_INFO (interface_info)) {
163 /* Check flags before enums: flags are a subtype of enum. */
164 *(guint *)out_arg = arg->v_uint;
165 } else if (GI_IS_ENUM_INFO (interface_info)) {
166 *(gint *)out_arg = arg->v_int;
167 } else if (GI_IS_STRUCT_INFO (interface_info)) {
168 if (!arg_cache->is_pointer) {
169 if (arg->v_pointer != NULL) {
170 gsize item_size =
171 _pygi_gi_type_info_size (arg_cache->type_info);
172 memcpy (out_arg, arg->v_pointer, item_size);
173 }
174 } else {
175 *((gpointer *)out_arg) = arg->v_pointer;
176 }
177 } else {
178 *((gpointer *)out_arg) = arg->v_pointer;
179 }
180 break;
181 }
182 case GI_TYPE_TAG_VOID:
183 case GI_TYPE_TAG_UTF8:
184 case GI_TYPE_TAG_FILENAME:
185 case GI_TYPE_TAG_ARRAY:
186 case GI_TYPE_TAG_GLIST:
187 case GI_TYPE_TAG_GSLIST:
188 case GI_TYPE_TAG_GHASH:
189 case GI_TYPE_TAG_ERROR:
190 *((gpointer *)out_arg) = arg->v_pointer;
191 break;
192 default:
193 g_assert_not_reached ();
194 }
195 }
196
197 static void
198 _pygi_closure_convert_ffi_arguments (PyGIInvokeArgState *state,
199 PyGICallableCache *cache, void **args)
200 {
201 for (guint i = 0; i < _pygi_callable_cache_args_len (cache); i++) {
202 PyGIArgCache *arg_cache = g_ptr_array_index (cache->args_cache, i);
203 gpointer arg_pointer;
204
205 if (arg_cache->direction & PYGI_DIRECTION_FROM_PYTHON) {
206 state[i].arg_value.v_pointer = *(gpointer *)args[i];
207
208 if (state[i].arg_value.v_pointer == NULL) continue;
209
210 state[i].arg_pointer.v_pointer = state[i].arg_value.v_pointer;
211 arg_pointer = state[i].arg_value.v_pointer;
212 } else {
213 arg_pointer = args[i];
214 }
215
216 switch (arg_cache->type_tag) {
217 case GI_TYPE_TAG_BOOLEAN:
218 state[i].arg_value.v_boolean = *(gboolean *)arg_pointer;
219 break;
220 case GI_TYPE_TAG_INT8:
221 state[i].arg_value.v_int8 = *(gint8 *)arg_pointer;
222 break;
223 case GI_TYPE_TAG_UINT8:
224 state[i].arg_value.v_uint8 = *(guint8 *)arg_pointer;
225 break;
226 case GI_TYPE_TAG_INT16:
227 state[i].arg_value.v_int16 = *(gint16 *)arg_pointer;
228 break;
229 case GI_TYPE_TAG_UINT16:
230 state[i].arg_value.v_uint16 = *(guint16 *)arg_pointer;
231 break;
232 case GI_TYPE_TAG_INT32:
233 state[i].arg_value.v_int32 = *(gint32 *)arg_pointer;
234 break;
235 case GI_TYPE_TAG_UINT32:
236 case GI_TYPE_TAG_UNICHAR:
237 state[i].arg_value.v_uint32 = *(guint32 *)arg_pointer;
238 break;
239 case GI_TYPE_TAG_INT64:
240 state[i].arg_value.v_int64 = *(gint64 *)arg_pointer;
241 break;
242 case GI_TYPE_TAG_UINT64:
243 state[i].arg_value.v_uint64 = *(guint64 *)arg_pointer;
244 break;
245 case GI_TYPE_TAG_GTYPE:
246 state[i].arg_value.v_size = *(gsize *)arg_pointer;
247 break;
248 case GI_TYPE_TAG_FLOAT:
249 state[i].arg_value.v_float = *(gfloat *)arg_pointer;
250 break;
251 case GI_TYPE_TAG_DOUBLE:
252 state[i].arg_value.v_double = *(gdouble *)arg_pointer;
253 break;
254 case GI_TYPE_TAG_UTF8:
255 case GI_TYPE_TAG_FILENAME:
256 state[i].arg_value.v_string = *(gchar **)arg_pointer;
257 break;
258 case GI_TYPE_TAG_INTERFACE: {
259 GIRegisteredTypeInfo *interface;
260
261 interface = ((PyGIInterfaceCache *)arg_cache)->interface_info;
262
263 if (GI_IS_FLAGS_INFO (interface)) {
264 /* Check flags before enums: flags are a subtype of enum. */
265 state[i].arg_value.v_uint = *(guint *)arg_pointer;
266 } else if (GI_IS_ENUM_INFO (interface)) {
267 state[i].arg_value.v_int = *(gint *)arg_pointer;
268 } else {
269 state[i].arg_value.v_pointer = *(gpointer *)arg_pointer;
270 }
271 break;
272 }
273 case GI_TYPE_TAG_ERROR:
274 case GI_TYPE_TAG_GHASH:
275 case GI_TYPE_TAG_GLIST:
276 case GI_TYPE_TAG_GSLIST:
277 case GI_TYPE_TAG_ARRAY:
278 case GI_TYPE_TAG_VOID:
279 state[i].arg_value.v_pointer = *(gpointer *)arg_pointer;
280 break;
281 default:
282 g_assert_not_reached ();
283 }
284 }
285
286 if (pygi_callable_cache_can_throw_gerror (cache)) {
287 guint error_index = _pygi_callable_cache_args_len (cache);
288
289 state[error_index].arg_value.v_pointer =
290 *(gpointer *)args[error_index];
291 }
292 }
293
294 static gboolean
295 _invoke_state_init_from_cache (PyGIInvokeState *state,
296 PyGIClosureCache *closure_cache, void **args)
297 {
298 PyGICallableCache *cache = (PyGICallableCache *)closure_cache;
299
300 state->n_args = _pygi_callable_cache_args_len (cache);
301 state->n_py_in_args = state->n_args;
302
303 /* Increment after setting the number of Python input args */
304 if (pygi_callable_cache_can_throw_gerror (cache)) {
305 state->n_args++;
306 }
307
308 state->py_in_args = PyTuple_New (state->n_py_in_args);
309 if (state->py_in_args == NULL) {
310 PyErr_NoMemory ();
311 return FALSE;
312 }
313
314 state->args = NULL;
315 state->error = NULL;
316
317 if (!_pygi_invoke_arg_state_init (state)) {
318 return FALSE;
319 }
320
321 state->ffi_args = NULL;
322
323 _pygi_closure_convert_ffi_arguments (state->args, cache, args);
324 return TRUE;
325 }
326
327 static void
328 _invoke_state_clear (PyGIInvokeState *state)
329 {
330 _pygi_invoke_arg_state_free (state);
331 Py_XDECREF (state->py_in_args);
332 }
333
334 static gboolean
335 _pygi_closure_convert_arguments (PyGIInvokeState *state,
336 PyGIClosureCache *closure_cache)
337 {
338 PyGICallableCache *cache = (PyGICallableCache *)closure_cache;
339 gssize n_in_args = 0;
340
341 for (guint i = 0; i < _pygi_callable_cache_args_len (cache); i++) {
342 PyGIArgCache *arg_cache = g_ptr_array_index (cache->args_cache, i);
343
344 if (arg_cache->direction & PYGI_DIRECTION_TO_PYTHON) {
345 PyObject *value;
346
347 if (cache->has_user_data && (cache->user_data_index) == i) {
348 if (state->user_data == NULL) {
349 /* user_data can be NULL for connect functions which don't accept
350 * user_data or as the default for user_data in the middle of function
351 * arguments.
352 */
353 Py_INCREF (Py_None);
354 value = Py_None;
355 } else {
356 /* Extend the callbacks args with user_data as variable args. */
357 gssize j, user_data_len;
358 PyObject *py_user_data = state->user_data;
359
360 if (!PyTuple_Check (py_user_data)) {
361 PyErr_SetString (
362 PyExc_TypeError,
363 "expected tuple for callback user_data");
364 return FALSE;
365 }
366
367 user_data_len = PyTuple_Size (py_user_data);
368 _PyTuple_Resize (&state->py_in_args,
369 state->n_py_in_args + user_data_len - 1);
370
371 for (j = 0; j < user_data_len; j++, n_in_args++) {
372 value = PyTuple_GetItem (py_user_data, j);
373 Py_INCREF (value);
374 PyTuple_SET_ITEM (state->py_in_args, n_in_args, value);
375 }
376 /* We can assume user_data args are never going to be inout,
377 * so just continue here.
378 */
379 continue;
380 }
381 } else if (arg_cache->meta_type != PYGI_META_ARG_TYPE_PARENT) {
382 continue;
383 } else {
384 gpointer cleanup_data = NULL;
385
386 value = arg_cache->to_py_marshaller (state, cache, arg_cache,
387 &state->args[i].arg_value,
388 &cleanup_data);
389 state->args[i].to_py_arg_cleanup_data = cleanup_data;
390
391 if (value == NULL) {
392 pygi_marshal_cleanup_args_to_py_parameter_fail (state,
393 cache, i);
394 return FALSE;
395 }
396 }
397
398 PyTuple_SET_ITEM (state->py_in_args, n_in_args, value);
399 n_in_args++;
400 }
401 }
402
403 if (_PyTuple_Resize (&state->py_in_args, n_in_args) == -1) return FALSE;
404
405 return TRUE;
406 }
407
408 static gboolean
409 _pygi_closure_set_out_arguments (PyGIInvokeState *state,
410 PyGICallableCache *cache, PyObject *py_retval,
411 void *resp)
412 {
413 gssize i_py_retval = 0;
414 gboolean success;
415
416 if (cache->return_cache->type_tag != GI_TYPE_TAG_VOID) {
417 PyObject *item = py_retval;
418
419 if (PyTuple_Check (py_retval)) {
420 item = PyTuple_GET_ITEM (py_retval, 0);
421 }
422
423 success = cache->return_cache->from_py_marshaller (
424 state, cache, cache->return_cache, item, &state->return_arg,
425 &state->args[0].arg_cleanup_data);
426
427 if (!success) {
428 pygi_marshal_cleanup_args_return_fail (state, cache);
429 return FALSE;
430 }
431
432 _pygi_closure_assign_pyobj_to_retval (resp, &state->return_arg,
433 cache->return_cache);
434 i_py_retval++;
435 }
436
437 for (guint i = 0; i < _pygi_callable_cache_args_len (cache); i++) {
438 PyGIArgCache *arg_cache = g_ptr_array_index (cache->args_cache, i);
439
440 if (arg_cache->direction & PYGI_DIRECTION_FROM_PYTHON) {
441 PyObject *item = py_retval;
442
443 if (arg_cache->type_tag == GI_TYPE_TAG_ERROR) {
444 *(GError **)state->args[i].arg_pointer.v_pointer = NULL;
445 continue;
446 }
447
448 if (PyTuple_Check (py_retval)) {
449 item = PyTuple_GET_ITEM (py_retval, i_py_retval);
450 } else if (i_py_retval != 0) {
451 pygi_marshal_cleanup_args_to_py_parameter_fail (state, cache,
452 i_py_retval);
453 return FALSE;
454 }
455
456 success = arg_cache->from_py_marshaller (
457 state, cache, arg_cache, item, &state->args[i].arg_value,
458 &state->args[i_py_retval].arg_cleanup_data);
459
460 if (!success) {
461 pygi_marshal_cleanup_args_to_py_parameter_fail (state, cache,
462 i_py_retval);
463 return FALSE;
464 }
465
466 _pygi_closure_assign_pyobj_to_out_argument (
467 state->args[i].arg_pointer.v_pointer,
468 &state->args[i].arg_value, arg_cache);
469
470 i_py_retval++;
471 }
472 }
473
474 return TRUE;
475 }
476
477 static void
478 _pygi_closure_clear_retvals (PyGIInvokeState *state, PyGICallableCache *cache,
479 gpointer resp)
480 {
481 GIArgument arg = PYGI_ARG_INIT;
482
483 if (cache->return_cache->type_tag != GI_TYPE_TAG_VOID) {
484 _pygi_closure_assign_pyobj_to_retval (resp, &arg, cache->return_cache);
485 }
486
487 for (guint i = 0; i < _pygi_callable_cache_args_len (cache); i++) {
488 PyGIArgCache *arg_cache = g_ptr_array_index (cache->args_cache, i);
489
490 if (arg_cache->direction & PYGI_DIRECTION_FROM_PYTHON) {
491 _pygi_closure_assign_pyobj_to_out_argument (
492 state->args[i].arg_pointer.v_pointer, &arg, arg_cache);
493 }
494 }
495
496 if (pygi_callable_cache_can_throw_gerror (cache)) {
497 gssize error_index = state->n_args - 1;
498 GError **error =
499 (GError **)state->args[error_index].arg_value.v_pointer;
500
501 if (error != NULL) {
502 pygi_gerror_exception_check (error);
503 }
504 }
505 }
506
507 static void
508 _pygi_invoke_closure_clear_py_data (PyGICClosure *invoke_closure)
509 {
510 PyGILState_STATE state = PyGILState_Ensure ();
511
512 Py_CLEAR (invoke_closure->function);
513 Py_CLEAR (invoke_closure->user_data);
514
515 PyGILState_Release (state);
516 }
517
518 static void
519 _pygi_closure_handle (ffi_cif *cif, void *result, void **args, void *data)
520 {
521 PyGILState_STATE py_state;
522 PyGICClosure *closure = data;
523 PyObject *retval;
524 gboolean success;
525 PyGIInvokeState state = {
526 0,
527 };
528
529 /* Ignore closures when Python is not initialized. This can happen in cases
530 * where calling Python implemented vfuncs can happen at shutdown time.
531 * See: https://bugzilla.gnome.org/show_bug.cgi?id=722562 */
532 if (!Py_IsInitialized ()) {
533 return;
534 }
535
536 /* Lock the GIL as we are coming into this code without the lock and we
537 may be executing python code */
538 py_state = PyGILState_Ensure ();
539
540 if (closure->cache == NULL) goto end;
541
542 state.user_data = closure->user_data;
543
544 _invoke_state_init_from_cache (&state, closure->cache, args);
545
546 if (!_pygi_closure_convert_arguments (&state, closure->cache)) {
547 _pygi_closure_clear_retvals (&state, closure->cache, result);
548 goto end;
549 }
550
551 retval =
552 PyObject_CallObject ((PyObject *)closure->function, state.py_in_args);
553
554 if (retval == NULL) {
555 _pygi_closure_clear_retvals (&state, closure->cache, result);
556 goto end;
557 }
558
559 pygi_marshal_cleanup_args_to_py_marshal_success (&state, closure->cache);
560 success = _pygi_closure_set_out_arguments (&state, closure->cache, retval,
561 result);
562
563 if (!success) {
564 pygi_marshal_cleanup_args_from_py_marshal_success (&state,
565 closure->cache);
566 _pygi_closure_clear_retvals (&state, closure->cache, result);
567 }
568
569 Py_DECREF (retval);
570
571 end:
572
573 if (PyErr_Occurred ()) PyErr_Print ();
574
575 /* Now that the closure has finished we can make a decision about how
576 to free it. Scope call gets free'd at the end of wrap_gi_function_info_invoke.
577 Scope notified will be freed when the notify is called.
578 Scope async closures free only their python data now and the closure later
579 during the next creation of a closure. This minimizes potential ref leaks
580 at least in regards to the python objects.
581 (you can't free the closure you are currently using!)
582 */
583 switch (closure->scope) {
584 case GI_SCOPE_TYPE_CALL:
585 case GI_SCOPE_TYPE_NOTIFIED:
586 break;
587 case GI_SCOPE_TYPE_ASYNC:
588 /* Append this PyGICClosure to a list of closure that we will free
589 after we're done with this function invokation */
590 _pygi_invoke_closure_clear_py_data (closure);
591 async_free_list = g_slist_prepend (async_free_list, closure);
592 break;
593 case GI_SCOPE_TYPE_INVALID:
594 case GI_SCOPE_TYPE_FOREVER:
595 /* Handle new scopes added by gobject-introspection */
596 g_critical (
597 "Unknown scope reached inside %s. Please file an issue "
598 "at https://gitlab.gnome.org/GNOME/pygobject/issues/new",
599 gi_base_info_get_name (GI_BASE_INFO (closure->info)));
600 break;
601 default:
602 g_assert_not_reached ();
603 }
604
605 _invoke_state_clear (&state);
606 PyGILState_Release (py_state);
607 }
608
609 void
610 _pygi_invoke_closure_free (PyGICClosure *invoke_closure)
611 {
612 gi_callable_info_destroy_closure (invoke_closure->info,
613 invoke_closure->closure);
614
615 if (invoke_closure->info)
616 gi_base_info_unref ((GIBaseInfo *)invoke_closure->info);
617
618 invoke_closure->cache = NULL;
619
620 _pygi_invoke_closure_clear_py_data (invoke_closure);
621
622 g_slice_free (PyGICClosure, invoke_closure);
623 }
624
625
626 PyGICClosure *
627 _pygi_make_native_closure (GICallableInfo *info, PyGIClosureCache *cache,
628 GIScopeType scope, PyObject *py_function,
629 PyObject *py_user_data)
630 {
631 PyGICClosure *closure;
632 ffi_closure *fficlosure;
633
634 /* Begin by cleaning up old async functions */
635 g_slist_free_full (async_free_list,
636 (GDestroyNotify)_pygi_invoke_closure_free);
637 async_free_list = NULL;
638
639 /* Build the closure itself */
640 closure = g_slice_new0 (PyGICClosure);
641 closure->info = (GICallableInfo *)gi_base_info_ref ((GIBaseInfo *)info);
642 closure->function = py_function;
643 closure->user_data = py_user_data;
644 closure->cache = cache;
645
646 Py_INCREF (py_function);
647 Py_XINCREF (closure->user_data);
648
649 fficlosure = gi_callable_info_create_closure (
650 info, &closure->cif, _pygi_closure_handle, closure);
651
652 closure->closure = fficlosure;
653
654 /* Give the closure the information it needs to determine when
655 to free itself later */
656 closure->scope = scope;
657
658 return closure;
659 }