1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
4 * Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>, Red Hat, Inc.
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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
22 #include "pygi-marshal-cleanup.h"
25 _cleanup_caller_allocates (PyGIInvokeState *state,
29 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)cache;
31 if (iface_cache->g_type == G_TYPE_BOXED) {
33 size = g_struct_info_get_size (iface_cache->interface_info);
34 g_slice_free1 (size, data);
35 } else if (iface_cache->g_type == G_TYPE_VALUE) {
36 g_slice_free (GValue, data);
37 } else if (iface_cache->is_foreign) {
38 pygi_struct_foreign_release ((GIBaseInfo *)iface_cache->interface_info,
46 * Cleanup during invoke can happen in multiple
47 * stages, each of which can be the result of a
48 * successful compleation of that stage or an error
49 * occured which requires partial cleanup.
51 * For the most part, either the C interface being
52 * invoked or the python object which wraps the
53 * parameters, handle their lifecycles but in some
54 * cases, where we have intermediate objects,
55 * or when we fail processing a parameter, we need
56 * to handle the clean up manually.
58 * There are two argument processing stages.
59 * They are the in stage, where we process python
60 * parameters into their C counterparts, and the out
61 * stage, where we process out C parameters back
62 * into python objects. The in stage also sets up
63 * temporary out structures for caller allocated
64 * parameters which need to be cleaned up either on
65 * in stage failure or at the completion of the out
66 * stage (either success or failure)
68 * The in stage must call one of these cleanup functions:
69 * - pygi_marshal_cleanup_args_from_py_marshal_success
70 * (continue to out stage)
71 * - pygi_marshal_cleanup_args_from_py_parameter_fail
72 * (final, exit from invoke)
74 * The out stage must call one of these cleanup functions which are all final:
75 * - pygi_marshal_cleanup_args_to_py_marshal_success
76 * - pygi_marshal_cleanup_args_return_fail
77 * - pygi_marshal_cleanup_args_to_py_parameter_fail
81 pygi_marshal_cleanup_args_from_py_marshal_success (PyGIInvokeState *state,
82 PyGICallableCache *cache)
86 /* For in success, call cleanup for all GI_DIRECTION_IN values only. */
87 for (i = 0; i < cache->n_args; i++) {
88 PyGIArgCache *arg_cache = cache->args_cache[i];
89 PyGIMarshalCleanupFunc cleanup_func = arg_cache->from_py_cleanup;
92 arg_cache->direction == PYGI_DIRECTION_FROM_PYTHON &&
93 state->args[i]->v_pointer != NULL)
94 cleanup_func (state, arg_cache, state->args[i]->v_pointer, TRUE);
99 pygi_marshal_cleanup_args_to_py_marshal_success (PyGIInvokeState *state,
100 PyGICallableCache *cache)
102 /* clean up the return if available */
103 if (cache->return_cache != NULL) {
104 PyGIMarshalCleanupFunc cleanup_func = cache->return_cache->to_py_cleanup;
105 if (cleanup_func && state->return_arg.v_pointer != NULL)
108 state->return_arg.v_pointer,
112 /* Now clean up args */
113 GSList *cache_item = cache->to_py_args;
115 PyGIArgCache *arg_cache = (PyGIArgCache *) cache_item->data;
116 PyGIMarshalCleanupFunc cleanup_func = arg_cache->to_py_cleanup;
117 gpointer data = state->args[arg_cache->c_arg_index]->v_pointer;
119 if (cleanup_func != NULL && data != NULL)
125 cache_item = cache_item->next;
130 pygi_marshal_cleanup_args_from_py_parameter_fail (PyGIInvokeState *state,
131 PyGICallableCache *cache,
132 gssize failed_arg_index)
136 state->failed = TRUE;
138 for (i = 0; i < cache->n_args && i <= failed_arg_index; i++) {
139 PyGIArgCache *arg_cache = cache->args_cache[i];
140 PyGIMarshalCleanupFunc cleanup_func = arg_cache->from_py_cleanup;
141 gpointer data = state->args[i]->v_pointer;
144 arg_cache->direction == PYGI_DIRECTION_FROM_PYTHON &&
149 i < failed_arg_index);
151 } else if (arg_cache->is_caller_allocates && data != NULL) {
152 _cleanup_caller_allocates (state,
160 pygi_marshal_cleanup_args_return_fail (PyGIInvokeState *state,
161 PyGICallableCache *cache)
163 state->failed = TRUE;
167 pygi_marshal_cleanup_args_to_py_parameter_fail (PyGIInvokeState *state,
168 PyGICallableCache *cache,
169 gssize failed_to_py_arg_index)
171 state->failed = TRUE;
175 _pygi_marshal_cleanup_from_py_utf8 (PyGIInvokeState *state,
176 PyGIArgCache *arg_cache,
178 gboolean was_processed)
180 /* We strdup strings so always free if we have processed this
181 parameter for input */
187 _pygi_marshal_cleanup_to_py_utf8 (PyGIInvokeState *state,
188 PyGIArgCache *arg_cache,
190 gboolean was_processed)
192 /* Python copies the string so we need to free it
193 if the interface is transfering ownership,
194 whether or not it has been processed yet */
195 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
200 _pygi_marshal_cleanup_from_py_interface_object (PyGIInvokeState *state,
201 PyGIArgCache *arg_cache,
203 gboolean was_processed)
205 /* If we processed the parameter but fail before invoking the method,
206 we need to remove the ref we added */
207 if (was_processed && state->failed && data != NULL &&
208 arg_cache->transfer == GI_TRANSFER_EVERYTHING)
209 g_object_unref (G_OBJECT(data));
213 _pygi_marshal_cleanup_to_py_interface_object (PyGIInvokeState *state,
214 PyGIArgCache *arg_cache,
216 gboolean was_processed)
218 /* If we error out and the object is not marshalled into a PyGObject
219 we must take care of removing the ref */
220 if (!was_processed && arg_cache->transfer == GI_TRANSFER_EVERYTHING)
221 g_object_unref (G_OBJECT(data));
225 _pygi_marshal_cleanup_from_py_interface_struct_gvalue (PyGIInvokeState *state,
226 PyGIArgCache *arg_cache,
228 gboolean was_processed)
231 PyObject *py_arg = PyTuple_GET_ITEM (state->py_in_args,
232 arg_cache->py_arg_index);
233 GType py_object_type =
234 pyg_type_from_object_strict ( (PyObject *) py_arg->ob_type, FALSE);
236 if (py_object_type != G_TYPE_VALUE) {
237 g_value_unset ((GValue *) data);
238 g_slice_free (GValue, data);
244 _pygi_marshal_cleanup_from_py_interface_struct_foreign (PyGIInvokeState *state,
245 PyGIArgCache *arg_cache,
247 gboolean was_processed)
249 if (state->failed && was_processed)
250 pygi_struct_foreign_release (
251 ( (PyGIInterfaceCache *)arg_cache)->interface_info,
256 _pygi_marshal_cleanup_to_py_interface_struct_foreign (PyGIInvokeState *state,
257 PyGIArgCache *arg_cache,
259 gboolean was_processed)
261 if (!was_processed && arg_cache->transfer == GI_TRANSFER_EVERYTHING)
262 pygi_struct_foreign_release (
263 ( (PyGIInterfaceCache *)arg_cache)->interface_info,
268 _wrap_c_array (PyGIInvokeState *state,
269 PyGISequenceCache *sequence_cache,
275 if (sequence_cache->fixed_size >= 0) {
276 len = sequence_cache->fixed_size;
277 } else if (sequence_cache->is_zero_terminated) {
278 len = g_strv_length ((gchar **)data);
279 } else if (sequence_cache->len_arg_index >= 0) {
280 GIArgument *len_arg = state->args[sequence_cache->len_arg_index];
281 len = len_arg->v_long;
284 array_ = g_array_new (FALSE,
286 sequence_cache->item_size);
291 g_free (array_->data);
299 _pygi_marshal_cleanup_from_py_array (PyGIInvokeState *state,
300 PyGIArgCache *arg_cache,
302 gboolean was_processed)
305 GArray *array_ = NULL;
306 GPtrArray *ptr_array_ = NULL;
307 PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache;
309 /* If this isn't a garray create one to help process variable sized
311 if (sequence_cache->array_type == GI_ARRAY_TYPE_C) {
312 array_ = _wrap_c_array (state, sequence_cache, data);
317 } else if (sequence_cache->array_type == GI_ARRAY_TYPE_PTR_ARRAY) {
318 ptr_array_ = (GPtrArray *) data;
320 array_ = (GArray *) data;
323 /* clean up items first */
324 if (sequence_cache->item_cache->from_py_cleanup != NULL) {
326 guint len = (array_ != NULL) ? array_->len : ptr_array_->len;
327 PyGIMarshalCleanupFunc cleanup_func =
328 sequence_cache->item_cache->from_py_cleanup;
330 for (i = 0; i < len; i++) {
333 /* case 1: GPtrArray */
334 if (ptr_array_ != NULL)
335 item = g_ptr_array_index (ptr_array_, i);
336 /* case 2: C array or GArray with object pointers */
337 else if (sequence_cache->item_cache->is_pointer)
338 item = g_array_index (array_, gpointer, i);
339 /* case 3: C array or GArray with simple types or structs */
341 item = array_->data + i * sequence_cache->item_size;
343 cleanup_func (state, sequence_cache->item_cache, item, TRUE);
347 /* Only free the array when we didn't transfer ownership */
348 if (sequence_cache->array_type == GI_ARRAY_TYPE_C) {
349 g_array_free (array_, arg_cache->transfer == GI_TRANSFER_NOTHING);
350 } else if (state->failed ||
351 arg_cache->transfer == GI_TRANSFER_NOTHING) {
353 g_array_free (array_, TRUE);
355 g_ptr_array_free (ptr_array_, TRUE);
361 _pygi_marshal_cleanup_to_py_array (PyGIInvokeState *state,
362 PyGIArgCache *arg_cache,
364 gboolean was_processed)
366 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING ||
367 arg_cache->transfer == GI_TRANSFER_CONTAINER) {
368 GArray *array_ = NULL;
369 GPtrArray *ptr_array_ = NULL;
370 PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache;
372 /* If this isn't a garray create one to help process variable sized
374 if (sequence_cache->array_type == GI_ARRAY_TYPE_C) {
375 array_ = _wrap_c_array (state, sequence_cache, data);
380 } else if (sequence_cache->array_type == GI_ARRAY_TYPE_PTR_ARRAY) {
381 ptr_array_ = (GPtrArray *) data;
383 array_ = (GArray *) data;
386 if (sequence_cache->item_cache->to_py_cleanup != NULL) {
388 guint len = (array_ != NULL) ? array_->len : ptr_array_->len;
390 PyGIMarshalCleanupFunc cleanup_func = sequence_cache->item_cache->to_py_cleanup;
391 for (i = 0; i < len; i++) {
393 sequence_cache->item_cache,
394 (array_ != NULL) ? g_array_index (array_, gpointer, i) : g_ptr_array_index (ptr_array_, i),
400 g_array_free (array_, TRUE);
402 g_ptr_array_free (ptr_array_, TRUE);
407 _pygi_marshal_cleanup_from_py_glist (PyGIInvokeState *state,
408 PyGIArgCache *arg_cache,
410 gboolean was_processed)
414 PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache;
416 list_ = (GSList *)data;
418 /* clean up items first */
419 if (sequence_cache->item_cache->from_py_cleanup != NULL) {
420 PyGIMarshalCleanupFunc cleanup_func =
421 sequence_cache->item_cache->from_py_cleanup;
422 GSList *node = list_;
423 while (node != NULL) {
425 sequence_cache->item_cache,
433 arg_cache->transfer == GI_TRANSFER_NOTHING ||
434 arg_cache->transfer == GI_TRANSFER_CONTAINER) {
435 switch (arg_cache->type_tag) {
436 case GI_TYPE_TAG_GLIST:
437 g_list_free ( (GList *)list_);
439 case GI_TYPE_TAG_GSLIST:
440 g_slist_free (list_);
443 g_assert_not_reached();
450 _pygi_marshal_cleanup_to_py_glist (PyGIInvokeState *state,
451 PyGIArgCache *arg_cache,
453 gboolean was_processed)
455 PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache;
457 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING ||
458 arg_cache->transfer == GI_TRANSFER_CONTAINER) {
459 GSList *list_ = (GSList *)data;
461 if (sequence_cache->item_cache->to_py_cleanup != NULL) {
462 PyGIMarshalCleanupFunc cleanup_func =
463 sequence_cache->item_cache->to_py_cleanup;
464 GSList *node = list_;
466 while (node != NULL) {
468 sequence_cache->item_cache,
475 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING) {
476 switch (arg_cache->type_tag) {
477 case GI_TYPE_TAG_GLIST:
478 g_list_free ( (GList *)list_);
480 case GI_TYPE_TAG_GSLIST:
481 g_slist_free (list_);
484 g_assert_not_reached();
491 _pygi_marshal_cleanup_from_py_ghash (PyGIInvokeState *state,
492 PyGIArgCache *arg_cache,
494 gboolean was_processed)
501 PyGIHashCache *hash_cache = (PyGIHashCache *)arg_cache;
503 hash_ = (GHashTable *)data;
505 /* clean up keys and values first */
506 if (hash_cache->key_cache->from_py_cleanup != NULL ||
507 hash_cache->value_cache->from_py_cleanup != NULL) {
508 GHashTableIter hiter;
512 PyGIMarshalCleanupFunc key_cleanup_func =
513 hash_cache->key_cache->from_py_cleanup;
514 PyGIMarshalCleanupFunc value_cleanup_func =
515 hash_cache->value_cache->from_py_cleanup;
517 g_hash_table_iter_init (&hiter, hash_);
518 while (g_hash_table_iter_next (&hiter, &key, &value)) {
519 if (key != NULL && key_cleanup_func != NULL)
520 key_cleanup_func (state,
521 hash_cache->key_cache,
524 if (value != NULL && value_cleanup_func != NULL)
525 value_cleanup_func (state,
526 hash_cache->value_cache,
533 arg_cache->transfer == GI_TRANSFER_NOTHING ||
534 arg_cache->transfer == GI_TRANSFER_CONTAINER)
535 g_hash_table_destroy (hash_);
541 _pygi_marshal_cleanup_to_py_ghash (PyGIInvokeState *state,
542 PyGIArgCache *arg_cache,
544 gboolean was_processed)
549 /* assume hashtable has boxed key and value */
550 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
551 g_hash_table_destroy ( (GHashTable *)data);