File: pygobject-props.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 (309 lines) | stat: -rw-r--r-- 8,888 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
/* -*- Mode: C; c-basic-offset: 4 -*-
 * pygtk- Python bindings for the GTK toolkit.
 * Copyright (C) 1998-2003  James Henstridge
 *
 *   pygobject.c: wrapper for the GObject type.
 *
 * 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 "pygi-property.h"
#include "pygi-type.h"
#include "pygi-value.h"
#include "pygi-util.h"
#include "pygobject-object.h"
#include "pygobject-props.h"

typedef struct {
    PyObject_HEAD
    GParamSpec **props;
    guint n_props;
    guint index;
} PyGPropsIter;

PYGI_DEFINE_TYPE ("gi._gi.GPropsIter", PyGPropsIter_Type, PyGPropsIter);

static void
pyg_props_iter_dealloc (PyGPropsIter *self)
{
    g_free (self->props);
    PyObject_Free ((PyObject *)self);
}

static PyObject *
pygobject_props_iter_next (PyGPropsIter *iter)
{
    if (iter->index < iter->n_props)
        return pygi_fundamental_new (iter->props[iter->index++]);
    else {
        PyErr_SetNone (PyExc_StopIteration);
        return NULL;
    }
}

typedef struct {
    PyObject_HEAD
    /* a reference to the object containing the properties */
    PyGObject *pygobject;
    GType gtype;
} PyGProps;

PYGI_DEFINE_TYPE ("gi._gi.GProps", PyGProps_Type, PyGProps);

static void
PyGProps_dealloc (PyGProps *self)
{
    PyObject_GC_UnTrack ((PyObject *)self);

    Py_CLEAR (self->pygobject);

    PyObject_GC_Del ((PyObject *)self);
}

/* Copied from glib. gobject uses hyphens in property names, but in Python
 * we can only represent hyphens as underscores. Convert underscores to
 * hyphens for glib compatibility. */
static void
canonicalize_key (gchar *key)
{
    gchar *p;

    for (p = key; *p != 0; p++) {
        gchar c = *p;

        if (c != '-' && (c < '0' || c > '9') && (c < 'A' || c > 'Z')
            && (c < 'a' || c > 'z'))
            *p = '-';
    }
}

static PyObject *
PyGProps_getattro (PyGProps *self, PyObject *attr)
{
    char *attr_name, *property_name;
    GObjectClass *class;
    GParamSpec *pspec;

    attr_name = PyUnicode_AsUTF8 (attr);
    if (!attr_name) {
        PyErr_Clear ();
        return PyObject_GenericGetAttr ((PyObject *)self, attr);
    }

    class = g_type_class_ref (self->gtype);

    /* g_object_class_find_property recurses through the class hierarchy,
     * so the resulting pspec tells us the owner_type that owns the property
     * we're dealing with. */
    property_name = g_strdup (attr_name);
    canonicalize_key (property_name);
    pspec = g_object_class_find_property (class, property_name);
    g_free (property_name);
    g_type_class_unref (class);

    if (!pspec) {
        return PyObject_GenericGetAttr ((PyObject *)self, attr);
    }

    if (!self->pygobject) {
        /* If we're doing it without an instance, return a GParamSpec */
        return pygi_fundamental_new (pspec);
    }

    return pygi_get_property_value (self->pygobject, pspec);
}

static int
PyGProps_setattro (PyGProps *self, PyObject *attr, PyObject *pvalue)
{
    GParamSpec *pspec;
    char *attr_name, *property_name;
    GObject *obj;

    if (pvalue == NULL) {
        PyErr_SetString (PyExc_TypeError,
                         "properties cannot be "
                         "deleted");
        return -1;
    }

    attr_name = PyUnicode_AsUTF8 (attr);
    if (!attr_name) {
        PyErr_Clear ();
        return PyObject_GenericSetAttr ((PyObject *)self, attr, pvalue);
    }

    if (!self->pygobject) {
        PyErr_SetString (PyExc_TypeError,
                         "cannot set GOject properties without an instance");
        return -1;
    }

    obj = self->pygobject->obj;

    property_name = g_strdup (attr_name);
    canonicalize_key (property_name);

    /* g_object_class_find_property recurses through the class hierarchy,
     * so the resulting pspec tells us the owner_type that owns the property
     * we're dealing with. */
    pspec =
        g_object_class_find_property (G_OBJECT_GET_CLASS (obj), property_name);
    g_free (property_name);
    if (!pspec) {
        return PyObject_GenericSetAttr ((PyObject *)self, attr, pvalue);
    }

    pygi_set_property_value (self->pygobject, pspec, pvalue);
    if (PyErr_Occurred ()) return -1;

    return 0;
}

