| 1 |
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2011 Laszlo Pandy <[email protected]> |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "pygi-signal-closure.h" |
| 20 |
|
| 21 |
#include "pygi-argument.h" |
| 22 |
#include "pygi-boxed.h" |
| 23 |
#include "pygi-repository.h" |
| 24 |
#include "pygi-value.h" |
| 25 |
|
| 26 |
static GISignalInfo * |
| 27 |
_pygi_lookup_signal_from_g_type (GType g_type, const gchar *signal_name) |
| 28 |
{ |
| 29 |
GIRepository *repository; |
| 30 |
GIBaseInfo *info; |
| 31 |
GISignalInfo *signal_info = NULL; |
| 32 |
|
| 33 |
repository = pygi_repository_get_default (); |
| 34 |
info = gi_repository_find_by_gtype (repository, g_type); |
| 35 |
if (info == NULL) return NULL; |
| 36 |
|
| 37 |
if (GI_IS_OBJECT_INFO (info)) |
| 38 |
signal_info = |
| 39 |
gi_object_info_find_signal (GI_OBJECT_INFO (info), signal_name); |
| 40 |
else if (GI_IS_INTERFACE_INFO (info)) |
| 41 |
signal_info = gi_interface_info_find_signal (GI_INTERFACE_INFO (info), |
| 42 |
signal_name); |
| 43 |
|
| 44 |
gi_base_info_unref (info); |
| 45 |
|
| 46 |
return signal_info; |
| 47 |
} |
| 48 |
|
| 49 |
static void |
| 50 |
pygi_signal_closure_invalidate (gpointer data, GClosure *closure) |
| 51 |
{ |
| 52 |
PyGClosure *pc = (PyGClosure *)closure; |
| 53 |
PyGILState_STATE state; |
| 54 |
|
| 55 |
state = PyGILState_Ensure (); |
| 56 |
Py_XDECREF (pc->callback); |
| 57 |
Py_XDECREF (pc->extra_args); |
| 58 |
Py_XDECREF (pc->swap_data); |
| 59 |
PyGILState_Release (state); |
| 60 |
|
| 61 |
pc->callback = NULL; |
| 62 |
pc->extra_args = NULL; |
| 63 |
pc->swap_data = NULL; |
| 64 |
|
| 65 |
gi_base_info_unref (((PyGISignalClosure *)pc)->signal_info); |
| 66 |
((PyGISignalClosure *)pc)->signal_info = NULL; |
| 67 |
} |
| 68 |
|
| 69 |
static gboolean |
| 70 |
array_length_from_parameter (GICallableInfo *callable_info, |
| 71 |
GITypeInfo *type_info, GValue *values, |
| 72 |
guint n_values, gsize *array_len) |
| 73 |
{ |
| 74 |
guint length_arg_index; |
| 75 |
GIArgInfo length_arg_info; |
| 76 |
GITypeInfo length_type_info; |
| 77 |
GIArgument length_arg; |
| 78 |
gboolean result; |
| 79 |
|
| 80 |
if (!gi_type_info_get_array_length_index (type_info, &length_arg_index)) { |
| 81 |
return FALSE; |
| 82 |
} |
| 83 |
|
| 84 |
g_assert (length_arg_index + 1 < n_values); |
| 85 |
|
| 86 |
gi_callable_info_load_arg (callable_info, length_arg_index, |
| 87 |
&length_arg_info); |
| 88 |
gi_arg_info_load_type_info (&length_arg_info, &length_type_info); |
| 89 |
|
| 90 |
/* Take the next value: values[0] is "self". */ |
| 91 |
length_arg = _pygi_argument_from_g_value (&(values[length_arg_index + 1]), |
| 92 |
&length_type_info); |
| 93 |
result = pygi_argument_to_gsize ( |
| 94 |
length_arg, gi_type_info_get_tag (&length_type_info), array_len); |
| 95 |
|
| 96 |
gi_base_info_clear (&length_arg_info); |
| 97 |
gi_base_info_clear (&length_type_info); |
| 98 |
|
| 99 |
return result; |
| 100 |
} |
| 101 |
|
| 102 |
static void |
| 103 |
pygi_signal_closure_marshal (GClosure *closure, GValue *return_value, |
| 104 |
guint n_param_values, const GValue *param_values, |
| 105 |
gpointer invocation_hint, gpointer marshal_data) |
| 106 |
{ |
| 107 |
PyGILState_STATE state; |
| 108 |
PyGClosure *pc = (PyGClosure *)closure; |
| 109 |
PyObject *params, *ret = NULL; |
| 110 |
guint i; |
| 111 |
GISignalInfo *signal_info; |
| 112 |
gint n_sig_info_args; |
| 113 |
gint sig_info_highest_arg; |
| 114 |
GSList *list_item = NULL; |
| 115 |
GSList *pass_by_ref_structs = NULL; |
| 116 |
|
| 117 |
state = PyGILState_Ensure (); |
| 118 |
|
| 119 |
signal_info = ((PyGISignalClosure *)closure)->signal_info; |
| 120 |
n_sig_info_args = |
| 121 |
gi_callable_info_get_n_args (GI_CALLABLE_INFO (signal_info)); |
| 122 |
g_assert_cmpint (n_sig_info_args, >=, 0); |
| 123 |
/* the first argument to a signal callback is instance, |
| 124 |
but instance is not counted in the introspection data */ |
| 125 |
sig_info_highest_arg = n_sig_info_args + 1; |
| 126 |
g_assert_cmpint (sig_info_highest_arg, ==, n_param_values); |
| 127 |
|
| 128 |
/* construct Python tuple for the parameter values */ |
| 129 |
params = PyTuple_New (n_param_values); |
| 130 |
for (i = 0; i < n_param_values; i++) { |
| 131 |
/* swap in a different initial data for connect_object() */ |
| 132 |
if (i == 0 && G_CCLOSURE_SWAP_DATA (closure)) { |
| 133 |
g_return_if_fail (pc->swap_data != NULL); |
| 134 |
Py_INCREF (pc->swap_data); |
| 135 |
PyTuple_SetItem (params, 0, pc->swap_data); |
| 136 |
|
| 137 |
} else if (i == 0) { |
| 138 |
PyObject *item = pyg_value_to_pyobject (¶m_values[i], FALSE); |
| 139 |
|
| 140 |
if (!item) { |
| 141 |
goto out; |
| 142 |
} |
| 143 |
PyTuple_SetItem (params, i, item); |
| 144 |
|
| 145 |
} else if (i < (guint)sig_info_highest_arg) { |
| 146 |
GIArgInfo arg_info; |
| 147 |
GITypeInfo *type_info; |
| 148 |
GITypeTag type_tag; |
| 149 |
GIArgument arg; |
| 150 |
PyObject *item = NULL; |
| 151 |
gboolean pass_struct_by_ref = FALSE; |
| 152 |
|
| 153 |
gi_callable_info_load_arg (GI_CALLABLE_INFO (signal_info), i - 1, |
| 154 |
&arg_info); |
| 155 |
type_info = gi_arg_info_get_type_info (&arg_info); |
| 156 |
|
| 157 |
arg = _pygi_argument_from_g_value (¶m_values[i], type_info); |
| 158 |
type_tag = gi_type_info_get_tag (type_info); |
| 159 |
|
| 160 |
/* Hack to ensure struct arguments are passed-by-reference allowing |
| 161 |
* callback implementors to modify the struct values. This is needed |
| 162 |
* for keeping backwards compatibility and should be removed in future |
| 163 |
* versions which support signal output arguments as return values. |
| 164 |
* See: https://bugzilla.gnome.org/show_bug.cgi?id=735486 |
| 165 |
* |
| 166 |
* Note the logic here must match the logic path taken in pygi_argument_to_py. |
| 167 |
*/ |
| 168 |
if (type_tag == GI_TYPE_TAG_INTERFACE) { |
| 169 |
GIBaseInfo *info = gi_type_info_get_interface (type_info); |
| 170 |
|
| 171 |
if (GI_IS_STRUCT_INFO (info) || GI_IS_UNION_INFO (info)) { |
| 172 |
GType gtype = gi_registered_type_info_get_g_type ( |
| 173 |
(GIRegisteredTypeInfo *)info); |
| 174 |
gboolean is_foreign = |
| 175 |
(GI_IS_STRUCT_INFO (info)) |
| 176 |
&& (gi_struct_info_is_foreign ((GIStructInfo *)info)); |
| 177 |
|
| 178 |
if (!is_foreign && !g_type_is_a (gtype, G_TYPE_VALUE) |
| 179 |
&& g_type_is_a (gtype, G_TYPE_BOXED)) { |
| 180 |
pass_struct_by_ref = TRUE; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
gi_base_info_unref (info); |
| 185 |
} |
| 186 |
|
| 187 |
if (pass_struct_by_ref) { |
| 188 |
gsize array_length; |
| 189 |
|
| 190 |
if (array_length_from_parameter ( |
| 191 |
GI_CALLABLE_INFO (signal_info), type_info, |
| 192 |
param_values, n_param_values, &array_length)) |
| 193 |
item = pygi_argument_to_py_with_array_length ( |
| 194 |
type_info, arg, GI_TRANSFER_EVERYTHING, array_length); |
| 195 |
else |
| 196 |
item = pygi_argument_to_py (type_info, arg, |
| 197 |
GI_TRANSFER_EVERYTHING); |
| 198 |
|
| 199 |
if (item |
| 200 |
&& PyObject_IsInstance (item, |
| 201 |
(PyObject *)&PyGIBoxed_Type)) { |
| 202 |
((PyGBoxed *)item)->free_on_dealloc = FALSE; |
| 203 |
pass_by_ref_structs = |
| 204 |
g_slist_prepend (pass_by_ref_structs, item); |
| 205 |
} |
| 206 |
} else { |
| 207 |
gsize array_length; |
| 208 |
|
| 209 |
if (array_length_from_parameter ( |
| 210 |
GI_CALLABLE_INFO (signal_info), type_info, |
| 211 |
param_values, n_param_values, &array_length)) |
| 212 |
item = pygi_argument_to_py_with_array_length ( |
| 213 |
type_info, arg, GI_TRANSFER_NOTHING, array_length); |
| 214 |
else |
| 215 |
item = pygi_argument_to_py (type_info, arg, |
| 216 |
GI_TRANSFER_NOTHING); |
| 217 |
} |
| 218 |
|
| 219 |
gi_base_info_unref (type_info); |
| 220 |
gi_base_info_clear (&arg_info); |
| 221 |
|
| 222 |
if (item == NULL) { |
| 223 |
PyErr_Print (); |
| 224 |
goto out; |
| 225 |
} |
| 226 |
PyTuple_SetItem (params, i, item); |
| 227 |
} |
| 228 |
} |
| 229 |
/* params passed to function may have extra arguments */ |
| 230 |
if (pc->extra_args) { |
| 231 |
PyObject *tuple = params; |
| 232 |
params = PySequence_Concat (tuple, pc->extra_args); |
| 233 |
Py_DECREF (tuple); |
| 234 |
} |
| 235 |
ret = PyObject_CallObject (pc->callback, params); |
| 236 |
if (ret == NULL) { |
| 237 |
if (pc->exception_handler) |
| 238 |
pc->exception_handler (return_value, n_param_values, param_values); |
| 239 |
else |
| 240 |
PyErr_Print (); |
| 241 |
goto out; |
| 242 |
} |
| 243 |
|
| 244 |
if (G_IS_VALUE (return_value) |
| 245 |
&& pyg_value_from_pyobject (return_value, ret) != 0) { |
| 246 |
PyErr_SetString (PyExc_TypeError, |
| 247 |
"can't convert return value to desired type"); |
| 248 |
|
| 249 |
if (pc->exception_handler) |
| 250 |
pc->exception_handler (return_value, n_param_values, param_values); |
| 251 |
else |
| 252 |
PyErr_Print (); |
| 253 |
} |
| 254 |
Py_DECREF (ret); |
| 255 |
|
| 256 |
/* Run through the list of structs which have been passed by reference and |
| 257 |
* check if they are being held longer than the duration of the callback |
| 258 |
* execution. This is determined if the ref count is greater than 1. |
| 259 |
* A single ref is held by the argument list and any more would mean the callback |
| 260 |
* stored a ref somewhere else. In this case we make an internal copy of |
| 261 |
* the boxed struct so Python can own the memory to it. |
| 262 |
*/ |
| 263 |
list_item = pass_by_ref_structs; |
| 264 |
while (list_item) { |
| 265 |
PyObject *item = list_item->data; |
| 266 |
if (Py_REFCNT (item) > 1) { |
| 267 |
pygi_boxed_copy_in_place ((PyGIBoxed *)item); |
| 268 |
} |
| 269 |
list_item = g_slist_next (list_item); |
| 270 |
} |
| 271 |
|
| 272 |
out: |
| 273 |
g_slist_free (pass_by_ref_structs); |
| 274 |
Py_DECREF (params); |
| 275 |
PyGILState_Release (state); |
| 276 |
} |
| 277 |
|
| 278 |
GClosure * |
| 279 |
pygi_signal_closure_new (PyGObject *instance, GType g_type, |
| 280 |
const gchar *signal_name, PyObject *callback, |
| 281 |
PyObject *extra_args, PyObject *swap_data) |
| 282 |
{ |
| 283 |
GClosure *closure = NULL; |
| 284 |
PyGISignalClosure *pygi_closure = NULL; |
| 285 |
GISignalInfo *signal_info = NULL; |
| 286 |
|
| 287 |
g_return_val_if_fail (callback != NULL, NULL); |
| 288 |
|
| 289 |
signal_info = _pygi_lookup_signal_from_g_type (g_type, signal_name); |
| 290 |
if (signal_info == NULL) return NULL; |
| 291 |
|
| 292 |
closure = g_closure_new_simple (sizeof (PyGISignalClosure), NULL); |
| 293 |
g_closure_add_invalidate_notifier (closure, NULL, |
| 294 |
pygi_signal_closure_invalidate); |
| 295 |
g_closure_set_marshal (closure, pygi_signal_closure_marshal); |
| 296 |
|
| 297 |
pygi_closure = (PyGISignalClosure *)closure; |
| 298 |
|
| 299 |
pygi_closure->signal_info = signal_info; |
| 300 |
pygi_closure->pyg_closure.callback = Py_NewRef (callback); |
| 301 |
|
| 302 |
if (extra_args != NULL && !Py_IsNone (extra_args)) { |
| 303 |
Py_INCREF (extra_args); |
| 304 |
if (!PyTuple_Check (extra_args)) { |
| 305 |
PyObject *tmp = PyTuple_New (1); |
| 306 |
PyTuple_SetItem (tmp, 0, extra_args); |
| 307 |
extra_args = tmp; |
| 308 |
} |
| 309 |
pygi_closure->pyg_closure.extra_args = extra_args; |
| 310 |
} |
| 311 |
if (swap_data) { |
| 312 |
pygi_closure->pyg_closure.swap_data = Py_NewRef (swap_data); |
| 313 |
closure->derivative_flag = TRUE; |
| 314 |
} |
| 315 |
|
| 316 |
return closure; |
| 317 |
} |