Imported Upstream version 3.13.92
[platform/upstream/pygobject2.git] / gi / pygi-signal-closure.c
1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 /*
3  * Copyright (c) 2011  Laszlo Pandy <lpandy@src.gnome.org>
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-private.h"
20 #include "pygi-value.h"
21
22 static GISignalInfo *
23 _pygi_lookup_signal_from_g_type (GType g_type,
24                                  const gchar *signal_name)
25 {
26     GIRepository *repository;
27     GIBaseInfo *info;
28     GISignalInfo *signal_info = NULL;
29
30     repository = g_irepository_get_default();
31     info = g_irepository_find_by_gtype (repository, g_type);
32     if (info == NULL)
33         return NULL;
34
35     if (GI_IS_OBJECT_INFO (info))
36         signal_info = g_object_info_find_signal ((GIObjectInfo *) info,
37                                                  signal_name);
38     else if (GI_IS_INTERFACE_INFO (info))
39         signal_info = g_interface_info_find_signal ((GIInterfaceInfo *) info,
40                                                     signal_name);
41
42     g_base_info_unref (info);
43     return signal_info;
44 }
45
46 static void
47 pygi_signal_closure_invalidate(gpointer data,
48                                GClosure *closure)
49 {
50     PyGClosure *pc = (PyGClosure *)closure;
51     PyGILState_STATE state;
52
53     state = PyGILState_Ensure();
54     Py_XDECREF(pc->callback);
55     Py_XDECREF(pc->extra_args);
56     Py_XDECREF(pc->swap_data);
57     PyGILState_Release(state);
58
59     pc->callback = NULL;
60     pc->extra_args = NULL;
61     pc->swap_data = NULL;
62
63     g_base_info_unref (((PyGISignalClosure *) pc)->signal_info);
64     ((PyGISignalClosure *) pc)->signal_info = NULL;
65 }
66
67 static void
68 pygi_signal_closure_marshal(GClosure *closure,
69                             GValue *return_value,
70                             guint n_param_values,
71                             const GValue *param_values,
72                             gpointer invocation_hint,
73                             gpointer marshal_data)
74 {
75     PyGILState_STATE state;
76     PyGClosure *pc = (PyGClosure *)closure;
77     PyObject *params, *ret = NULL;
78     guint i;
79     GISignalInfo *signal_info;
80     gint n_sig_info_args;
81     gint sig_info_highest_arg;
82     GSList *list_item = NULL;
83     GSList *pass_by_ref_structs = NULL;
84
85     state = PyGILState_Ensure();
86
87     signal_info = ((PyGISignalClosure *)closure)->signal_info;
88     n_sig_info_args = g_callable_info_get_n_args(signal_info);
89     /* the first argument to a signal callback is instance,
90        but instance is not counted in the introspection data */
91     sig_info_highest_arg = n_sig_info_args + 1;
92     g_assert_cmpint(sig_info_highest_arg, ==, n_param_values);
93
94     /* construct Python tuple for the parameter values */
95     params = PyTuple_New(n_param_values);
96     for (i = 0; i < n_param_values; i++) {
97         /* swap in a different initial data for connect_object() */
98         if (i == 0 && G_CCLOSURE_SWAP_DATA(closure)) {
99             g_return_if_fail(pc->swap_data != NULL);
100             Py_INCREF(pc->swap_data);
101             PyTuple_SetItem(params, 0, pc->swap_data);
102
103         } else if (i == 0) {
104             PyObject *item = pyg_value_as_pyobject(&param_values[i], FALSE);
105
106             if (!item) {
107                 goto out;
108             }
109             PyTuple_SetItem(params, i, item);
110
111         } else if (i < sig_info_highest_arg) {
112             GIArgInfo arg_info;
113             GITypeInfo type_info;
114             GITypeTag type_tag;
115             GIArgument arg = { 0, };
116             PyObject *item = NULL;
117             gboolean free_array = FALSE;
118             gboolean pass_struct_by_ref = FALSE;
119
120             g_callable_info_load_arg(signal_info, i - 1, &arg_info);
121             g_arg_info_load_type(&arg_info, &type_info);
122
123             arg = _pygi_argument_from_g_value(&param_values[i], &type_info);
124
125             type_tag = g_type_info_get_tag (&type_info);
126             if (type_tag == GI_TYPE_TAG_ARRAY) {
127                 /* Skip the self argument of param_values */
128                 arg.v_pointer = _pygi_argument_to_array (&arg,
129                                                          _pygi_argument_array_length_marshal,
130                                                          (void *)(param_values + 1),
131                                                          signal_info,
132                                                          &type_info,
133                                                          &free_array);
134             }
135
136             /* Hack to ensure struct arguments are passed-by-reference allowing
137              * callback implementors to modify the struct values. This is needed
138              * for keeping backwards compatibility and should be removed in future
139              * versions which support signal output arguments as return values.
140              * See: https://bugzilla.gnome.org/show_bug.cgi?id=735486
141              *
142              * Note the logic here must match the logic path taken in _pygi_argument_to_object.
143              */
144             if (type_tag == GI_TYPE_TAG_INTERFACE) {
145                 GIBaseInfo *info = g_type_info_get_interface (&type_info);
146                 GIInfoType info_type = g_base_info_get_type (info);
147
148                 if (info_type == GI_INFO_TYPE_STRUCT ||
149                         info_type == GI_INFO_TYPE_BOXED ||
150                         info_type == GI_INFO_TYPE_UNION) {
151
152                     GType gtype = g_registered_type_info_get_g_type ((GIRegisteredTypeInfo *) info);
153                     gboolean is_foreign = (info_type == GI_INFO_TYPE_STRUCT) &&
154                                           (g_struct_info_is_foreign ((GIStructInfo *) info));
155
156                     if (!is_foreign && !g_type_is_a (gtype, G_TYPE_VALUE) &&
157                             g_type_is_a (gtype, G_TYPE_BOXED)) {
158                         pass_struct_by_ref = TRUE;
159                     }
160                 }
161
162                 g_base_info_unref (info);
163             }
164
165             if (pass_struct_by_ref) {
166                 /* transfer everything will ensure the struct is not copied when wrapped. */
167                 item = _pygi_argument_to_object (&arg, &type_info, GI_TRANSFER_EVERYTHING);
168                 if (item && PyObject_IsInstance (item, (PyObject *) &PyGIBoxed_Type)) {
169                     ((PyGBoxed *)item)->free_on_dealloc = FALSE;
170                     pass_by_ref_structs = g_slist_prepend (pass_by_ref_structs, item);
171                 }
172
173             } else {
174                 item = _pygi_argument_to_object (&arg, &type_info, GI_TRANSFER_NOTHING);
175             }
176
177             if (free_array) {
178                 g_array_free (arg.v_pointer, FALSE);
179             }
180
181             if (item == NULL) {
182                 goto out;
183             }
184             PyTuple_SetItem(params, i, item);
185         }
186     }
187     /* params passed to function may have extra arguments */
188     if (pc->extra_args) {
189         PyObject *tuple = params;
190         params = PySequence_Concat(tuple, pc->extra_args);
191         Py_DECREF(tuple);
192     }
193     ret = PyObject_CallObject(pc->callback, params);
194     if (ret == NULL) {
195         if (pc->exception_handler)
196             pc->exception_handler(return_value, n_param_values, param_values);
197         else
198             PyErr_Print();
199         goto out;
200     }
201
202     if (G_IS_VALUE(return_value) && pyg_value_from_pyobject(return_value, ret) != 0) {
203         PyErr_SetString(PyExc_TypeError,
204                         "can't convert return value to desired type");
205
206         if (pc->exception_handler)
207             pc->exception_handler(return_value, n_param_values, param_values);
208         else
209             PyErr_Print();
210     }
211     Py_DECREF(ret);
212
213     /* Run through the list of structs which have been passed by reference and
214      * check if they are being held longer than the duration of the callback
215      * execution. This is determined if the ref count is greater than 1.
216      * A single ref is held by the argument list and any more would mean the callback
217      * stored a ref somewhere else. In this case we make an internal copy of
218      * the boxed struct so Python can own the memory to it.
219      */
220     list_item = pass_by_ref_structs;
221     while (list_item) {
222         PyObject *item = list_item->data;
223         if (item->ob_refcnt > 1) {
224             _pygi_boxed_copy_in_place ((PyGIBoxed *)item);
225         }
226         list_item = g_slist_next (list_item);
227     }
228
229  out:
230     g_slist_free (pass_by_ref_structs);
231     Py_DECREF(params);
232     PyGILState_Release(state);
233 }
234
235 GClosure *
236 pygi_signal_closure_new (PyGObject *instance,
237                          GType g_type,
238                          const gchar *signal_name,
239                          PyObject *callback,
240                          PyObject *extra_args,
241                          PyObject *swap_data)
242 {
243     GClosure *closure = NULL;
244     PyGISignalClosure *pygi_closure = NULL;
245     GISignalInfo *signal_info = NULL;
246
247     g_return_val_if_fail(callback != NULL, NULL);
248
249     signal_info = _pygi_lookup_signal_from_g_type (g_type, signal_name);
250     if (signal_info == NULL)
251         return NULL;
252
253     closure = g_closure_new_simple(sizeof(PyGISignalClosure), NULL);
254     g_closure_add_invalidate_notifier(closure, NULL, pygi_signal_closure_invalidate);
255     g_closure_set_marshal(closure, pygi_signal_closure_marshal);
256
257     pygi_closure = (PyGISignalClosure *)closure;
258
259     pygi_closure->signal_info = signal_info;
260     Py_INCREF(callback);
261     pygi_closure->pyg_closure.callback = callback;
262
263     if (extra_args != NULL && extra_args != Py_None) {
264         Py_INCREF(extra_args);
265         if (!PyTuple_Check(extra_args)) {
266             PyObject *tmp = PyTuple_New(1);
267             PyTuple_SetItem(tmp, 0, extra_args);
268             extra_args = tmp;
269         }
270         pygi_closure->pyg_closure.extra_args = extra_args;
271     }
272     if (swap_data) {
273         Py_INCREF(swap_data);
274         pygi_closure->pyg_closure.swap_data = swap_data;
275         closure->derivative_flag = TRUE;
276     }
277
278     return closure;
279 }