static int
pygobject_props_traverse (PyGProps *self, visitproc visit, void *arg)
{
    if (self->pygobject && visit ((PyObject *)self->pygobject, arg) < 0)
        return -1;
    return 0;
}

static PyObject *
pygobject_props_get_iter (PyGProps *self)
{
    PyGPropsIter *iter;
    GObjectClass *class;

    iter = PyObject_New (PyGPropsIter, &PyGPropsIter_Type);
    class = g_type_class_ref (self->gtype);
    iter->props = g_object_class_list_properties (class, &iter->n_props);
    iter->index = 0;
    g_type_class_unref (class);
    return (PyObject *)iter;
}

static PyObject *
pygobject_props_dir (PyGProps *self)
{
    GObjectClass *class;
    GParamSpec **props;
    guint n_props = 0, i;
    PyObject *prop_str;
    PyObject *props_list;

    class = g_type_class_ref (self->gtype);
    props = g_object_class_list_properties (class, &n_props);
    props_list = PyList_New (n_props);

    for (i = 0; i < n_props; i++) {
        char *name;
        name = g_strdup (g_param_spec_get_name (props[i]));
        /* hyphens cannot belong in identifiers */
        g_strdelimit (name, "-", '_');
        prop_str = PyUnicode_FromString (name);

        PyList_SetItem (props_list, i, prop_str);
        g_free (name);
    }

    g_type_class_unref (class);

    if (props) g_free (props);

    return props_list;
}

static PyMethodDef pygobject_props_methods[] = {
    { "__dir__", (PyCFunction)pygobject_props_dir, METH_NOARGS },
    { NULL, NULL, 0 },
};


static Py_ssize_t
PyGProps_length (PyGProps *self)
{
    GObjectClass *class;
    GParamSpec **props;
    guint n_props;

    class = g_type_class_ref (self->gtype);
    props = g_object_class_list_properties (class, &n_props);
    g_type_class_unref (class);
    g_free (props);

    return (Py_ssize_t)n_props;
}

static PySequenceMethods _PyGProps_as_sequence = {
    (lenfunc)PyGProps_length, 0, 0, 0, 0, 0, 0,
};

PYGI_DEFINE_TYPE ("gi._gi.GPropsDescr", PyGPropsDescr_Type, PyObject);

static PyObject *
pyg_props_descr_descr_get (PyObject *self, PyObject *obj, PyObject *type)
{
    PyGProps *gprops;

    gprops = PyObject_GC_New (PyGProps, &PyGProps_Type);
    if (obj == NULL || Py_IsNone (obj)) {
        gprops->pygobject = NULL;
        gprops->gtype = pyg_type_from_object (type);
    } else {
        if (!PyObject_IsInstance (obj, (PyObject *)&PyGObject_Type)) {
            PyErr_SetString (PyExc_TypeError,
                             "cannot use GObject property"
                             " descriptor on non-GObject instances");
            return NULL;
        }
        gprops->pygobject = (PyGObject *)Py_NewRef (obj);
        gprops->gtype = pyg_type_from_object (obj);
    }
    return (PyObject *)gprops;
}

int
pyg_object_props_register_types (PyObject *d)
{
    /* GProps */
    PyGProps_Type.tp_dealloc = (destructor)PyGProps_dealloc;
    PyGProps_Type.tp_as_sequence = (PySequenceMethods *)&_PyGProps_as_sequence;
    PyGProps_Type.tp_getattro = (getattrofunc)PyGProps_getattro;
    PyGProps_Type.tp_setattro = (setattrofunc)PyGProps_setattro;
    PyGProps_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC;
    PyGProps_Type.tp_doc =
        "The properties of the GObject accessible as "
        "Python attributes.";
    PyGProps_Type.tp_traverse = (traverseproc)pygobject_props_traverse;
    PyGProps_Type.tp_iter = (getiterfunc)pygobject_props_get_iter;
    PyGProps_Type.tp_methods = pygobject_props_methods;
    if (PyType_Ready (&PyGProps_Type) < 0) return -1;

    /* GPropsDescr */
    PyGPropsDescr_Type.tp_flags = Py_TPFLAGS_DEFAULT;
    PyGPropsDescr_Type.tp_descr_get = pyg_props_descr_descr_get;
    if (PyType_Ready (&PyGPropsDescr_Type) < 0) return -1;


    /* GPropsIter */
    PyGPropsIter_Type.tp_dealloc = (destructor)pyg_props_iter_dealloc;
    PyGPropsIter_Type.tp_flags = Py_TPFLAGS_DEFAULT;
    PyGPropsIter_Type.tp_doc = "GObject properties iterator";
    PyGPropsIter_Type.tp_iter = PyObject_SelfIter;
    PyGPropsIter_Type.tp_iternext = (iternextfunc)pygobject_props_iter_next;
    if (PyType_Ready (&PyGPropsIter_Type) < 0) return -1;

    return 0;
}