tests: Remove TestMainLoop.test_concurrency
[pygobject.git] / gi / pygi-marshal-cleanup.c
blob906be58c54e4dac65cfccf8f91f4e99dee417840
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, see <http://www.gnu.org/licenses/>.
20 #include "pygi-marshal-cleanup.h"
21 #include "pygi-foreign.h"
22 #include <glib.h>
24 static inline void
25 _cleanup_caller_allocates (PyGIInvokeState *state,
26 PyGIArgCache *cache,
27 PyObject *py_obj,
28 gpointer data,
29 gboolean was_processed)
31 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)cache;
33 /* check GValue first because GValue is also a boxed sub-type */
34 if (g_type_is_a (iface_cache->g_type, G_TYPE_VALUE)) {
35 if (was_processed)
36 g_value_unset (data);
37 g_slice_free (GValue, data);
38 } else if (g_type_is_a (iface_cache->g_type, G_TYPE_BOXED)) {
39 gsize size;
40 if (was_processed)
41 return; /* will be cleaned up at deallocation */
42 size = g_struct_info_get_size (iface_cache->interface_info);
43 g_slice_free1 (size, data);
44 } else if (iface_cache->is_foreign) {
45 if (was_processed)
46 return; /* will be cleaned up at deallocation */
47 pygi_struct_foreign_release ((GIBaseInfo *)iface_cache->interface_info,
48 data);
49 } else {
50 if (was_processed)
51 return; /* will be cleaned up at deallocation */
52 g_free (data);
56 /**
57 * Cleanup during invoke can happen in multiple
58 * stages, each of which can be the result of a
59 * successful compleation of that stage or an error
60 * occured which requires partial cleanup.
62 * For the most part, either the C interface being
63 * invoked or the python object which wraps the
64 * parameters, handle their lifecycles but in some
65 * cases, where we have intermediate objects,
66 * or when we fail processing a parameter, we need
67 * to handle the clean up manually.
69 * There are two argument processing stages.
70 * They are the in stage, where we process python
71 * parameters into their C counterparts, and the out
72 * stage, where we process out C parameters back
73 * into python objects. The in stage also sets up
74 * temporary out structures for caller allocated
75 * parameters which need to be cleaned up either on
76 * in stage failure or at the completion of the out
77 * stage (either success or failure)
79 * The in stage must call one of these cleanup functions:
80 * - pygi_marshal_cleanup_args_from_py_marshal_success
81 * (continue to out stage)
82 * - pygi_marshal_cleanup_args_from_py_parameter_fail
83 * (final, exit from invoke)
85 * The out stage must call one of these cleanup functions which are all final:
86 * - pygi_marshal_cleanup_args_to_py_marshal_success
87 * - pygi_marshal_cleanup_args_return_fail
88 * - pygi_marshal_cleanup_args_to_py_parameter_fail
90 **/
91 void
92 pygi_marshal_cleanup_args_from_py_marshal_success (PyGIInvokeState *state,
93 PyGICallableCache *cache)
95 gssize i;
97 for (i = 0; (gsize)i < _pygi_callable_cache_args_len (cache); i++) {
98 PyGIArgCache *arg_cache = _pygi_callable_cache_get_arg (cache, i);
99 PyGIMarshalCleanupFunc cleanup_func = arg_cache->from_py_cleanup;
100 gpointer cleanup_data = state->args[i].arg_cleanup_data;
102 /* Only cleanup using args_cleanup_data when available.
103 * It is the responsibility of the various "from_py" marshalers to return
104 * cleanup_data which is then passed into their respective cleanup function.
105 * PyGIInvokeState.args_cleanup_data stores this data (via _invoke_marshal_in_args)
106 * for the duration of the invoke up until this point.
108 if (cleanup_func && cleanup_data != NULL && arg_cache->py_arg_index >= 0 &&
109 arg_cache->direction & PYGI_DIRECTION_FROM_PYTHON) {
110 PyObject *py_arg = PyTuple_GET_ITEM (state->py_in_args, arg_cache->py_arg_index);
111 cleanup_func (state, arg_cache, py_arg, cleanup_data, TRUE);
112 state->args[i].arg_cleanup_data = NULL;
117 void
118 pygi_marshal_cleanup_args_to_py_marshal_success (PyGIInvokeState *state,
119 PyGICallableCache *cache)
121 GSList *cache_item;
122 /* clean up the return if available */
123 if (cache->return_cache != NULL) {
124 PyGIMarshalCleanupFunc cleanup_func = cache->return_cache->to_py_cleanup;
125 if (cleanup_func && state->return_arg.v_pointer != NULL)
126 cleanup_func (state,
127 cache->return_cache,
128 NULL,
129 state->return_arg.v_pointer,
130 TRUE);
133 /* Now clean up args */
134 cache_item = cache->to_py_args;
135 while (cache_item) {
136 PyGIArgCache *arg_cache = (PyGIArgCache *) cache_item->data;
137 PyGIMarshalCleanupFunc cleanup_func = arg_cache->to_py_cleanup;
138 gpointer data = state->args[arg_cache->c_arg_index].arg_value.v_pointer;
140 if (cleanup_func != NULL && data != NULL)
141 cleanup_func (state,
142 arg_cache,
143 NULL,
144 data,
145 TRUE);
146 else if (arg_cache->is_caller_allocates && data != NULL) {
147 _cleanup_caller_allocates (state,
148 arg_cache,
149 NULL,
150 data,
151 TRUE);
154 cache_item = cache_item->next;
158 void
159 pygi_marshal_cleanup_args_from_py_parameter_fail (PyGIInvokeState *state,
160 PyGICallableCache *cache,
161 gssize failed_arg_index)
163 gssize i;
165 state->failed = TRUE;
167 for (i = 0; (gsize)i < _pygi_callable_cache_args_len (cache) && i <= failed_arg_index; i++) {
168 PyGIArgCache *arg_cache = _pygi_callable_cache_get_arg (cache, i);
169 PyGIMarshalCleanupFunc cleanup_func = arg_cache->from_py_cleanup;
170 gpointer cleanup_data = state->args[i].arg_cleanup_data;
171 PyObject *py_arg = NULL;
173 if (arg_cache->py_arg_index < 0) {
174 continue;
176 py_arg = PyTuple_GET_ITEM (state->py_in_args, arg_cache->py_arg_index);
178 if (cleanup_func && cleanup_data != NULL &&
179 arg_cache->direction == PYGI_DIRECTION_FROM_PYTHON) {
180 cleanup_func (state,
181 arg_cache,
182 py_arg,
183 cleanup_data,
184 i < failed_arg_index);
186 } else if (arg_cache->is_caller_allocates && cleanup_data != NULL) {
187 _cleanup_caller_allocates (state,
188 arg_cache,
189 py_arg,
190 cleanup_data,
191 FALSE);
193 state->args[i].arg_cleanup_data = NULL;
197 void
198 pygi_marshal_cleanup_args_return_fail (PyGIInvokeState *state,
199 PyGICallableCache *cache)
201 state->failed = TRUE;
204 void
205 pygi_marshal_cleanup_args_to_py_parameter_fail (PyGIInvokeState *state,
206 PyGICallableCache *cache,
207 gssize failed_to_py_arg_index)
209 state->failed = TRUE;