Debian Codegraph @

gi/pygi-info.c

raw
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
3 *
4 * Copyright (C) 2005-2009 Johan Dahlin <[email protected]>
5 * Copyright (C) 2013 Simon Feltman <[email protected]>
6 *
7 * pygi-info.c: GI.*Info wrappers.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "pygi-type.h"
24
25 #include "pygi-argument.h"
26 #include "pygi-basictype.h"
27 #include "pygi-fundamental.h"
28 #include "pygi-invoke.h"
29 #include "pygi-util.h"
30
31 /* _generate_doc_string
32 *
33 * C wrapper to call Python implemented "gi.docstring.generate_doc_string"
34 */
35 static PyObject *
36 _generate_doc_string (PyGIBaseInfo *self)
37 {
38 static PyObject *_py_generate_doc_string = NULL;
39
40 if (_py_generate_doc_string == NULL) {
41 PyObject *mod = PyImport_ImportModule ("gi.docstring");
42 if (!mod) return NULL;
43
44 _py_generate_doc_string =
45 PyObject_GetAttrString (mod, "generate_doc_string");
46 if (_py_generate_doc_string == NULL) {
47 Py_DECREF (mod);
48 return NULL;
49 }
50 Py_DECREF (mod);
51 }
52
53 return PyObject_CallFunctionObjArgs (_py_generate_doc_string, self, NULL);
54 }
55
56 static PyObject *
57 _generate_signature (PyGICallableInfo *self)
58 {
59 static PyObject *_py_generate_signature = NULL;
60
61 if (_py_generate_signature == NULL) {
62 PyObject *mod = PyImport_ImportModule ("gi._signature");
63 if (!mod) return NULL;
64
65 _py_generate_signature =
66 PyObject_GetAttrString (mod, "generate_signature");
67 if (_py_generate_signature == NULL) {
68 Py_DECREF (mod);
69 return NULL;
70 }
71 Py_DECREF (mod);
72 }
73
74 return PyObject_CallFunctionObjArgs (_py_generate_signature, self, NULL);
75 }
76
77 static PyObject *
78 _get_info_string (const gchar *value)
79 {
80 if (value == NULL) {
81 Py_RETURN_NONE;
82 }
83 return pygi_utf8_to_py (value);
84 }
85
86 /* TODO Maybe rework this to be a macro which can do the type safety properly */
87 typedef GIBaseInfo *(*GetChildInfoCallback) (GIBaseInfo *info);
88
89 static PyObject *
90 _get_child_info (PyGIBaseInfo *self, GetChildInfoCallback get_child_info)
91 {
92 GIBaseInfo *info;
93 PyObject *py_info;
94
95 info = get_child_info ((GIBaseInfo *)self->info);
96 if (info == NULL) {
97 Py_RETURN_NONE;
98 }
99
100 py_info = _pygi_info_new (info);
101 gi_base_info_unref (info);
102 return py_info;
103 }
104
105 typedef GIBaseInfo *(*GetChildInfoByNameCallback) (GIBaseInfo *info,
106 const char *name);
107
108 static PyObject *
109 _get_child_info_by_name (PyGIBaseInfo *self, PyObject *py_name,
110 GetChildInfoByNameCallback get_child_info_by_name)
111 {
112 GIBaseInfo *info;
113 PyObject *py_info;
114 char *name;
115
116 if (!pygi_utf8_from_py (py_name, &name)) return NULL;
117
118 info = get_child_info_by_name (self->info, name);
119 g_free (name);
120 if (info == NULL) {
121 Py_RETURN_NONE;
122 }
123
124 py_info = _pygi_info_new (info);
125 gi_base_info_unref (info);
126 return py_info;
127 }
128
129
130 /* _make_infos_tuple
131 *
132 * Build a tuple from the common API pattern in GI of having a
133 * function which returns a count and an indexed GIBaseInfo
134 * in the range of 0 to count;
135 */
136 /* TODO Maybe rework this to be a macro which can do the type safety properly */
137 typedef unsigned int (*GetNInfosCallback) (GIBaseInfo *info);
138 typedef GIBaseInfo *(*MakeInfosCallback) (GIBaseInfo *info, unsigned int idx);
139
140 static PyObject *
141 _make_infos_tuple (PyGIBaseInfo *self, GetNInfosCallback get_n_infos,
142 MakeInfosCallback get_info)
143 {
144 gint n_infos;
145 PyObject *infos;
146 gint i;
147
148 n_infos = get_n_infos ((GIBaseInfo *)self->info);
149
150 infos = PyTuple_New (n_infos);
151 if (infos == NULL) {
152 return NULL;
153 }
154
155 for (i = 0; i < n_infos; i++) {
156 GIBaseInfo *info;
157 PyObject *py_info;
158
159 info = (GIBaseInfo *)get_info (self->info, i);
160 g_assert (info != NULL);
161
162 py_info = _pygi_info_new (info);
163
164 gi_base_info_unref (info);
165
166 if (py_info == NULL) {
167 Py_CLEAR (infos);
168 break;
169 }
170
171 PyTuple_SET_ITEM (infos, i, py_info);
172 }
173
174 return infos;
175 }
176
177
178 /* BaseInfo */
179
180 /* We need to be careful about calling gi_base_info_get_name because
181 * calling it with a GI_INFO_TYPE_TYPE will crash.
182 * See: https://bugzilla.gnome.org/show_bug.cgi?id=709456
183 */
184 static const char *
185 _safe_base_info_get_name (GIBaseInfo *info)
186 {
187 if (GI_IS_TYPE_INFO (info)) {
188 return "type_type_instance";
189 } else {
190 return gi_base_info_get_name (info);
191 }
192 }
193
194 static void
195 _base_info_dealloc (PyGIBaseInfo *self)
196 {
197 if (self->inst_weakreflist != NULL)
198 PyObject_ClearWeakRefs ((PyObject *)self);
199
200 gi_base_info_unref (self->info);
201
202 Py_TYPE (self)->tp_free ((PyObject *)self);
203 }
204
205 static PyObject *
206 _base_info_repr (PyGIBaseInfo *self)
207 {
208 return PyUnicode_FromFormat ("%s(%s)", Py_TYPE ((PyObject *)self)->tp_name,
209 _safe_base_info_get_name (self->info));
210 }
211
212 static PyObject *
213 _wrap_gi_base_info_equal (PyGIBaseInfo *self, PyObject *other)
214 {
215 GIBaseInfo *other_info;
216
217 if (!PyObject_TypeCheck (other, &PyGIBaseInfo_Type)) {
218 Py_INCREF (Py_NotImplemented);
219 return Py_NotImplemented;
220 }
221
222 other_info = ((PyGIBaseInfo *)other)->info;
223 if (gi_base_info_equal (self->info, other_info)) {
224 Py_RETURN_TRUE;
225 } else {
226 Py_RETURN_FALSE;
227 }
228 }
229
230 static PyObject *
231 _base_info_richcompare (PyGIBaseInfo *self, PyObject *other, int op)
232 {
233 PyObject *res;
234
235 switch (op) {
236 case Py_EQ:
237 return _wrap_gi_base_info_equal (self, other);
238 case Py_NE:
239 res = _wrap_gi_base_info_equal (self, other);
240 if (Py_IsTrue (res)) {
241 Py_DECREF (res);
242 Py_RETURN_FALSE;
243 } else {
244 Py_DECREF (res);
245 Py_RETURN_TRUE;
246 }
247 default:
248 res = Py_NotImplemented;
249 break;
250 }
251 return Py_NewRef (res);
252 }
253
254 PYGI_DEFINE_TYPE ("gi.BaseInfo", PyGIBaseInfo_Type, PyGIBaseInfo);
255
256 static PyObject *
257 _wrap_gi_base_info_get_name (PyGIBaseInfo *self)
258 {
259 const gchar *name;
260 PyObject *is_keyword, *obj;
261
262 name = _safe_base_info_get_name (self->info);
263 is_keyword = pyg_is_python_keyword (name);
264 if (!is_keyword) return NULL;
265
266 /* escape keywords */
267 if (PyObject_IsTrue (is_keyword)) {
268 gchar *escaped = g_strconcat (name, "_", NULL);
269 obj = pygi_utf8_to_py (escaped);
270 g_free (escaped);
271 } else {
272 obj = pygi_utf8_to_py (name);
273 }
274 Py_DECREF (is_keyword);
275
276 return obj;
277 }
278
279 static PyObject *
280 _wrap_gi_base_info_get_name_unescaped (PyGIBaseInfo *self)
281 {
282 return _get_info_string (_safe_base_info_get_name (self->info));
283 }
284
285 static PyObject *
286 _wrap_gi_base_info_get_namespace (PyGIBaseInfo *self)
287 {
288 return _get_info_string (gi_base_info_get_namespace (self->info));
289 }
290
291 static PyObject *
292 _wrap_gi_base_info_is_deprecated (PyGIBaseInfo *self)
293 {
294 if (gi_base_info_is_deprecated (self->info))
295 Py_RETURN_TRUE;
296 else
297 Py_RETURN_FALSE;
298 }
299
300 static PyObject *
301 _wrap_gi_base_info_get_attribute (PyGIBaseInfo *self, PyObject *arg)
302 {
303 char *name;
304 const char *value;
305
306 if (!pygi_utf8_from_py (arg, &name)) return NULL;
307
308 value = gi_base_info_get_attribute (self->info, name);
309 g_free (name);
310 if (value == NULL) {
311 Py_RETURN_NONE;
312 }
313 return pygi_utf8_to_py (value);
314 }
315
316 static PyObject *
317 _wrap_gi_base_info_get_container (PyGIBaseInfo *self)
318 {
319 /* Note: don't use _get_child_info because gi_base_info_get_container
320 * is marked as [transfer none] and therefore returns a borrowed ref.
321 */
322 GIBaseInfo *info;
323
324 info = gi_base_info_get_container (self->info);
325
326 if (info == NULL) {
327 Py_RETURN_NONE;
328 }
329
330 return _pygi_info_new (info);
331 }
332
333
334 static PyMethodDef _PyGIBaseInfo_methods[] = {
335 { "get_name", (PyCFunction)_wrap_gi_base_info_get_name, METH_NOARGS },
336 { "get_name_unescaped", (PyCFunction)_wrap_gi_base_info_get_name_unescaped,
337 METH_NOARGS },
338 { "get_namespace", (PyCFunction)_wrap_gi_base_info_get_namespace,
339 METH_NOARGS },
340 { "is_deprecated", (PyCFunction)_wrap_gi_base_info_is_deprecated,
341 METH_NOARGS },
342 { "get_attribute", (PyCFunction)_wrap_gi_base_info_get_attribute, METH_O },
343 { "get_container", (PyCFunction)_wrap_gi_base_info_get_container,
344 METH_NOARGS },
345 { "equal", (PyCFunction)_wrap_gi_base_info_equal, METH_O },
346 { NULL, NULL, 0 },
347 };
348
349 /* _base_info_getattro:
350 *
351 * The usage of __getattr__ is needed because the get/set method table
352 * does not work for __doc__.
353 */
354 static PyObject *
355 _base_info_getattro (PyGIBaseInfo *self, PyObject *name)
356 {
357 PyObject *result;
358
359 static PyObject *docstr;
360 if (docstr == NULL) {
361 docstr = PyUnicode_InternFromString ("__doc__");
362 if (docstr == NULL) return NULL;
363 }
364
365 Py_INCREF (name);
366 PyUnicode_InternInPlace (&name);
367
368 if (name == docstr) {
369 result = _generate_doc_string (self);
370 } else {
371 result = PyObject_GenericGetAttr ((PyObject *)self, name);
372 }
373
374 Py_DECREF (name);
375 return result;
376 }
377
378 static PyObject *
379 _base_info_attr_name (PyGIBaseInfo *self, void *closure)
380 {
381 return _wrap_gi_base_info_get_name (self);
382 }
383
384 static PyObject *
385 _base_info_attr_module (PyGIBaseInfo *self, void *closure)
386 {
387 return PyUnicode_FromFormat ("gi.repository.%s",
388 gi_base_info_get_namespace (self->info));
389 }
390
391 static PyGetSetDef _base_info_getsets[] = {
392 { "__name__", (getter)_base_info_attr_name, (setter)0, "Name", NULL },
393 { "__module__", (getter)_base_info_attr_module, (setter)0, "Module name",
394 NULL },
395 { NULL, 0, 0 },
396 };
397
398 static PyObject *_callable_info_vectorcall (PyGICallableInfo *self,
399 PyObject *const *args,
400 size_t nargsf, PyObject *kwnames);
401 static PyObject *_function_info_vectorcall (PyGICallableInfo *self,
402 PyObject *const *args,
403 size_t nargsf, PyObject *kwnames);
404
405 PyObject *
406 _pygi_info_new (GIBaseInfo *info)
407 {
408 PyTypeObject *type = NULL;
409 vectorcallfunc vectorcall = NULL;
410 PyGIBaseInfo *self;
411
412 if (GI_IS_FUNCTION_INFO (info)) {
413 type = &PyGIFunctionInfo_Type;
414 vectorcall = (vectorcallfunc)_function_info_vectorcall;
415 } else if (GI_IS_CALLBACK_INFO (info)) {
416 type = &PyGICallbackInfo_Type;
417 vectorcall = (vectorcallfunc)_callable_info_vectorcall;
418 } else if (GI_IS_STRUCT_INFO (info)) {
419 type = &PyGIStructInfo_Type;
420 } else if (GI_IS_ENUM_INFO (info)) {
421 type = &PyGIEnumInfo_Type;
422 } else if (GI_IS_OBJECT_INFO (info)) {
423 type = &PyGIObjectInfo_Type;
424 } else if (GI_IS_INTERFACE_INFO (info)) {
425 type = &PyGIInterfaceInfo_Type;
426 } else if (GI_IS_CONSTANT_INFO (info)) {
427 type = &PyGIConstantInfo_Type;
428 } else if (GI_IS_UNION_INFO (info)) {
429 type = &PyGIUnionInfo_Type;
430 } else if (GI_IS_VALUE_INFO (info)) {
431 type = &PyGIValueInfo_Type;
432 } else if (GI_IS_SIGNAL_INFO (info)) {
433 type = &PyGISignalInfo_Type;
434 vectorcall = (vectorcallfunc)_callable_info_vectorcall;
435 } else if (GI_IS_VFUNC_INFO (info)) {
436 type = &PyGIVFuncInfo_Type;
437 vectorcall = (vectorcallfunc)_callable_info_vectorcall;
438 } else if (GI_IS_PROPERTY_INFO (info)) {
439 type = &PyGIPropertyInfo_Type;
440 } else if (GI_IS_FIELD_INFO (info)) {
441 type = &PyGIFieldInfo_Type;
442 } else if (GI_IS_ARG_INFO (info)) {
443 type = &PyGIArgInfo_Type;
444 } else if (GI_IS_TYPE_INFO (info)) {
445 type = &PyGITypeInfo_Type;
446 } else if (GI_IS_UNRESOLVED_INFO (info)) {
447 type = &PyGIUnresolvedInfo_Type;
448 } else {
449 PyErr_SetString (PyExc_RuntimeError, "Invalid info type");
450 return NULL;
451 }
452
453 self = (PyGIBaseInfo *)type->tp_alloc (type, 0);
454 if (self == NULL) {
455 return NULL;
456 }
457
458 self->info = gi_base_info_ref (info);
459 self->inst_weakreflist = NULL;
460
461 if (vectorcall != NULL) {
462 ((PyGICallableInfo *)self)->vectorcall = vectorcall;
463 }
464
465 return (PyObject *)self;
466 }
467
468 GIBaseInfo *
469 _pygi_object_get_gi_info (PyObject *object, PyTypeObject *type)
470 {
471 PyObject *py_info;
472 GIBaseInfo *info = NULL;
473
474 py_info = PyObject_GetAttrString (object, "__info__");
475 if (py_info == NULL) {
476 return NULL;
477 }
478 if (!PyObject_TypeCheck (py_info, type)) {
479 PyErr_Format (PyExc_TypeError,
480 "attribute '__info__' must be %s, not %s", type->tp_name,
481 Py_TYPE (py_info)->tp_name);
482 goto out;
483 }
484
485 info = ((PyGIBaseInfo *)py_info)->info;
486 gi_base_info_ref (info);
487
488 out:
489 Py_DECREF (py_info);
490
491 return info;
492 }
493
494
495 /* CallableInfo */
496 PYGI_DEFINE_TYPE ("gi.CallableInfo", PyGICallableInfo_Type, PyGICallableInfo);
497
498 static void
499 _callable_info_dealloc (PyGICallableInfo *self)
500 {
501 if (self->cache != NULL)
502 pygi_callable_cache_free ((PyGICallableCache *)self->cache);
503 _base_info_dealloc ((PyGIBaseInfo *)self);
504 }
505
506 /* _callable_info_call:
507 *
508 * Shared wrapper for invoke which can be bound (instance method or class constructor)
509 * or unbound (function or static method).
510 */
511 static PyObject *
512 _callable_info_vectorcall (PyGICallableInfo *self, PyObject *const *args,
513 size_t nargsf, PyObject *kwnames)
514 {
515 return pygi_callable_info_invoke (self, args, nargsf, kwnames);
516 }
517
518 static PyObject *
519 _callable_info_repr (PyGICallableInfo *self)
520 {
521 return PyUnicode_FromFormat ("%s(%s)", Py_TYPE ((PyObject *)self)->tp_name,
522 _safe_base_info_get_name (self->base.info));
523 }
524
525 /* _function_info_vectorcall:
526 *
527 * Specialization of _callable_info_call for GIFunctionInfo which
528 * handles constructor error conditions.
529 */
530 static PyObject *
531 _function_info_vectorcall (PyGICallableInfo *self, PyObject *const *args,
532 size_t nargsf, PyObject *kwnames)
533 {
534 GIFunctionInfoFlags flags;
535
536 /* Ensure constructors are only called as class methods on the class
537 * implementing the constructor and not on sub-classes.
538 */
539 flags = gi_function_info_get_flags ((GIFunctionInfo *)self->base.info);
540 if (flags & GI_FUNCTION_IS_CONSTRUCTOR) {
541 Py_ssize_t nargs;
542 PyObject *cls;
543 PyObject *py_str_name;
544 const gchar *str_name;
545 GIBaseInfo *container_info =
546 gi_base_info_get_container (self->base.info);
547
548 g_assert (container_info != NULL);
549
550 nargs = PyVectorcall_NARGS (nargsf);
551 cls = nargs > 0 ? args[0] : NULL;
552 if (cls == NULL) {
553 PyErr_BadArgument ();
554 return NULL;
555 }
556 py_str_name = PyObject_GetAttrString (cls, "__name__");
557 if (py_str_name == NULL) return NULL;
558
559 if (!PyUnicode_Check (py_str_name)) {
560 PyErr_SetString (PyExc_TypeError,
561 "cls.__name__ attribute is not a string");
562 Py_DECREF (py_str_name);
563 return NULL;
564 }
565
566 str_name = PyUnicode_AsUTF8 (py_str_name);
567 if (strcmp (str_name, _safe_base_info_get_name (container_info))) {
568 PyErr_Format (
569 PyExc_TypeError,
570 "%s constructor cannot be used to create instances of "
571 "a subclass %s",
572 _safe_base_info_get_name (container_info), str_name);
573 Py_DECREF (py_str_name);
574 return NULL;
575 }
576 Py_DECREF (py_str_name);
577 }
578
579 return _callable_info_vectorcall (self, args, nargsf, kwnames);
580 }
581
582 /* _function_info_descr_get
583 *
584 * Descriptor protocol implementation for functions, methods, and constructors.
585 */
586 static PyObject *
587 _function_info_descr_get (PyGICallableInfo *self, PyObject *obj,
588 PyObject *type)
589 {
590 if (obj != NULL && obj != Py_None)
591 return PyMethod_New ((PyObject *)self, obj);
592
593 return Py_NewRef (self);
594 }
595
596 /* _vfunc_info_descr_get
597 *
598 * Descriptor protocol implementation for virtual functions.
599 */
600 static PyObject *
601 _vfunc_info_descr_get (PyGICallableInfo *self, PyObject *obj, PyObject *type)
602 {
603 PyObject *result;
604 PyObject *bound_arg = NULL;
605
606 if (type == NULL) type = (PyObject *)Py_TYPE (obj);
607
608 bound_arg = PyObject_GetAttrString (type, "__gtype__");
609 if (bound_arg == NULL) return NULL;
610
611 /* _new_bound_callable_info adds its own ref so free the one from GetAttrString */
612 result = PyMethod_New ((PyObject *)self, bound_arg);
613 Py_DECREF (bound_arg);
614 return result;
615 }
616
617 static PyObject *
618 _wrap_gi_callable_info_get_arguments (PyGIBaseInfo *self)
619 {
620 return _make_infos_tuple (self,
621 (GetNInfosCallback)gi_callable_info_get_n_args,
622 (MakeInfosCallback)gi_callable_info_get_arg);
623 }
624
625 static PyObject *
626 _wrap_gi_callable_info_get_return_type (PyGIBaseInfo *self)
627 {
628 return _get_child_info (
629 self, (GetChildInfoCallback)gi_callable_info_get_return_type);
630 }
631
632 static PyObject *
633 _wrap_gi_callable_info_get_caller_owns (PyGIBaseInfo *self)
634 {
635 return pygi_guint_to_py (
636 gi_callable_info_get_caller_owns (GI_CALLABLE_INFO (self->info)));
637 }
638
639 static PyObject *
640 _wrap_gi_callable_info_may_return_null (PyGIBaseInfo *self)
641 {
642 return pygi_gboolean_to_py (
643 gi_callable_info_may_return_null (GI_CALLABLE_INFO (self->info)));
644 }
645
646 static PyObject *
647 _wrap_gi_callable_info_skip_return (PyGIBaseInfo *self)
648 {
649 return pygi_gboolean_to_py (
650 gi_callable_info_skip_return (GI_CALLABLE_INFO (self->info)));
651 }
652
653 static PyObject *
654 _wrap_gi_callable_info_get_return_attribute (PyGIBaseInfo *self,
655 PyObject *py_name)
656 {
657 gchar *name;
658 const gchar *attr;
659
660 if (!pygi_utf8_from_py (py_name, &name)) return NULL;
661
662 attr = gi_callable_info_get_return_attribute (
663 GI_CALLABLE_INFO (self->info), name);
664 if (attr) {
665 g_free (name);
666 return pygi_utf8_to_py (attr);
667 } else {
668 PyErr_Format (PyExc_AttributeError, "return attribute %s not found",
669 name);
670 g_free (name);
671 return NULL;
672 }
673 }
674
675 static PyObject *
676 _wrap_gi_callable_info_can_throw_gerror (PyGIBaseInfo *self)
677 {
678 if (gi_callable_info_can_throw_gerror (GI_CALLABLE_INFO (self->info)))
679 Py_RETURN_TRUE;
680 else
681 Py_RETURN_FALSE;
682 }
683
684 static PyMethodDef _PyGICallableInfo_methods[] = {
685 { "invoke", (PyCFunction)PyObject_Call, METH_VARARGS | METH_KEYWORDS },
686 { "get_arguments", (PyCFunction)_wrap_gi_callable_info_get_arguments,
687 METH_NOARGS },
688 { "get_return_type", (PyCFunction)_wrap_gi_callable_info_get_return_type,
689 METH_NOARGS },
690 { "get_caller_owns", (PyCFunction)_wrap_gi_callable_info_get_caller_owns,
691 METH_NOARGS },
692 { "may_return_null", (PyCFunction)_wrap_gi_callable_info_may_return_null,
693 METH_NOARGS },
694 { "skip_return", (PyCFunction)_wrap_gi_callable_info_skip_return,
695 METH_NOARGS },
696 { "get_return_attribute",
697 (PyCFunction)_wrap_gi_callable_info_get_return_attribute, METH_O },
698 { "can_throw_gerror", (PyCFunction)_wrap_gi_callable_info_can_throw_gerror,
699 METH_NOARGS },
700 { NULL, NULL, 0 },
701 };
702
703 static PyObject *
704 _callable_info_signature (PyGICallableInfo *self)
705 {
706 return _generate_signature (self);
707 }
708
709 static PyGetSetDef _PyGICallableInfo_getsets[] = {
710 { "__signature__", (getter)_callable_info_signature, (setter)NULL },
711 { NULL, NULL, NULL },
712 };
713
714
715 PyGIFunctionCache *
716 pygi_callable_info_get_cache (PyGICallableInfo *self)
717 {
718 PyGIFunctionCache *function_cache;
719 GIBaseInfo *info = self->base.info;
720
721 if (self->cache != NULL) return self->cache;
722
723 if (GI_IS_FUNCTION_INFO (info)) {
724 GIFunctionInfoFlags flags;
725
726 flags = gi_function_info_get_flags (GI_FUNCTION_INFO (info));
727
728 if (flags & GI_FUNCTION_IS_CONSTRUCTOR) {
729 function_cache =
730 pygi_constructor_cache_new (GI_CALLABLE_INFO (info));
731 } else if (flags & GI_FUNCTION_IS_METHOD) {
732 function_cache = pygi_method_cache_new (GI_CALLABLE_INFO (info));
733 } else {
734 function_cache = pygi_function_cache_new (GI_CALLABLE_INFO (info));
735 }
736 } else if (GI_IS_VFUNC_INFO (info)) {
737 function_cache = pygi_vfunc_cache_new (GI_CALLABLE_INFO (info));
738 } else if (GI_IS_CALLBACK_INFO (info)) {
739 g_error ("Cannot invoke callback types");
740 } else {
741 function_cache = pygi_method_cache_new (GI_CALLABLE_INFO (info));
742 }
743
744 self->cache = function_cache;
745
746 return function_cache;
747 }
748
749 /* CallbackInfo */
750 PYGI_DEFINE_TYPE ("gi.CallbackInfo", PyGICallbackInfo_Type, PyGICallableInfo);
751
752 static PyMethodDef _PyGICallbackInfo_methods[] = {
753 { NULL, NULL, 0 },
754 };
755
756 /* ErrorDomainInfo */
757 PYGI_DEFINE_TYPE ("gi.ErrorDomainInfo", PyGIErrorDomainInfo_Type,
758 PyGIBaseInfo);
759
760 static PyMethodDef _PyGIErrorDomainInfo_methods[] = {
761 { NULL, NULL, 0 },
762 };
763
764 /* SignalInfo */
765 PYGI_DEFINE_TYPE ("gi.SignalInfo", PyGISignalInfo_Type, PyGICallableInfo);
766
767 static PyObject *
768 _wrap_gi_signal_info_get_flags (PyGIBaseInfo *self)
769 {
770 return pygi_guint_to_py (
771 gi_signal_info_get_flags ((GISignalInfo *)self->info));
772 }
773
774 static PyObject *
775 _wrap_gi_signal_info_get_class_closure (PyGIBaseInfo *self)
776 {
777 return _get_child_info (
778 self, (GetChildInfoCallback)gi_signal_info_get_class_closure);
779 }
780
781 static PyObject *
782 _wrap_gi_signal_info_true_stops_emit (PyGIBaseInfo *self)
783 {
784 return pygi_gboolean_to_py (
785 gi_signal_info_true_stops_emit ((GISignalInfo *)self->info));
786 }
787
788 static PyMethodDef _PyGISignalInfo_methods[] = {
789 { "get_flags", (PyCFunction)_wrap_gi_signal_info_get_flags, METH_NOARGS },
790 { "get_class_closure", (PyCFunction)_wrap_gi_signal_info_get_class_closure,
791 METH_NOARGS },
792 { "true_stops_emit", (PyCFunction)_wrap_gi_signal_info_true_stops_emit,
793 METH_NOARGS },
794 { NULL, NULL, 0 },
795 };
796
797 /* PropertyInfo */
798 PYGI_DEFINE_TYPE ("gi.PropertyInfo", PyGIPropertyInfo_Type, PyGIBaseInfo);
799
800 static PyObject *
801 _wrap_gi_property_info_get_flags (PyGIBaseInfo *self)
802 {
803 return pygi_guint_to_py (
804 gi_property_info_get_flags ((GIPropertyInfo *)self->info));
805 }
806
807 static PyObject *
808 _wrap_gi_property_info_get_type_info (PyGIBaseInfo *self)
809 {
810 return _get_child_info (
811 self, (GetChildInfoCallback)gi_property_info_get_type_info);
812 }
813
814 static PyObject *
815 _wrap_gi_property_info_get_ownership_transfer (PyGIBaseInfo *self)
816 {
817 return pygi_guint_to_py (gi_property_info_get_ownership_transfer (
818 (GIPropertyInfo *)self->info));
819 }
820
821 static PyMethodDef _PyGIPropertyInfo_methods[] = {
822 { "get_flags", (PyCFunction)_wrap_gi_property_info_get_flags,
823 METH_NOARGS },
824 { "get_type_info", (PyCFunction)_wrap_gi_property_info_get_type_info,
825 METH_NOARGS },
826 { "get_ownership_transfer",
827 (PyCFunction)_wrap_gi_property_info_get_ownership_transfer,
828 METH_NOARGS },
829 { NULL, NULL, 0 },
830 };
831
832
833 /* ArgInfo */
834 PYGI_DEFINE_TYPE ("gi.ArgInfo", PyGIArgInfo_Type, PyGIBaseInfo);
835
836 static PyObject *
837 _wrap_gi_arg_info_get_direction (PyGIBaseInfo *self)
838 {
839 return pygi_guint_to_py (
840 gi_arg_info_get_direction ((GIArgInfo *)self->info));
841 }
842
843 static PyObject *
844 _wrap_gi_arg_info_is_caller_allocates (PyGIBaseInfo *self)
845 {
846 return pygi_gboolean_to_py (
847 gi_arg_info_is_caller_allocates ((GIArgInfo *)self->info));
848 }
849
850 static PyObject *
851 _wrap_gi_arg_info_is_return_value (PyGIBaseInfo *self)
852 {
853 return pygi_gboolean_to_py (
854 gi_arg_info_is_return_value ((GIArgInfo *)self->info));
855 }
856
857 static PyObject *
858 _wrap_gi_arg_info_is_optional (PyGIBaseInfo *self)
859 {
860 return pygi_gboolean_to_py (
861 gi_arg_info_is_optional ((GIArgInfo *)self->info));
862 }
863
864 static PyObject *
865 _wrap_gi_arg_info_may_be_null (PyGIBaseInfo *self)
866 {
867 return pygi_gboolean_to_py (
868 gi_arg_info_may_be_null ((GIArgInfo *)self->info));
869 }
870
871 static PyObject *
872 _wrap_gi_arg_info_get_ownership_transfer (PyGIBaseInfo *self)
873 {
874 return pygi_guint_to_py (
875 gi_arg_info_get_ownership_transfer ((GIArgInfo *)self->info));
876 }
877
878 static PyObject *
879 _wrap_gi_arg_info_get_scope (PyGIBaseInfo *self)
880 {
881 return pygi_guint_to_py (gi_arg_info_get_scope ((GIArgInfo *)self->info));
882 }
883
884 static PyObject *
885 _wrap_gi_arg_info_get_closure_index (PyGIBaseInfo *self)
886 {
887 unsigned int closure_index;
888
889 if (gi_arg_info_get_closure_index ((GIArgInfo *)self->info,
890 &closure_index))
891 return pygi_guint_to_py (closure_index);
892 else
893 return pygi_gint_to_py (-1);
894 }
895
896 static PyObject *
897 _wrap_gi_arg_info_get_destroy_index (PyGIBaseInfo *self)
898 {
899 unsigned int destroy_index;
900 if (gi_arg_info_get_destroy_index ((GIArgInfo *)self->info,
901 &destroy_index))
902 return pygi_guint_to_py (destroy_index);
903 else
904 return pygi_gint_to_py (-1);
905 }
906
907 static PyObject *
908 _wrap_gi_arg_info_get_type_info (PyGIBaseInfo *self)
909 {
910 return _get_child_info (self,
911 (GetChildInfoCallback)gi_arg_info_get_type_info);
912 }
913
914 static PyMethodDef _PyGIArgInfo_methods[] = {
915 { "get_direction", (PyCFunction)_wrap_gi_arg_info_get_direction,
916 METH_NOARGS },
917 { "is_caller_allocates",
918 (PyCFunction)_wrap_gi_arg_info_is_caller_allocates, METH_NOARGS },
919 { "is_return_value", (PyCFunction)_wrap_gi_arg_info_is_return_value,
920 METH_NOARGS },
921 { "is_optional", (PyCFunction)_wrap_gi_arg_info_is_optional, METH_NOARGS },
922 { "may_be_null", (PyCFunction)_wrap_gi_arg_info_may_be_null, METH_NOARGS },
923 { "get_ownership_transfer",
924 (PyCFunction)_wrap_gi_arg_info_get_ownership_transfer, METH_NOARGS },
925 { "get_scope", (PyCFunction)_wrap_gi_arg_info_get_scope, METH_NOARGS },
926 { "get_closure_index", (PyCFunction)_wrap_gi_arg_info_get_closure_index,
927 METH_NOARGS },
928 { "get_destroy_index", (PyCFunction)_wrap_gi_arg_info_get_destroy_index,
929 METH_NOARGS },
930 { "get_type_info", (PyCFunction)_wrap_gi_arg_info_get_type_info,
931 METH_NOARGS },
932 { NULL, NULL, 0 },
933 };
934
935
936 /* TypeInfo */
937 PYGI_DEFINE_TYPE ("gi.TypeInfo", PyGITypeInfo_Type, PyGIBaseInfo);
938
939 static PyObject *
940 _wrap_gi_type_info_is_pointer (PyGIBaseInfo *self)
941 {
942 return pygi_gboolean_to_py (
943 gi_type_info_is_pointer (GI_TYPE_INFO (self->info)));
944 }
945
946 static PyObject *
947 _wrap_gi_type_info_get_tag (PyGIBaseInfo *self)
948 {
949 return pygi_guint_to_py (gi_type_info_get_tag (GI_TYPE_INFO (self->info)));
950 }
951
952 static PyObject *
953 _wrap_gi_type_info_get_tag_as_string (PyGIBaseInfo *self)
954 {
955 GITypeTag tag = gi_type_info_get_tag (GI_TYPE_INFO (self->info));
956 return pygi_utf8_to_py (gi_type_tag_to_string (tag));
957 }
958
959 static PyObject *
960 _wrap_gi_type_info_get_param_type (PyGIBaseInfo *self, PyObject *py_n)
961 {
962 GIBaseInfo *info;
963 PyObject *py_info;
964 gint n;
965
966 if (!pygi_gint_from_py (py_n, &n)) return NULL;
967
968 info = (GIBaseInfo *)gi_type_info_get_param_type ((GITypeInfo *)self->info,
969 n);
970 if (info == NULL) {
971 Py_RETURN_NONE;
972 }
973
974 py_info = _pygi_info_new (info);
975 gi_base_info_unref (info);
976 return py_info;
977 }
978
979 static PyObject *
980 _wrap_gi_type_info_get_interface (PyGIBaseInfo *self)
981 {
982 return _get_child_info (self,
983 (GetChildInfoCallback)gi_type_info_get_interface);
984 }
985
986 static PyObject *
987 _wrap_gi_type_info_get_array_length_index (PyGIBaseInfo *self)
988 {
989 unsigned int idx;
990
991 if (gi_type_info_get_array_length_index (GI_TYPE_INFO (self->info), &idx))
992 return pygi_guint_to_py (idx);
993 else
994 return pygi_gint_to_py (-1);
995 }
996
997 static PyObject *
998 _wrap_gi_type_info_get_array_fixed_size (PyGIBaseInfo *self)
999 {
1000 size_t size;
1001 if (gi_type_info_get_array_fixed_size (GI_TYPE_INFO (self->info), &size))
1002 return pygi_gsize_to_py (size);
1003
1004 g_assert_not_reached ();
1005 }
1006
1007 static PyObject *
1008 _wrap_gi_type_info_is_zero_terminated (PyGIBaseInfo *self)
1009 {
1010 return pygi_gboolean_to_py (
1011 gi_type_info_is_zero_terminated (GI_TYPE_INFO (self->info)));
1012 }
1013
1014 static PyObject *
1015 _wrap_gi_type_info_get_array_type (PyGIBaseInfo *self)
1016 {
1017 return pygi_guint_to_py (
1018 gi_type_info_get_array_type (GI_TYPE_INFO (self->info)));
1019 }
1020
1021 static PyMethodDef _PyGITypeInfo_methods[] = {
1022 { "is_pointer", (PyCFunction)_wrap_gi_type_info_is_pointer, METH_NOARGS },
1023 { "get_tag", (PyCFunction)_wrap_gi_type_info_get_tag, METH_NOARGS },
1024 { "get_tag_as_string", (PyCFunction)_wrap_gi_type_info_get_tag_as_string,
1025 METH_NOARGS },
1026 { "get_param_type", (PyCFunction)_wrap_gi_type_info_get_param_type,
1027 METH_O },
1028 { "get_interface", (PyCFunction)_wrap_gi_type_info_get_interface,
1029 METH_NOARGS },
1030 { "get_array_length_index",
1031 (PyCFunction)_wrap_gi_type_info_get_array_length_index, METH_NOARGS },
1032 { "get_array_fixed_size",
1033 (PyCFunction)_wrap_gi_type_info_get_array_fixed_size, METH_NOARGS },
1034 { "is_zero_terminated", (PyCFunction)_wrap_gi_type_info_is_zero_terminated,
1035 METH_NOARGS },
1036 { "get_array_type", (PyCFunction)_wrap_gi_type_info_get_array_type,
1037 METH_NOARGS },
1038 { NULL, NULL, 0 },
1039 };
1040
1041
1042 /* FunctionInfo */
1043 PYGI_DEFINE_TYPE ("gi.FunctionInfo", PyGIFunctionInfo_Type, PyGICallableInfo);
1044
1045 static PyObject *
1046 _wrap_gi_function_info_is_constructor (PyGIBaseInfo *self)
1047 {
1048 GIFunctionInfoFlags flags;
1049 gboolean is_constructor;
1050
1051 flags = gi_function_info_get_flags ((GIFunctionInfo *)self->info);
1052 is_constructor = flags & GI_FUNCTION_IS_CONSTRUCTOR;
1053
1054 return pygi_gboolean_to_py (is_constructor);
1055 }
1056
1057 static PyObject *
1058 _wrap_gi_function_info_is_method (PyGIBaseInfo *self)
1059 {
1060 GIFunctionInfoFlags flags;
1061 gboolean is_method;
1062
1063 flags = gi_function_info_get_flags ((GIFunctionInfo *)self->info);
1064 is_method = flags & GI_FUNCTION_IS_METHOD;
1065
1066 return pygi_gboolean_to_py (is_method);
1067 }
1068
1069 gsize
1070 _pygi_g_type_tag_size (GITypeTag type_tag)
1071 {
1072 gsize size = 0;
1073
1074 switch (type_tag) {
1075 case GI_TYPE_TAG_BOOLEAN:
1076 size = sizeof (gboolean);
1077 break;
1078 case GI_TYPE_TAG_INT8:
1079 case GI_TYPE_TAG_UINT8:
1080 size = sizeof (gint8);
1081 break;
1082 case GI_TYPE_TAG_INT16:
1083 case GI_TYPE_TAG_UINT16:
1084 size = sizeof (gint16);
1085 break;
1086 case GI_TYPE_TAG_INT32:
1087 case GI_TYPE_TAG_UINT32:
1088 size = sizeof (gint32);
1089 break;
1090 case GI_TYPE_TAG_INT64:
1091 case GI_TYPE_TAG_UINT64:
1092 size = sizeof (gint64);
1093 break;
1094 case GI_TYPE_TAG_FLOAT:
1095 size = sizeof (gfloat);
1096 break;
1097 case GI_TYPE_TAG_DOUBLE:
1098 size = sizeof (gdouble);
1099 break;
1100 case GI_TYPE_TAG_GTYPE:
1101 size = sizeof (GType);
1102 break;
1103 case GI_TYPE_TAG_UNICHAR:
1104 size = sizeof (gunichar);
1105 break;
1106 case GI_TYPE_TAG_VOID:
1107 case GI_TYPE_TAG_UTF8:
1108 case GI_TYPE_TAG_FILENAME:
1109 case GI_TYPE_TAG_ARRAY:
1110 case GI_TYPE_TAG_INTERFACE:
1111 case GI_TYPE_TAG_GLIST:
1112 case GI_TYPE_TAG_GSLIST:
1113 case GI_TYPE_TAG_GHASH:
1114 case GI_TYPE_TAG_ERROR:
1115 PyErr_Format (PyExc_TypeError,
1116 "Unable to know the size (assuming %s is not a pointer)",
1117 gi_type_tag_to_string (type_tag));
1118 break;
1119 default:
1120 g_assert_not_reached ();
1121 }
1122
1123 return size;
1124 }
1125
1126 gsize
1127 _pygi_gi_type_info_size (GITypeInfo *type_info)
1128 {
1129 gsize size = 0;
1130
1131 GITypeTag type_tag;
1132
1133 type_tag = gi_type_info_get_tag (type_info);
1134 switch (type_tag) {
1135 case GI_TYPE_TAG_BOOLEAN:
1136 case GI_TYPE_TAG_INT8:
1137 case GI_TYPE_TAG_UINT8:
1138 case GI_TYPE_TAG_INT16:
1139 case GI_TYPE_TAG_UINT16:
1140 case GI_TYPE_TAG_INT32:
1141 case GI_TYPE_TAG_UINT32:
1142 case GI_TYPE_TAG_INT64:
1143 case GI_TYPE_TAG_UINT64:
1144 case GI_TYPE_TAG_FLOAT:
1145 case GI_TYPE_TAG_DOUBLE:
1146 case GI_TYPE_TAG_GTYPE:
1147 case GI_TYPE_TAG_UNICHAR:
1148 size = _pygi_g_type_tag_size (type_tag);
1149 g_assert (size > 0);
1150 break;
1151 case GI_TYPE_TAG_INTERFACE: {
1152 GIBaseInfo *info;
1153
1154 info = gi_type_info_get_interface (type_info);
1155
1156 switch (pygi_interface_type_tag (info)) {
1157 case PYGI_INTERFACE_TYPE_TAG_STRUCT:
1158 if (gi_type_info_is_pointer (type_info)) {
1159 size = sizeof (gpointer);
1160 } else {
1161 size = gi_struct_info_get_size ((GIStructInfo *)info);
1162 }
1163 break;
1164 case PYGI_INTERFACE_TYPE_TAG_UNION:
1165 if (gi_type_info_is_pointer (type_info)) {
1166 size = sizeof (gpointer);
1167 } else {
1168 size = gi_union_info_get_size ((GIUnionInfo *)info);
1169 }
1170 break;
1171 case PYGI_INTERFACE_TYPE_TAG_ENUM:
1172 case PYGI_INTERFACE_TYPE_TAG_FLAGS:
1173 if (gi_type_info_is_pointer (type_info)) {
1174 size = sizeof (gpointer);
1175 } else {
1176 GITypeTag enum_type_tag;
1177
1178 enum_type_tag =
1179 gi_enum_info_get_storage_type ((GIEnumInfo *)info);
1180 size = _pygi_g_type_tag_size (enum_type_tag);
1181 }
1182 break;
1183 case PYGI_INTERFACE_TYPE_TAG_OBJECT:
1184 case PYGI_INTERFACE_TYPE_TAG_INTERFACE:
1185 case PYGI_INTERFACE_TYPE_TAG_CALLBACK:
1186 size = sizeof (gpointer);
1187 break;
1188 default:
1189 g_assert_not_reached ();
1190 }
1191
1192 gi_base_info_unref (info);
1193 break;
1194 }
1195 case GI_TYPE_TAG_ARRAY:
1196 case GI_TYPE_TAG_VOID:
1197 case GI_TYPE_TAG_UTF8:
1198 case GI_TYPE_TAG_FILENAME:
1199 case GI_TYPE_TAG_GLIST:
1200 case GI_TYPE_TAG_GSLIST:
1201 case GI_TYPE_TAG_GHASH:
1202 case GI_TYPE_TAG_ERROR:
1203 size = sizeof (gpointer);
1204 break;
1205 default:
1206 g_assert_not_reached ();
1207 }
1208
1209 return size;
1210 }
1211
1212 static PyObject *
1213 _wrap_gi_function_info_get_symbol (PyGIBaseInfo *self)
1214 {
1215 return _get_info_string (
1216 gi_function_info_get_symbol (GI_FUNCTION_INFO (self->info)));
1217 }
1218
1219 static PyObject *
1220 _wrap_gi_function_info_get_flags (PyGIBaseInfo *self)
1221 {
1222 return pygi_guint_to_py (
1223 gi_function_info_get_flags (GI_FUNCTION_INFO (self->info)));
1224 }
1225
1226 static PyObject *
1227 _wrap_gi_function_info_get_property (PyGIBaseInfo *self)
1228 {
1229 return _get_child_info (
1230 self, (GetChildInfoCallback)gi_function_info_get_property);
1231 }
1232
1233 static PyObject *
1234 _wrap_gi_function_info_get_vfunc (PyGIBaseInfo *self)
1235 {
1236 return _get_child_info (self,
1237 (GetChildInfoCallback)gi_function_info_get_vfunc);
1238 }
1239
1240 static PyObject *
1241 _wrap_gi_function_info_get_finish_func (PyGICallableInfo *self)
1242 {
1243 PyGIFunctionCache *cache = pygi_callable_info_get_cache (self);
1244
1245 if (PyErr_Occurred ()) return NULL;
1246
1247 if (cache == NULL) return NULL;
1248
1249 if (cache->async_finish) {
1250 Py_INCREF (cache->async_finish);
1251 return cache->async_finish;
1252 }
1253
1254 Py_RETURN_NONE;
1255 }
1256
1257 static PyMethodDef _PyGIFunctionInfo_methods[] = {
1258 { "is_constructor", (PyCFunction)_wrap_gi_function_info_is_constructor,
1259 METH_NOARGS },
1260 { "is_method", (PyCFunction)_wrap_gi_function_info_is_method,
1261 METH_NOARGS },
1262 { "get_symbol", (PyCFunction)_wrap_gi_function_info_get_symbol,
1263 METH_NOARGS },
1264 { "get_flags", (PyCFunction)_wrap_gi_function_info_get_flags,
1265 METH_NOARGS },
1266 { "get_property", (PyCFunction)_wrap_gi_function_info_get_property,
1267 METH_NOARGS },
1268 { "get_vfunc", (PyCFunction)_wrap_gi_function_info_get_vfunc,
1269 METH_NOARGS },
1270 { "get_finish_func", (PyCFunction)_wrap_gi_function_info_get_finish_func,
1271 METH_NOARGS },
1272 { NULL, NULL, 0 },
1273 };
1274
1275 /* RegisteredTypeInfo */
1276 PYGI_DEFINE_TYPE ("gi.RegisteredTypeInfo", PyGIRegisteredTypeInfo_Type,
1277 PyGIBaseInfo);
1278
1279 static PyObject *
1280 _wrap_gi_registered_type_info_get_type_name (PyGIBaseInfo *self)
1281 {
1282 return _get_info_string (gi_registered_type_info_get_type_name (
1283 GI_REGISTERED_TYPE_INFO (self->info)));
1284 }
1285
1286 static PyObject *
1287 _wrap_gi_registered_type_info_get_type_init (PyGIBaseInfo *self)
1288 {
1289 return _get_info_string (
1290 gi_registered_type_info_get_type_init_function_name (
1291 GI_REGISTERED_TYPE_INFO (self->info)));
1292 }
1293
1294 static PyObject *
1295 _wrap_gi_registered_type_info_get_g_type (PyGIBaseInfo *self)
1296 {
1297 GType type;
1298
1299 type = gi_registered_type_info_get_g_type (
1300 (GIRegisteredTypeInfo *)self->info);
1301
1302 return pyg_type_wrapper_new (type);
1303 }
1304
1305 static PyMethodDef _PyGIRegisteredTypeInfo_methods[] = {
1306 { "get_type_name",
1307 (PyCFunction)_wrap_gi_registered_type_info_get_type_name, METH_NOARGS },
1308 { "get_type_init",
1309 (PyCFunction)_wrap_gi_registered_type_info_get_type_init, METH_NOARGS },
1310 { "get_g_type", (PyCFunction)_wrap_gi_registered_type_info_get_g_type,
1311 METH_NOARGS },
1312 { NULL, NULL, 0 },
1313 };
1314
1315
1316 /* GIStructInfo */
1317 PYGI_DEFINE_TYPE ("StructInfo", PyGIStructInfo_Type, PyGIBaseInfo);
1318
1319 static PyObject *
1320 _wrap_gi_struct_info_get_fields (PyGIBaseInfo *self)
1321 {
1322 return _make_infos_tuple (self,
1323 (GetNInfosCallback)gi_struct_info_get_n_fields,
1324 (MakeInfosCallback)gi_struct_info_get_field);
1325 }
1326
1327 static PyObject *
1328 _wrap_gi_struct_info_get_methods (PyGIBaseInfo *self)
1329 {
1330 return _make_infos_tuple (self,
1331 (GetNInfosCallback)gi_struct_info_get_n_methods,
1332 (MakeInfosCallback)gi_struct_info_get_method);
1333 }
1334
1335 static PyObject *
1336 _wrap_gi_struct_info_get_size (PyGIBaseInfo *self)
1337 {
1338 return pygi_gsize_to_py (
1339 gi_struct_info_get_size (GI_STRUCT_INFO (self->info)));
1340 }
1341
1342 static PyObject *
1343 _wrap_gi_struct_info_get_alignment (PyGIBaseInfo *self)
1344 {
1345 return pygi_gsize_to_py (
1346 gi_struct_info_get_alignment (GI_STRUCT_INFO (self->info)));
1347 }
1348
1349 static PyObject *
1350 _wrap_gi_struct_info_is_gtype_struct (PyGIBaseInfo *self)
1351 {
1352 return pygi_gboolean_to_py (
1353 gi_struct_info_is_gtype_struct (GI_STRUCT_INFO (self->info)));
1354 }
1355
1356 static PyObject *
1357 _wrap_gi_struct_info_is_foreign (PyGIBaseInfo *self)
1358 {
1359 return pygi_gboolean_to_py (
1360 gi_struct_info_is_foreign (GI_STRUCT_INFO (self->info)));
1361 }
1362
1363 static PyObject *
1364 _wrap_gi_struct_info_find_method (PyGIBaseInfo *self, PyObject *py_name)
1365 {
1366 return _get_child_info_by_name (
1367 self, py_name, (GetChildInfoByNameCallback)gi_struct_info_find_method);
1368 }
1369
1370 static PyObject *
1371 _wrap_gi_struct_info_find_field (PyGIBaseInfo *self, PyObject *py_name)
1372 {
1373 return _get_child_info_by_name (
1374 self, py_name, (GetChildInfoByNameCallback)gi_struct_info_find_field);
1375 }
1376
1377 static PyMethodDef _PyGIStructInfo_methods[] = {
1378 { "get_fields", (PyCFunction)_wrap_gi_struct_info_get_fields,
1379 METH_NOARGS },
1380 { "find_field", (PyCFunction)_wrap_gi_struct_info_find_field, METH_O },
1381 { "get_methods", (PyCFunction)_wrap_gi_struct_info_get_methods,
1382 METH_NOARGS },
1383 { "find_method", (PyCFunction)_wrap_gi_struct_info_find_method, METH_O },
1384 { "get_size", (PyCFunction)_wrap_gi_struct_info_get_size, METH_NOARGS },
1385 { "get_alignment", (PyCFunction)_wrap_gi_struct_info_get_alignment,
1386 METH_NOARGS },
1387 { "is_gtype_struct", (PyCFunction)_wrap_gi_struct_info_is_gtype_struct,
1388 METH_NOARGS },
1389 { "is_foreign", (PyCFunction)_wrap_gi_struct_info_is_foreign,
1390 METH_NOARGS },
1391 { NULL, NULL, 0 },
1392 };
1393
1394 gboolean
1395 pygi_gi_struct_info_is_simple (GIStructInfo *struct_info)
1396 {
1397 gboolean is_simple;
1398 gint n_field_infos;
1399 gint i;
1400
1401 is_simple = TRUE;
1402
1403 n_field_infos = gi_struct_info_get_n_fields (struct_info);
1404
1405 for (i = 0; i < n_field_infos && is_simple; i++) {
1406 GIFieldInfo *field_info;
1407 GITypeInfo *field_type_info;
1408 GITypeTag field_type_tag;
1409
1410 field_info = gi_struct_info_get_field (struct_info, i);
1411 field_type_info = gi_field_info_get_type_info (field_info);
1412
1413
1414 field_type_tag = gi_type_info_get_tag (field_type_info);
1415
1416 switch (field_type_tag) {
1417 case GI_TYPE_TAG_BOOLEAN:
1418 case GI_TYPE_TAG_INT8:
1419 case GI_TYPE_TAG_UINT8:
1420 case GI_TYPE_TAG_INT16:
1421 case GI_TYPE_TAG_UINT16:
1422 case GI_TYPE_TAG_INT32:
1423 case GI_TYPE_TAG_UINT32:
1424 case GI_TYPE_TAG_INT64:
1425 case GI_TYPE_TAG_UINT64:
1426 case GI_TYPE_TAG_FLOAT:
1427 case GI_TYPE_TAG_DOUBLE:
1428 case GI_TYPE_TAG_UNICHAR:
1429 if (gi_type_info_is_pointer (field_type_info)) {
1430 is_simple = FALSE;
1431 }
1432 break;
1433 case GI_TYPE_TAG_VOID:
1434 case GI_TYPE_TAG_GTYPE:
1435 case GI_TYPE_TAG_ERROR:
1436 case GI_TYPE_TAG_UTF8:
1437 case GI_TYPE_TAG_FILENAME:
1438 case GI_TYPE_TAG_ARRAY:
1439 case GI_TYPE_TAG_GLIST:
1440 case GI_TYPE_TAG_GSLIST:
1441 case GI_TYPE_TAG_GHASH:
1442 is_simple = FALSE;
1443 break;
1444 case GI_TYPE_TAG_INTERFACE: {
1445 GIBaseInfo *info;
1446
1447 info = gi_type_info_get_interface (field_type_info);
1448
1449 switch (pygi_interface_type_tag (info)) {
1450 case PYGI_INTERFACE_TYPE_TAG_STRUCT:
1451 if (gi_type_info_is_pointer (field_type_info)) {
1452 is_simple = FALSE;
1453 } else {
1454 is_simple =
1455 pygi_gi_struct_info_is_simple ((GIStructInfo *)info);
1456 }
1457 break;
1458 case PYGI_INTERFACE_TYPE_TAG_UNION:
1459 /* TODO */
1460 is_simple = FALSE;
1461 break;
1462 case PYGI_INTERFACE_TYPE_TAG_ENUM:
1463 case PYGI_INTERFACE_TYPE_TAG_FLAGS:
1464 if (gi_type_info_is_pointer (field_type_info)) {
1465 is_simple = FALSE;
1466 }
1467 break;
1468 case PYGI_INTERFACE_TYPE_TAG_OBJECT:
1469 case PYGI_INTERFACE_TYPE_TAG_INTERFACE:
1470 case PYGI_INTERFACE_TYPE_TAG_CALLBACK:
1471 is_simple = FALSE;
1472 break;
1473 default:
1474 g_assert_not_reached ();
1475 }
1476
1477 gi_base_info_unref (info);
1478 break;
1479 }
1480 default:
1481 g_assert_not_reached ();
1482 }
1483
1484 gi_base_info_unref ((GIBaseInfo *)field_type_info);
1485 gi_base_info_unref ((GIBaseInfo *)field_info);
1486 }
1487
1488 return is_simple;
1489 }
1490
1491
1492 /* EnumInfo */
1493 PYGI_DEFINE_TYPE ("gi.EnumInfo", PyGIEnumInfo_Type, PyGIBaseInfo);
1494
1495 static PyObject *
1496 _wrap_gi_enum_info_get_values (PyGIBaseInfo *self)
1497 {
1498 return _make_infos_tuple (self,
1499 (GetNInfosCallback)gi_enum_info_get_n_values,
1500 (MakeInfosCallback)gi_enum_info_get_value);
1501 }
1502
1503 static PyObject *
1504 _wrap_gi_enum_info_is_flags (PyGIBaseInfo *self)
1505 {
1506 if (GI_IS_FLAGS_INFO (self->info)) {
1507 /* Check flags before enums: flags are a subtype of enum. */
1508 Py_RETURN_TRUE;
1509 } else if (GI_IS_ENUM_INFO (self->info)) {
1510 Py_RETURN_FALSE;
1511 } else {
1512 g_assert_not_reached ();
1513 }
1514 }
1515
1516 static PyObject *
1517 _wrap_gi_enum_info_get_methods (PyGIBaseInfo *self)
1518 {
1519 return _make_infos_tuple (self,
1520 (GetNInfosCallback)gi_enum_info_get_n_methods,
1521 (MakeInfosCallback)gi_enum_info_get_method);
1522 }
1523
1524 static PyObject *
1525 _wrap_gi_enum_info_get_storage_type (PyGIBaseInfo *self)
1526 {
1527 return pygi_guint_to_py (
1528 gi_enum_info_get_storage_type (GI_ENUM_INFO (self->info)));
1529 }
1530
1531 static PyMethodDef _PyGIEnumInfo_methods[] = {
1532 { "get_values", (PyCFunction)_wrap_gi_enum_info_get_values, METH_NOARGS },
1533 { "is_flags", (PyCFunction)_wrap_gi_enum_info_is_flags, METH_NOARGS },
1534 { "get_methods", (PyCFunction)_wrap_gi_enum_info_get_methods,
1535 METH_NOARGS },
1536 { "get_storage_type", (PyCFunction)_wrap_gi_enum_info_get_storage_type,
1537 METH_NOARGS },
1538 { NULL, NULL, 0 },
1539 };
1540
1541
1542 /* ObjectInfo */
1543 PYGI_DEFINE_TYPE ("ObjectInfo", PyGIObjectInfo_Type, PyGIBaseInfo);
1544
1545 static PyObject *
1546 _wrap_gi_object_info_get_parent (PyGIBaseInfo *self)
1547 {
1548 return _get_child_info (self,
1549 (GetChildInfoCallback)gi_object_info_get_parent);
1550 }
1551
1552 static PyObject *
1553 _wrap_gi_object_info_get_methods (PyGIBaseInfo *self)
1554 {
1555 return _make_infos_tuple (self,
1556 (GetNInfosCallback)gi_object_info_get_n_methods,
1557 (MakeInfosCallback)gi_object_info_get_method);
1558 }
1559
1560 static PyObject *
1561 _wrap_gi_object_info_find_method (PyGIBaseInfo *self, PyObject *py_name)
1562 {
1563 return _get_child_info_by_name (
1564 self, py_name, (GetChildInfoByNameCallback)gi_object_info_find_method);
1565 }
1566
1567 static PyObject *
1568 _wrap_gi_object_info_get_fields (PyGIBaseInfo *self)
1569 {
1570 return _make_infos_tuple (self,
1571 (GetNInfosCallback)gi_object_info_get_n_fields,
1572 (MakeInfosCallback)gi_object_info_get_field);
1573 }
1574
1575 static PyObject *
1576 _wrap_gi_object_info_get_properties (PyGIBaseInfo *self)
1577 {
1578 return _make_infos_tuple (
1579 self, (GetNInfosCallback)gi_object_info_get_n_properties,
1580 (MakeInfosCallback)gi_object_info_get_property);
1581 }
1582
1583 static PyObject *
1584 _wrap_gi_object_info_get_signals (PyGIBaseInfo *self)
1585 {
1586 return _make_infos_tuple (self,
1587 (GetNInfosCallback)gi_object_info_get_n_signals,
1588 (MakeInfosCallback)gi_object_info_get_signal);
1589 }
1590
1591 static PyObject *
1592 _wrap_gi_object_info_get_interfaces (PyGIBaseInfo *self)
1593 {
1594 return _make_infos_tuple (
1595 self, (GetNInfosCallback)gi_object_info_get_n_interfaces,
1596 (MakeInfosCallback)gi_object_info_get_interface);
1597 }
1598
1599 static PyObject *
1600 _wrap_gi_object_info_get_constants (PyGIBaseInfo *self)
1601 {
1602 return _make_infos_tuple (
1603 self, (GetNInfosCallback)gi_object_info_get_n_constants,
1604 (MakeInfosCallback)gi_object_info_get_constant);
1605 }
1606
1607 static PyObject *
1608 _wrap_gi_object_info_get_vfuncs (PyGIBaseInfo *self)
1609 {
1610 return _make_infos_tuple (self,
1611 (GetNInfosCallback)gi_object_info_get_n_vfuncs,
1612 (MakeInfosCallback)gi_object_info_get_vfunc);
1613 }
1614
1615 static PyObject *
1616 _wrap_gi_object_info_get_abstract (PyGIBaseInfo *self)
1617 {
1618 gboolean is_abstract =
1619 gi_object_info_get_abstract ((GIObjectInfo *)self->info);
1620 return pygi_gboolean_to_py (is_abstract);
1621 }
1622
1623 static PyObject *
1624 _wrap_gi_object_info_get_type_name (PyGIBaseInfo *self)
1625 {
1626 return _get_info_string (
1627 gi_object_info_get_type_name (GI_OBJECT_INFO (self->info)));
1628 }
1629
1630 static PyObject *
1631 _wrap_gi_object_info_get_type_init (PyGIBaseInfo *self)
1632 {
1633 return _get_info_string (gi_object_info_get_type_init_function_name (
1634 GI_OBJECT_INFO (self->info)));
1635 }
1636
1637 static PyObject *
1638 _wrap_gi_object_info_get_fundamental (PyGIBaseInfo *self)
1639 {
1640 return pygi_gboolean_to_py (
1641 gi_object_info_get_fundamental ((GIObjectInfo *)self->info));
1642 }
1643
1644 static PyObject *
1645 _wrap_gi_object_info_get_class_struct (PyGIBaseInfo *self)
1646 {
1647 return _get_child_info (
1648 self, (GetChildInfoCallback)gi_object_info_get_class_struct);
1649 }
1650
1651 static PyObject *
1652 _wrap_gi_object_info_find_vfunc (PyGIBaseInfo *self, PyObject *py_name)
1653 {
1654 return _get_child_info_by_name (
1655 self, py_name, (GetChildInfoByNameCallback)gi_object_info_find_vfunc);
1656 }
1657
1658 static PyObject *
1659 _wrap_gi_object_info_get_unref_function (PyGIBaseInfo *self)
1660 {
1661 return _get_info_string (
1662 gi_object_info_get_unref_function_name (GI_OBJECT_INFO (self->info)));
1663 }
1664
1665 static PyObject *
1666 _wrap_gi_object_info_get_ref_function (PyGIBaseInfo *self)
1667 {
1668 return _get_info_string (
1669 gi_object_info_get_ref_function_name (GI_OBJECT_INFO (self->info)));
1670 }
1671
1672 static PyObject *
1673 _wrap_gi_object_info_get_set_value_function (PyGIBaseInfo *self)
1674 {
1675 return _get_info_string (gi_object_info_get_set_value_function_name (
1676 GI_OBJECT_INFO (self->info)));
1677 }
1678
1679 static PyObject *
1680 _wrap_gi_object_info_get_get_value_function (PyGIBaseInfo *self)
1681 {
1682 return _get_info_string (gi_object_info_get_get_value_function_name (
1683 GI_OBJECT_INFO (self->info)));
1684 }
1685
1686 static PyMethodDef _PyGIObjectInfo_methods[] = {
1687 { "get_parent", (PyCFunction)_wrap_gi_object_info_get_parent,
1688 METH_NOARGS },
1689 { "get_methods", (PyCFunction)_wrap_gi_object_info_get_methods,
1690 METH_NOARGS },
1691 { "find_method", (PyCFunction)_wrap_gi_object_info_find_method, METH_O },
1692 { "get_fields", (PyCFunction)_wrap_gi_object_info_get_fields,
1693 METH_NOARGS },
1694 { "get_properties", (PyCFunction)_wrap_gi_object_info_get_properties,
1695 METH_NOARGS },
1696 { "get_signals", (PyCFunction)_wrap_gi_object_info_get_signals,
1697 METH_NOARGS },
1698 { "get_interfaces", (PyCFunction)_wrap_gi_object_info_get_interfaces,
1699 METH_NOARGS },
1700 { "get_constants", (PyCFunction)_wrap_gi_object_info_get_constants,
1701 METH_NOARGS },
1702 { "get_vfuncs", (PyCFunction)_wrap_gi_object_info_get_vfuncs,
1703 METH_NOARGS },
1704 { "find_vfunc", (PyCFunction)_wrap_gi_object_info_find_vfunc, METH_O },
1705 { "get_abstract", (PyCFunction)_wrap_gi_object_info_get_abstract,
1706 METH_NOARGS },
1707 { "get_type_name", (PyCFunction)_wrap_gi_object_info_get_type_name,
1708 METH_NOARGS },
1709 { "get_type_init", (PyCFunction)_wrap_gi_object_info_get_type_init,
1710 METH_NOARGS },
1711 { "get_fundamental", (PyCFunction)_wrap_gi_object_info_get_fundamental,
1712 METH_NOARGS },
1713 { "get_class_struct", (PyCFunction)_wrap_gi_object_info_get_class_struct,
1714 METH_NOARGS },
1715 { "get_unref_function",
1716 (PyCFunction)_wrap_gi_object_info_get_unref_function, METH_NOARGS },
1717 { "get_ref_function", (PyCFunction)_wrap_gi_object_info_get_ref_function,
1718 METH_NOARGS },
1719 { "get_set_value_function",
1720 (PyCFunction)_wrap_gi_object_info_get_set_value_function, METH_NOARGS },
1721 { "get_get_value_function",
1722 (PyCFunction)_wrap_gi_object_info_get_get_value_function, METH_NOARGS },
1723 { NULL, NULL, 0 },
1724 };
1725
1726
1727 /* GIInterfaceInfo */
1728 PYGI_DEFINE_TYPE ("InterfaceInfo", PyGIInterfaceInfo_Type, PyGIBaseInfo);
1729
1730 static PyObject *
1731 _wrap_gi_interface_info_get_methods (PyGIBaseInfo *self)
1732 {
1733 return _make_infos_tuple (
1734 self, (GetNInfosCallback)gi_interface_info_get_n_methods,
1735 (MakeInfosCallback)gi_interface_info_get_method);
1736 }
1737
1738 static PyObject *
1739 _wrap_gi_interface_info_find_method (PyGIBaseInfo *self, PyObject *py_name)
1740 {
1741 return _get_child_info_by_name (
1742 self, py_name,
1743 (GetChildInfoByNameCallback)gi_interface_info_find_method);
1744 }
1745
1746 static PyObject *
1747 _wrap_gi_interface_info_get_constants (PyGIBaseInfo *self)
1748 {
1749 return _make_infos_tuple (
1750 self, (GetNInfosCallback)gi_interface_info_get_n_constants,
1751 (MakeInfosCallback)gi_interface_info_get_constant);
1752 }
1753
1754 static PyObject *
1755 _wrap_gi_interface_info_get_vfuncs (PyGIBaseInfo *self)
1756 {
1757 return _make_infos_tuple (
1758 self, (GetNInfosCallback)gi_interface_info_get_n_vfuncs,
1759 (MakeInfosCallback)gi_interface_info_get_vfunc);
1760 }
1761
1762 static PyObject *
1763 _wrap_gi_interface_info_find_vfunc (PyGIBaseInfo *self, PyObject *py_name)
1764 {
1765 return _get_child_info_by_name (
1766 self, py_name,
1767 (GetChildInfoByNameCallback)gi_interface_info_find_vfunc);
1768 }
1769
1770 static PyObject *
1771 _wrap_gi_interface_info_get_prerequisites (PyGIBaseInfo *self)
1772 {
1773 return _make_infos_tuple (
1774 self, (GetNInfosCallback)gi_interface_info_get_n_prerequisites,
1775 (MakeInfosCallback)gi_interface_info_get_prerequisite);
1776 }
1777
1778 static PyObject *
1779 _wrap_gi_interface_info_get_properties (PyGIBaseInfo *self)
1780 {
1781 return _make_infos_tuple (
1782 self, (GetNInfosCallback)gi_interface_info_get_n_properties,
1783 (MakeInfosCallback)gi_interface_info_get_property);
1784 }
1785
1786 static PyObject *
1787 _wrap_gi_interface_info_get_iface_struct (PyGIBaseInfo *self)
1788 {
1789 return _get_child_info (
1790 self, (GetChildInfoCallback)gi_interface_info_get_iface_struct);
1791 }
1792
1793 static PyObject *
1794 _wrap_gi_interface_info_get_signals (PyGIBaseInfo *self)
1795 {
1796 return _make_infos_tuple (
1797 self, (GetNInfosCallback)gi_interface_info_get_n_signals,
1798 (MakeInfosCallback)gi_interface_info_get_signal);
1799 }
1800
1801 static PyObject *
1802 _wrap_gi_interface_info_find_signal (PyGIBaseInfo *self, PyObject *py_name)
1803 {
1804 return _get_child_info_by_name (
1805 self, py_name,
1806 (GetChildInfoByNameCallback)gi_interface_info_find_signal);
1807 }
1808
1809 static PyMethodDef _PyGIInterfaceInfo_methods[] = {
1810 { "get_prerequisites",
1811 (PyCFunction)_wrap_gi_interface_info_get_prerequisites, METH_NOARGS },
1812 { "get_properties", (PyCFunction)_wrap_gi_interface_info_get_properties,
1813 METH_NOARGS },
1814 { "get_methods", (PyCFunction)_wrap_gi_interface_info_get_methods,
1815 METH_NOARGS },
1816 { "find_method", (PyCFunction)_wrap_gi_interface_info_find_method,
1817 METH_O },
1818 { "get_signals", (PyCFunction)_wrap_gi_interface_info_get_signals,
1819 METH_NOARGS },
1820 { "find_signal", (PyCFunction)_wrap_gi_interface_info_find_signal,
1821 METH_O },
1822 { "get_vfuncs", (PyCFunction)_wrap_gi_interface_info_get_vfuncs,
1823 METH_NOARGS },
1824 { "get_constants", (PyCFunction)_wrap_gi_interface_info_get_constants,
1825 METH_NOARGS },
1826 { "get_iface_struct",
1827 (PyCFunction)_wrap_gi_interface_info_get_iface_struct, METH_NOARGS },
1828 { "find_vfunc", (PyCFunction)_wrap_gi_interface_info_find_vfunc, METH_O },
1829 { NULL, NULL, 0 },
1830 };
1831
1832 /* GIConstantInfo */
1833 PYGI_DEFINE_TYPE ("gi.ConstantInfo", PyGIConstantInfo_Type, PyGIBaseInfo);
1834
1835 static PyObject *
1836 _wrap_gi_constant_info_get_value (PyGIBaseInfo *self)
1837 {
1838 GIConstantInfo *info = GI_CONSTANT_INFO (self->info);
1839 GITypeInfo *type_info = gi_constant_info_get_type_info (info);
1840 GIArgument value = PYGI_ARG_INIT;
1841 PyObject *object;
1842
1843 gi_constant_info_get_value (info, &value);
1844
1845 object = pygi_argument_to_py (type_info, value, GI_TRANSFER_NOTHING);
1846
1847 gi_constant_info_free_value (info, &value);
1848 gi_base_info_unref (type_info);
1849
1850 return object;
1851 }
1852
1853 static PyMethodDef _PyGIConstantInfo_methods[] = {
1854 { "get_value", (PyCFunction)_wrap_gi_constant_info_get_value,
1855 METH_NOARGS },
1856 { NULL, NULL, 0 },
1857 };
1858
1859 /* GIValueInfo */
1860 PYGI_DEFINE_TYPE ("gi.ValueInfo", PyGIValueInfo_Type, PyGIBaseInfo);
1861
1862 static PyObject *
1863 _wrap_gi_value_info_get_value (PyGIBaseInfo *self)
1864 {
1865 gint64 value;
1866
1867 value = gi_value_info_get_value ((GIValueInfo *)self->info);
1868
1869 return pygi_gint64_to_py (value);
1870 }
1871
1872
1873 static PyMethodDef _PyGIValueInfo_methods[] = {
1874 { "get_value", (PyCFunction)_wrap_gi_value_info_get_value, METH_NOARGS },
1875 { NULL, NULL, 0 },
1876 };
1877
1878
1879 /* GIFieldInfo */
1880 PYGI_DEFINE_TYPE ("gi.FieldInfo", PyGIFieldInfo_Type, PyGIBaseInfo);
1881
1882 static gboolean
1883 field_array_length (GIFieldInfo *info, void *struct_data_ptr,
1884 gsize *array_length)
1885 {
1886 unsigned int length_index;
1887 GIFieldInfo *array_len_field;
1888 GIArgument arg = PYGI_ARG_INIT;
1889 GITypeInfo *type_info = gi_field_info_get_type_info (info);
1890 GIBaseInfo *container_info;
1891 gboolean result = TRUE;
1892
1893 if (!gi_type_info_get_array_length_index (type_info, &length_index)) {
1894 gi_base_info_unref (type_info);
1895 return FALSE;
1896 }
1897
1898 gi_base_info_unref (type_info);
1899
1900 container_info =
1901 gi_base_info_get_container ((GIBaseInfo *)info); /* borrowed! */
1902
1903 if (GI_IS_UNION_INFO (container_info)) {
1904 array_len_field = gi_union_info_get_field (
1905 (GIUnionInfo *)container_info, length_index);
1906 } else if (GI_IS_STRUCT_INFO (container_info)) {
1907 array_len_field = gi_struct_info_get_field (
1908 (GIStructInfo *)container_info, length_index);
1909 } else if (GI_IS_OBJECT_INFO (container_info)) {
1910 array_len_field = gi_object_info_get_field (
1911 (GIObjectInfo *)container_info, length_index);
1912 } else {
1913 /* Other types don't have fields. */
1914 g_assert_not_reached ();
1915 }
1916
1917 if (array_len_field == NULL) {
1918 return FALSE;
1919 }
1920
1921 if (gi_field_info_get_field (array_len_field, struct_data_ptr, &arg)) {
1922 GITypeInfo *array_len_type_info =
1923 gi_field_info_get_type_info (array_len_field);
1924
1925 if (array_len_type_info != NULL) {
1926 result = pygi_argument_to_gsize (
1927 arg, gi_type_info_get_tag (array_len_type_info), array_length);
1928 gi_base_info_unref (array_len_type_info);
1929 }
1930 }
1931
1932 gi_base_info_unref (array_len_field);
1933 return result;
1934 }
1935
1936 static gint
1937 _pygi_gi_registered_type_info_check_object (GIRegisteredTypeInfo *info,
1938 PyObject *object)
1939 {
1940 gint retval;
1941
1942 GType g_type;
1943 PyObject *py_type;
1944 gchar *type_name_expected = NULL;
1945
1946 if ((GI_IS_STRUCT_INFO (info))
1947 && (gi_struct_info_is_foreign ((GIStructInfo *)info))) {
1948 /* TODO: Could we check is the correct foreign type? */
1949 return 1;
1950 }
1951
1952 g_type = gi_registered_type_info_get_g_type (info);
1953 if (g_type != G_TYPE_NONE) {
1954 py_type = pygi_type_get_from_g_type (g_type);
1955 } else {
1956 py_type = pygi_type_import_by_gi_info ((GIBaseInfo *)info);
1957 }
1958
1959 if (py_type == NULL) {
1960 return 0;
1961 }
1962
1963 g_assert (PyType_Check (py_type));
1964
1965 retval = PyObject_IsInstance (object, py_type);
1966 if (!retval) {
1967 type_name_expected =
1968 _pygi_gi_base_info_get_fullname ((GIBaseInfo *)info);
1969 }
1970
1971 Py_DECREF (py_type);
1972
1973 if (!retval) {
1974 PyTypeObject *object_type;
1975
1976 if (type_name_expected == NULL) {
1977 return -1;
1978 }
1979
1980 object_type = (PyTypeObject *)PyObject_Type (object);
1981 if (object_type == NULL) {
1982 g_free (type_name_expected);
1983 return -1;
1984 }
1985
1986 PyErr_Format (PyExc_TypeError, "Must be %s, not %s",
1987 type_name_expected, object_type->tp_name);
1988
1989 g_free (type_name_expected);
1990 }
1991
1992 return retval;
1993 }
1994
1995 static PyObject *
1996 _wrap_gi_field_info_get_value (PyGIBaseInfo *self, PyObject *args)
1997 {
1998 PyObject *instance;
1999 GIBaseInfo *container_info;
2000 gpointer container;
2001 GITypeInfo *field_type_info;
2002 GIArgument value = PYGI_ARG_INIT;
2003 PyObject *py_value = NULL;
2004 gsize array_length;
2005
2006 if (!PyArg_ParseTuple (args, "O:FieldInfo.get_value", &instance)) {
2007 return NULL;
2008 }
2009
2010 container_info = gi_base_info_get_container (self->info); /* borrowed! */
2011 g_assert (container_info != NULL);
2012
2013 /* Check the instance. */
2014 if (!_pygi_gi_registered_type_info_check_object (
2015 (GIRegisteredTypeInfo *)container_info, instance)) {
2016 _PyGI_ERROR_PREFIX ("argument 1: ");
2017 return NULL;
2018 }
2019
2020 /* Get the pointer to the container. */
2021 if (GI_IS_UNION_INFO (container_info)
2022 || GI_IS_STRUCT_INFO (container_info)) {
2023 container = pyg_boxed_get (instance, void);
2024 } else if (GI_IS_OBJECT_INFO (container_info)) {
2025 if (pygi_check_fundamental (container_info))
2026 container = pygi_fundamental_get (instance);
2027 else
2028 container = pygobject_get (instance);
2029 } else {
2030 /* Other types don't have fields. */
2031 g_assert_not_reached ();
2032 }
2033
2034 if (container == NULL) {
2035 PyErr_Format (PyExc_RuntimeError,
2036 "object at %p of type %s is not initialized", instance,
2037 Py_TYPE (instance)->tp_name);
2038 return NULL;
2039 }
2040
2041 /* Get the field's value. */
2042 field_type_info = gi_field_info_get_type_info (GI_FIELD_INFO (self->info));
2043
2044 /* A few types are not handled by gi_field_info_get_field, so do it here. */
2045 if (!gi_type_info_is_pointer (field_type_info)
2046 && gi_type_info_get_tag (field_type_info) == GI_TYPE_TAG_INTERFACE) {
2047 GIBaseInfo *interface_info;
2048
2049 if (!(gi_field_info_get_flags ((GIFieldInfo *)self->info)
2050 & GI_FIELD_IS_READABLE)) {
2051 PyErr_SetString (PyExc_RuntimeError, "field is not readable");
2052 goto out;
2053 }
2054
2055 interface_info = gi_type_info_get_interface (field_type_info);
2056
2057 switch (pygi_interface_type_tag (interface_info)) {
2058 case PYGI_INTERFACE_TYPE_TAG_UNION:
2059 PyErr_SetString (PyExc_NotImplementedError,
2060 "getting an union is not supported yet");
2061 gi_base_info_unref (interface_info);
2062
2063 goto out;
2064 case PYGI_INTERFACE_TYPE_TAG_STRUCT: {
2065 gsize offset;
2066
2067 offset = gi_field_info_get_offset ((GIFieldInfo *)self->info);
2068
2069 value.v_pointer = (char *)container + offset;
2070
2071 gi_base_info_unref (interface_info);
2072
2073 goto argument_to_object;
2074 }
2075 case PYGI_INTERFACE_TYPE_TAG_ENUM:
2076 case PYGI_INTERFACE_TYPE_TAG_FLAGS:
2077 case PYGI_INTERFACE_TYPE_TAG_OBJECT:
2078 case PYGI_INTERFACE_TYPE_TAG_INTERFACE:
2079 case PYGI_INTERFACE_TYPE_TAG_CALLBACK:
2080 gi_base_info_unref (interface_info);
2081 break;
2082 default:
2083 g_assert_not_reached ();
2084 }
2085 }
2086
2087 if (!gi_field_info_get_field ((GIFieldInfo *)self->info, container,
2088 &value)) {
2089 PyErr_SetString (PyExc_RuntimeError, "unable to get the value");
2090 goto out;
2091 }
2092
2093 argument_to_object:
2094 if (field_array_length (GI_FIELD_INFO (self->info), container,
2095 &array_length))
2096 py_value = pygi_argument_to_py_with_array_length (
2097 field_type_info, value, GI_TRANSFER_NOTHING, array_length);
2098 else
2099 py_value =
2100 pygi_argument_to_py (field_type_info, value, GI_TRANSFER_NOTHING);
2101
2102 out:
2103 gi_base_info_unref ((GIBaseInfo *)field_type_info);
2104
2105 return py_value;
2106 }
2107
2108 static PyObject *
2109 _wrap_gi_field_info_set_value (PyGIBaseInfo *self, PyObject *args)
2110 {
2111 PyGIArgumentFromPyCleanupData arg_cleanup = { 0 };
2112 PyObject *instance;
2113 PyObject *py_value;
2114 GIBaseInfo *container_info;
2115 gpointer pointer;
2116 GITypeInfo *field_type_info;
2117 GIArgument value;
2118 PyObject *retval = NULL;
2119
2120 if (!PyArg_ParseTuple (args, "OO:FieldInfo.set_value", &instance,
2121 &py_value)) {
2122 return NULL;
2123 }
2124
2125 container_info = gi_base_info_get_container (self->info);
2126 g_assert (container_info != NULL);
2127
2128 /* Check the instance. */
2129 if (!_pygi_gi_registered_type_info_check_object (
2130 (GIRegisteredTypeInfo *)container_info, instance)) {
2131 _PyGI_ERROR_PREFIX ("argument 1: ");
2132 return NULL;
2133 }
2134
2135 /* Get the pointer to the container. */
2136 if (GI_IS_UNION_INFO (container_info)
2137 || GI_IS_STRUCT_INFO (container_info)) {
2138 pointer = pyg_boxed_get (instance, void);
2139 } else if (GI_IS_OBJECT_INFO (container_info)) {
2140 if (pygi_check_fundamental (container_info))
2141 pointer = pygi_fundamental_get (instance);
2142 else
2143 pointer = pygobject_get (instance);
2144 } else {
2145 /* Other types don't have fields. */
2146 g_assert_not_reached ();
2147 }
2148
2149 if (pointer == NULL) {
2150 PyErr_Format (PyExc_RuntimeError,
2151 "object at %p of type %s is not initialized", instance,
2152 Py_TYPE (instance)->tp_name);
2153 return NULL;
2154 }
2155
2156 field_type_info = gi_field_info_get_type_info ((GIFieldInfo *)self->info);
2157
2158 /* Set the field's value. */
2159 /* A few types are not handled by gi_field_info_set_field, so do it here. */
2160 if (!gi_type_info_is_pointer (field_type_info)
2161 && gi_type_info_get_tag (field_type_info) == GI_TYPE_TAG_INTERFACE) {
2162 GIBaseInfo *info;
2163
2164 if (!(gi_field_info_get_flags ((GIFieldInfo *)self->info)
2165 & GI_FIELD_IS_WRITABLE)) {
2166 PyErr_SetString (PyExc_RuntimeError, "field is not writable");
2167 goto out;
2168 }
2169
2170 info = gi_type_info_get_interface (field_type_info);
2171
2172 if (GI_IS_UNION_INFO (info)) {
2173 PyErr_SetString (PyExc_NotImplementedError,
2174 "setting an union is not supported yet");
2175 goto out;
2176 } else if (GI_IS_STRUCT_INFO (info)) {
2177 gboolean is_simple;
2178 gsize offset;
2179 gssize size;
2180
2181 is_simple = pygi_gi_struct_info_is_simple ((GIStructInfo *)info);
2182
2183 if (!is_simple) {
2184 PyErr_SetString (PyExc_TypeError,
2185 "cannot set a structure which has no "
2186 "well-defined ownership transfer rules");
2187 gi_base_info_unref (info);
2188 goto out;
2189 }
2190
2191 /* transfer = nothing, since we will copy the memory and the struct has no pointer references. */
2192 value = pygi_argument_from_py (field_type_info, py_value,
2193 GI_TRANSFER_NOTHING, &arg_cleanup);
2194 if (PyErr_Occurred ()) {
2195 gi_base_info_unref (info);
2196 goto out;
2197 }
2198
2199 offset = gi_field_info_get_offset ((GIFieldInfo *)self->info);
2200 size = gi_struct_info_get_size ((GIStructInfo *)info);
2201 g_assert (size > 0);
2202
2203 memmove ((char *)pointer + offset, value.v_pointer, size);
2204
2205 gi_base_info_unref (info);
2206
2207 retval = Py_None;
2208 goto out;
2209 } else {
2210 /* Fallback. */
2211 }
2212
2213 gi_base_info_unref (info);
2214 } else if (gi_type_info_is_pointer (field_type_info)
2215 && (gi_type_info_get_tag (field_type_info) == GI_TYPE_TAG_VOID
2216 || gi_type_info_get_tag (field_type_info)
2217 == GI_TYPE_TAG_UTF8)) {
2218 size_t offset;
2219 GITransfer transfer = gi_type_info_get_tag (field_type_info)
2220 == GI_TYPE_TAG_VOID
2221 ? GI_TRANSFER_NOTHING
2222 : GI_TRANSFER_EVERYTHING;
2223
2224 value = pygi_argument_from_py (field_type_info, py_value, transfer,
2225 &arg_cleanup);
2226 if (PyErr_Occurred ()) {
2227 goto out;
2228 }
2229
2230 offset = gi_field_info_get_offset ((GIFieldInfo *)self->info);
2231 G_STRUCT_MEMBER (gpointer, pointer, offset) =
2232 (gpointer)value.v_pointer;
2233
2234 retval = Py_None;
2235 goto out;
2236 }
2237
2238 value = pygi_argument_from_py (field_type_info, py_value,
2239 GI_TRANSFER_EVERYTHING, &arg_cleanup);
2240 if (PyErr_Occurred ()) {
2241 goto out;
2242 }
2243
2244 if (!gi_field_info_set_field ((GIFieldInfo *)self->info, pointer,
2245 &value)) {
2246 PyErr_SetString (PyExc_RuntimeError, "unable to set value for field");
2247 goto out;
2248 }
2249
2250 retval = Py_None;
2251
2252 out:
2253 pygi_argument_from_py_cleanup (&arg_cleanup);
2254 gi_base_info_unref ((GIBaseInfo *)field_type_info);
2255
2256 Py_XINCREF (retval);
2257 return retval;
2258 }
2259
2260 static PyObject *
2261 _wrap_gi_field_info_get_flags (PyGIBaseInfo *self)
2262 {
2263 return pygi_guint_to_py (
2264 gi_field_info_get_flags (GI_FIELD_INFO (self->info)));
2265 }
2266
2267 static PyObject *
2268 _wrap_gi_field_info_get_size (PyGIBaseInfo *self)
2269 {
2270 return pygi_gsize_to_py (
2271 gi_field_info_get_size (GI_FIELD_INFO (self->info)));
2272 }
2273
2274 static PyObject *
2275 _wrap_gi_field_info_get_offset (PyGIBaseInfo *self)
2276 {
2277 return pygi_gsize_to_py (
2278 gi_field_info_get_offset (GI_FIELD_INFO (self->info)));
2279 }
2280
2281 static PyObject *
2282 _wrap_gi_field_info_get_type_info (PyGIBaseInfo *self)
2283 {
2284 return _get_child_info (self,
2285 (GetChildInfoCallback)gi_field_info_get_type_info);
2286 }
2287
2288 static PyMethodDef _PyGIFieldInfo_methods[] = {
2289 { "get_value", (PyCFunction)_wrap_gi_field_info_get_value, METH_VARARGS },
2290 { "set_value", (PyCFunction)_wrap_gi_field_info_set_value, METH_VARARGS },
2291 { "get_flags", (PyCFunction)_wrap_gi_field_info_get_flags, METH_VARARGS },
2292 { "get_size", (PyCFunction)_wrap_gi_field_info_get_size, METH_VARARGS },
2293 { "get_offset", (PyCFunction)_wrap_gi_field_info_get_offset,
2294 METH_VARARGS },
2295 { "get_type_info", (PyCFunction)_wrap_gi_field_info_get_type_info,
2296 METH_VARARGS },
2297 { NULL, NULL, 0 },
2298 };
2299
2300
2301 /* GIUnresolvedInfo */
2302 PYGI_DEFINE_TYPE ("gi.UnresolvedInfo", PyGIUnresolvedInfo_Type, PyGIBaseInfo);
2303
2304 static PyMethodDef _PyGIUnresolvedInfo_methods[] = {
2305 { NULL, NULL, 0 },
2306 };
2307
2308 /* GIVFuncInfo */
2309 PYGI_DEFINE_TYPE ("gi.VFuncInfo", PyGIVFuncInfo_Type, PyGICallableInfo);
2310
2311 static PyObject *
2312 _wrap_gi_vfunc_info_get_flags (PyGIBaseInfo *self)
2313 {
2314 return pygi_guint_to_py (
2315 gi_vfunc_info_get_flags ((GIVFuncInfo *)self->info));
2316 }
2317
2318 static PyObject *
2319 _wrap_gi_vfunc_info_get_offset (PyGIBaseInfo *self)
2320 {
2321 return pygi_gsize_to_py (
2322 gi_vfunc_info_get_offset ((GIVFuncInfo *)self->info));
2323 }
2324
2325 static PyObject *
2326 _wrap_gi_vfunc_info_get_signal (PyGIBaseInfo *self)
2327 {
2328 return _get_child_info (self,
2329 (GetChildInfoCallback)gi_vfunc_info_get_signal);
2330 }
2331
2332 static PyObject *
2333 _wrap_gi_vfunc_info_get_invoker (PyGIBaseInfo *self)
2334 {
2335 return _get_child_info (self,
2336 (GetChildInfoCallback)gi_vfunc_info_get_invoker);
2337 }
2338
2339 static PyMethodDef _PyGIVFuncInfo_methods[] = {
2340 { "get_flags", (PyCFunction)_wrap_gi_vfunc_info_get_flags, METH_NOARGS },
2341 { "get_offset", (PyCFunction)_wrap_gi_vfunc_info_get_offset, METH_NOARGS },
2342 { "get_signal", (PyCFunction)_wrap_gi_vfunc_info_get_signal, METH_NOARGS },
2343 { "get_invoker", (PyCFunction)_wrap_gi_vfunc_info_get_invoker,
2344 METH_NOARGS },
2345 { NULL, NULL, 0 },
2346 };
2347
2348
2349 /* GIUnionInfo */
2350 PYGI_DEFINE_TYPE ("gi.UnionInfo", PyGIUnionInfo_Type, PyGIBaseInfo);
2351
2352 static PyObject *
2353 _wrap_gi_union_info_get_fields (PyGIBaseInfo *self)
2354 {
2355 return _make_infos_tuple (self,
2356 (GetNInfosCallback)gi_union_info_get_n_fields,
2357 (MakeInfosCallback)gi_union_info_get_field);
2358 }
2359
2360 static PyObject *
2361 _wrap_gi_union_info_get_methods (PyGIBaseInfo *self)
2362 {
2363 return _make_infos_tuple (self,
2364 (GetNInfosCallback)gi_union_info_get_n_methods,
2365 (MakeInfosCallback)gi_union_info_get_method);
2366 }
2367
2368 static PyObject *
2369 _wrap_gi_union_info_get_size (PyGIBaseInfo *self)
2370 {
2371 return pygi_gsize_to_py (
2372 gi_union_info_get_size (GI_UNION_INFO (self->info)));
2373 }
2374
2375 static PyObject *
2376 _wrap_gi_union_info_get_alignment (PyGIBaseInfo *self)
2377 {
2378 return pygi_gsize_to_py (
2379 gi_union_info_get_alignment (GI_UNION_INFO (self->info)));
2380 }
2381
2382 static PyMethodDef _PyGIUnionInfo_methods[] = {
2383 { "get_fields", (PyCFunction)_wrap_gi_union_info_get_fields, METH_NOARGS },
2384 { "get_methods", (PyCFunction)_wrap_gi_union_info_get_methods,
2385 METH_NOARGS },
2386 { "get_size", (PyCFunction)_wrap_gi_union_info_get_size, METH_NOARGS },
2387 { "get_alignment", (PyCFunction)_wrap_gi_union_info_get_alignment,
2388 METH_NOARGS },
2389 { NULL, NULL, 0 },
2390 };
2391
2392 /* Private */
2393
2394 gchar *
2395 _pygi_gi_base_info_get_fullname (GIBaseInfo *info)
2396 {
2397 GIBaseInfo *container_info;
2398 gchar *fullname;
2399
2400 container_info = gi_base_info_get_container (info);
2401 if (container_info != NULL) {
2402 fullname = g_strdup_printf (
2403 "%s.%s.%s", gi_base_info_get_namespace (container_info),
2404 _safe_base_info_get_name (container_info),
2405 _safe_base_info_get_name (info));
2406 } else {
2407 fullname = g_strdup_printf ("%s.%s", gi_base_info_get_namespace (info),
2408 _safe_base_info_get_name (info));
2409 }
2410
2411 if (fullname == NULL) {
2412 PyErr_NoMemory ();
2413 }
2414
2415 return fullname;
2416 }
2417
2418
2419 /**
2420 * Returns 0 on success, or -1 and sets an exception.
2421 */
2422 int
2423 pygi_info_register_types (PyObject *m)
2424 {
2425 #define _PyGI_REGISTER_TYPE(m, type, cname, base) \
2426 Py_SET_TYPE (&type, &PyType_Type); \
2427 type.tp_flags |= (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE); \
2428 type.tp_weaklistoffset = offsetof (PyGIBaseInfo, inst_weakreflist); \
2429 type.tp_methods = _PyGI##cname##_methods; \
2430 type.tp_base = &base; \
2431 if (PyType_Ready (&type) < 0) return -1; \
2432 Py_INCREF ((PyObject *)&type); \
2433 if (PyModule_AddObject (m, #cname, (PyObject *)&type) < 0) { \
2434 Py_DECREF ((PyObject *)&type); \
2435 return -1; \
2436 };
2437
2438 Py_SET_TYPE (&PyGIBaseInfo_Type, &PyType_Type);
2439
2440 PyGIBaseInfo_Type.tp_dealloc = (destructor)_base_info_dealloc;
2441 PyGIBaseInfo_Type.tp_repr = (reprfunc)_base_info_repr;
2442 PyGIBaseInfo_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
2443 PyGIBaseInfo_Type.tp_weaklistoffset =
2444 offsetof (PyGIBaseInfo, inst_weakreflist);
2445 PyGIBaseInfo_Type.tp_methods = _PyGIBaseInfo_methods;
2446 PyGIBaseInfo_Type.tp_richcompare = (richcmpfunc)_base_info_richcompare;
2447 PyGIBaseInfo_Type.tp_getset = _base_info_getsets;
2448 PyGIBaseInfo_Type.tp_getattro = (getattrofunc)_base_info_getattro;
2449
2450 if (PyType_Ready (&PyGIBaseInfo_Type) < 0) return -1;
2451 Py_INCREF ((PyObject *)&PyGIBaseInfo_Type);
2452 if (PyModule_AddObject (m, "BaseInfo", (PyObject *)&PyGIBaseInfo_Type)
2453 < 0) {
2454 Py_DECREF ((PyObject *)&PyGIBaseInfo_Type);
2455 return -1;
2456 }
2457
2458 PyGICallableInfo_Type.tp_dealloc = (destructor)_callable_info_dealloc;
2459 PyGICallableInfo_Type.tp_flags |= Py_TPFLAGS_HAVE_VECTORCALL;
2460 PyGICallableInfo_Type.tp_vectorcall_offset =
2461 offsetof (PyGICallableInfo, vectorcall);
2462 PyGICallableInfo_Type.tp_call = PyVectorcall_Call;
2463 PyGICallableInfo_Type.tp_repr = (reprfunc)_callable_info_repr;
2464 PyGICallableInfo_Type.tp_getset = _PyGICallableInfo_getsets;
2465 _PyGI_REGISTER_TYPE (m, PyGICallableInfo_Type, CallableInfo,
2466 PyGIBaseInfo_Type);
2467
2468 PyGIFunctionInfo_Type.tp_flags |= Py_TPFLAGS_METHOD_DESCRIPTOR;
2469 PyGIFunctionInfo_Type.tp_descr_get =
2470 (descrgetfunc)_function_info_descr_get;
2471 _PyGI_REGISTER_TYPE (m, PyGIFunctionInfo_Type, FunctionInfo,
2472 PyGICallableInfo_Type);
2473
2474 PyGIVFuncInfo_Type.tp_descr_get = (descrgetfunc)_vfunc_info_descr_get;
2475 _PyGI_REGISTER_TYPE (m, PyGIVFuncInfo_Type, VFuncInfo,
2476 PyGICallableInfo_Type);
2477
2478 _PyGI_REGISTER_TYPE (m, PyGISignalInfo_Type, SignalInfo,
2479 PyGICallableInfo_Type);
2480
2481 _PyGI_REGISTER_TYPE (m, PyGIUnresolvedInfo_Type, UnresolvedInfo,
2482 PyGIBaseInfo_Type);
2483 _PyGI_REGISTER_TYPE (m, PyGICallbackInfo_Type, CallbackInfo,
2484 PyGICallableInfo_Type);
2485 _PyGI_REGISTER_TYPE (m, PyGIRegisteredTypeInfo_Type, RegisteredTypeInfo,
2486 PyGIBaseInfo_Type);
2487 _PyGI_REGISTER_TYPE (m, PyGIStructInfo_Type, StructInfo,
2488 PyGIRegisteredTypeInfo_Type);
2489 _PyGI_REGISTER_TYPE (m, PyGIEnumInfo_Type, EnumInfo,
2490 PyGIRegisteredTypeInfo_Type);
2491 _PyGI_REGISTER_TYPE (m, PyGIObjectInfo_Type, ObjectInfo,
2492 PyGIRegisteredTypeInfo_Type);
2493 _PyGI_REGISTER_TYPE (m, PyGIInterfaceInfo_Type, InterfaceInfo,
2494 PyGIRegisteredTypeInfo_Type);
2495 _PyGI_REGISTER_TYPE (m, PyGIConstantInfo_Type, ConstantInfo,
2496 PyGIBaseInfo_Type);
2497 _PyGI_REGISTER_TYPE (m, PyGIValueInfo_Type, ValueInfo, PyGIBaseInfo_Type);
2498 _PyGI_REGISTER_TYPE (m, PyGIFieldInfo_Type, FieldInfo, PyGIBaseInfo_Type);
2499 _PyGI_REGISTER_TYPE (m, PyGIUnionInfo_Type, UnionInfo,
2500 PyGIRegisteredTypeInfo_Type);
2501 _PyGI_REGISTER_TYPE (m, PyGIErrorDomainInfo_Type, ErrorDomainInfo,
2502 PyGIBaseInfo_Type);
2503 _PyGI_REGISTER_TYPE (m, PyGIPropertyInfo_Type, PropertyInfo,
2504 PyGIBaseInfo_Type);
2505 _PyGI_REGISTER_TYPE (m, PyGIArgInfo_Type, ArgInfo, PyGIBaseInfo_Type);
2506 _PyGI_REGISTER_TYPE (m, PyGITypeInfo_Type, TypeInfo, PyGIBaseInfo_Type);
2507
2508 #undef _PyGI_REGISTER_TYPE
2509
2510 #define _PyGI_ENUM_BEGIN(name) \
2511 { \
2512 const char *__enum_name = #name; \
2513 PyObject *__enum_value = NULL; \
2514 PyObject *__new_enum_cls = NULL; \
2515 PyObject *__enum_instance_dict = PyDict_New (); \
2516 PyObject *__module_name = PyObject_GetAttrString (m, "__name__"); \
2517 PyDict_SetItemString (__enum_instance_dict, "__module__", \
2518 __module_name); \
2519 Py_DECREF (__module_name);
2520
2521 #define _PyGI_ENUM_ADD_VALUE(prefix, name) \
2522 __enum_value = pygi_guint_to_py (prefix##_##name); \
2523 if (PyDict_SetItemString (__enum_instance_dict, #name, __enum_value) \
2524 < 0) { \
2525 Py_DECREF (__enum_instance_dict); \
2526 Py_DECREF (__enum_value); \
2527 return -1; \
2528 } \
2529 Py_DECREF (__enum_value);
2530
2531 #define _PyGI_ENUM_END \
2532 __new_enum_cls = PyObject_CallFunction ( \
2533 (PyObject *)&PyType_Type, "s(O)O", __enum_name, \
2534 (PyObject *)&PyType_Type, __enum_instance_dict); \
2535 Py_DECREF (__enum_instance_dict); \
2536 PyModule_AddObject (m, __enum_name, __new_enum_cls); /* steals ref */ \
2537 }
2538
2539
2540 /* GIDirection */
2541 _PyGI_ENUM_BEGIN (Direction)
2542 _PyGI_ENUM_ADD_VALUE (GI_DIRECTION, IN)
2543 _PyGI_ENUM_ADD_VALUE (GI_DIRECTION, OUT)
2544 _PyGI_ENUM_ADD_VALUE (GI_DIRECTION, INOUT)
2545 _PyGI_ENUM_END
2546
2547
2548 /* GITransfer */
2549 _PyGI_ENUM_BEGIN (Transfer)
2550 _PyGI_ENUM_ADD_VALUE (GI_TRANSFER, NOTHING)
2551 _PyGI_ENUM_ADD_VALUE (GI_TRANSFER, CONTAINER)
2552 _PyGI_ENUM_ADD_VALUE (GI_TRANSFER, EVERYTHING)
2553 _PyGI_ENUM_END
2554
2555 /* GIArrayType */
2556 _PyGI_ENUM_BEGIN (ArrayType)
2557 _PyGI_ENUM_ADD_VALUE (GI_ARRAY_TYPE, C)
2558 _PyGI_ENUM_ADD_VALUE (GI_ARRAY_TYPE, ARRAY)
2559 _PyGI_ENUM_ADD_VALUE (GI_ARRAY_TYPE, PTR_ARRAY)
2560 _PyGI_ENUM_ADD_VALUE (GI_ARRAY_TYPE, BYTE_ARRAY)
2561 _PyGI_ENUM_END
2562
2563 /* GIScopeType */
2564 _PyGI_ENUM_BEGIN (ScopeType)
2565 _PyGI_ENUM_ADD_VALUE (GI_SCOPE_TYPE, INVALID)
2566 _PyGI_ENUM_ADD_VALUE (GI_SCOPE_TYPE, CALL)
2567 _PyGI_ENUM_ADD_VALUE (GI_SCOPE_TYPE, ASYNC)
2568 _PyGI_ENUM_ADD_VALUE (GI_SCOPE_TYPE, NOTIFIED)
2569 _PyGI_ENUM_END
2570
2571 /* GIVFuncInfoFlags */
2572 _PyGI_ENUM_BEGIN (VFuncInfoFlags)
2573 _PyGI_ENUM_ADD_VALUE (GI_VFUNC_MUST, CHAIN_UP)
2574 _PyGI_ENUM_ADD_VALUE (GI_VFUNC_MUST, OVERRIDE)
2575 _PyGI_ENUM_ADD_VALUE (GI_VFUNC_MUST, NOT_OVERRIDE)
2576 _PyGI_ENUM_END
2577
2578 /* GIFieldInfoFlags */
2579 _PyGI_ENUM_BEGIN (FieldInfoFlags)
2580 _PyGI_ENUM_ADD_VALUE (GI_FIELD, IS_READABLE)
2581 _PyGI_ENUM_ADD_VALUE (GI_FIELD, IS_WRITABLE)
2582 _PyGI_ENUM_END
2583
2584 /* GIFunctionInfoFlags */
2585 _PyGI_ENUM_BEGIN (FunctionInfoFlags)
2586 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, IS_METHOD)
2587 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, IS_CONSTRUCTOR)
2588 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, IS_GETTER)
2589 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, IS_SETTER)
2590 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, WRAPS_VFUNC)
2591 _PyGI_ENUM_END
2592
2593 /* GITypeTag */
2594 _PyGI_ENUM_BEGIN (TypeTag)
2595 /* Basic types */
2596 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, VOID)
2597 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, BOOLEAN)
2598 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INT8)
2599 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UINT8)
2600 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INT16)
2601 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UINT16)
2602 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INT32)
2603 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UINT32)
2604 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INT64)
2605 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UINT64)
2606 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, FLOAT)
2607 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, DOUBLE)
2608 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, GTYPE)
2609 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UTF8)
2610 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, FILENAME)
2611
2612 /* Non-basic types; compare with G_TYPE_TAG_IS_BASIC */
2613 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, ARRAY)
2614 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INTERFACE)
2615 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, GLIST)
2616 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, GSLIST)
2617 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, GHASH)
2618 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, ERROR)
2619
2620 /* Another basic type */
2621 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UNICHAR)
2622 _PyGI_ENUM_END
2623
2624 #undef _PyGI_ENUM_BEGIN
2625 #undef _PyGI_ENUM_ADD_VALUE
2626 #undef _PyGI_ENUM_END
2627
2628 return 0;
2629 }