File: pygi-fundamental.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 (304 lines) | stat: -rw-r--r-- 8,826 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
/* -*- Mode: C; c-basic-offset: 4 -*-
 * vim: tabstop=4 shiftwidth=4 expandtab
 *
 * Copyright (C) 2009 Simon van der Linden <svdlinden@src.gnome.org>
 * Copyright (C) 2010 Tomeu Vizoso <tomeu.vizoso@collabora.co.uk>
 * Copyright (C) 2012 Bastian Winkler <buz@netbuz.org>
 *
 *   pygi-fundamental.c: wrapper to handle instances of fundamental types.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
 * USA
 */


#include "pygobject-types.h"
#include "pygi-info.h"
#include "pygi-util.h"
#include "pygi-fundamental.h"
#include "pygi-repository.h"
#include "pygobject-object.h"  // for pygobject_lookup_class


static PyGIFundamental *_pygi_fundamental_new_internal (PyTypeObject *type,
                                                        gpointer pointer);


static void
fundamental_dealloc (PyGIFundamental *self)
{
    pygi_fundamental_unref (self);
    self->instance = NULL;

    PyObject_GC_UnTrack ((PyObject *)self);
    if (self->weaklist) PyObject_ClearWeakRefs ((PyObject *)self);

    Py_TYPE (self)->tp_free ((PyObject *)self);
}

static PyObject *
fundamental_new (PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
    GIBaseInfo *info;
    gpointer pointer;
    PyGIFundamental *self = NULL;
    GType g_type;

    info = _pygi_object_get_gi_info ((PyObject *)type, &PyGIObjectInfo_Type);
    if (info == NULL) {
        if (PyErr_ExceptionMatches (PyExc_AttributeError)) {
            PyErr_Format (PyExc_TypeError,
                          "missing introspection information");
        }
        return NULL;
    }

    g_type = pyg_type_from_object ((PyObject *)type);
    if (G_TYPE_IS_ABSTRACT (g_type)) {
        PyErr_Format (PyExc_TypeError, "cannot instantiate abstract type %s",
                      g_type_name (g_type));
        return NULL;
    }

    pointer = g_type_create_instance (g_type);
    if (pointer == NULL) {
        PyErr_NoMemory ();
        goto out;
    }

    self = _pygi_fundamental_new_internal (type, pointer);
    if (self == NULL) {
        g_free (pointer);
        PyErr_Format (PyExc_TypeError,
                      "cannot instantiate Fundamental Python wrapper type %s",
                      g_type_name (g_type));
        goto out;
    }

out:
    gi_base_info_unref (info);

    return (PyObject *)self;
}

static int
fundamental_init (PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { NULL };

    if (!PyArg_ParseTupleAndKeywords (args, kwargs, ":Fundamental.__init__",
                                      kwlist)) {
        return -1;
    }
    return 0;
}

static PyObject *
fundamental_richcompare (PyObject *self, PyObject *other, int op)
{
    if (Py_TYPE (self) == Py_TYPE (other)) {
        return pyg_ptr_richcompare (((PyGIFundamental *)self)->instance,
                                    ((PyGIFundamental *)other)->instance, op);
    } else {
        Py_INCREF (Py_NotImplemented);
        return Py_NotImplemented;
    }
}

static Py_hash_t
fundamental_hash (PyGIFundamental *self)
{
    return (Py_hash_t)(gintptr)(self->instance);
}

static PyObject *
fundamental_repr (PyGIFundamental *self)
{
    gchar buf[128];

    g_snprintf (buf, sizeof (buf), "<%s at 0x%" G_GUINTPTR_FORMAT ">",
                g_type_name (self->gtype), (guintptr)self->instance);
    return PyUnicode_FromString (buf);
}

PYGI_DEFINE_TYPE ("gi.Fundamental", PyGIFundamental_Type, PyGIFundamental);


PyObject *
pygi_fundamental_new (gpointer instance)
{
    GType gtype;
    PyTypeObject *type;
    PyGIFundamental *self;

    if (!instance) {
        Py_RETURN_NONE;
    }

    gtype = G_TYPE_FROM_INSTANCE (instance);
    type = pygobject_lookup_class (gtype);

    self = _pygi_fundamental_new_internal (type, instance);
    pygi_fundamental_ref (self);
    return (PyObject *)self;
}

