Debian Codegraph @

gi/pygflags.c

raw
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 * pygflags.c: GFlags wrapper
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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "pygi-type.h"
23
24 #include "pygflags.h"
25 #include "pygi-util.h"
26
27 GQuark pygflags_class_key;
28
29 PyTypeObject *PyGFlags_Type;
30 static PyObject *IntFlag_Type;
31
32 PyObject *
33 pyg_flags_val_new (PyObject *pyclass, guint value)
34 {
35 PyObject *retval, *intvalue;
36 PyObject *args[2] = { NULL };
37
38 intvalue = PyLong_FromUnsignedLong (value);
39 if (!intvalue) return NULL;
40
41 args[1] = intvalue;
42 retval = PyObject_Vectorcall (pyclass, &args[1],
43 1 + PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
44 if (!retval && PyErr_ExceptionMatches (PyExc_ValueError)) {
45 PyErr_Clear ();
46 return intvalue;
47 }
48 Py_DECREF (intvalue);
49
50 return retval;
51 }
52
53 PyObject *
54 pyg_flags_from_gtype (GType gtype, guint value)
55 {
56 PyObject *pyclass;
57
58 g_return_val_if_fail (gtype != G_TYPE_INVALID, NULL);
59
60 /* Get a wrapper class by:
61 * 1. check for one attached to the gtype
62 * 2. lookup one in a typelib
63 * 3. creating a new one
64 */
65 pyclass = (PyObject *)g_type_get_qdata (gtype, pygflags_class_key);
66 if (!pyclass) pyclass = pygi_type_import_by_g_type (gtype);
67 if (!pyclass)
68 pyclass = pyg_flags_add (NULL, g_type_name (gtype), NULL, gtype);
69 if (!pyclass) return PyLong_FromUnsignedLong (value);
70
71 return pyg_flags_val_new (pyclass, value);
72 }
73
74 static void
75 add_value (PyObject *dict, const char *value_nick, unsigned int value)
76 {
77 char *upper = g_ascii_strup (value_nick, -1);
78 char *c;
79 PyObject *v;
80
81 for (c = upper; *c != '\0'; c++) {
82 if (*c == '-') *c = '_';
83 }
84
85 /* skip if the name already exists in the dictionary */
86 if (PyMapping_HasKeyString (dict, upper)) {
87 g_free (upper);
88 return;
89 }
90
91 v = PyLong_FromUnsignedLong (value);
92 PyMapping_SetItemString (dict, upper, v);
93 Py_DECREF (v);
94 g_free (upper);
95 }
96
97 PyObject *
98 pyg_flags_add_full (PyObject *module, const char *typename, GType gtype,
99 GIFlagsInfo *info)
100 {
101 PyObject *stub;
102 PyObject *base_class, *method_name, *flags_name, *bases, *values;
103 PyObject *args[4] = { NULL };
104
105 if (gtype == G_TYPE_NONE && info == NULL) {
106 PyErr_SetString (PyExc_ValueError,
107 "cannot create enum without a GType or EnumInfo");
108 return NULL;
109 }
110 if (gtype != G_TYPE_NONE && !g_type_is_a (gtype, G_TYPE_FLAGS)) {
111 PyErr_Format (PyExc_TypeError,
112 "Trying to register gtype '%s' as flags when in fact it "
113 "is of type '%s'",
114 g_type_name (gtype),
115 g_type_name (G_TYPE_FUNDAMENTAL (gtype)));
116 return NULL;
117 }
118 if (info != NULL) {
119 GType info_gtype = gi_registered_type_info_get_g_type (
120 GI_REGISTERED_TYPE_INFO (info));
121
122 if (info_gtype != gtype) {
123 PyErr_Format (PyExc_ValueError,
124 "gtype '%s' does not match FlagsInfo '%s'",
125 g_type_name (gtype),
126 gi_base_info_get_name (GI_BASE_INFO (info)));
127 return NULL;
128 }
129 }
130
131 if (gtype == G_TYPE_NONE)
132 base_class = IntFlag_Type;
133 else
134 base_class = (PyObject *)PyGFlags_Type;
135 flags_name = PyUnicode_FromString (typename);
136 bases = PyTuple_New (1);
137 PyTuple_SET_ITEM (bases, 0, Py_NewRef (base_class));
138
139 args[0] = (PyObject *)Py_TYPE (base_class);
140 args[1] = flags_name;
141 args[2] = bases;
142
143 method_name = PyUnicode_FromString ("__prepare__");
144 values = PyObject_VectorcallMethod (
145 method_name, args, 3 + PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
146 Py_DECREF (method_name);
147 if (!values) {
148 Py_DECREF (flags_name);
149 Py_DECREF (bases);
150 return NULL;
151 }
152
153 /* Collect values from registered GType */
154 if (gtype != G_TYPE_NONE) {
155 GFlagsClass *fclass;
156 unsigned int i;
157
158 fclass = G_FLAGS_CLASS (g_type_class_ref (gtype));
159 for (i = 0; i < fclass->n_values; i++) {
160 add_value (values, fclass->values[i].value_nick,
161 fclass->values[i].value);
162 }
163 g_type_class_unref (fclass);
164 }
165
166 /* Collect values from GIFlagsInfo (which may include additional aliases) */
167 if (info != NULL) {
168 unsigned int i, length;
169
170 length = gi_enum_info_get_n_values (GI_ENUM_INFO (info));
171 for (i = 0; i < length; i++) {
172 GIValueInfo *v = gi_enum_info_get_value (GI_ENUM_INFO (info), i);
173
174 add_value (values, gi_base_info_get_name (GI_BASE_INFO (v)),
175 (unsigned int)gi_value_info_get_value (v));
176 gi_base_info_unref (v);
177 }
178 }
179
180 if (module && !Py_IsNone (module)) {
181 PyObject *module_name = PyModule_GetNameObject (module);
182
183 PyMapping_SetItemString (values, "__module__", module_name);
184 Py_DECREF (module_name);
185 }
186 if (gtype != G_TYPE_NONE) {
187 PyObject *o = pyg_type_wrapper_new (gtype);
188
189 PyMapping_SetItemString (values, "__gtype__", o);
190 Py_DECREF (o);
191 }
192
193 args[3] = values;
194
195 stub = PyObject_Vectorcall ((PyObject *)Py_TYPE (base_class), &args[1],
196 3 + PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
197 Py_DECREF (values);
198 Py_DECREF (bases);
199 Py_DECREF (flags_name);
200
201 if (!stub) return NULL;
202
203 ((PyTypeObject *)stub)->tp_flags &= ~Py_TPFLAGS_BASETYPE;
204 if (gtype != G_TYPE_NONE) {
205 g_type_set_qdata (gtype, pygflags_class_key, stub);
206 }
207
208 return stub;
209 }
210
211
212 /*
213 * pyg_flags_add
214 * Dynamically create a class derived from PyGFlags based on the given GType.
215 */
216 PyObject *
217 pyg_flags_add (PyObject *module, const char *typename,
218 const char *strip_prefix, GType gtype)
219 {
220 PyGILState_STATE state;
221 PyObject *stub;
222
223 g_return_val_if_fail (typename != NULL, NULL);
224 if (!g_type_is_a (gtype, G_TYPE_FLAGS)) {
225 g_warning (
226 "Trying to register gtype '%s' as flags when in fact it is of "
227 "type '%s'",
228 g_type_name (gtype), g_type_name (G_TYPE_FUNDAMENTAL (gtype)));
229 return NULL;
230 }
231
232 state = PyGILState_Ensure ();
233
234 stub = pyg_flags_add_full (module, typename, gtype, NULL);
235 if (!stub) {
236 PyGILState_Release (state);
237 return NULL;
238 }
239
240 if (module) {
241 GFlagsClass *fclass;
242 guint i;
243
244 /* Add it to the module name space */
245 PyModule_AddObject (module, (char *)typename, stub);
246 Py_INCREF (stub);
247
248 /* Register flags values */
249 fclass = G_FLAGS_CLASS (g_type_class_ref (gtype));
250 for (i = 0; i < fclass->n_values; i++) {
251 PyObject *item, *intval;
252 PyObject *args[2] = { NULL };
253 char *prefix;
254
255 intval = PyLong_FromUnsignedLong (fclass->values[i].value);
256 args[1] = intval;
257 item = PyObject_Vectorcall (
258 stub, &args[1], 1 + PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
259 Py_DECREF (intval);
260
261 prefix = g_strdup (pyg_constant_strip_prefix (
262 fclass->values[i].value_name, strip_prefix));
263 PyModule_AddObject (module, prefix, item);
264 g_free (prefix);
265 }
266 g_type_class_unref (fclass);
267 }
268
269 PyGILState_Release (state);
270 return stub;
271 }
272
273 gboolean
274 pyg_flags_register (PyTypeObject *flags_class, char *type_name)
275 {
276 gboolean result = FALSE;
277 GType gtype = G_TYPE_INVALID;
278 GFlagsValue *flags_values = NULL;
279 PyObject *values = NULL, *pygtype = NULL;
280 Py_ssize_t length, i;
281
282 values = PySequence_List ((PyObject *)flags_class);
283 if (!values) goto out;
284
285 length = PyList_Size (values);
286 flags_values = g_new0 (GFlagsValue, length + 1);
287 for (i = 0; i < length; i++) {
288 PyObject *value = PyList_GetItem (values, i);
289 PyObject *name = PyObject_GetAttrString (value, "name");
290 char *c;
291
292 if (!name) {
293 goto out;
294 }
295 if (!PyUnicode_Check (name)) {
296 PyErr_SetString (PyExc_TypeError,
297 "flags value names should be strings");
298 Py_DECREF (name);
299 goto out;
300 }
301
302 flags_values[i].value = (guint)PyLong_AsUnsignedLongMask (value);
303 flags_values[i].value_name =
304 g_strdup (PyUnicode_AsUTF8AndSize (name, NULL));
305 c = g_ascii_strdown (flags_values[i].value_name, -1);
306 flags_values[i].value_nick = c;
307 while (*c != '\0') {
308 if (*c == '_') *c = '-';
309 c++;
310 }
311 Py_DECREF (name);
312 }
313
314 gtype = g_flags_register_static (type_name, flags_values);
315 if (gtype == G_TYPE_INVALID) {
316 PyErr_Format (PyExc_RuntimeError, "Unable to register flags '%s'",
317 type_name);
318 goto out;
319 }
320 g_type_set_qdata (gtype, pygflags_class_key, flags_class);
321
322 pygtype = pyg_type_wrapper_new (gtype);
323 if (!pygtype) goto out;
324 PyObject_SetAttrString ((PyObject *)flags_class, "__gtype__", pygtype);
325
326 result = TRUE;
327
328 out:
329 Py_XDECREF (values);
330 Py_XDECREF (pygtype);
331 /* If type registration succeeded, this data should not be freed */
332 if (gtype == G_TYPE_INVALID) {
333 g_free (type_name);
334 if (flags_values != NULL) {
335 GFlagsValue *v;
336 for (v = flags_values; v->value_name != NULL; v++) {
337 g_free ((char *)v->value_name);
338 g_free ((char *)v->value_nick);
339 }
340 g_free (flags_values);
341 }
342 }
343 return result;
344 }
345
346 static GType
347 get_flags_gtype (PyTypeObject *type)
348 {
349 PyObject *pytc;
350 GType gtype;
351
352 pytc = PyObject_GetAttrString ((PyObject *)type, "__gtype__");
353 if (!pytc) return G_TYPE_INVALID;
354
355 if (!PyObject_TypeCheck (pytc, &PyGTypeWrapper_Type)) {
356 Py_DECREF (pytc);
357 PyErr_SetString (PyExc_TypeError,
358 "__gtype__ attribute not a typecode");
359 return G_TYPE_INVALID;
360 }
361
362 gtype = pyg_type_from_object (pytc);
363 Py_DECREF (pytc);
364
365 if (!G_TYPE_IS_FLAGS (gtype)) {
366 PyErr_SetString (PyExc_TypeError,
367 "__gtype__ attribute not a flags typecode");
368 return G_TYPE_INVALID;
369 }
370
371 return gtype;
372 }
373
374 static PyObject *
375 pyg_flags_get_first_value_name (PyObject *self, void *closure)
376 {
377 GType gtype;
378 GFlagsClass *flags_class;
379 GFlagsValue *flags_value;
380 PyObject *retval;
381
382 gtype = get_flags_gtype (Py_TYPE (self));
383 if (gtype == G_TYPE_INVALID) return NULL;
384 flags_class = g_type_class_ref (gtype);
385 g_assert (G_IS_FLAGS_CLASS (flags_class));
386 flags_value = g_flags_get_first_value (
387 flags_class, (guint)PyLong_AsUnsignedLongMask ((PyObject *)self));
388 if (flags_value)
389 retval = PyUnicode_FromString (flags_value->value_name);
390 else {
391 retval = Py_NewRef (Py_None);
392 }
393 g_type_class_unref (flags_class);
394
395 return retval;
396 }
397
398 static PyObject *
399 pyg_flags_get_first_value_nick (PyObject *self, void *closure)
400 {
401 GType gtype;
402 GFlagsClass *flags_class;
403 GFlagsValue *flags_value;
404 PyObject *retval;
405
406 gtype = get_flags_gtype (Py_TYPE (self));
407 if (gtype == G_TYPE_INVALID) return NULL;
408 flags_class = g_type_class_ref (gtype);
409 g_assert (G_IS_FLAGS_CLASS (flags_class));
410
411 flags_value = g_flags_get_first_value (
412 flags_class, (guint)PyLong_AsUnsignedLongMask ((PyObject *)self));
413 if (flags_value)
414 retval = PyUnicode_FromString (flags_value->value_nick);
415 else {
416 retval = Py_NewRef (Py_None);
417 }
418 g_type_class_unref (flags_class);
419
420 return retval;
421 }
422
423 static PyObject *
424 pyg_flags_get_value_names (PyObject *self, void *closure)
425 {
426 GType gtype;
427 GFlagsClass *flags_class;
428 PyObject *retval;
429 guint i;
430
431 gtype = get_flags_gtype (Py_TYPE (self));
432 if (gtype == G_TYPE_INVALID) return NULL;
433 flags_class = g_type_class_ref (gtype);
434 g_assert (G_IS_FLAGS_CLASS (flags_class));
435
436 retval = PyList_New (0);
437 for (i = 0; i < flags_class->n_values; i++) {
438 PyObject *value_name;
439
440 if ((PyLong_AsUnsignedLongMask ((PyObject *)self)
441 & flags_class->values[i].value)
442 == flags_class->values[i].value) {
443 value_name =
444 PyUnicode_FromString (flags_class->values[i].value_name);
445 PyList_Append (retval, value_name);
446 Py_DECREF (value_name);
447 }
448 }
449
450 g_type_class_unref (flags_class);
451
452 return retval;
453 }
454
455 static PyObject *
456 pyg_flags_get_value_nicks (PyObject *self, void *closure)
457 {
458 GType gtype;
459 GFlagsClass *flags_class;
460 PyObject *retval;
461 guint i;
462
463 gtype = get_flags_gtype (Py_TYPE (self));
464 if (gtype == G_TYPE_INVALID) return NULL;
465 flags_class = g_type_class_ref (gtype);
466 g_assert (G_IS_FLAGS_CLASS (flags_class));
467
468 retval = PyList_New (0);
469 for (i = 0; i < flags_class->n_values; i++)
470 if ((PyLong_AsUnsignedLongMask ((PyObject *)self)
471 & flags_class->values[i].value)
472 == flags_class->values[i].value) {
473 PyObject *py_nick =
474 PyUnicode_FromString (flags_class->values[i].value_nick);
475 PyList_Append (retval, py_nick);
476 Py_DECREF (py_nick);
477 }
478
479 g_type_class_unref (flags_class);
480
481 return retval;
482 }
483
484 static PyGetSetDef pyg_flags_getsets[] = {
485 { "first_value_name", (getter)pyg_flags_get_first_value_name, (setter)0 },
486 { "first_value_nick", (getter)pyg_flags_get_first_value_nick, (setter)0 },
487 { "value_names", (getter)pyg_flags_get_value_names, (setter)0 },
488 { "value_nicks", (getter)pyg_flags_get_value_nicks, (setter)0 },
489 { NULL, 0, 0 },
490 };
491
492 /**
493 * Returns 0 on success, or -1 and sets an exception.
494 */
495 int
496 pygi_flags_register_types (PyObject *mod)
497 {
498 PyObject *enum_module, *item;
499 int i;
500
501 pygflags_class_key = g_quark_from_static_string ("PyGFlags::class");
502
503 enum_module = PyImport_ImportModule ("enum");
504 if (!enum_module) return -1;
505
506 IntFlag_Type = PyObject_GetAttrString (enum_module, "IntFlag");
507 Py_DECREF (enum_module);
508 if (!IntFlag_Type) return -1;
509
510 enum_module = PyImport_ImportModule ("gi._enum");
511 if (!enum_module) return -1;
512
513 PyGFlags_Type =
514 (PyTypeObject *)PyObject_GetAttrString (enum_module, "GFlags");
515 Py_DECREF (enum_module);
516 if (!PyGFlags_Type) return -1;
517
518 item = pyg_type_wrapper_new (G_TYPE_FLAGS);
519 PyObject_SetAttrString ((PyObject *)PyGFlags_Type, "__gtype__", item);
520 Py_DECREF (item);
521
522 for (i = 0; pyg_flags_getsets[i].name != NULL; i++) {
523 item = PyDescr_NewGetSet (PyGFlags_Type, &pyg_flags_getsets[i]);
524 PyObject_SetAttrString ((PyObject *)PyGFlags_Type,
525 pyg_flags_getsets[i].name, item);
526 Py_DECREF (item);
527 }
528
529 PyModule_AddObject (mod, "GFlags", Py_NewRef ((PyObject *)PyGFlags_Type));
530
531 return 0;
532 }