1 /* -*- Mode: C; c-basic-offset: 4 -*-
2  * pygtk- Python bindings for the GTK toolkit.
3  * Copyright (C) 1998-2003  James Henstridge
4  * Copyright (C) 2004       Johan Dahlin
5  *
6  *   pygenum.c: GEnum and GFlag wrappers
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
21  * USA
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27 
28 #include <pyglib.h>
29 
30 #include "pygobject-private.h"
31 #include "pygparamspec.h"
32 
33 PYGLIB_DEFINE_TYPE("gobject.GParamSpec", PyGParamSpec_Type, PyGParamSpec);
34 
35 static PyObject*
pyg_param_spec_richcompare(PyObject * self,PyObject * other,int op)36 pyg_param_spec_richcompare(PyObject *self, PyObject *other, int op)
37 {
38     if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGParamSpec_Type)
39         return _pyglib_generic_ptr_richcompare(((PyGParamSpec*)self)->pspec,
40                                                ((PyGParamSpec*)other)->pspec,
41                                                op);
42     else {
43         Py_INCREF(Py_NotImplemented);
44         return Py_NotImplemented;
45     }
46 }
47 
48 static long
pyg_param_spec_hash(PyGParamSpec * self)49 pyg_param_spec_hash(PyGParamSpec *self)
50 {
51     return (long)self->pspec;
52 }
53 
54 static PyObject *
pyg_param_spec_repr(PyGParamSpec * self)55 pyg_param_spec_repr(PyGParamSpec *self)
56 {
57     char buf[80];
58 
59     g_snprintf(buf, sizeof(buf), "<%s '%s'>",
60 	       G_PARAM_SPEC_TYPE_NAME(self->pspec),
61 	       g_param_spec_get_name(self->pspec));
62     return PYGLIB_PyUnicode_FromString(buf);
63 }
64 
65 static void
pyg_param_spec_dealloc(PyGParamSpec * self)66 pyg_param_spec_dealloc(PyGParamSpec *self)
67 {
68     g_param_spec_unref(self->pspec);
69     PyObject_DEL(self);
70 }
71 
72 
73 static PyObject *
pygenum_from_pspec(GParamSpec * pspec)74 pygenum_from_pspec(GParamSpec *pspec)
75 {
76     PyObject *pyclass;
77     GParamSpecEnum *enum_pspec;
78     GType enum_type;
79 
80     enum_pspec = G_PARAM_SPEC_ENUM(pspec);
81     enum_type = G_ENUM_CLASS_TYPE(enum_pspec->enum_class);
82     pyclass = (PyObject*)g_type_get_qdata(enum_type, pygenum_class_key);
83     if (pyclass == NULL) {
84 	pyclass = pyg_enum_add(NULL, g_type_name(enum_type), NULL, enum_type);
85 	if (pyclass == NULL)
86 	    pyclass = Py_None;
87     }
88 
89     Py_INCREF(pyclass);
90     return pyclass;
91 }
92 
93 static PyObject *
pygflags_from_pspec(GParamSpec * pspec)94 pygflags_from_pspec(GParamSpec *pspec)
95 {
96     PyObject *pyclass;
97     GParamSpecFlags *flag_pspec;
98     GType flag_type;
99 
100     flag_pspec = G_PARAM_SPEC_FLAGS(pspec);
101     flag_type = G_FLAGS_CLASS_TYPE(flag_pspec->flags_class);
102     pyclass = (PyObject*)g_type_get_qdata(flag_type, pygflags_class_key);
103     if (pyclass == NULL) {
104 	pyclass = pyg_flags_add(NULL, g_type_name(flag_type), NULL, flag_type);
105 	if (pyclass == NULL)
106 	    pyclass = Py_None;
107     }
108     Py_INCREF(pyclass);
109     return pyclass;
110 }
111 
112 static PyObject *
pyg_param_spec_getattr(PyGParamSpec * self,const gchar * attr)113 pyg_param_spec_getattr(PyGParamSpec *self, const gchar *attr)
114 {
115     GParamSpec *pspec;
116 
117     pspec = self->pspec;
118 
119     /* common attributes */
120     if (!strcmp(attr, "__gtype__")) {
121 	return pyg_type_wrapper_new(G_PARAM_SPEC_TYPE(pspec));
122     } else if (!strcmp(attr, "name")) {
123 	return Py_BuildValue("s", g_param_spec_get_name(pspec));
124     } else if (!strcmp(attr, "nick")) {
125 	return Py_BuildValue("s", g_param_spec_get_nick(pspec));
126     } else if (!strcmp(attr, "blurb") || !strcmp(attr, "__doc__")) {
127 	return Py_BuildValue("s", g_param_spec_get_blurb(pspec));
128     } else if (!strcmp(attr, "flags")) {
129 	return PYGLIB_PyLong_FromLong(pspec->flags);
130     } else if (!strcmp(attr, "value_type")) {
131 	return pyg_type_wrapper_new(pspec->value_type);
132     } else if (!strcmp(attr, "owner_type")) {
133 	return pyg_type_wrapper_new(pspec->owner_type);
134     }
135 
136     if (G_IS_PARAM_SPEC_CHAR(pspec)) {
137 	if (!strcmp(attr, "__members__")) {
138 	    return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
139 				 "blurb", "default_value", "flags",
140 				 "maximum", "minimum", "name", "nick",
141 				 "owner_type", "value_type");
142 	} else if (!strcmp(attr, "default_value")) {
143 	    return PYGLIB_PyUnicode_FromFormat(
144 		"%c", G_PARAM_SPEC_CHAR(pspec)->default_value);
145 	} else if (!strcmp(attr, "minimum")) {
146 	    return PYGLIB_PyLong_FromLong(G_PARAM_SPEC_CHAR(pspec)->minimum);
147 	} else if (!strcmp(attr, "maximum")) {
148 	    return PYGLIB_PyLong_FromLong(G_PARAM_SPEC_CHAR(pspec)->maximum);
149 	}
150     } else if (G_IS_PARAM_SPEC_UCHAR(pspec)) {
151 	if (!strcmp(attr, "__members__")) {
152 	    return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
153 				 "blurb", "default_value",
154 				 "flags", "maximum", "minimum",
155 				 "name", "nick", "owner_type",
156 				 "value_type");
157 	} else if (!strcmp(attr, "default_value")) {
158 	    return PYGLIB_PyUnicode_FromFormat(
159 		"%c", G_PARAM_SPEC_UCHAR(pspec)->default_value);
160 	} else if (!strcmp(attr, "minimum")) {
161 	    return PYGLIB_PyLong_FromLong(G_PARAM_SPEC_UCHAR(pspec)->minimum);
162 	} else if (!strcmp(attr, "maximum")) {
163 	    return PYGLIB_PyLong_FromLong(G_PARAM_SPEC_UCHAR(pspec)->maximum);
164 	}
165     } else if (G_IS_PARAM_SPEC_BOOLEAN(pspec)) {
166 	if (!strcmp(attr, "__members__")) {
167 	    return Py_BuildValue("[sssssssss]", "__doc__", "__gtype__",
168 				 "blurb", "default_value",
169 				 "flags", "name", "nick", "owner_type",
170 				 "value_type");
171 	} else if (!strcmp(attr, "default_value")) {
172 	    return PyBool_FromLong(G_PARAM_SPEC_BOOLEAN(pspec)->default_value);
173 	}
174     } else if (G_IS_PARAM_SPEC_INT(pspec)) {
175 	if (!strcmp(attr, "__members__")) {
176 	    return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
177 				 "blurb", "default_value",
178 				 "flags", "maximum", "minimum", "name",
179 				 "nick", "owner_type", "value_type");
180 	} else if (!strcmp(attr, "default_value")) {
181 	    return PYGLIB_PyLong_FromLong(G_PARAM_SPEC_INT(pspec)->default_value);
182 	} else if (!strcmp(attr, "minimum")) {
183 	    return PYGLIB_PyLong_FromLong(G_PARAM_SPEC_INT(pspec)->minimum);
184 	} else if (!strcmp(attr, "maximum")) {
185 	    return PYGLIB_PyLong_FromLong(G_PARAM_SPEC_INT(pspec)->maximum);
186 	}
187     } else if (G_IS_PARAM_SPEC_UINT(pspec)) {
188 	if (!strcmp(attr, "__members__")) {
189 	    return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
190 				 "blurb", "default_value",
191 				 "flags", "maximum", "minimum",
192 				 "name", "nick", "owner_type",
193 				 "value_type");
194 	} else if (!strcmp(attr, "default_value")) {
195 	    return PyLong_FromUnsignedLong(G_PARAM_SPEC_UINT(pspec)->default_value);
196 	} else if (!strcmp(attr, "minimum")) {
197 	    return PyLong_FromUnsignedLong(G_PARAM_SPEC_UINT(pspec)->minimum);
198 	} else if (!strcmp(attr, "maximum")) {
199 	    return PyLong_FromUnsignedLong(G_PARAM_SPEC_UINT(pspec)->maximum);
200 	}
201     } else if (G_IS_PARAM_SPEC_LONG(pspec)) {
202 	if (!strcmp(attr, "__members__")) {
203 	    return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
204 				 "blurb", "default_value",
205 				 "flags", "maximum", "minimum", "name",
206 				 "nick", "owner_type", "value_type");
207 	} else if (!strcmp(attr, "default_value")) {
208 	    return PyLong_FromLong(G_PARAM_SPEC_LONG(pspec)->default_value);
209 	} else if (!strcmp(attr, "minimum")) {
210 	    return PyLong_FromLong(G_PARAM_SPEC_LONG(pspec)->minimum);
211 	} else if (!strcmp(attr, "maximum")) {
212 	    return PyLong_FromLong(G_PARAM_SPEC_LONG(pspec)->maximum);
213 	}
214     } else if (G_IS_PARAM_SPEC_ULONG(pspec)) {
215 	if (!strcmp(attr, "__members__")) {
216 	    return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
217 				 "blurb", "default_value",
218 				 "flags", "maximum", "minimum", "name",
219 				 "nick", "owner_type", "value_type");
220 	} else if (!strcmp(attr, "default_value")) {
221 	    return PyLong_FromUnsignedLong(G_PARAM_SPEC_ULONG(pspec)->default_value);
222 	} else if (!strcmp(attr, "minimum")) {
223 	    return PyLong_FromUnsignedLong(G_PARAM_SPEC_ULONG(pspec)->minimum);
224 	} else if (!strcmp(attr, "maximum")) {
225 	    return PyLong_FromUnsignedLong(G_PARAM_SPEC_ULONG(pspec)->maximum);
226 	}
227     } else if (G_IS_PARAM_SPEC_INT64(pspec)) {
228 	if (!strcmp(attr, "__members__")) {
229 	    return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
230 				 "blurb", "default_value",
231 				 "flags", "maximum", "minimum", "name",
232 				 "nick", "owner_type", "value_type");
233 	} else if (!strcmp(attr, "default_value")) {
234 	    return PyLong_FromLongLong(G_PARAM_SPEC_INT64(pspec)->default_value);
235 	} else if (!strcmp(attr, "minimum")) {
236 	    return PyLong_FromLongLong(G_PARAM_SPEC_INT64(pspec)->minimum);
237 	} else if (!strcmp(attr, "maximum")) {
238 	    return PyLong_FromLongLong(G_PARAM_SPEC_INT64(pspec)->maximum);
239 	}
240     } else if (G_IS_PARAM_SPEC_UINT64(pspec)) {
241 	if (!strcmp(attr, "__members__")) {
242 	    return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
243 				 "blurb", "default_value",
244 				 "flags", "maximum", "minimum",
245 				 "name", "nick", "owner_type",
246 				 "value_type");
247 	} else if (!strcmp(attr, "default_value")) {
248 	    return PyLong_FromUnsignedLongLong(G_PARAM_SPEC_UINT64(pspec)->default_value);
249 	} else if (!strcmp(attr, "minimum")) {
250 	    return PyLong_FromUnsignedLongLong(G_PARAM_SPEC_UINT64(pspec)->minimum);
251 	} else if (!strcmp(attr, "maximum")) {
252 	    return PyLong_FromUnsignedLongLong(G_PARAM_SPEC_UINT64(pspec)->maximum);
253 	}
254     } else if (G_IS_PARAM_SPEC_UNICHAR(pspec)) {
255 	if (!strcmp(attr, "__members__")) {
256 	    return Py_BuildValue("[sssssssss]", "__doc__", "__gtype__",
257 				 "blurb", "default_value",
258 				 "flags", "name", "nick", "owner_type",
259 				 "value_type");
260 	} else if (!strcmp(attr, "default_value")) {
261 	    return PYGLIB_PyUnicode_FromFormat(
262 		"%c", G_PARAM_SPEC_UNICHAR(pspec)->default_value);
263 	}
264     } else if (G_IS_PARAM_SPEC_ENUM(pspec)) {
265 	if (!strcmp(attr, "__members__")) {
266 	    return Py_BuildValue("[ssssssssss]", "__doc__", "__gtype__",
267 				 "blurb", "default_value", "enum_class",
268 				 "flags", "name", "nick", "owner_type",
269 				 "value_type");
270 	} else if (!strcmp(attr, "default_value")) {
271 	    return pyg_enum_from_gtype(
272 		pspec->value_type, G_PARAM_SPEC_ENUM(pspec)->default_value);
273 	} else if (!strcmp(attr, "enum_class")) {
274 	    return pygenum_from_pspec(pspec);
275 	}
276     } else if (G_IS_PARAM_SPEC_FLAGS(pspec)) {
277 	if (!strcmp(attr, "__members__")) {
278 	    return Py_BuildValue("[ssssssssss]", "__doc__", "__gtype__",
279 				 "blurb", "default_value",
280 				 "flags", "flags_class", "name", "nick",
281 				 "owner_type", "value_type");
282 	} else if (!strcmp(attr, "default_value")) {
283 	    return pyg_flags_from_gtype(
284 		pspec->value_type, G_PARAM_SPEC_FLAGS(pspec)->default_value);
285 	} else if (!strcmp(attr, "flags_class")) {
286 	    return pygflags_from_pspec(pspec);
287 	}
288     } else if (G_IS_PARAM_SPEC_FLOAT(pspec)) {
289 	if (!strcmp(attr, "__members__")) {
290 	    return Py_BuildValue("[ssssssssssss]", "__doc__", "__gtype__",
291 				 "blurb", "epsilon",
292 				 "flags", "maximum", "minimum", "name", "nick", "owner_type",
293 				 "value_type",
294 				 "default_value");
295 	} else if (!strcmp(attr, "default_value")) {
296 	    return PyFloat_FromDouble(G_PARAM_SPEC_FLOAT(pspec)->default_value);
297 	} else if (!strcmp(attr, "minimum")) {
298 	    return PyFloat_FromDouble(G_PARAM_SPEC_FLOAT(pspec)->minimum);
299 	} else if (!strcmp(attr, "maximum")) {
300 	    return PyFloat_FromDouble(G_PARAM_SPEC_FLOAT(pspec)->maximum);
301 	} else if (!strcmp(attr, "epsilon")) {
302 	    return PyFloat_FromDouble(G_PARAM_SPEC_FLOAT(pspec)->epsilon);
303 	}
304     } else if (G_IS_PARAM_SPEC_DOUBLE(pspec)) {
305 	if (!strcmp(attr, "__members__")) {
306 	    return Py_BuildValue("[ssssssssssss]", "__doc__", "__gtype__",
307 				 "blurb", "default_value", "epsilon",
308 				 "flags", "maximum", "minimum", "name", "nick",
309 				 "owner_type", "value_type");
310 	} else if (!strcmp(attr, "default_value")) {
311 	    return PyFloat_FromDouble(
312 		G_PARAM_SPEC_DOUBLE(pspec)->default_value);
313 	} else if (!strcmp(attr, "minimum")) {
314 	    return PyFloat_FromDouble(G_PARAM_SPEC_DOUBLE(pspec)->minimum);
315 	} else if (!strcmp(attr, "maximum")) {
316 	    return PyFloat_FromDouble(G_PARAM_SPEC_DOUBLE(pspec)->maximum);
317 	} else if (!strcmp(attr, "epsilon")) {
318 	    return PyFloat_FromDouble(G_PARAM_SPEC_DOUBLE(pspec)->epsilon);
319 	}
320     } else if (G_IS_PARAM_SPEC_STRING(pspec)) {
321 	if (!strcmp(attr, "__members__")) {
322 	    return Py_BuildValue("[ssssssssssssss]", "__doc__", "__gtype__",
323 				 "blurb", "cset_firth", "cset_nth", "default_value",
324 				 "ensure_non_null", "flags", "name", "nick",
325 				 "null_fold_if_empty", "owner_type", "substitutor",
326 				 "value_type");
327 	} else if (!strcmp(attr, "default_value")) {
328 	    return Py_BuildValue(
329 		"s", G_PARAM_SPEC_STRING(pspec)->default_value);
330 	} else if (!strcmp(attr, "cset_first")) {
331 	    return Py_BuildValue(
332 		"s", G_PARAM_SPEC_STRING(pspec)->cset_first);
333 	} else if (!strcmp(attr, "cset_nth")) {
334 	    return Py_BuildValue(
335 		"s", G_PARAM_SPEC_STRING(pspec)->cset_nth);
336 	} else if (!strcmp(attr, "substitutor")) {
337 	    return Py_BuildValue(
338 		"c", G_PARAM_SPEC_STRING(pspec)->substitutor);
339 	} else if (!strcmp(attr, "null_fold_if_empty")) {
340 	    return PyBool_FromLong(
341 		G_PARAM_SPEC_STRING(pspec)->null_fold_if_empty);
342 	} else if (!strcmp(attr, "ensure_non_null")) {
343 	    return PyBool_FromLong(
344 		G_PARAM_SPEC_STRING(pspec)->ensure_non_null);
345 	}
346     } else {
347 	if (!strcmp(attr, "__members__")) {
348 	    return Py_BuildValue("[ssssssss]", "__doc__", "__gtype__", "blurb",
349 				 "flags", "name", "nick",
350 				 "owner_type", "value_type");
351 
352 	/* This is actually not what's exported by GObjects paramspecs,
353 	 * But we exported this in earlier versions, so it's better to keep it here
354 	 * compatibility. But don't add it in __members__, to "hide" it.
355 	 */
356 	} else if (!strcmp(attr, "default_value")) {
357 	    /* XXX: Raise deprecation warning */
358 	    Py_INCREF(Py_None);
359 	    return Py_None;
360 	}
361     }
362 
363     PyErr_SetString(PyExc_AttributeError, attr);
364     return NULL;
365 }
366 
367 /**
368  * pyg_param_spec_new:
369  * @pspec: a GParamSpec.
370  *
371  * Creates a wrapper for a GParamSpec.
372  *
373  * Returns: the GParamSpec wrapper.
374  */
375 PyObject *
pyg_param_spec_new(GParamSpec * pspec)376 pyg_param_spec_new(GParamSpec *pspec)
377 {
378     PyGParamSpec *self;
379 
380     self = (PyGParamSpec *)PyObject_NEW(PyGParamSpec,
381 					&PyGParamSpec_Type);
382     if (self == NULL)
383 	return NULL;
384 
385     self->pspec = g_param_spec_ref(pspec);
386     return (PyObject *)self;
387 }
388 
389 void
pygobject_paramspec_register_types(PyObject * d)390 pygobject_paramspec_register_types(PyObject *d)
391 {
392     Py_TYPE(&PyGParamSpec_Type) = &PyType_Type;
393     PyGParamSpec_Type.tp_dealloc = (destructor)pyg_param_spec_dealloc;
394     PyGParamSpec_Type.tp_getattr = (getattrfunc)pyg_param_spec_getattr;
395     PyGParamSpec_Type.tp_richcompare = pyg_param_spec_richcompare;
396     PyGParamSpec_Type.tp_flags = Py_TPFLAGS_DEFAULT;
397     PyGParamSpec_Type.tp_repr = (reprfunc)pyg_param_spec_repr;
398     PyGParamSpec_Type.tp_hash = (hashfunc)pyg_param_spec_hash;
399 
400 
401     if (PyType_Ready(&PyGParamSpec_Type))
402 	return;
403     PyDict_SetItemString(d, "GParamSpec", (PyObject *)&PyGParamSpec_Type);
404 }
405