File: pygi-cache-object.c

package info (click to toggle)
pygobject 3.55.3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,728 kB
  • sloc: ansic: 39,419; python: 26,856; sh: 114; makefile: 81; xml: 35; cpp: 1
file content (333 lines) | stat: -rw-r--r-- 12,103 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* -*- Mode: C; c-basic-offset: 4 -*-
 * vim: tabstop=4 shiftwidth=4 expandtab
 *
 * Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>
 * Copyright (C) 2014 Simon Feltman <sfeltman@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include "pygi-fundamental.h"
#include "pygobject-object.h"
#include "pygi-cache-private.h"

/* pygi_marshal_from_py_object:
 * py_arg: (in):
 * arg: (out):
 */
static gboolean
pygi_marshal_from_py_object (PyObject *py_arg, /*in*/
                             GIArgument *arg,  /*out*/
                             GITransfer transfer)
{
    GObject *gobj;

    if (Py_IsNone (py_arg)) {
        arg->v_pointer = NULL;
        return TRUE;
    }

    if (PyObject_TypeCheck (py_arg, &PyGIFundamental_Type)) {
        arg->v_pointer = pygi_fundamental_get (py_arg);
        if (transfer == GI_TRANSFER_EVERYTHING) {
            pygi_fundamental_ref ((PyGIFundamental *)py_arg);
        }
        return TRUE;
    }

    if (!pygobject_check (py_arg, &PyGObject_Type)) {
        PyObject *repr = PyObject_Repr (py_arg);
        PyErr_Format (PyExc_TypeError, "expected GObject but got %s",
                      PyUnicode_AsUTF8 (repr));
        Py_DECREF (repr);
        return FALSE;
    }

    gobj = pygobject_get (py_arg);
    if (gobj == NULL) {
        PyErr_Format (PyExc_RuntimeError,
                      "object at %p of type %s is not initialized", py_arg,
                      Py_TYPE (py_arg)->tp_name);
        return FALSE;
    }

    if (transfer == GI_TRANSFER_EVERYTHING) {
        /* For transfer everything, add a new ref that the callee will take ownership of.
         * Pythons existing ref to the GObject will be managed with the PyGObject wrapper.
         */
        g_object_ref (gobj);
    }

    arg->v_pointer = gobj;
    return TRUE;
}

/* pygi_arg_gobject_out_arg_from_py:
 * py_arg: (in):
 * arg: (out):
 *
 * A specialization for marshaling Python GObjects used for out/return values
 * from a Python implemented vfuncs, signals, or an assignment to a GObject property.
 */
static gboolean
pygi_arg_gobject_out_arg_from_py (PyObject *py_arg, /* in */
                                  GIArgument *arg,  /* out */
                                  GITransfer transfer)
{
    GObject *gobj;
    if (!pygi_marshal_from_py_object (py_arg, arg, transfer)) {
        return FALSE;
    }

    /* HACK: At this point the basic marshaling of the GObject was successful
     * but we add some special case hacks for vfunc returns due to buggy APIs:
     * https://bugzilla.gnome.org/show_bug.cgi?id=693393
     */
    gobj = arg->v_pointer;
    // In Python 3.13 the py_arg refcount is 3, in 3.14 only 1
    if (Py_REFCNT (py_arg) == 1 && gobj->ref_count == 1) {
        /* If both object ref counts are only 1 at this point (the reference held
         * in a return tuple), we assume the GObject will be free'd before reaching
         * its target and become invalid. So instead of getting invalid object errors
         * we add a new GObject ref.
         *
         * Note that this check does not hold up for Python 3.14
         */
        PyObject *repr = PyObject_Repr (py_arg);
        gchar *msg = g_strdup_printf (
            "Adding extra reference for %s, "
            "for nothing is holding a reference to this object. "
            "This could potentially lead to a memory leak. "
            "See: https://bugzilla.gnome.org/show_bug.cgi?id=687522",
            PyUnicode_AsUTF8 (repr));
        Py_DECREF (repr);
        if (PyErr_WarnEx (PyExc_RuntimeWarning, msg, 2)) {
            g_free (msg);
            return FALSE;
        }
        g_free (msg);

        g_object_ref (gobj);
    }

    return TRUE;
}

