Debian Codegraph @

gi/pygi-cache-object.c

raw
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-fundamental.h"
22 #include "pygobject-object.h"
23 #include "pygi-cache-private.h"
24
25 /* pygi_marshal_from_py_object:
26 * py_arg: (in):
27 * arg: (out):
28 */
29 static gboolean
30 pygi_marshal_from_py_object (PyObject *py_arg, /*in*/
31 GIArgument *arg, /*out*/
32 GITransfer transfer)
33 {
34 GObject *gobj;
35
36 if (Py_IsNone (py_arg)) {
37 arg->v_pointer = NULL;
38 return TRUE;
39 }
40
41 if (PyObject_TypeCheck (py_arg, &PyGIFundamental_Type)) {
42 arg->v_pointer = pygi_fundamental_get (py_arg);
43 if (transfer == GI_TRANSFER_EVERYTHING) {
44 pygi_fundamental_ref ((PyGIFundamental *)py_arg);
45 }
46 return TRUE;
47 }
48
49 if (!pygobject_check (py_arg, &PyGObject_Type)) {
50 PyObject *repr = PyObject_Repr (py_arg);
51 PyErr_Format (PyExc_TypeError, "expected GObject but got %s",
52 PyUnicode_AsUTF8 (repr));
53 Py_DECREF (repr);
54 return FALSE;
55 }
56
57 gobj = pygobject_get (py_arg);
58 if (gobj == NULL) {
59 PyErr_Format (PyExc_RuntimeError,
60 "object at %p of type %s is not initialized", py_arg,
61 Py_TYPE (py_arg)->tp_name);
62 return FALSE;
63 }
64
65 if (transfer == GI_TRANSFER_EVERYTHING) {
66 /* For transfer everything, add a new ref that the callee will take ownership of.
67 * Pythons existing ref to the GObject will be managed with the PyGObject wrapper.
68 */
69 g_object_ref (gobj);
70 }
71
72 arg->v_pointer = gobj;
73 return TRUE;
74 }
75
76 /* pygi_arg_gobject_out_arg_from_py:
77 * py_arg: (in):
78 * arg: (out):
79 *
80 * A specialization for marshaling Python GObjects used for out/return values
81 * from a Python implemented vfuncs, signals, or an assignment to a GObject property.
82 */
83 static gboolean
84 pygi_arg_gobject_out_arg_from_py (PyObject *py_arg, /* in */
85 GIArgument *arg, /* out */
86 GITransfer transfer)
87 {
88 GObject *gobj;
89 if (!pygi_marshal_from_py_object (py_arg, arg, transfer)) {
90 return FALSE;
91 }
92
93 /* HACK: At this point the basic marshaling of the GObject was successful
94 * but we add some special case hacks for vfunc returns due to buggy APIs:
95 * https://bugzilla.gnome.org/show_bug.cgi?id=693393
96 */
97 gobj = arg->v_pointer;
98 // In Python 3.13 the py_arg refcount is 3, in 3.14 only 1
99 if (Py_REFCNT (py_arg) == 1 && gobj->ref_count == 1) {
100 /* If both object ref counts are only 1 at this point (the reference held
101 * in a return tuple), we assume the GObject will be free'd before reaching
102 * its target and become invalid. So instead of getting invalid object errors
103 * we add a new GObject ref.
104 *
105 * Note that this check does not hold up for Python 3.14
106 */
107 PyObject *repr = PyObject_Repr (py_arg);
108 gchar *msg = g_strdup_printf (
109 "Adding extra reference for %s, "
110 "for nothing is holding a reference to this object. "
111 "This could potentially lead to a memory leak. "
112 "See: https://bugzilla.gnome.org/show_bug.cgi?id=687522",
113 PyUnicode_AsUTF8 (repr));
114 Py_DECREF (repr);
115 if (PyErr_WarnEx (PyExc_RuntimeWarning, msg, 2)) {
116 g_free (msg);
117 return FALSE;
118 }
119 g_free (msg);
120
121 g_object_ref (gobj);
122 }
123
124 return TRUE;
125 }
126
127 /*
128 * GObject from Python
129 */
130
131 typedef gboolean (*PyGIObjectMarshalFromPyFunc) (PyObject *py_arg,
132 GIArgument *arg,
133 GITransfer transfer);
134
135 static gboolean
136 _pygi_marshal_from_py_interface_object (PyGIInvokeState *state,
137 PyGICallableCache *callable_cache,
138 PyGIArgCache *arg_cache,
139 PyObject *py_arg, GIArgument *arg,
140 gpointer *cleanup_data,
141 PyGIObjectMarshalFromPyFunc func)
142 {
143 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
144
145 if (Py_IsNone (py_arg)) {
146 arg->v_pointer = NULL;
147 return TRUE;
148 }
149
150 if (PyObject_IsInstance (py_arg, iface_cache->py_type)
151 || (pygobject_check (py_arg, &PyGObject_Type)
152 && g_type_is_a (G_OBJECT_TYPE (pygobject_get (py_arg)),
153 iface_cache->g_type))) {
154 gboolean res;
155 res = func (py_arg, arg, arg_cache->transfer);
156 *cleanup_data = arg->v_pointer;
157 return res;
158
159 } else {
160 PyObject *module = PyObject_GetAttrString (py_arg, "__module__");
161 const char *arg_name = pygi_arg_cache_get_name (arg_cache);
162 PyErr_Format (PyExc_TypeError,
163 "argument %s: Expected %s, but got %s%s%s",
164 arg_name ? arg_name : "self",
165 ((PyGIInterfaceCache *)arg_cache)->type_name,
166 module ? PyUnicode_AsUTF8 (module) : "",
167 module ? "." : "", Py_TYPE (py_arg)->tp_name);
168 if (module) Py_DECREF (module);
169 return FALSE;
170 }
171 }
172
173 static gboolean
174 _pygi_marshal_from_py_called_from_c_interface_object (
175 PyGIInvokeState *state, PyGICallableCache *callable_cache,
176 PyGIArgCache *arg_cache, PyObject *py_arg, GIArgument *arg,
177 gpointer *cleanup_data)
178 {
179 return _pygi_marshal_from_py_interface_object (
180 state, callable_cache, arg_cache, py_arg, arg, cleanup_data,
181 pygi_arg_gobject_out_arg_from_py);
182 }
183
184 static gboolean
185 _pygi_marshal_from_py_called_from_py_interface_object (
186 PyGIInvokeState *state, PyGICallableCache *callable_cache,
187 PyGIArgCache *arg_cache, PyObject *py_arg, GIArgument *arg,
188 gpointer *cleanup_data)
189 {
190 return _pygi_marshal_from_py_interface_object (
191 state, callable_cache, arg_cache, py_arg, arg, cleanup_data,
192 pygi_marshal_from_py_object);
193 }
194
195 /*
196 * GObject to Python
197 */
198
199 static PyObject *
200 pygi_arg_object_to_py (GIArgument *arg, GITransfer transfer)
201 {
202 PyObject *pyobj;
203
204 if (arg->v_pointer == NULL) {
205 pyobj = Py_None;
206 Py_INCREF (pyobj);
207 } else if (G_IS_OBJECT (arg->v_pointer)) {
208 pyobj =
209 pygobject_new_full (arg->v_pointer,
210 /*steal=*/transfer == GI_TRANSFER_EVERYTHING,
211 /*type=*/NULL);
212 } else {
213 pyobj = pygi_fundamental_new (arg->v_pointer);
214 if (pyobj && transfer == GI_TRANSFER_EVERYTHING)
215 pygi_fundamental_unref ((PyGIFundamental *)pyobj);
216 }
217
218 return pyobj;
219 }
220
221 static PyObject *
222 _pygi_marshal_to_py_called_from_c_interface_object_cache_adapter (
223 PyGIInvokeState *state, PyGICallableCache *callable_cache,
224 PyGIArgCache *arg_cache, GIArgument *arg, gpointer *cleanup_data)
225 {
226 /* HACK:
227 * The following hack is to work around GTK sending signals which
228 * contain floating widgets in them. This assumes control of how
229 * references are added by the PyGObject wrapper and avoids the sink
230 * behavior by explicitly passing GI_TRANSFER_EVERYTHING as the transfer
231 * mode and then re-forcing the object as floating afterwards.
232 *
233 * See: https://bugzilla.gnome.org/show_bug.cgi?id=693400
234 *
235 * (a GtkCellRendererText issue that has since been fixed)
236 * In modern bindings this should no longer be needed. We sink the object
237 * as soon as it's created and that's that.
238 */
239 if (arg->v_pointer != NULL && arg_cache->transfer == GI_TRANSFER_NOTHING
240 && G_IS_OBJECT (arg->v_pointer)
241 && g_object_is_floating (arg->v_pointer)) {
242 g_object_ref_sink (arg->v_pointer);
243 }
244
245 return pygi_arg_object_to_py (arg, arg_cache->transfer);
246 }
247
248 static PyObject *
249 _pygi_marshal_to_py_called_from_py_interface_object_cache_adapter (
250 PyGIInvokeState *state, PyGICallableCache *callable_cache,
251 PyGIArgCache *arg_cache, GIArgument *arg, gpointer *cleanup_data)
252 {
253 return pygi_arg_object_to_py (arg, arg_cache->transfer);
254 }
255
256 static void
257 _pygi_marshal_cleanup_to_py_interface_object (PyGIInvokeState *state,
258 PyGIArgCache *arg_cache,
259 gpointer cleanup_data,
260 gpointer data,
261 gboolean was_processed)
262 {
263 if (was_processed && state->failed && data != NULL
264 && arg_cache->transfer == GI_TRANSFER_EVERYTHING) {
265 if (G_IS_OBJECT (data)) {
266 g_object_unref (G_OBJECT (data));
267 } else {
268 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
269 GIObjectInfoUnrefFunction unref_func;
270
271 unref_func = gi_object_info_get_unref_function_pointer (
272 (GIObjectInfo *)iface_cache->interface_info);
273 if (unref_func) unref_func (data);
274 }
275 }
276 }
277
278 static void
279 _pygi_marshal_cleanup_from_py_interface_object (PyGIInvokeState *state,
280 PyGIArgCache *arg_cache,
281 PyObject *py_arg,
282 gpointer data,
283 gboolean was_processed)
284 {
285 /* If we processed the parameter but fail before invoking the method,
286 we need to remove the ref we added */
287 if (was_processed && state->failed && data != NULL
288 && arg_cache->transfer == GI_TRANSFER_EVERYTHING)
289 g_object_unref (G_OBJECT (data));
290 }
291
292 PyGIArgCache *
293 pygi_arg_gobject_new_from_info (GITypeInfo *type_info, GIArgInfo *arg_info,
294 GITransfer transfer, PyGIDirection direction,
295 GIRegisteredTypeInfo *iface_info,
296 PyGICallableCache *callable_cache)
297 {
298 PyGIArgCache *cache = pygi_arg_interface_new_from_info (
299 type_info, arg_info, transfer, direction, iface_info);
300
301 if (cache == NULL) return NULL;
302
303 if (direction & PYGI_DIRECTION_FROM_PYTHON) {
304 if (callable_cache != NULL
305 && callable_cache->calling_context
306 == PYGI_CALLING_CONTEXT_IS_FROM_C) {
307 cache->from_py_marshaller =
308 _pygi_marshal_from_py_called_from_c_interface_object;
309 } else {
310 cache->from_py_marshaller =
311 _pygi_marshal_from_py_called_from_py_interface_object;
312 }
313
314 cache->from_py_cleanup =
315 _pygi_marshal_cleanup_from_py_interface_object;
316 }
317
318 if (direction & PYGI_DIRECTION_TO_PYTHON) {
319 if (callable_cache != NULL
320 && callable_cache->calling_context
321 == PYGI_CALLING_CONTEXT_IS_FROM_C) {
322 cache->to_py_marshaller =
323 _pygi_marshal_to_py_called_from_c_interface_object_cache_adapter;
324 } else {
325 cache->to_py_marshaller =
326 _pygi_marshal_to_py_called_from_py_interface_object_cache_adapter;
327 }
328
329 cache->to_py_cleanup = _pygi_marshal_cleanup_to_py_interface_object;
330 }
331
332 return cache;
333 }