static PyGIFundamental *
_pygi_fundamental_new_internal (PyTypeObject *type, gpointer instance)
{
    PyGIFundamental *self;
    GIObjectInfo *info;

    if (!PyType_IsSubtype (type, &PyGIFundamental_Type)) {
        PyErr_SetString (PyExc_TypeError,
                         "must be a subtype of gi.Fundamental");
        return NULL;
    }

    info = GI_OBJECT_INFO (
        _pygi_object_get_gi_info ((PyObject *)type, &PyGIObjectInfo_Type));
    if (info == NULL) {
        if (PyErr_ExceptionMatches (PyExc_AttributeError)) {
            PyErr_Format (PyExc_TypeError,
                          "missing introspection information");
        }
        return NULL;
    }

    self = (PyGIFundamental *)type->tp_alloc (type, 0);
    if (self == NULL) {
        return NULL;
    }

    self->gtype = pyg_type_from_object ((PyObject *)type);
    self->instance = instance;

    self->ref_func = gi_object_info_get_ref_function_pointer (info);
    self->unref_func = gi_object_info_get_unref_function_pointer (info);

    /* A special case for GParamSpec. It has a floating reference when created.
     * We make sure we have a proper reference, so we can ref/unref normally.
     */
    if (G_TYPE_IS_PARAM (self->gtype)) {
        g_param_spec_ref_sink (self->instance);
    }

    gi_base_info_unref (info);

    return self;
}

void
pygi_fundamental_ref (PyGIFundamental *self)
{
    if (self->ref_func && self->instance) self->ref_func (self->instance);
}

void
pygi_fundamental_unref (PyGIFundamental *self)
{
    if (self->unref_func && self->instance) self->unref_func (self->instance);
}

GTypeInstance *
pygi_fundamental_get (PyObject *self)
{
    if (PyObject_TypeCheck (self, &PyGIFundamental_Type)) {
        return ((PyGIFundamental *)self)->instance;
    } else {
        PyErr_SetString (PyExc_TypeError, "Expected GObject Fundamental type");
        return NULL;
    }
}

int
pygi_fundamental_register_types (PyObject *m)
{
    Py_SET_TYPE (&PyGIFundamental_Type, &PyType_Type);
    g_assert (Py_TYPE (&PyGIFundamental_Type) != NULL);

    PyGIFundamental_Type.tp_alloc = PyType_GenericAlloc;
    PyGIFundamental_Type.tp_new = (newfunc)fundamental_new;
    PyGIFundamental_Type.tp_init = (initproc)fundamental_init;
    PyGIFundamental_Type.tp_dealloc = (destructor)fundamental_dealloc;
    PyGIFundamental_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
    PyGIFundamental_Type.tp_richcompare = fundamental_richcompare;
    PyGIFundamental_Type.tp_repr = (reprfunc)fundamental_repr;
    PyGIFundamental_Type.tp_hash = (hashfunc)fundamental_hash;
    PyGIFundamental_Type.tp_weaklistoffset =
        offsetof (PyGIFundamental, weaklist);

    if (PyType_Ready (&PyGIFundamental_Type)) return -1;
    if (PyModule_AddObject (m, "Fundamental",
                            (PyObject *)&PyGIFundamental_Type))
        return -1;

    return 0;
}


GTypeInstance *
pygi_fundamental_from_value (const GValue *value)
{
    GIRepository *repository =
        pygi_repository_get_default ();  // Access to system wide (default) repo instead
    GIBaseInfo *info =
        gi_repository_find_by_gtype (repository, G_VALUE_TYPE (value));
    GTypeInstance *instance = NULL;

    if (info == NULL) return NULL;

    if (GI_IS_OBJECT_INFO (info)) {
        GIObjectInfoGetValueFunction get_value_func =
            gi_object_info_get_get_value_function_pointer (
                (GIObjectInfo *)info);
        if (get_value_func) {
            instance = get_value_func (value);
        }
    }

    gi_base_info_unref (info);

    return instance;
}

gboolean
pygi_fundamental_set_value (GValue *value, GTypeInstance *instance)
{
    GIRepository *repository;
    GIBaseInfo *info;
    gboolean result = FALSE;

    if (instance == NULL) return result;

    repository = pygi_repository_get_default ();
    info = gi_repository_find_by_gtype (repository,
                                        G_TYPE_FROM_INSTANCE (instance));

    if (info == NULL) return result;

    if (GI_IS_OBJECT_INFO (info)) {
        GIObjectInfoSetValueFunction set_value_func =
            gi_object_info_get_set_value_function_pointer (
                (GIObjectInfo *)info);
        if (set_value_func) {
            set_value_func (value, instance);
            result = TRUE;
        }
    }

    gi_base_info_unref (info);
    return result;
}