| 1 |
/* -*- Mode: C; c-basic-offset: 4 -*- |
| 2 |
* vim: tabstop=4 shiftwidth=4 expandtab |
| 3 |
* |
| 4 |
* Copyright (C) 2011 John (J5) Palmieri <[email protected]> |
| 5 |
* Copyright (C) 2014 Simon Feltman <[email protected]> |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Lesser General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2.1 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public |
| 18 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "pygi-async.h" |
| 22 |
#include "pygi-ccallback.h" |
| 23 |
#include "pygi-closure.h" |
| 24 |
#include "pygi-cache-private.h" |
| 25 |
|
| 26 |
extern PyObject *_PyGIDefaultArgPlaceholder; |
| 27 |
|
| 28 |
/* _pygi_destroy_notify_dummy: |
| 29 |
* |
| 30 |
* Dummy method used in the occasion when a method has a GDestroyNotify |
| 31 |
* argument without user data. |
| 32 |
*/ |
| 33 |
static void |
| 34 |
_pygi_destroy_notify_dummy (gpointer data) |
| 35 |
{ |
| 36 |
} |
| 37 |
|
| 38 |
static gboolean |
| 39 |
_pygi_marshal_from_py_interface_callback (PyGIInvokeState *state, |
| 40 |
PyGICallableCache *callable_cache, |
| 41 |
PyGIArgCache *arg_cache, |
| 42 |
PyObject *py_arg, GIArgument *arg, |
| 43 |
gpointer *cleanup_data) |
| 44 |
{ |
| 45 |
GICallableInfo *callable_info; |
| 46 |
PyGICClosure *closure; |
| 47 |
PyGIArgCache *user_data_cache = NULL; |
| 48 |
PyGIArgCache *destroy_cache = NULL; |
| 49 |
PyGICallbackCache *callback_cache; |
| 50 |
PyObject *py_user_data = NULL; |
| 51 |
|
| 52 |
callback_cache = (PyGICallbackCache *)arg_cache; |
| 53 |
|
| 54 |
if (py_arg == _PyGIDefaultArgPlaceholder) { |
| 55 |
/* We need to have an async to "marshal" instead in this case. */ |
| 56 |
if (!state->py_async) return FALSE; |
| 57 |
|
| 58 |
if (callback_cache->user_data_index <= 0) return FALSE; |
| 59 |
|
| 60 |
user_data_cache = _pygi_callable_cache_get_arg ( |
| 61 |
callable_cache, callback_cache->user_data_index); |
| 62 |
|
| 63 |
Py_INCREF (state->py_async); |
| 64 |
arg->v_pointer = pygi_async_finish_cb; |
| 65 |
state->args[user_data_cache->c_arg_index].arg_value.v_pointer = |
| 66 |
state->py_async; |
| 67 |
|
| 68 |
return TRUE; |
| 69 |
} |
| 70 |
|
| 71 |
if (callback_cache->has_user_data && callback_cache->user_data_index > 0) { |
| 72 |
user_data_cache = _pygi_callable_cache_get_arg ( |
| 73 |
callable_cache, callback_cache->user_data_index); |
| 74 |
if (user_data_cache->py_arg_index < state->n_py_in_args) { |
| 75 |
/* py_user_data is a borrowed reference. */ |
| 76 |
py_user_data = PyTuple_GetItem (state->py_in_args, |
| 77 |
user_data_cache->py_arg_index); |
| 78 |
if (!py_user_data) return FALSE; |
| 79 |
/* NULL out user_data if it was not supplied and the default arg placeholder |
| 80 |
* was used instead. |
| 81 |
*/ |
| 82 |
if (py_user_data == _PyGIDefaultArgPlaceholder) { |
| 83 |
py_user_data = NULL; |
| 84 |
} else if (callable_cache->user_data_varargs_arg == NULL) { |
| 85 |
/* For non-variable length user data, place the user data in a |
| 86 |
* single item tuple which is concatenated to the callbacks arguments. |
| 87 |
* This allows callback input arg marshaling to always expect a |
| 88 |
* tuple for user data. Note the |
| 89 |
*/ |
| 90 |
py_user_data = Py_BuildValue ("(O)", py_user_data, NULL); |
| 91 |
} else { |
| 92 |
/* increment the ref borrowed from PyTuple_GetItem above */ |
| 93 |
Py_INCREF (py_user_data); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
if (Py_IsNone (py_arg)) { |
| 99 |
return TRUE; |
| 100 |
} |
| 101 |
|
| 102 |
if (!PyCallable_Check (py_arg)) { |
| 103 |
PyErr_Format (PyExc_TypeError, |
| 104 |
"Callback needs to be a function or method not %s", |
| 105 |
Py_TYPE (py_arg)->tp_name); |
| 106 |
|
| 107 |
return FALSE; |
| 108 |
} |
| 109 |
|
| 110 |
callable_info = (GICallableInfo *)callback_cache->interface_info; |
| 111 |
|
| 112 |
closure = _pygi_make_native_closure ( |
| 113 |
callable_info, callback_cache->closure_cache, callback_cache->scope, |
| 114 |
py_arg, py_user_data); |
| 115 |
|
| 116 |
if (closure->closure != NULL) |
| 117 |
arg->v_pointer = gi_callable_info_get_closure_native_address ( |
| 118 |
callable_info, closure->closure); |
| 119 |
else |
| 120 |
arg->v_pointer = NULL; |
| 121 |
|
| 122 |
/* always decref the user data as _pygi_make_native_closure adds its own ref */ |
| 123 |
Py_XDECREF (py_user_data); |
| 124 |
|
| 125 |
/* The PyGICClosure instance is used as user data passed into the C function. |
| 126 |
* The return trip to python will marshal this back and pull the python user data out. |
| 127 |
*/ |
| 128 |
if (user_data_cache != NULL) { |
| 129 |
state->args[user_data_cache->c_arg_index].arg_value.v_pointer = |
| 130 |
closure; |
| 131 |
} |
| 132 |
|
| 133 |
/* Setup a GDestroyNotify callback if this method supports it along with |
| 134 |
* a user data field. The user data field is a requirement in order |
| 135 |
* free resources and ref counts associated with this arguments closure. |
| 136 |
* In case a user data field is not available, show a warning giving |
| 137 |
* explicit information and setup a dummy notification to avoid a crash |
| 138 |
* later on in _pygi_destroy_notify_callback_closure. |
| 139 |
*/ |
| 140 |
if (callback_cache->has_destroy_notify |
| 141 |
&& callback_cache->destroy_notify_index > 0) { |
| 142 |
destroy_cache = _pygi_callable_cache_get_arg ( |
| 143 |
callable_cache, callback_cache->destroy_notify_index); |
| 144 |
} |
| 145 |
|
| 146 |
if (destroy_cache) { |
| 147 |
if (user_data_cache != NULL) { |
| 148 |
state->args[destroy_cache->c_arg_index].arg_value.v_pointer = |
| 149 |
_pygi_invoke_closure_free; |
| 150 |
} else { |
| 151 |
char *full_name = |
| 152 |
pygi_callable_cache_get_full_name (callable_cache); |
| 153 |
gchar *msg = g_strdup_printf ( |
| 154 |
"Callables passed to %s will leak references because " |
| 155 |
"the method does not support a user_data argument. " |
| 156 |
"See: https://bugzilla.gnome.org/show_bug.cgi?id=685598", |
| 157 |
full_name); |
| 158 |
g_free (full_name); |
| 159 |
if (PyErr_WarnEx (PyExc_RuntimeWarning, msg, 2)) { |
| 160 |
g_free (msg); |
| 161 |
_pygi_invoke_closure_free (closure); |
| 162 |
return FALSE; |
| 163 |
} |
| 164 |
g_free (msg); |
| 165 |
state->args[destroy_cache->c_arg_index].arg_value.v_pointer = |
| 166 |
_pygi_destroy_notify_dummy; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/* Use the PyGIClosure as data passed to cleanup for GI_SCOPE_TYPE_CALL. */ |
| 171 |
*cleanup_data = closure; |
| 172 |
|
| 173 |
return TRUE; |
| 174 |
} |
| 175 |
|
| 176 |
static PyObject * |
| 177 |
_pygi_marshal_to_py_interface_callback (PyGIInvokeState *state, |
| 178 |
PyGICallableCache *callable_cache, |
| 179 |
PyGIArgCache *arg_cache, |
| 180 |
GIArgument *arg, |
| 181 |
gpointer *arg_cleanup_data) |
| 182 |
{ |
| 183 |
PyGICallbackCache *callback_cache = (PyGICallbackCache *)arg_cache; |
| 184 |
gpointer user_data = NULL; |
| 185 |
GDestroyNotify destroy_notify = NULL; |
| 186 |
|
| 187 |
if (callback_cache->has_user_data) |
| 188 |
user_data = |
| 189 |
state->args[callback_cache->user_data_index].arg_value.v_pointer; |
| 190 |
|
| 191 |
if (callback_cache->has_destroy_notify) |
| 192 |
destroy_notify = state->args[callback_cache->destroy_notify_index] |
| 193 |
.arg_value.v_pointer; |
| 194 |
|
| 195 |
return _pygi_ccallback_new ( |
| 196 |
arg->v_pointer, user_data, callback_cache->scope, |
| 197 |
GI_CALLABLE_INFO (callback_cache->interface_info), destroy_notify); |
| 198 |
} |
| 199 |
|
| 200 |
static void |
| 201 |
_pygi_marshal_cleanup_from_py_interface_callback (PyGIInvokeState *state, |
| 202 |
PyGIArgCache *arg_cache, |
| 203 |
PyObject *py_arg, |
| 204 |
gpointer data, |
| 205 |
gboolean was_processed) |
| 206 |
{ |
| 207 |
PyGICallbackCache *callback_cache = (PyGICallbackCache *)arg_cache; |
| 208 |
|
| 209 |
if (was_processed && callback_cache->scope == GI_SCOPE_TYPE_CALL) { |
| 210 |
_pygi_invoke_closure_free (data); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
static void |
| 215 |
_callback_cache_free_func (PyGICallbackCache *cache) |
| 216 |
{ |
| 217 |
if (cache != NULL) { |
| 218 |
if (cache->interface_info != NULL) |
| 219 |
gi_base_info_unref ((GIBaseInfo *)cache->interface_info); |
| 220 |
|
| 221 |
if (cache->closure_cache != NULL) { |
| 222 |
pygi_callable_cache_free ( |
| 223 |
(PyGICallableCache *)cache->closure_cache); |
| 224 |
cache->closure_cache = NULL; |
| 225 |
} |
| 226 |
|
| 227 |
g_slice_free (PyGICallbackCache, cache); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
PyGIArgCache * |
| 232 |
pygi_arg_callback_new_from_info (GITypeInfo *type_info, |
| 233 |
GIArgInfo *arg_info, /* may be null */ |
| 234 |
GITransfer transfer, PyGIDirection direction, |
| 235 |
GICallbackInfo *iface_info, |
| 236 |
PyGICallableCache *callable_cache) |
| 237 |
{ |
| 238 |
PyGICallbackCache *callback_cache; |
| 239 |
PyGIArgCache *arg_cache; |
| 240 |
|
| 241 |
callback_cache = g_slice_new0 (PyGICallbackCache); |
| 242 |
if (callback_cache == NULL) return NULL; |
| 243 |
|
| 244 |
arg_cache = (PyGIArgCache *)callback_cache; |
| 245 |
unsigned int child_offset = 0; |
| 246 |
|
| 247 |
pygi_arg_base_setup ((PyGIArgCache *)arg_cache, type_info, arg_info, |
| 248 |
transfer, direction); |
| 249 |
|
| 250 |
if (callable_cache != NULL) child_offset = callable_cache->args_offset; |
| 251 |
|
| 252 |
((PyGIArgCache *)arg_cache)->destroy_notify = |
| 253 |
(GDestroyNotify)_callback_cache_free_func; |
| 254 |
|
| 255 |
callback_cache->has_user_data = |
| 256 |
arg_info != NULL |
| 257 |
&& gi_arg_info_get_closure_index (arg_info, |
| 258 |
&callback_cache->user_data_index); |
| 259 |
if (callback_cache->has_user_data) |
| 260 |
callback_cache->user_data_index += child_offset; |
| 261 |
|
| 262 |
callback_cache->has_destroy_notify = |
| 263 |
arg_info != NULL |
| 264 |
&& gi_arg_info_get_destroy_index ( |
| 265 |
arg_info, &callback_cache->destroy_notify_index); |
| 266 |
if (callback_cache->has_destroy_notify) |
| 267 |
callback_cache->destroy_notify_index += child_offset; |
| 268 |
|
| 269 |
if (callback_cache->has_user_data) { |
| 270 |
PyGIArgCache *user_data_arg_cache = pygi_arg_cache_alloc (); |
| 271 |
user_data_arg_cache->meta_type = PYGI_META_ARG_TYPE_CHILD_WITH_PYARG; |
| 272 |
user_data_arg_cache->direction = direction; |
| 273 |
|
| 274 |
PyGIArgCache *existing = _pygi_callable_cache_get_arg ( |
| 275 |
callable_cache, callback_cache->user_data_index); |
| 276 |
if (existing != NULL) { |
| 277 |
/* user_data preceded the callback in the arg list and was already |
| 278 |
* built as a PARENT by the main loop. Reuse its py_arg_index so |
| 279 |
* we don't leave a hole in the [0..n_py_args) numbering. |
| 280 |
*/ |
| 281 |
|
| 282 |
/* user_data should never be (out); if it ever is, we'd also need to |
| 283 |
* remove `existing` from `callable_cache->to_py_args` and decrememnt |
| 284 |
* `n_to_py_args` before freeing it. */ |
| 285 |
g_assert (!(existing->direction & PYGI_DIRECTION_TO_PYTHON)); |
| 286 |
|
| 287 |
user_data_arg_cache->py_arg_index = existing->py_arg_index; |
| 288 |
user_data_arg_cache->c_arg_index = existing->c_arg_index; |
| 289 |
user_data_arg_cache->type_tag = existing->type_tag; |
| 290 |
pygi_arg_cache_free (existing); |
| 291 |
} |
| 292 |
|
| 293 |
_pygi_callable_cache_set_arg (callable_cache, |
| 294 |
callback_cache->user_data_index, |
| 295 |
user_data_arg_cache); |
| 296 |
} |
| 297 |
|
| 298 |
if (callback_cache->has_destroy_notify) { |
| 299 |
PyGIArgCache *destroy_arg_cache = pygi_arg_cache_alloc (); |
| 300 |
destroy_arg_cache->meta_type = PYGI_META_ARG_TYPE_CHILD; |
| 301 |
destroy_arg_cache->direction = direction; |
| 302 |
_pygi_callable_cache_set_arg (callable_cache, |
| 303 |
callback_cache->destroy_notify_index, |
| 304 |
destroy_arg_cache); |
| 305 |
} |
| 306 |
|
| 307 |
callback_cache->scope = arg_info != NULL ? gi_arg_info_get_scope (arg_info) |
| 308 |
: GI_SCOPE_TYPE_INVALID; |
| 309 |
gi_base_info_ref (iface_info); |
| 310 |
callback_cache->interface_info = GI_BASE_INFO (iface_info); |
| 311 |
|
| 312 |
if (direction & PYGI_DIRECTION_FROM_PYTHON) { |
| 313 |
callback_cache->closure_cache = pygi_closure_cache_new ( |
| 314 |
GI_CALLABLE_INFO (callback_cache->interface_info)); |
| 315 |
arg_cache->from_py_marshaller = |
| 316 |
_pygi_marshal_from_py_interface_callback; |
| 317 |
arg_cache->from_py_cleanup = |
| 318 |
_pygi_marshal_cleanup_from_py_interface_callback; |
| 319 |
|
| 320 |
if (callback_cache->scope == GI_SCOPE_TYPE_ASYNC) |
| 321 |
callback_cache->arg_cache.async_context = |
| 322 |
PYGI_ASYNC_CONTEXT_CALLBACK; |
| 323 |
} |
| 324 |
|
| 325 |
if (direction & PYGI_DIRECTION_TO_PYTHON) { |
| 326 |
callback_cache->arg_cache.to_py_marshaller = |
| 327 |
_pygi_marshal_to_py_interface_callback; |
| 328 |
} |
| 329 |
|
| 330 |
return (PyGIArgCache *)callback_cache; |
| 331 |
} |