| 1 |
/* -*- Mode: C; c-basic-offset: 4 -*- |
| 2 |
* vim: tabstop=4 shiftwidth=4 expandtab |
| 3 |
* |
| 4 |
* Copyright (C) 2011 John (J5) Palmieri <[email protected]>, Red Hat, Inc. |
| 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-marshal-cleanup.h" |
| 21 |
#include "pygi-foreign.h" |
| 22 |
|
| 23 |
static inline void |
| 24 |
_cleanup_caller_allocates (PyGIInvokeState *state, PyGIArgCache *cache, |
| 25 |
PyObject *py_obj, gpointer data, |
| 26 |
gboolean was_processed) |
| 27 |
{ |
| 28 |
PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)cache; |
| 29 |
|
| 30 |
/* check GValue first because GValue is also a boxed sub-type */ |
| 31 |
if (g_type_is_a (iface_cache->g_type, G_TYPE_VALUE)) { |
| 32 |
if (was_processed) g_value_unset (data); |
| 33 |
g_slice_free (GValue, data); |
| 34 |
} else if (g_type_is_a (iface_cache->g_type, G_TYPE_BOXED)) { |
| 35 |
gsize size; |
| 36 |
if (was_processed) return; /* will be cleaned up at deallocation */ |
| 37 |
size = gi_struct_info_get_size ( |
| 38 |
GI_STRUCT_INFO (iface_cache->interface_info)); |
| 39 |
g_slice_free1 (size, data); |
| 40 |
} else if (iface_cache->is_foreign) { |
| 41 |
if (was_processed) return; /* will be cleaned up at deallocation */ |
| 42 |
pygi_struct_foreign_release ( |
| 43 |
GI_BASE_INFO (iface_cache->interface_info), data); |
| 44 |
} else { |
| 45 |
if (was_processed) return; /* will be cleaned up at deallocation */ |
| 46 |
g_free (data); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Cleanup during invoke can happen in multiple |
| 52 |
* stages, each of which can be the result of a |
| 53 |
* successful compleation of that stage or an error |
| 54 |
* occured which requires partial cleanup. |
| 55 |
* |
| 56 |
* For the most part, either the C interface being |
| 57 |
* invoked or the python object which wraps the |
| 58 |
* parameters, handle their lifecycles but in some |
| 59 |
* cases, where we have intermediate objects, |
| 60 |
* or when we fail processing a parameter, we need |
| 61 |
* to handle the clean up manually. |
| 62 |
* |
| 63 |
* There are two argument processing stages. |
| 64 |
* They are the in stage, where we process python |
| 65 |
* parameters into their C counterparts, and the out |
| 66 |
* stage, where we process out C parameters back |
| 67 |
* into python objects. The in stage also sets up |
| 68 |
* temporary out structures for caller allocated |
| 69 |
* parameters which need to be cleaned up either on |
| 70 |
* in stage failure or at the completion of the out |
| 71 |
* stage (either success or failure) |
| 72 |
* |
| 73 |
* The in stage must call one of these cleanup functions: |
| 74 |
* - pygi_marshal_cleanup_args_from_py_marshal_success |
| 75 |
* (continue to out stage) |
| 76 |
* - pygi_marshal_cleanup_args_from_py_parameter_fail |
| 77 |
* (final, exit from invoke) |
| 78 |
* |
| 79 |
* The out stage must call one of these cleanup functions which are all final: |
| 80 |
* - pygi_marshal_cleanup_args_to_py_marshal_success |
| 81 |
* - pygi_marshal_cleanup_args_return_fail |
| 82 |
* - pygi_marshal_cleanup_args_to_py_parameter_fail |
| 83 |
* |
| 84 |
**/ |
| 85 |
void |
| 86 |
pygi_marshal_cleanup_args_from_py_marshal_success (PyGIInvokeState *state, |
| 87 |
PyGICallableCache *cache) |
| 88 |
{ |
| 89 |
PyObject *error_type, *error_value, *error_traceback; |
| 90 |
gboolean have_error = !!PyErr_Occurred (); |
| 91 |
|
| 92 |
if (have_error) PyErr_Fetch (&error_type, &error_value, &error_traceback); |
| 93 |
|
| 94 |
for (guint i = 0; i < _pygi_callable_cache_args_len (cache); i++) { |
| 95 |
PyGIArgCache *arg_cache = _pygi_callable_cache_get_arg (cache, i); |
| 96 |
PyGIMarshalFromPyCleanupFunc cleanup_func = arg_cache->from_py_cleanup; |
| 97 |
gpointer cleanup_data = state->args[i].arg_cleanup_data; |
| 98 |
|
| 99 |
/* Only cleanup using args_cleanup_data when available. |
| 100 |
* It is the responsibility of the various "from_py" marshalers to return |
| 101 |
* cleanup_data which is then passed into their respective cleanup function. |
| 102 |
* PyGIInvokeState.args_cleanup_data stores this data (via _invoke_marshal_in_args) |
| 103 |
* for the duration of the invoke up until this point. |
| 104 |
*/ |
| 105 |
if (cleanup_func && cleanup_data != NULL |
| 106 |
&& arg_cache->py_arg_index >= 0 |
| 107 |
&& arg_cache->direction & PYGI_DIRECTION_FROM_PYTHON) { |
| 108 |
PyObject *py_arg = |
| 109 |
PyTuple_GET_ITEM (state->py_in_args, arg_cache->py_arg_index); |
| 110 |
cleanup_func (state, arg_cache, py_arg, cleanup_data, TRUE); |
| 111 |
state->args[i].arg_cleanup_data = NULL; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
if (have_error) PyErr_Restore (error_type, error_value, error_traceback); |
| 116 |
} |
| 117 |
|
| 118 |
void |
| 119 |
pygi_marshal_cleanup_args_to_py_marshal_success (PyGIInvokeState *state, |
| 120 |
PyGICallableCache *cache) |
| 121 |
{ |
| 122 |
GSList *cache_item; |
| 123 |
PyObject *error_type, *error_value, *error_traceback; |
| 124 |
gboolean have_error = !!PyErr_Occurred (); |
| 125 |
|
| 126 |
if (have_error) PyErr_Fetch (&error_type, &error_value, &error_traceback); |
| 127 |
|
| 128 |
/* clean up the return if available */ |
| 129 |
if (cache->return_cache != NULL) { |
| 130 |
PyGIMarshalToPyCleanupFunc cleanup_func = |
| 131 |
cache->return_cache->to_py_cleanup; |
| 132 |
if (cleanup_func && state->return_arg.v_pointer != NULL) |
| 133 |
cleanup_func (state, cache->return_cache, |
| 134 |
state->to_py_return_arg_cleanup_data, |
| 135 |
state->return_arg.v_pointer, TRUE); |
| 136 |
} |
| 137 |
|
| 138 |
/* Now clean up args */ |
| 139 |
cache_item = cache->to_py_args; |
| 140 |
while (cache_item) { |
| 141 |
PyGIArgCache *arg_cache = (PyGIArgCache *)cache_item->data; |
| 142 |
PyGIMarshalToPyCleanupFunc cleanup_func = arg_cache->to_py_cleanup; |
| 143 |
gpointer data = |
| 144 |
state->args[arg_cache->c_arg_index].arg_value.v_pointer; |
| 145 |
|
| 146 |
if (cleanup_func != NULL && data != NULL) |
| 147 |
cleanup_func ( |
| 148 |
state, arg_cache, |
| 149 |
state->args[arg_cache->c_arg_index].to_py_arg_cleanup_data, |
| 150 |
data, TRUE); |
| 151 |
else if (pygi_arg_cache_is_caller_allocates (arg_cache) |
| 152 |
&& data != NULL) { |
| 153 |
_cleanup_caller_allocates ( |
| 154 |
state, arg_cache, |
| 155 |
state->args[arg_cache->c_arg_index].to_py_arg_cleanup_data, |
| 156 |
data, TRUE); |
| 157 |
} |
| 158 |
|
| 159 |
cache_item = cache_item->next; |
| 160 |
} |
| 161 |
|
| 162 |
if (have_error) PyErr_Restore (error_type, error_value, error_traceback); |
| 163 |
} |
| 164 |
|
| 165 |
void |
| 166 |
pygi_marshal_cleanup_args_from_py_parameter_fail (PyGIInvokeState *state, |
| 167 |
PyGICallableCache *cache, |
| 168 |
gssize failed_arg_index) |
| 169 |
{ |
| 170 |
PyObject *error_type, *error_value, *error_traceback; |
| 171 |
|
| 172 |
g_assert (PyErr_Occurred ()); |
| 173 |
|
| 174 |
PyErr_Fetch (&error_type, &error_value, &error_traceback); |
| 175 |
|
| 176 |
state->failed = TRUE; |
| 177 |
|
| 178 |
for (guint i = 0; i < _pygi_callable_cache_args_len (cache) |
| 179 |
&& i <= (guint)failed_arg_index; |
| 180 |
i++) { |
| 181 |
PyGIArgCache *arg_cache = _pygi_callable_cache_get_arg (cache, i); |
| 182 |
PyGIMarshalFromPyCleanupFunc cleanup_func = arg_cache->from_py_cleanup; |
| 183 |
gpointer cleanup_data = state->args[i].arg_cleanup_data; |
| 184 |
PyObject *py_arg = NULL; |
| 185 |
|
| 186 |
if (arg_cache->py_arg_index < 0) { |
| 187 |
continue; |
| 188 |
} |
| 189 |
py_arg = PyTuple_GET_ITEM (state->py_in_args, arg_cache->py_arg_index); |
| 190 |
|
| 191 |
if (cleanup_func && cleanup_data != NULL |
| 192 |
&& arg_cache->direction == PYGI_DIRECTION_FROM_PYTHON) { |
| 193 |
cleanup_func (state, arg_cache, py_arg, cleanup_data, |
| 194 |
i < (guint)failed_arg_index); |
| 195 |
|
| 196 |
} else if (pygi_arg_cache_is_caller_allocates (arg_cache) |
| 197 |
&& cleanup_data != NULL) { |
| 198 |
_cleanup_caller_allocates (state, arg_cache, py_arg, cleanup_data, |
| 199 |
FALSE); |
| 200 |
} |
| 201 |
state->args[i].arg_cleanup_data = NULL; |
| 202 |
} |
| 203 |
|
| 204 |
PyErr_Restore (error_type, error_value, error_traceback); |
| 205 |
} |
| 206 |
|
| 207 |
void |
| 208 |
pygi_marshal_cleanup_args_return_fail (PyGIInvokeState *state, |
| 209 |
PyGICallableCache *cache) |
| 210 |
{ |
| 211 |
state->failed = TRUE; |
| 212 |
} |
| 213 |
|
| 214 |
void |
| 215 |
pygi_marshal_cleanup_args_to_py_parameter_fail (PyGIInvokeState *state, |
| 216 |
PyGICallableCache *cache, |
| 217 |
gssize failed_to_py_arg_index) |
| 218 |
{ |
| 219 |
state->failed = TRUE; |
| 220 |
} |