/*
 * GObject from Python
 */

typedef gboolean (*PyGIObjectMarshalFromPyFunc) (PyObject *py_arg,
                                                 GIArgument *arg,
                                                 GITransfer transfer);

static gboolean
_pygi_marshal_from_py_interface_object (PyGIInvokeState *state,
                                        PyGICallableCache *callable_cache,
                                        PyGIArgCache *arg_cache,
                                        PyObject *py_arg, GIArgument *arg,
                                        gpointer *cleanup_data,
                                        PyGIObjectMarshalFromPyFunc func)
{
    PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;

    if (Py_IsNone (py_arg)) {
        arg->v_pointer = NULL;
        return TRUE;
    }

    if (PyObject_IsInstance (py_arg, iface_cache->py_type)
        || (pygobject_check (py_arg, &PyGObject_Type)
            && g_type_is_a (G_OBJECT_TYPE (pygobject_get (py_arg)),
                            iface_cache->g_type))) {
        gboolean res;
        res = func (py_arg, arg, arg_cache->transfer);
        *cleanup_data = arg->v_pointer;
        return res;

    } else {
        PyObject *module = PyObject_GetAttrString (py_arg, "__module__");
        const char *arg_name = pygi_arg_cache_get_name (arg_cache);
        PyErr_Format (PyExc_TypeError,
                      "argument %s: Expected %s, but got %s%s%s",
                      arg_name ? arg_name : "self",
                      ((PyGIInterfaceCache *)arg_cache)->type_name,
                      module ? PyUnicode_AsUTF8 (module) : "",
                      module ? "." : "", Py_TYPE (py_arg)->tp_name);
        if (module) Py_DECREF (module);
        return FALSE;
    }
}

static gboolean
_pygi_marshal_from_py_called_from_c_interface_object (
    PyGIInvokeState *state, PyGICallableCache *callable_cache,
    PyGIArgCache *arg_cache, PyObject *py_arg, GIArgument *arg,
    gpointer *cleanup_data)
{
    return _pygi_marshal_from_py_interface_object (
        state, callable_cache, arg_cache, py_arg, arg, cleanup_data,
        pygi_arg_gobject_out_arg_from_py);
}

static gboolean
_pygi_marshal_from_py_called_from_py_interface_object (
    PyGIInvokeState *state, PyGICallableCache *callable_cache,
    PyGIArgCache *arg_cache, PyObject *py_arg, GIArgument *arg,
    gpointer *cleanup_data)
{
    return _pygi_marshal_from_py_interface_object (
        state, callable_cache, arg_cache, py_arg, arg, cleanup_data,
        pygi_marshal_from_py_object);
}

/*
 * GObject to Python
 */

static PyObject *
pygi_arg_object_to_py (GIArgument *arg, GITransfer transfer)
{
    PyObject *pyobj;

    if (arg->v_pointer == NULL) {
        pyobj = Py_None;
        Py_INCREF (pyobj);
    } else if (G_IS_OBJECT (arg->v_pointer)) {
        pyobj =
            pygobject_new_full (arg->v_pointer,
                                /*steal=*/transfer == GI_TRANSFER_EVERYTHING,
                                /*type=*/NULL);
    } else {
        pyobj = pygi_fundamental_new (arg->v_pointer);
        if (pyobj && transfer == GI_TRANSFER_EVERYTHING)
            pygi_fundamental_unref ((PyGIFundamental *)pyobj);
    }

    return pyobj;
}

