| 1 |
/* -*- Mode: C; c-basic-offset: 4 -*- |
| 2 |
* pygtk- Python bindings for the GTK toolkit. |
| 3 |
* Copyright (C) 1998-2003 James Henstridge |
| 4 |
* |
| 5 |
* pygobject.c: wrapper for the GObject type. |
| 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 "pygi-property.h" |
| 23 |
#include "pygi-type.h" |
| 24 |
#include "pygi-value.h" |
| 25 |
#include "pygi-util.h" |
| 26 |
#include "pygobject-object.h" |
| 27 |
#include "pygobject-props.h" |
| 28 |
|
| 29 |
typedef struct { |
| 30 |
PyObject_HEAD |
| 31 |
GParamSpec **props; |
| 32 |
guint n_props; |
| 33 |
guint index; |
| 34 |
} PyGPropsIter; |
| 35 |
|
| 36 |
PYGI_DEFINE_TYPE ("gi._gi.GPropsIter", PyGPropsIter_Type, PyGPropsIter); |
| 37 |
|
| 38 |
static void |
| 39 |
pyg_props_iter_dealloc (PyGPropsIter *self) |
| 40 |
{ |
| 41 |
g_free (self->props); |
| 42 |
PyObject_Free ((PyObject *)self); |
| 43 |
} |
| 44 |
|
| 45 |
static PyObject * |
| 46 |
pygobject_props_iter_next (PyGPropsIter *iter) |
| 47 |
{ |
| 48 |
if (iter->index < iter->n_props) |
| 49 |
return pygi_fundamental_new (iter->props[iter->index++]); |
| 50 |
else { |
| 51 |
PyErr_SetNone (PyExc_StopIteration); |
| 52 |
return NULL; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
typedef struct { |
| 57 |
PyObject_HEAD |
| 58 |
/* a reference to the object containing the properties */ |
| 59 |
PyGObject *pygobject; |
| 60 |
GType gtype; |
| 61 |
} PyGProps; |
| 62 |
|
| 63 |
PYGI_DEFINE_TYPE ("gi._gi.GProps", PyGProps_Type, PyGProps); |
| 64 |
|
| 65 |
static void |
| 66 |
PyGProps_dealloc (PyGProps *self) |
| 67 |
{ |
| 68 |
PyObject_GC_UnTrack ((PyObject *)self); |
| 69 |
|
| 70 |
Py_CLEAR (self->pygobject); |
| 71 |
|
| 72 |
PyObject_GC_Del ((PyObject *)self); |
| 73 |
} |
| 74 |
|
| 75 |
/* Copied from glib. gobject uses hyphens in property names, but in Python |
| 76 |
* we can only represent hyphens as underscores. Convert underscores to |
| 77 |
* hyphens for glib compatibility. */ |
| 78 |
static void |
| 79 |
canonicalize_key (gchar *key) |
| 80 |
{ |
| 81 |
gchar *p; |
| 82 |
|
| 83 |
for (p = key; *p != 0; p++) { |
| 84 |
gchar c = *p; |
| 85 |
|
| 86 |
if (c != '-' && (c < '0' || c > '9') && (c < 'A' || c > 'Z') |
| 87 |
&& (c < 'a' || c > 'z')) |
| 88 |
*p = '-'; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
static PyObject * |
| 93 |
PyGProps_getattro (PyGProps *self, PyObject *attr) |
| 94 |
{ |
| 95 |
char *attr_name, *property_name; |
| 96 |
GObjectClass *class; |
| 97 |
GParamSpec *pspec; |
| 98 |
|
| 99 |
attr_name = PyUnicode_AsUTF8 (attr); |
| 100 |
if (!attr_name) { |
| 101 |
PyErr_Clear (); |
| 102 |
return PyObject_GenericGetAttr ((PyObject *)self, attr); |
| 103 |
} |
| 104 |
|
| 105 |
class = g_type_class_ref (self->gtype); |
| 106 |
|
| 107 |
/* g_object_class_find_property recurses through the class hierarchy, |
| 108 |
* so the resulting pspec tells us the owner_type that owns the property |
| 109 |
* we're dealing with. */ |
| 110 |
property_name = g_strdup (attr_name); |
| 111 |
canonicalize_key (property_name); |
| 112 |
pspec = g_object_class_find_property (class, property_name); |
| 113 |
g_free (property_name); |
| 114 |
g_type_class_unref (class); |
| 115 |
|
| 116 |
if (!pspec) { |
| 117 |
return PyObject_GenericGetAttr ((PyObject *)self, attr); |
| 118 |
} |
| 119 |
|
| 120 |
if (!self->pygobject) { |
| 121 |
/* If we're doing it without an instance, return a GParamSpec */ |
| 122 |
return pygi_fundamental_new (pspec); |
| 123 |
} |
| 124 |
|
| 125 |
return pygi_get_property_value (self->pygobject, pspec); |
| 126 |
} |
| 127 |
|
| 128 |
static int |
| 129 |
PyGProps_setattro (PyGProps *self, PyObject *attr, PyObject *pvalue) |
| 130 |
{ |
| 131 |
GParamSpec *pspec; |
| 132 |
char *attr_name, *property_name; |
| 133 |
GObject *obj; |
| 134 |
|
| 135 |
if (pvalue == NULL) { |
| 136 |
PyErr_SetString (PyExc_TypeError, |
| 137 |
"properties cannot be " |
| 138 |
"deleted"); |
| 139 |
return -1; |
| 140 |
} |
| 141 |
|
| 142 |
attr_name = PyUnicode_AsUTF8 (attr); |
| 143 |
if (!attr_name) { |
| 144 |
PyErr_Clear (); |
| 145 |
return PyObject_GenericSetAttr ((PyObject *)self, attr, pvalue); |
| 146 |
} |
| 147 |
|
| 148 |
if (!self->pygobject) { |
| 149 |
PyErr_SetString (PyExc_TypeError, |
| 150 |
"cannot set GOject properties without an instance"); |
| 151 |
return -1; |
| 152 |
} |
| 153 |
|
| 154 |
obj = self->pygobject->obj; |
| 155 |
|
| 156 |
property_name = g_strdup (attr_name); |
| 157 |
canonicalize_key (property_name); |
| 158 |
|
| 159 |
/* g_object_class_find_property recurses through the class hierarchy, |
| 160 |
* so the resulting pspec tells us the owner_type that owns the property |
| 161 |
* we're dealing with. */ |
| 162 |
pspec = |
| 163 |
g_object_class_find_property (G_OBJECT_GET_CLASS (obj), property_name); |
| 164 |
g_free (property_name); |
| 165 |
if (!pspec) { |
| 166 |
return PyObject_GenericSetAttr ((PyObject *)self, attr, pvalue); |
| 167 |
} |
| 168 |
|
| 169 |
pygi_set_property_value (self->pygobject, pspec, pvalue); |
| 170 |
if (PyErr_Occurred ()) return -1; |
| 171 |
|
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
static int |
| 176 |
pygobject_props_traverse (PyGProps *self, visitproc visit, void *arg) |
| 177 |
{ |
| 178 |
if (self->pygobject && visit ((PyObject *)self->pygobject, arg) < 0) |
| 179 |
return -1; |
| 180 |
return 0; |
| 181 |
} |
| 182 |
|
| 183 |
static PyObject * |
| 184 |
pygobject_props_get_iter (PyGProps *self) |
| 185 |
{ |
| 186 |
PyGPropsIter *iter; |
| 187 |
GObjectClass *class; |
| 188 |
|
| 189 |
iter = PyObject_New (PyGPropsIter, &PyGPropsIter_Type); |
| 190 |
class = g_type_class_ref (self->gtype); |
| 191 |
iter->props = g_object_class_list_properties (class, &iter->n_props); |
| 192 |
iter->index = 0; |
| 193 |
g_type_class_unref (class); |
| 194 |
return (PyObject *)iter; |
| 195 |
} |
| 196 |
|
| 197 |
static PyObject * |
| 198 |
pygobject_props_dir (PyGProps *self) |
| 199 |
{ |
| 200 |
GObjectClass *class; |
| 201 |
GParamSpec **props; |
| 202 |
guint n_props = 0, i; |
| 203 |
PyObject *prop_str; |
| 204 |
PyObject *props_list; |
| 205 |
|
| 206 |
class = g_type_class_ref (self->gtype); |
| 207 |
props = g_object_class_list_properties (class, &n_props); |
| 208 |
props_list = PyList_New (n_props); |
| 209 |
|
| 210 |
for (i = 0; i < n_props; i++) { |
| 211 |
char *name; |
| 212 |
name = g_strdup (g_param_spec_get_name (props[i])); |
| 213 |
/* hyphens cannot belong in identifiers */ |
| 214 |
g_strdelimit (name, "-", '_'); |
| 215 |
prop_str = PyUnicode_FromString (name); |
| 216 |
|
| 217 |
PyList_SetItem (props_list, i, prop_str); |
| 218 |
g_free (name); |
| 219 |
} |
| 220 |
|
| 221 |
g_type_class_unref (class); |
| 222 |
|
| 223 |
if (props) g_free (props); |
| 224 |
|
| 225 |
return props_list; |
| 226 |
} |
| 227 |
|
| 228 |
static PyMethodDef pygobject_props_methods[] = { |
| 229 |
{ "__dir__", (PyCFunction)pygobject_props_dir, METH_NOARGS }, |
| 230 |
{ NULL, NULL, 0 }, |
| 231 |
}; |
| 232 |
|
| 233 |
|
| 234 |
static Py_ssize_t |
| 235 |
PyGProps_length (PyGProps *self) |
| 236 |
{ |
| 237 |
GObjectClass *class; |
| 238 |
GParamSpec **props; |
| 239 |
guint n_props; |
| 240 |
|
| 241 |
class = g_type_class_ref (self->gtype); |
| 242 |
props = g_object_class_list_properties (class, &n_props); |
| 243 |
g_type_class_unref (class); |
| 244 |
g_free (props); |
| 245 |
|
| 246 |
return (Py_ssize_t)n_props; |
| 247 |
} |
| 248 |
|
| 249 |
static PySequenceMethods _PyGProps_as_sequence = { |
| 250 |
(lenfunc)PyGProps_length, 0, 0, 0, 0, 0, 0, |
| 251 |
}; |
| 252 |
|
| 253 |
PYGI_DEFINE_TYPE ("gi._gi.GPropsDescr", PyGPropsDescr_Type, PyObject); |
| 254 |
|
| 255 |
static PyObject * |
| 256 |
pyg_props_descr_descr_get (PyObject *self, PyObject *obj, PyObject *type) |
| 257 |
{ |
| 258 |
PyGProps *gprops; |
| 259 |
|
| 260 |
gprops = PyObject_GC_New (PyGProps, &PyGProps_Type); |
| 261 |
if (obj == NULL || Py_IsNone (obj)) { |
| 262 |
gprops->pygobject = NULL; |
| 263 |
gprops->gtype = pyg_type_from_object (type); |
| 264 |
} else { |
| 265 |
if (!PyObject_IsInstance (obj, (PyObject *)&PyGObject_Type)) { |
| 266 |
PyErr_SetString (PyExc_TypeError, |
| 267 |
"cannot use GObject property" |
| 268 |
" descriptor on non-GObject instances"); |
| 269 |
return NULL; |
| 270 |
} |
| 271 |
gprops->pygobject = (PyGObject *)Py_NewRef (obj); |
| 272 |
gprops->gtype = pyg_type_from_object (obj); |
| 273 |
} |
| 274 |
return (PyObject *)gprops; |
| 275 |
} |
| 276 |
|
| 277 |
int |
| 278 |
pyg_object_props_register_types (PyObject *d) |
| 279 |
{ |
| 280 |
/* GProps */ |
| 281 |
PyGProps_Type.tp_dealloc = (destructor)PyGProps_dealloc; |
| 282 |
PyGProps_Type.tp_as_sequence = (PySequenceMethods *)&_PyGProps_as_sequence; |
| 283 |
PyGProps_Type.tp_getattro = (getattrofunc)PyGProps_getattro; |
| 284 |
PyGProps_Type.tp_setattro = (setattrofunc)PyGProps_setattro; |
| 285 |
PyGProps_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC; |
| 286 |
PyGProps_Type.tp_doc = |
| 287 |
"The properties of the GObject accessible as " |
| 288 |
"Python attributes."; |
| 289 |
PyGProps_Type.tp_traverse = (traverseproc)pygobject_props_traverse; |
| 290 |
PyGProps_Type.tp_iter = (getiterfunc)pygobject_props_get_iter; |
| 291 |
PyGProps_Type.tp_methods = pygobject_props_methods; |
| 292 |
if (PyType_Ready (&PyGProps_Type) < 0) return -1; |
| 293 |
|
| 294 |
/* GPropsDescr */ |
| 295 |
PyGPropsDescr_Type.tp_flags = Py_TPFLAGS_DEFAULT; |
| 296 |
PyGPropsDescr_Type.tp_descr_get = pyg_props_descr_descr_get; |
| 297 |
if (PyType_Ready (&PyGPropsDescr_Type) < 0) return -1; |
| 298 |
|
| 299 |
|
| 300 |
/* GPropsIter */ |
| 301 |
PyGPropsIter_Type.tp_dealloc = (destructor)pyg_props_iter_dealloc; |
| 302 |
PyGPropsIter_Type.tp_flags = Py_TPFLAGS_DEFAULT; |
| 303 |
PyGPropsIter_Type.tp_doc = "GObject properties iterator"; |
| 304 |
PyGPropsIter_Type.tp_iter = PyObject_SelfIter; |
| 305 |
PyGPropsIter_Type.tp_iternext = (iternextfunc)pygobject_props_iter_next; |
| 306 |
if (PyType_Ready (&PyGPropsIter_Type) < 0) return -1; |
| 307 |
|
| 308 |
return 0; |
| 309 |
} |