static PyObject *
_pygi_marshal_to_py_called_from_c_interface_object_cache_adapter (
    PyGIInvokeState *state, PyGICallableCache *callable_cache,
    PyGIArgCache *arg_cache, GIArgument *arg, gpointer *cleanup_data)
{
    /* HACK:
     * The following hack is to work around GTK sending signals which
     * contain floating widgets in them. This assumes control of how
     * references are added by the PyGObject wrapper and avoids the sink
     * behavior by explicitly passing GI_TRANSFER_EVERYTHING as the transfer
     * mode and then re-forcing the object as floating afterwards.
     *
     * See: https://bugzilla.gnome.org/show_bug.cgi?id=693400
     *
     * (a GtkCellRendererText issue that has since been fixed)
     * In modern bindings this should no longer be needed. We sink the object
     * as soon as it's created and that's that.
     */
    if (arg->v_pointer != NULL && arg_cache->transfer == GI_TRANSFER_NOTHING
        && G_IS_OBJECT (arg->v_pointer)
        && g_object_is_floating (arg->v_pointer)) {
        g_object_ref_sink (arg->v_pointer);
    }

    return pygi_arg_object_to_py (arg, arg_cache->transfer);
}

static PyObject *
_pygi_marshal_to_py_called_from_py_interface_object_cache_adapter (
    PyGIInvokeState *state, PyGICallableCache *callable_cache,
    PyGIArgCache *arg_cache, GIArgument *arg, gpointer *cleanup_data)
{
    return pygi_arg_object_to_py (arg, arg_cache->transfer);
}

static void
_pygi_marshal_cleanup_to_py_interface_object (PyGIInvokeState *state,
                                              PyGIArgCache *arg_cache,
                                              gpointer cleanup_data,
                                              gpointer data,
                                              gboolean was_processed)
{
    if (was_processed && state->failed && data != NULL
        && arg_cache->transfer == GI_TRANSFER_EVERYTHING) {
        if (G_IS_OBJECT (data)) {
            g_object_unref (G_OBJECT (data));
        } else {
            PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
            GIObjectInfoUnrefFunction unref_func;

            unref_func = gi_object_info_get_unref_function_pointer (
                (GIObjectInfo *)iface_cache->interface_info);
            if (unref_func) unref_func (data);
        }
    }
}

static void
_pygi_marshal_cleanup_from_py_interface_object (PyGIInvokeState *state,
                                                PyGIArgCache *arg_cache,
                                                PyObject *py_arg,
                                                gpointer data,
                                                gboolean was_processed)
{
    /* If we processed the parameter but fail before invoking the method,
       we need to remove the ref we added */
    if (was_processed && state->failed && data != NULL
        && arg_cache->transfer == GI_TRANSFER_EVERYTHING)
        g_object_unref (G_OBJECT (data));
}

PyGIArgCache *
pygi_arg_gobject_new_from_info (GITypeInfo *type_info, GIArgInfo *arg_info,
                                GITransfer transfer, PyGIDirection direction,
                                GIRegisteredTypeInfo *iface_info,
                                PyGICallableCache *callable_cache)
{
    PyGIArgCache *cache = pygi_arg_interface_new_from_info (
        type_info, arg_info, transfer, direction, iface_info);

    if (cache == NULL) return NULL;

    if (direction & PYGI_DIRECTION_FROM_PYTHON) {
        if (callable_cache != NULL
            && callable_cache->calling_context
                   == PYGI_CALLING_CONTEXT_IS_FROM_C) {
            cache->from_py_marshaller =
                _pygi_marshal_from_py_called_from_c_interface_object;
        } else {
            cache->from_py_marshaller =
                _pygi_marshal_from_py_called_from_py_interface_object;
        }

        cache->from_py_cleanup =
            _pygi_marshal_cleanup_from_py_interface_object;
    }

    if (direction & PYGI_DIRECTION_TO_PYTHON) {
        if (callable_cache != NULL
            && callable_cache->calling_context
                   == PYGI_CALLING_CONTEXT_IS_FROM_C) {
            cache->to_py_marshaller =
                _pygi_marshal_to_py_called_from_c_interface_object_cache_adapter;
        } else {
            cache->to_py_marshaller =
                _pygi_marshal_to_py_called_from_py_interface_object_cache_adapter;
        }

        cache->to_py_cleanup = _pygi_marshal_cleanup_to_py_interface_object;
    }

    return cache;
}