Field setters: Remove unneeded type/range checks and resulting unused code.
[pygobject.git] / gi / pygi-info.c
blobcc8656b4ea419aa764864a627426eecf70d46afc
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
4 * Copyright (C) 2005-2009 Johan Dahlin <johan@gnome.org>
5 * Copyright (C) 2013 Simon Feltman <sfeltman@gnome.org>
7 * pygi-info.c: GI.*Info wrappers.
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.
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.
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/>.
23 #include "pygi-private.h"
24 #include "pygi-cache.h"
25 #include "pygobject-private.h"
27 #include <pyglib-python-compat.h>
30 /* _generate_doc_string
32 * C wrapper to call Python implemented "gi.docstring.generate_doc_string"
34 static PyObject *
35 _generate_doc_string(PyGIBaseInfo *self)
37 static PyObject *_py_generate_doc_string = NULL;
39 if (_py_generate_doc_string == NULL) {
40 PyObject *mod = PyImport_ImportModule ("gi.docstring");
41 if (!mod)
42 return NULL;
44 _py_generate_doc_string = PyObject_GetAttrString (mod, "generate_doc_string");
45 if (_py_generate_doc_string == NULL) {
46 Py_DECREF (mod);
47 return NULL;
49 Py_DECREF (mod);
52 return PyObject_CallFunctionObjArgs (_py_generate_doc_string, self, NULL);
55 static PyObject *
56 _get_info_string (PyGIBaseInfo *self,
57 const gchar* (*get_info_string)(GIBaseInfo*))
59 const gchar *value = get_info_string ((GIBaseInfo*)self->info);
60 if (value == NULL) {
61 Py_RETURN_NONE;
63 return PYGLIB_PyUnicode_FromString (value);
66 static PyObject *
67 _get_child_info (PyGIBaseInfo *self,
68 GIBaseInfo* (*get_child_info)(GIBaseInfo*))
70 GIBaseInfo *info;
71 PyObject *py_info;
73 info = get_child_info ((GIBaseInfo*)self->info);
74 if (info == NULL) {
75 Py_RETURN_NONE;
78 py_info = _pygi_info_new (info);
79 g_base_info_unref (info);
80 return py_info;
84 static PyObject *
85 _get_child_info_by_name (PyGIBaseInfo *self, PyObject *py_name,
86 GIBaseInfo* (*get_child_info_by_name)(GIBaseInfo*, const gchar*))
88 GIBaseInfo *info;
89 PyObject *py_info;
90 char *name;
92 if (!PYGLIB_PyUnicode_Check (py_name)) {
93 PyErr_SetString (PyExc_TypeError, "expected string name");
94 return NULL;
97 name = PYGLIB_PyUnicode_AsString (py_name);
98 info = get_child_info_by_name ((GIObjectInfo*)self->info, name);
99 if (info == NULL) {
100 Py_RETURN_NONE;
103 py_info = _pygi_info_new (info);
104 g_base_info_unref (info);
105 return py_info;
109 /* _make_infos_tuple
111 * Build a tuple from the common API pattern in GI of having a
112 * function which returns a count and an indexed GIBaseInfo
113 * in the range of 0 to count;
115 static PyObject *
116 _make_infos_tuple (PyGIBaseInfo *self,
117 gint (*get_n_infos)(GIBaseInfo*),
118 GIBaseInfo* (*get_info)(GIBaseInfo*, gint))
120 gint n_infos;
121 PyObject *infos;
122 gint i;
124 n_infos = get_n_infos ( (GIBaseInfo *) self->info);
126 infos = PyTuple_New (n_infos);
127 if (infos == NULL) {
128 return NULL;
131 for (i = 0; i < n_infos; i++) {
132 GIBaseInfo *info;
133 PyObject *py_info;
135 info = (GIBaseInfo *) get_info (self->info, i);
136 g_assert (info != NULL);
138 py_info = _pygi_info_new (info);
140 g_base_info_unref (info);
142 if (py_info == NULL) {
143 Py_CLEAR (infos);
144 break;
147 PyTuple_SET_ITEM (infos, i, py_info);
150 return infos;
154 /* BaseInfo */
156 /* We need to be careful about calling g_base_info_get_name because
157 * calling it with a GI_INFO_TYPE_TYPE will crash.
158 * See: https://bugzilla.gnome.org/show_bug.cgi?id=709456
160 static const char *
161 _safe_base_info_get_name (GIBaseInfo *info)
163 if (g_base_info_get_type (info) == GI_INFO_TYPE_TYPE) {
164 return "type_type_instance";
165 } else {
166 return g_base_info_get_name (info);
170 static void
171 _base_info_dealloc (PyGIBaseInfo *self)
173 if (self->inst_weakreflist != NULL)
174 PyObject_ClearWeakRefs ( (PyObject *) self);
176 g_base_info_unref (self->info);
178 if (self->cache != NULL)
179 pygi_callable_cache_free ( (PyGICallableCache *) self->cache);
181 Py_TYPE (self)->tp_free ((PyObject *)self);
184 static PyObject *
185 _base_info_repr (PyGIBaseInfo *self)
188 return PYGLIB_PyUnicode_FromFormat ("%s(%s)",
189 Py_TYPE( (PyObject *) self)->tp_name,
190 _safe_base_info_get_name (self->info));
193 static PyObject *
194 _wrap_g_base_info_equal (PyGIBaseInfo *self, PyObject *other)
196 GIBaseInfo *other_info;
198 if (!PyObject_TypeCheck (other, &PyGIBaseInfo_Type)) {
199 Py_INCREF (Py_NotImplemented);
200 return Py_NotImplemented;
203 other_info = ((PyGIBaseInfo *)other)->info;
204 if (g_base_info_equal (self->info, other_info)) {
205 Py_RETURN_TRUE;
206 } else {
207 Py_RETURN_FALSE;
211 static PyObject *
212 _base_info_richcompare (PyGIBaseInfo *self, PyObject *other, int op)
214 PyObject *res;
216 switch (op) {
217 case Py_EQ:
218 return _wrap_g_base_info_equal (self, other);
219 case Py_NE:
220 res = _wrap_g_base_info_equal (self, other);
221 if (res == Py_True) {
222 Py_DECREF (res);
223 Py_RETURN_FALSE;
224 } else {
225 Py_DECREF (res);
226 Py_RETURN_TRUE;
228 default:
229 res = Py_NotImplemented;
230 break;
232 Py_INCREF(res);
233 return res;
236 PYGLIB_DEFINE_TYPE("gi.BaseInfo", PyGIBaseInfo_Type, PyGIBaseInfo);
238 gboolean
239 _pygi_is_python_keyword (const gchar *name)
241 /* It may be better to use keyword.iskeyword(); keep in sync with
242 * python -c 'import keyword; print(keyword.kwlist)' */
243 #if PY_VERSION_HEX < 0x03000000
244 /* Python 2.x */
245 static const gchar* keywords[] = {"and", "as", "assert", "break", "class",
246 "continue", "def", "del", "elif", "else", "except", "exec", "finally",
247 "for", "from", "global", "if", "import", "in", "is", "lambda", "not",
248 "or", "pass", "print", "raise", "return", "try", "while", "with",
249 "yield", NULL};
250 #elif PY_VERSION_HEX < 0x04000000
251 /* Python 3.x; note that we explicitly keep "print"; it is not a keyword
252 * any more, but we do not want to break API between Python versions */
253 static const gchar* keywords[] = {"False", "None", "True", "and", "as",
254 "assert", "break", "class", "continue", "def", "del", "elif", "else",
255 "except", "finally", "for", "from", "global", "if", "import", "in",
256 "is", "lambda", "nonlocal", "not", "or", "pass", "raise", "return",
257 "try", "while", "with", "yield",
258 "print", NULL};
259 #else
260 #error Need keyword list for this major Python version
261 #endif
263 const gchar **i;
265 for (i = keywords; *i != NULL; ++i) {
266 if (strcmp (name, *i) == 0) {
267 return TRUE;
271 return FALSE;
274 static PyObject *
275 _wrap_g_base_info_get_type (PyGIBaseInfo *self)
277 return PYGLIB_PyLong_FromLong (g_base_info_get_type (self->info));
280 static PyObject *
281 _wrap_g_base_info_get_name (PyGIBaseInfo *self)
283 const gchar *name;
285 name = _safe_base_info_get_name (self->info);
287 /* escape keywords */
288 if (_pygi_is_python_keyword (name)) {
289 gchar *escaped = g_strconcat (name, "_", NULL);
290 PyObject *obj = PYGLIB_PyUnicode_FromString (escaped);
291 g_free (escaped);
292 return obj;
295 return PYGLIB_PyUnicode_FromString (name);
298 static PyObject *
299 _wrap_g_base_info_get_name_unescaped (PyGIBaseInfo *self)
301 return _get_info_string (self, _safe_base_info_get_name);
304 static PyObject *
305 _wrap_g_base_info_get_namespace (PyGIBaseInfo *self)
307 return _get_info_string (self, g_base_info_get_namespace);
310 static PyObject *
311 _wrap_g_base_info_is_deprecated (PyGIBaseInfo *self)
313 if (g_base_info_is_deprecated (self->info))
314 Py_RETURN_TRUE;
315 else
316 Py_RETURN_FALSE;
319 static PyObject *
320 _wrap_g_base_info_get_attribute (PyGIBaseInfo *self, PyObject *arg)
322 char *name;
323 const char *value;
325 if (!PYGLIB_PyUnicode_Check (arg)) {
326 PyErr_SetString (PyExc_TypeError, "expected string name");
327 return NULL;
330 name = PYGLIB_PyUnicode_AsString (arg);
331 value = g_base_info_get_attribute (self->info, name);
332 if (value == NULL) {
333 Py_RETURN_NONE;
335 return PYGLIB_PyUnicode_FromString (value);
338 static PyObject *
339 _wrap_g_base_info_get_container (PyGIBaseInfo *self)
341 /* Note: don't use _get_child_info because g_base_info_get_container
342 * is marked as [transfer none] and therefore returns a borrowed ref.
344 GIBaseInfo *info;
346 info = g_base_info_get_container (self->info);
348 if (info == NULL) {
349 Py_RETURN_NONE;
352 return _pygi_info_new (info);
356 static PyMethodDef _PyGIBaseInfo_methods[] = {
357 { "get_type", (PyCFunction) _wrap_g_base_info_get_type, METH_NOARGS },
358 { "get_name", (PyCFunction) _wrap_g_base_info_get_name, METH_NOARGS },
359 { "get_name_unescaped", (PyCFunction) _wrap_g_base_info_get_name_unescaped, METH_NOARGS },
360 { "get_namespace", (PyCFunction) _wrap_g_base_info_get_namespace, METH_NOARGS },
361 { "is_deprecated", (PyCFunction) _wrap_g_base_info_is_deprecated, METH_NOARGS },
362 { "get_attribute", (PyCFunction) _wrap_g_base_info_get_attribute, METH_O },
363 { "get_container", (PyCFunction) _wrap_g_base_info_get_container, METH_NOARGS },
364 { "equal", (PyCFunction) _wrap_g_base_info_equal, METH_O },
365 { NULL, NULL, 0 }
368 /* _base_info_getattro:
370 * The usage of __getattr__ is needed because the get/set method table
371 * does not work for __doc__.
373 static PyObject *
374 _base_info_getattro(PyGIBaseInfo *self, PyObject *name)
376 PyObject *result;
378 static PyObject *docstr;
379 if (docstr == NULL) {
380 docstr= PYGLIB_PyUnicode_InternFromString("__doc__");
381 if (docstr == NULL)
382 return NULL;
385 Py_INCREF (name);
386 PYGLIB_PyUnicode_InternInPlace (&name);
388 if (name == docstr) {
389 result = _generate_doc_string (self);
390 } else {
391 result = PyObject_GenericGetAttr ((PyObject *)self, name);
394 Py_DECREF (name);
395 return result;
398 static PyObject *
399 _base_info_attr_name(PyGIBaseInfo *self, void *closure)
401 return _wrap_g_base_info_get_name (self);
404 static PyObject *
405 _base_info_attr_module(PyGIBaseInfo *self, void *closure)
407 return PYGLIB_PyUnicode_FromFormat ("gi.repository.%s",
408 g_base_info_get_namespace (self->info));
411 static PyGetSetDef _base_info_getsets[] = {
412 { "__name__", (getter)_base_info_attr_name, (setter)0, "Name", NULL},
413 { "__module__", (getter)_base_info_attr_module, (setter)0, "Module name", NULL},
414 { NULL, 0, 0 }
417 PyObject *
418 _pygi_info_new (GIBaseInfo *info)
420 GIInfoType info_type;
421 PyTypeObject *type = NULL;
422 PyGIBaseInfo *self;
424 info_type = g_base_info_get_type (info);
426 switch (info_type)
428 case GI_INFO_TYPE_INVALID:
429 PyErr_SetString (PyExc_RuntimeError, "Invalid info type");
430 return NULL;
431 case GI_INFO_TYPE_FUNCTION:
432 type = &PyGIFunctionInfo_Type;
433 break;
434 case GI_INFO_TYPE_CALLBACK:
435 type = &PyGICallbackInfo_Type;
436 break;
437 case GI_INFO_TYPE_STRUCT:
438 case GI_INFO_TYPE_BOXED:
439 type = &PyGIStructInfo_Type;
440 break;
441 case GI_INFO_TYPE_ENUM:
442 case GI_INFO_TYPE_FLAGS:
443 type = &PyGIEnumInfo_Type;
444 break;
445 case GI_INFO_TYPE_OBJECT:
446 type = &PyGIObjectInfo_Type;
447 break;
448 case GI_INFO_TYPE_INTERFACE:
449 type = &PyGIInterfaceInfo_Type;
450 break;
451 case GI_INFO_TYPE_CONSTANT:
452 type = &PyGIConstantInfo_Type;
453 break;
454 case GI_INFO_TYPE_UNION:
455 type = &PyGIUnionInfo_Type;
456 break;
457 case GI_INFO_TYPE_VALUE:
458 type = &PyGIValueInfo_Type;
459 break;
460 case GI_INFO_TYPE_SIGNAL:
461 type = &PyGISignalInfo_Type;
462 break;
463 case GI_INFO_TYPE_VFUNC:
464 type = &PyGIVFuncInfo_Type;
465 break;
466 case GI_INFO_TYPE_PROPERTY:
467 type = &PyGIPropertyInfo_Type;
468 break;
469 case GI_INFO_TYPE_FIELD:
470 type = &PyGIFieldInfo_Type;
471 break;
472 case GI_INFO_TYPE_ARG:
473 type = &PyGIArgInfo_Type;
474 break;
475 case GI_INFO_TYPE_TYPE:
476 type = &PyGITypeInfo_Type;
477 break;
478 case GI_INFO_TYPE_UNRESOLVED:
479 type = &PyGIUnresolvedInfo_Type;
480 break;
481 default:
482 g_assert_not_reached();
483 break;
486 self = (PyGIBaseInfo *) type->tp_alloc (type, 0);
487 if (self == NULL) {
488 return NULL;
491 self->info = g_base_info_ref (info);
492 self->inst_weakreflist = NULL;
493 self->cache = NULL;
495 return (PyObject *) self;
498 GIBaseInfo *
499 _pygi_object_get_gi_info (PyObject *object,
500 PyTypeObject *type)
502 PyObject *py_info;
503 GIBaseInfo *info = NULL;
505 py_info = PyObject_GetAttrString (object, "__info__");
506 if (py_info == NULL) {
507 return NULL;
509 if (!PyObject_TypeCheck (py_info, type)) {
510 PyErr_Format (PyExc_TypeError, "attribute '__info__' must be %s, not %s",
511 type->tp_name, Py_TYPE(py_info)->tp_name);
512 goto out;
515 info = ( (PyGIBaseInfo *) py_info)->info;
516 g_base_info_ref (info);
518 out:
519 Py_DECREF (py_info);
521 return info;
525 /* CallableInfo */
526 PYGLIB_DEFINE_TYPE ("gi.CallableInfo", PyGICallableInfo_Type, PyGICallableInfo);
528 /* _callable_info_call:
530 * Shared wrapper for invoke which can be bound (instance method or class constructor)
531 * or unbound (function or static method).
533 static PyObject *
534 _callable_info_call (PyGICallableInfo *self, PyObject *args, PyObject *kwargs)
536 /* Insert the bound arg at the beginning of the invoke method args. */
537 if (self->py_bound_arg) {
538 int i;
539 PyObject *result;
540 Py_ssize_t argcount = PyTuple_Size (args);
541 PyObject *newargs = PyTuple_New (argcount + 1);
542 if (newargs == NULL)
543 return NULL;
545 Py_INCREF (self->py_bound_arg);
546 PyTuple_SET_ITEM (newargs, 0, self->py_bound_arg);
548 for (i = 0; i < argcount; i++) {
549 PyObject *v = PyTuple_GET_ITEM (args, i);
550 Py_XINCREF (v);
551 PyTuple_SET_ITEM (newargs, i+1, v);
554 /* Invoke with the original GI info struct this wrapper was based upon.
555 * This is necessary to maintain the same cache for all bound versions.
557 result = _wrap_g_callable_info_invoke ((PyGIBaseInfo *)self->py_unbound_info,
558 newargs, kwargs);
559 Py_DECREF (newargs);
560 return result;
562 } else {
563 /* We should never have an unbound info when calling when calling invoke
564 * at this point because the descriptor implementation on sub-classes
565 * should return "self" not a copy when there is no bound arg.
567 g_assert (self->py_unbound_info == NULL);
568 return _wrap_g_callable_info_invoke ((PyGIBaseInfo *)self, args, kwargs);
573 /* _function_info_call:
575 * Specialization of _callable_info_call for GIFunctionInfo which
576 * handles constructor error conditions.
578 static PyObject *
579 _function_info_call (PyGICallableInfo *self, PyObject *args, PyObject *kwargs)
581 if (self->py_bound_arg) {
582 GIFunctionInfoFlags flags;
584 /* Ensure constructors are only called as class methods on the class
585 * implementing the constructor and not on sub-classes.
587 flags = g_function_info_get_flags ( (GIFunctionInfo*) self->base.info);
588 if (flags & GI_FUNCTION_IS_CONSTRUCTOR) {
589 PyObject *py_str_name;
590 const gchar *str_name;
591 GIBaseInfo *container_info = g_base_info_get_container (self->base.info);
592 g_assert (container_info != NULL);
594 py_str_name = PyObject_GetAttrString (self->py_bound_arg, "__name__");
595 if (py_str_name == NULL)
596 return NULL;
598 if (PyUnicode_Check (py_str_name) ) {
599 PyObject *tmp = PyUnicode_AsUTF8String (py_str_name);
600 Py_DECREF (py_str_name);
601 py_str_name = tmp;
604 #if PY_VERSION_HEX < 0x03000000
605 str_name = PyString_AsString (py_str_name);
606 #else
607 str_name = PyBytes_AsString (py_str_name);
608 #endif
610 if (strcmp (str_name, _safe_base_info_get_name (container_info))) {
611 PyErr_Format (PyExc_TypeError,
612 "%s constructor cannot be used to create instances of "
613 "a subclass %s",
614 _safe_base_info_get_name (container_info),
615 str_name);
616 Py_DECREF (py_str_name);
617 return NULL;
619 Py_DECREF (py_str_name);
623 return _callable_info_call (self, args, kwargs);
626 /* _new_bound_callable_info
628 * Utility function for sub-classes to create a bound version of themself.
630 static PyGICallableInfo *
631 _new_bound_callable_info (PyGICallableInfo *self, PyObject *bound_arg)
633 PyGICallableInfo *new_self;
635 /* Return self if this is already bound or there is nothing passed to bind. */
636 if (self->py_bound_arg != NULL || bound_arg == NULL || bound_arg == Py_None) {
637 Py_INCREF ((PyObject *)self);
638 return self;
641 new_self = (PyGICallableInfo *)_pygi_info_new (self->base.info);
642 if (new_self == NULL)
643 return NULL;
645 Py_INCREF ((PyObject *)self);
646 new_self->py_unbound_info = (struct PyGICallableInfo *)self;
648 Py_INCREF (bound_arg);
649 new_self->py_bound_arg = bound_arg;
651 return new_self;
654 /* _function_info_descr_get
656 * Descriptor protocol implementation for functions, methods, and constructors.
658 static PyObject *
659 _function_info_descr_get (PyGICallableInfo *self, PyObject *obj, PyObject *type) {
660 GIFunctionInfoFlags flags;
661 PyObject *bound_arg = NULL;
663 flags = g_function_info_get_flags ( (GIFunctionInfo*) self->base.info);
664 if (flags & GI_FUNCTION_IS_CONSTRUCTOR) {
665 if (type == NULL)
666 bound_arg = (PyObject *)(Py_TYPE(obj));
667 else
668 bound_arg = type;
669 } else if (flags & GI_FUNCTION_IS_METHOD) {
670 bound_arg = obj;
673 return (PyObject *)_new_bound_callable_info (self, bound_arg);
676 /* _vfunc_info_descr_get
678 * Descriptor protocol implementation for virtual functions.
680 static PyObject *
681 _vfunc_info_descr_get (PyGICallableInfo *self, PyObject *obj, PyObject *type) {
682 PyObject *result;
683 PyObject *bound_arg = NULL;
685 bound_arg = PyObject_GetAttrString (type, "__gtype__");
686 if (bound_arg == NULL)
687 return NULL;
689 /* _new_bound_callable_info adds its own ref so free the one from GetAttrString */
690 result = (PyObject *)_new_bound_callable_info (self, bound_arg);
691 Py_DECREF (bound_arg);
692 return result;
695 static void
696 _callable_info_dealloc (PyGICallableInfo *self)
698 Py_CLEAR (self->py_unbound_info);
699 Py_CLEAR (self->py_bound_arg);
701 PyGIBaseInfo_Type.tp_dealloc ((PyObject *) self);
704 static PyObject *
705 _wrap_g_callable_info_get_arguments (PyGIBaseInfo *self)
707 return _make_infos_tuple (self, g_callable_info_get_n_args, g_callable_info_get_arg);
710 static PyObject *
711 _wrap_g_callable_info_get_return_type (PyGIBaseInfo *self)
713 return _get_child_info (self, g_callable_info_get_return_type);
716 static PyObject *
717 _wrap_g_callable_info_get_caller_owns (PyGIBaseInfo *self)
719 return PYGLIB_PyLong_FromLong (
720 g_callable_info_get_caller_owns (self->info) );
723 static PyObject *
724 _wrap_g_callable_info_may_return_null (PyGIBaseInfo *self)
726 return PyBool_FromLong (
727 g_callable_info_may_return_null (self->info) );
730 static PyObject *
731 _wrap_g_callable_info_skip_return (PyGIBaseInfo *self)
733 return PyBool_FromLong (g_callable_info_skip_return (self->info));
736 static PyObject *
737 _wrap_g_callable_info_get_return_attribute (PyGIBaseInfo *self, PyObject *py_name)
739 gchar *name;
740 const gchar *attr;
742 if (!PYGLIB_PyUnicode_Check (py_name)) {
743 PyErr_SetString (PyExc_TypeError, "expected string name");
744 return NULL;
747 name = PYGLIB_PyUnicode_AsString (py_name);
748 attr = g_callable_info_get_return_attribute (self->info, name);
749 if (attr) {
750 return PYGLIB_PyUnicode_FromString (
751 g_callable_info_get_return_attribute (self->info, name));
752 } else {
753 PyErr_Format(PyExc_AttributeError, "return attribute %s not found", name);
754 return NULL;
758 static PyObject *
759 _wrap_g_callable_info_can_throw_gerror (PyGIBaseInfo *self)
761 if (g_callable_info_can_throw_gerror (self->info))
762 Py_RETURN_TRUE;
763 else
764 Py_RETURN_FALSE;
767 static PyMethodDef _PyGICallableInfo_methods[] = {
768 { "invoke", (PyCFunction) _wrap_g_callable_info_invoke, METH_VARARGS | METH_KEYWORDS },
769 { "get_arguments", (PyCFunction) _wrap_g_callable_info_get_arguments, METH_NOARGS },
770 { "get_return_type", (PyCFunction) _wrap_g_callable_info_get_return_type, METH_NOARGS },
771 { "get_caller_owns", (PyCFunction) _wrap_g_callable_info_get_caller_owns, METH_NOARGS },
772 { "may_return_null", (PyCFunction) _wrap_g_callable_info_may_return_null, METH_NOARGS },
773 { "skip_return", (PyCFunction) _wrap_g_callable_info_skip_return, METH_NOARGS },
774 { "get_return_attribute", (PyCFunction) _wrap_g_callable_info_get_return_attribute, METH_O },
775 { "can_throw_gerror", (PyCFunction) _wrap_g_callable_info_can_throw_gerror, METH_NOARGS },
776 { NULL, NULL, 0 }
779 /* CallbackInfo */
780 PYGLIB_DEFINE_TYPE ("gi.CallbackInfo", PyGICallbackInfo_Type, PyGICallableInfo);
782 static PyMethodDef _PyGICallbackInfo_methods[] = {
783 { NULL, NULL, 0 }
786 /* ErrorDomainInfo */
787 PYGLIB_DEFINE_TYPE ("gi.ErrorDomainInfo", PyGIErrorDomainInfo_Type, PyGIBaseInfo);
789 static PyMethodDef _PyGIErrorDomainInfo_methods[] = {
790 { NULL, NULL, 0 }
793 /* SignalInfo */
794 PYGLIB_DEFINE_TYPE ("gi.SignalInfo", PyGISignalInfo_Type, PyGICallableInfo);
796 static PyObject *
797 _wrap_g_signal_info_get_flags (PyGIBaseInfo *self)
799 return PYGLIB_PyLong_FromLong (
800 g_signal_info_get_flags ((GISignalInfo *)self->info) );
803 static PyObject *
804 _wrap_g_signal_info_get_class_closure (PyGIBaseInfo *self)
806 return _get_child_info (self, g_signal_info_get_class_closure);
809 static PyObject *
810 _wrap_g_signal_info_true_stops_emit (PyGIBaseInfo *self)
812 return PyBool_FromLong (
813 g_signal_info_true_stops_emit ((GISignalInfo *)self->info) );
816 static PyMethodDef _PyGISignalInfo_methods[] = {
817 { "get_flags", (PyCFunction) _wrap_g_signal_info_get_flags, METH_NOARGS },
818 { "get_class_closure", (PyCFunction) _wrap_g_signal_info_get_class_closure, METH_NOARGS },
819 { "true_stops_emit", (PyCFunction) _wrap_g_signal_info_true_stops_emit, METH_NOARGS },
820 { NULL, NULL, 0 }
823 /* PropertyInfo */
824 PYGLIB_DEFINE_TYPE ("gi.PropertyInfo", PyGIPropertyInfo_Type, PyGIBaseInfo);
826 static PyObject *
827 _wrap_g_property_info_get_flags (PyGIBaseInfo *self)
829 return PYGLIB_PyLong_FromLong (
830 g_property_info_get_flags ((GIPropertyInfo *)self->info) );
833 static PyObject *
834 _wrap_g_property_info_get_type (PyGIBaseInfo *self)
836 return _get_child_info (self, g_property_info_get_type);
839 static PyObject *
840 _wrap_g_property_info_get_ownership_transfer (PyGIBaseInfo *self)
842 return PYGLIB_PyLong_FromLong (
843 g_property_info_get_ownership_transfer ((GIPropertyInfo *)self->info) );
846 static PyMethodDef _PyGIPropertyInfo_methods[] = {
847 { "get_flags", (PyCFunction) _wrap_g_property_info_get_flags, METH_NOARGS },
848 { "get_type", (PyCFunction) _wrap_g_property_info_get_type, METH_NOARGS },
849 { "get_ownership_transfer", (PyCFunction) _wrap_g_property_info_get_ownership_transfer, METH_NOARGS },
850 { NULL, NULL, 0 }
854 /* ArgInfo */
855 PYGLIB_DEFINE_TYPE ("gi.ArgInfo", PyGIArgInfo_Type, PyGIBaseInfo);
857 static PyObject *
858 _wrap_g_arg_info_get_direction (PyGIBaseInfo *self)
860 return PYGLIB_PyLong_FromLong (
861 g_arg_info_get_direction ((GIArgInfo*)self->info) );
864 static PyObject *
865 _wrap_g_arg_info_is_caller_allocates (PyGIBaseInfo *self)
867 return PyBool_FromLong (
868 g_arg_info_is_caller_allocates ((GIArgInfo*)self->info) );
871 static PyObject *
872 _wrap_g_arg_info_is_return_value (PyGIBaseInfo *self)
874 return PyBool_FromLong (
875 g_arg_info_is_return_value ((GIArgInfo*)self->info) );
878 static PyObject *
879 _wrap_g_arg_info_is_optional (PyGIBaseInfo *self)
881 return PyBool_FromLong (
882 g_arg_info_is_optional ((GIArgInfo*)self->info) );
885 static PyObject *
886 _wrap_g_arg_info_may_be_null (PyGIBaseInfo *self)
888 return PyBool_FromLong (
889 g_arg_info_may_be_null ((GIArgInfo*)self->info) );
892 static PyObject *
893 _wrap_g_arg_info_get_ownership_transfer (PyGIBaseInfo *self)
895 return PYGLIB_PyLong_FromLong (
896 g_arg_info_get_ownership_transfer ((GIArgInfo *)self->info) );
899 static PyObject *
900 _wrap_g_arg_info_get_scope (PyGIBaseInfo *self)
902 return PYGLIB_PyLong_FromLong (
903 g_arg_info_get_scope ((GIArgInfo *)self->info) );
906 static PyObject *
907 _wrap_g_arg_info_get_closure (PyGIBaseInfo *self)
909 return PYGLIB_PyLong_FromLong (
910 g_arg_info_get_closure ((GIArgInfo *)self->info) );
913 static PyObject *
914 _wrap_g_arg_info_get_destroy (PyGIBaseInfo *self)
916 return PYGLIB_PyLong_FromLong (
917 g_arg_info_get_destroy ((GIArgInfo *)self->info) );
920 static PyObject *
921 _wrap_g_arg_info_get_type (PyGIBaseInfo *self)
923 return _get_child_info (self, g_arg_info_get_type);
926 static PyMethodDef _PyGIArgInfo_methods[] = {
927 { "get_direction", (PyCFunction) _wrap_g_arg_info_get_direction, METH_NOARGS },
928 { "is_caller_allocates", (PyCFunction) _wrap_g_arg_info_is_caller_allocates, METH_NOARGS },
929 { "is_return_value", (PyCFunction) _wrap_g_arg_info_is_return_value, METH_NOARGS },
930 { "is_optional", (PyCFunction) _wrap_g_arg_info_is_optional, METH_NOARGS },
931 { "may_be_null", (PyCFunction) _wrap_g_arg_info_may_be_null, METH_NOARGS },
932 { "get_ownership_transfer", (PyCFunction) _wrap_g_arg_info_get_ownership_transfer, METH_NOARGS },
933 { "get_scope", (PyCFunction) _wrap_g_arg_info_get_scope, METH_NOARGS },
934 { "get_closure", (PyCFunction) _wrap_g_arg_info_get_closure, METH_NOARGS },
935 { "get_destroy", (PyCFunction) _wrap_g_arg_info_get_destroy, METH_NOARGS },
936 { "get_type", (PyCFunction) _wrap_g_arg_info_get_type, METH_NOARGS },
937 { NULL, NULL, 0 }
941 /* TypeInfo */
942 PYGLIB_DEFINE_TYPE ("gi.TypeInfo", PyGITypeInfo_Type, PyGIBaseInfo);
944 static PyObject *
945 _wrap_g_type_info_is_pointer (PyGIBaseInfo *self)
947 return PyBool_FromLong (g_type_info_is_pointer (self->info));
950 static PyObject *
951 _wrap_g_type_info_get_tag (PyGIBaseInfo *self)
953 return PYGLIB_PyLong_FromLong (g_type_info_get_tag (self->info));
956 static PyObject *
957 _wrap_g_type_info_get_tag_as_string (PyGIBaseInfo *self)
959 GITypeTag tag = g_type_info_get_tag (self->info);
960 return PYGLIB_PyUnicode_FromString (g_type_tag_to_string(tag));
963 static PyObject *
964 _wrap_g_type_info_get_param_type (PyGIBaseInfo *self, PyObject *py_n)
966 GIBaseInfo *info;
967 PyObject *py_info;
968 gint n;
970 if (!PYGLIB_PyLong_Check (py_n)) {
971 PyErr_SetString(PyExc_TypeError, "expected integer value");
972 return NULL;
975 n = PYGLIB_PyLong_AsLong (py_n);
976 info = (GIBaseInfo *) g_type_info_get_param_type ( (GITypeInfo *) self->info, n);
977 if (info == NULL) {
978 Py_RETURN_NONE;
981 py_info = _pygi_info_new (info);
982 g_base_info_unref (info);
983 return py_info;
986 static PyObject *
987 _wrap_g_type_info_get_interface (PyGIBaseInfo *self)
989 return _get_child_info (self, g_type_info_get_interface);
992 static PyObject *
993 _wrap_g_type_info_get_array_length (PyGIBaseInfo *self)
995 return PYGLIB_PyLong_FromLong (g_type_info_get_array_length (self->info));
998 static PyObject *
999 _wrap_g_type_info_get_array_fixed_size (PyGIBaseInfo *self)
1001 return PYGLIB_PyLong_FromLong (g_type_info_get_array_fixed_size (self->info));
1004 static PyObject *
1005 _wrap_g_type_info_is_zero_terminated (PyGIBaseInfo *self)
1007 return PyBool_FromLong (g_type_info_is_zero_terminated (self->info));
1010 static PyObject *
1011 _wrap_g_type_info_get_array_type (PyGIBaseInfo *self)
1013 return PYGLIB_PyLong_FromLong (g_type_info_get_array_type (self->info));
1016 static PyMethodDef _PyGITypeInfo_methods[] = {
1017 { "is_pointer", (PyCFunction) _wrap_g_type_info_is_pointer, METH_NOARGS },
1018 { "get_tag", (PyCFunction) _wrap_g_type_info_get_tag, METH_NOARGS },
1019 { "get_tag_as_string", (PyCFunction) _wrap_g_type_info_get_tag_as_string, METH_NOARGS },
1020 { "get_param_type", (PyCFunction) _wrap_g_type_info_get_param_type, METH_O },
1021 { "get_interface", (PyCFunction) _wrap_g_type_info_get_interface, METH_NOARGS },
1022 { "get_array_length", (PyCFunction) _wrap_g_type_info_get_array_length, METH_NOARGS },
1023 { "get_array_fixed_size", (PyCFunction) _wrap_g_type_info_get_array_fixed_size, METH_NOARGS },
1024 { "is_zero_terminated", (PyCFunction) _wrap_g_type_info_is_zero_terminated, METH_NOARGS },
1025 { "get_array_type", (PyCFunction) _wrap_g_type_info_get_array_type, METH_NOARGS },
1026 { NULL, NULL, 0 }
1030 /* FunctionInfo */
1031 PYGLIB_DEFINE_TYPE ("gi.FunctionInfo", PyGIFunctionInfo_Type, PyGICallableInfo);
1033 static PyObject *
1034 _wrap_g_function_info_is_constructor (PyGIBaseInfo *self)
1036 GIFunctionInfoFlags flags;
1037 gboolean is_constructor;
1039 flags = g_function_info_get_flags ( (GIFunctionInfo*) self->info);
1040 is_constructor = flags & GI_FUNCTION_IS_CONSTRUCTOR;
1042 return PyBool_FromLong (is_constructor);
1045 static PyObject *
1046 _wrap_g_function_info_is_method (PyGIBaseInfo *self)
1048 GIFunctionInfoFlags flags;
1049 gboolean is_method;
1051 flags = g_function_info_get_flags ( (GIFunctionInfo*) self->info);
1052 is_method = flags & GI_FUNCTION_IS_METHOD;
1054 return PyBool_FromLong (is_method);
1057 gsize
1058 _pygi_g_type_tag_size (GITypeTag type_tag)
1060 gsize size = 0;
1062 switch (type_tag) {
1063 case GI_TYPE_TAG_BOOLEAN:
1064 size = sizeof (gboolean);
1065 break;
1066 case GI_TYPE_TAG_INT8:
1067 case GI_TYPE_TAG_UINT8:
1068 size = sizeof (gint8);
1069 break;
1070 case GI_TYPE_TAG_INT16:
1071 case GI_TYPE_TAG_UINT16:
1072 size = sizeof (gint16);
1073 break;
1074 case GI_TYPE_TAG_INT32:
1075 case GI_TYPE_TAG_UINT32:
1076 size = sizeof (gint32);
1077 break;
1078 case GI_TYPE_TAG_INT64:
1079 case GI_TYPE_TAG_UINT64:
1080 size = sizeof (gint64);
1081 break;
1082 case GI_TYPE_TAG_FLOAT:
1083 size = sizeof (gfloat);
1084 break;
1085 case GI_TYPE_TAG_DOUBLE:
1086 size = sizeof (gdouble);
1087 break;
1088 case GI_TYPE_TAG_GTYPE:
1089 size = sizeof (GType);
1090 break;
1091 case GI_TYPE_TAG_UNICHAR:
1092 size = sizeof (gunichar);
1093 break;
1094 case GI_TYPE_TAG_VOID:
1095 case GI_TYPE_TAG_UTF8:
1096 case GI_TYPE_TAG_FILENAME:
1097 case GI_TYPE_TAG_ARRAY:
1098 case GI_TYPE_TAG_INTERFACE:
1099 case GI_TYPE_TAG_GLIST:
1100 case GI_TYPE_TAG_GSLIST:
1101 case GI_TYPE_TAG_GHASH:
1102 case GI_TYPE_TAG_ERROR:
1103 PyErr_Format (PyExc_TypeError,
1104 "Unable to know the size (assuming %s is not a pointer)",
1105 g_type_tag_to_string (type_tag));
1106 break;
1109 return size;
1112 gsize
1113 _pygi_g_type_info_size (GITypeInfo *type_info)
1115 gsize size = 0;
1117 GITypeTag type_tag;
1119 type_tag = g_type_info_get_tag (type_info);
1120 switch (type_tag) {
1121 case GI_TYPE_TAG_BOOLEAN:
1122 case GI_TYPE_TAG_INT8:
1123 case GI_TYPE_TAG_UINT8:
1124 case GI_TYPE_TAG_INT16:
1125 case GI_TYPE_TAG_UINT16:
1126 case GI_TYPE_TAG_INT32:
1127 case GI_TYPE_TAG_UINT32:
1128 case GI_TYPE_TAG_INT64:
1129 case GI_TYPE_TAG_UINT64:
1130 case GI_TYPE_TAG_FLOAT:
1131 case GI_TYPE_TAG_DOUBLE:
1132 case GI_TYPE_TAG_GTYPE:
1133 case GI_TYPE_TAG_UNICHAR:
1134 size = _pygi_g_type_tag_size (type_tag);
1135 g_assert (size > 0);
1136 break;
1137 case GI_TYPE_TAG_INTERFACE:
1139 GIBaseInfo *info;
1140 GIInfoType info_type;
1142 info = g_type_info_get_interface (type_info);
1143 info_type = g_base_info_get_type (info);
1145 switch (info_type) {
1146 case GI_INFO_TYPE_STRUCT:
1147 if (g_type_info_is_pointer (type_info)) {
1148 size = sizeof (gpointer);
1149 } else {
1150 size = g_struct_info_get_size ( (GIStructInfo *) info);
1152 break;
1153 case GI_INFO_TYPE_UNION:
1154 if (g_type_info_is_pointer (type_info)) {
1155 size = sizeof (gpointer);
1156 } else {
1157 size = g_union_info_get_size ( (GIUnionInfo *) info);
1159 break;
1160 case GI_INFO_TYPE_ENUM:
1161 case GI_INFO_TYPE_FLAGS:
1162 if (g_type_info_is_pointer (type_info)) {
1163 size = sizeof (gpointer);
1164 } else {
1165 GITypeTag type_tag;
1167 type_tag = g_enum_info_get_storage_type ( (GIEnumInfo *) info);
1168 size = _pygi_g_type_tag_size (type_tag);
1170 break;
1171 case GI_INFO_TYPE_BOXED:
1172 case GI_INFO_TYPE_OBJECT:
1173 case GI_INFO_TYPE_INTERFACE:
1174 case GI_INFO_TYPE_CALLBACK:
1175 size = sizeof (gpointer);
1176 break;
1177 case GI_INFO_TYPE_VFUNC:
1178 case GI_INFO_TYPE_INVALID:
1179 case GI_INFO_TYPE_FUNCTION:
1180 case GI_INFO_TYPE_CONSTANT:
1181 case GI_INFO_TYPE_VALUE:
1182 case GI_INFO_TYPE_SIGNAL:
1183 case GI_INFO_TYPE_PROPERTY:
1184 case GI_INFO_TYPE_FIELD:
1185 case GI_INFO_TYPE_ARG:
1186 case GI_INFO_TYPE_TYPE:
1187 case GI_INFO_TYPE_UNRESOLVED:
1188 default:
1189 g_assert_not_reached();
1190 break;
1193 g_base_info_unref (info);
1194 break;
1196 case GI_TYPE_TAG_ARRAY:
1197 case GI_TYPE_TAG_VOID:
1198 case GI_TYPE_TAG_UTF8:
1199 case GI_TYPE_TAG_FILENAME:
1200 case GI_TYPE_TAG_GLIST:
1201 case GI_TYPE_TAG_GSLIST:
1202 case GI_TYPE_TAG_GHASH:
1203 case GI_TYPE_TAG_ERROR:
1204 size = sizeof (gpointer);
1205 break;
1208 return size;
1211 static PyObject *
1212 _wrap_g_function_info_get_symbol (PyGIBaseInfo *self)
1214 return _get_info_string (self, g_function_info_get_symbol);
1217 static PyObject *
1218 _wrap_g_function_info_get_flags (PyGIBaseInfo *self)
1220 return PYGLIB_PyLong_FromLong (g_function_info_get_flags (self->info));
1223 static PyObject *
1224 _wrap_g_function_info_get_property (PyGIBaseInfo *self)
1226 return _get_child_info (self, g_function_info_get_property);
1229 static PyObject *
1230 _wrap_g_function_info_get_vfunc (PyGIBaseInfo *self)
1232 return _get_child_info (self, g_function_info_get_vfunc);
1235 static PyMethodDef _PyGIFunctionInfo_methods[] = {
1236 { "is_constructor", (PyCFunction) _wrap_g_function_info_is_constructor, METH_NOARGS },
1237 { "is_method", (PyCFunction) _wrap_g_function_info_is_method, METH_NOARGS },
1238 { "get_symbol", (PyCFunction) _wrap_g_function_info_get_symbol, METH_NOARGS },
1239 { "get_flags", (PyCFunction) _wrap_g_function_info_get_flags, METH_NOARGS },
1240 { "get_property", (PyCFunction) _wrap_g_function_info_get_property, METH_NOARGS },
1241 { "get_vfunc", (PyCFunction) _wrap_g_function_info_get_vfunc, METH_NOARGS },
1242 { NULL, NULL, 0 }
1245 /* RegisteredTypeInfo */
1246 PYGLIB_DEFINE_TYPE ("gi.RegisteredTypeInfo", PyGIRegisteredTypeInfo_Type, PyGIBaseInfo);
1248 static PyObject *
1249 _wrap_g_registered_type_info_get_type_name (PyGIBaseInfo *self)
1251 return _get_info_string (self, g_registered_type_info_get_type_name);
1254 static PyObject *
1255 _wrap_g_registered_type_info_get_type_init (PyGIBaseInfo *self)
1257 return _get_info_string (self, g_registered_type_info_get_type_init);
1260 static PyObject *
1261 _wrap_g_registered_type_info_get_g_type (PyGIBaseInfo *self)
1263 GType type;
1265 type = g_registered_type_info_get_g_type ( (GIRegisteredTypeInfo *) self->info);
1267 return pyg_type_wrapper_new (type);
1270 static PyMethodDef _PyGIRegisteredTypeInfo_methods[] = {
1271 { "get_type_name", (PyCFunction) _wrap_g_registered_type_info_get_type_name, METH_NOARGS },
1272 { "get_type_init", (PyCFunction) _wrap_g_registered_type_info_get_type_init, METH_NOARGS },
1273 { "get_g_type", (PyCFunction) _wrap_g_registered_type_info_get_g_type, METH_NOARGS },
1274 { NULL, NULL, 0 }
1278 /* GIStructInfo */
1279 PYGLIB_DEFINE_TYPE ("StructInfo", PyGIStructInfo_Type, PyGIBaseInfo);
1281 static PyObject *
1282 _wrap_g_struct_info_get_fields (PyGIBaseInfo *self)
1284 return _make_infos_tuple (self, g_struct_info_get_n_fields, g_struct_info_get_field);
1287 static PyObject *
1288 _wrap_g_struct_info_get_methods (PyGIBaseInfo *self)
1290 return _make_infos_tuple (self, g_struct_info_get_n_methods, g_struct_info_get_method);
1293 static PyObject *
1294 _wrap_g_struct_info_get_size (PyGIBaseInfo *self)
1296 return PYGLIB_PyLong_FromSize_t (g_struct_info_get_size (self->info));
1299 static PyObject *
1300 _wrap_g_struct_info_get_alignment (PyGIBaseInfo *self)
1302 return PYGLIB_PyLong_FromSize_t (g_struct_info_get_alignment (self->info));
1305 static PyObject *
1306 _wrap_g_struct_info_is_gtype_struct (PyGIBaseInfo *self)
1308 return PyBool_FromLong (g_struct_info_is_gtype_struct (self->info));
1311 static PyObject *
1312 _wrap_g_struct_info_is_foreign (PyGIBaseInfo *self)
1314 return PyBool_FromLong (g_struct_info_is_foreign (self->info));
1317 static PyMethodDef _PyGIStructInfo_methods[] = {
1318 { "get_fields", (PyCFunction) _wrap_g_struct_info_get_fields, METH_NOARGS },
1319 { "get_methods", (PyCFunction) _wrap_g_struct_info_get_methods, METH_NOARGS },
1320 { "get_size", (PyCFunction) _wrap_g_struct_info_get_size, METH_NOARGS },
1321 { "get_alignment", (PyCFunction) _wrap_g_struct_info_get_alignment, METH_NOARGS },
1322 { "is_gtype_struct", (PyCFunction) _wrap_g_struct_info_is_gtype_struct, METH_NOARGS },
1323 { "is_foreign", (PyCFunction) _wrap_g_struct_info_is_foreign, METH_NOARGS },
1324 { NULL, NULL, 0 }
1327 gboolean
1328 pygi_g_struct_info_is_simple (GIStructInfo *struct_info)
1330 gboolean is_simple;
1331 gsize n_field_infos;
1332 gsize i;
1334 is_simple = TRUE;
1336 n_field_infos = g_struct_info_get_n_fields (struct_info);
1338 for (i = 0; i < n_field_infos && is_simple; i++) {
1339 GIFieldInfo *field_info;
1340 GITypeInfo *field_type_info;
1341 GITypeTag field_type_tag;
1343 field_info = g_struct_info_get_field (struct_info, i);
1344 field_type_info = g_field_info_get_type (field_info);
1347 field_type_tag = g_type_info_get_tag (field_type_info);
1349 switch (field_type_tag) {
1350 case GI_TYPE_TAG_BOOLEAN:
1351 case GI_TYPE_TAG_INT8:
1352 case GI_TYPE_TAG_UINT8:
1353 case GI_TYPE_TAG_INT16:
1354 case GI_TYPE_TAG_UINT16:
1355 case GI_TYPE_TAG_INT32:
1356 case GI_TYPE_TAG_UINT32:
1357 case GI_TYPE_TAG_INT64:
1358 case GI_TYPE_TAG_UINT64:
1359 case GI_TYPE_TAG_FLOAT:
1360 case GI_TYPE_TAG_DOUBLE:
1361 case GI_TYPE_TAG_UNICHAR:
1362 if (g_type_info_is_pointer (field_type_info)) {
1363 is_simple = FALSE;
1365 break;
1366 case GI_TYPE_TAG_VOID:
1367 case GI_TYPE_TAG_GTYPE:
1368 case GI_TYPE_TAG_ERROR:
1369 case GI_TYPE_TAG_UTF8:
1370 case GI_TYPE_TAG_FILENAME:
1371 case GI_TYPE_TAG_ARRAY:
1372 case GI_TYPE_TAG_GLIST:
1373 case GI_TYPE_TAG_GSLIST:
1374 case GI_TYPE_TAG_GHASH:
1375 is_simple = FALSE;
1376 break;
1377 case GI_TYPE_TAG_INTERFACE:
1379 GIBaseInfo *info;
1380 GIInfoType info_type;
1382 info = g_type_info_get_interface (field_type_info);
1383 info_type = g_base_info_get_type (info);
1385 switch (info_type) {
1386 case GI_INFO_TYPE_STRUCT:
1387 if (g_type_info_is_pointer (field_type_info)) {
1388 is_simple = FALSE;
1389 } else {
1390 is_simple = pygi_g_struct_info_is_simple ( (GIStructInfo *) info);
1392 break;
1393 case GI_INFO_TYPE_UNION:
1394 /* TODO */
1395 is_simple = FALSE;
1396 break;
1397 case GI_INFO_TYPE_ENUM:
1398 case GI_INFO_TYPE_FLAGS:
1399 if (g_type_info_is_pointer (field_type_info)) {
1400 is_simple = FALSE;
1402 break;
1403 case GI_INFO_TYPE_BOXED:
1404 case GI_INFO_TYPE_OBJECT:
1405 case GI_INFO_TYPE_CALLBACK:
1406 case GI_INFO_TYPE_INTERFACE:
1407 is_simple = FALSE;
1408 break;
1409 case GI_INFO_TYPE_VFUNC:
1410 case GI_INFO_TYPE_INVALID:
1411 case GI_INFO_TYPE_FUNCTION:
1412 case GI_INFO_TYPE_CONSTANT:
1413 case GI_INFO_TYPE_VALUE:
1414 case GI_INFO_TYPE_SIGNAL:
1415 case GI_INFO_TYPE_PROPERTY:
1416 case GI_INFO_TYPE_FIELD:
1417 case GI_INFO_TYPE_ARG:
1418 case GI_INFO_TYPE_TYPE:
1419 case GI_INFO_TYPE_UNRESOLVED:
1420 default:
1421 g_assert_not_reached();
1422 break;
1425 g_base_info_unref (info);
1426 break;
1430 g_base_info_unref ( (GIBaseInfo *) field_type_info);
1431 g_base_info_unref ( (GIBaseInfo *) field_info);
1434 return is_simple;
1438 /* EnumInfo */
1439 PYGLIB_DEFINE_TYPE ("gi.EnumInfo", PyGIEnumInfo_Type, PyGIBaseInfo);
1441 static PyObject *
1442 _wrap_g_enum_info_get_values (PyGIBaseInfo *self)
1444 return _make_infos_tuple (self, g_enum_info_get_n_values, g_enum_info_get_value);
1447 static PyObject *
1448 _wrap_g_enum_info_is_flags (PyGIBaseInfo *self)
1450 GIInfoType info_type = g_base_info_get_type ((GIBaseInfo *) self->info);
1452 if (info_type == GI_INFO_TYPE_ENUM) {
1453 Py_RETURN_FALSE;
1454 } else if (info_type == GI_INFO_TYPE_FLAGS) {
1455 Py_RETURN_TRUE;
1456 } else {
1457 g_assert_not_reached();
1461 static PyObject *
1462 _wrap_g_enum_info_get_methods (PyGIBaseInfo *self)
1464 return _make_infos_tuple (self, g_enum_info_get_n_methods, g_enum_info_get_method);
1467 static PyObject *
1468 _wrap_g_enum_info_get_storage_type (PyGIBaseInfo *self)
1470 return PYGLIB_PyLong_FromLong (g_enum_info_get_storage_type ((GIBaseInfo *) self->info));
1473 static PyMethodDef _PyGIEnumInfo_methods[] = {
1474 { "get_values", (PyCFunction) _wrap_g_enum_info_get_values, METH_NOARGS },
1475 { "is_flags", (PyCFunction) _wrap_g_enum_info_is_flags, METH_NOARGS },
1476 { "get_methods", (PyCFunction) _wrap_g_enum_info_get_methods, METH_NOARGS },
1477 { "get_storage_type", (PyCFunction) _wrap_g_enum_info_get_storage_type, METH_NOARGS },
1478 { NULL, NULL, 0 }
1482 /* ObjectInfo */
1483 PYGLIB_DEFINE_TYPE ("ObjectInfo", PyGIObjectInfo_Type, PyGIBaseInfo);
1485 static PyObject *
1486 _wrap_g_object_info_get_parent (PyGIBaseInfo *self)
1488 return _get_child_info (self, g_object_info_get_parent);
1491 static PyObject *
1492 _wrap_g_object_info_get_methods (PyGIBaseInfo *self)
1494 return _make_infos_tuple (self, g_object_info_get_n_methods, g_object_info_get_method);
1497 static PyObject *
1498 _wrap_g_object_info_find_method (PyGIBaseInfo *self, PyObject *py_name)
1500 return _get_child_info_by_name (self, py_name, g_object_info_find_method);
1503 static PyObject *
1504 _wrap_g_object_info_get_fields (PyGIBaseInfo *self)
1506 return _make_infos_tuple (self, g_object_info_get_n_fields, g_object_info_get_field);
1509 static PyObject *
1510 _wrap_g_object_info_get_properties (PyGIBaseInfo *self)
1512 return _make_infos_tuple (self, g_object_info_get_n_properties, g_object_info_get_property);
1515 static PyObject *
1516 _wrap_g_object_info_get_signals (PyGIBaseInfo *self)
1518 return _make_infos_tuple (self, g_object_info_get_n_signals, g_object_info_get_signal);
1521 static PyObject *
1522 _wrap_g_object_info_get_interfaces (PyGIBaseInfo *self)
1524 return _make_infos_tuple (self, g_object_info_get_n_interfaces, g_object_info_get_interface);
1527 static PyObject *
1528 _wrap_g_object_info_get_constants (PyGIBaseInfo *self)
1530 return _make_infos_tuple (self, g_object_info_get_n_constants, g_object_info_get_constant);
1533 static PyObject *
1534 _wrap_g_object_info_get_vfuncs (PyGIBaseInfo *self)
1536 return _make_infos_tuple (self, g_object_info_get_n_vfuncs, g_object_info_get_vfunc);
1539 static PyObject *
1540 _wrap_g_object_info_get_abstract (PyGIBaseInfo *self)
1542 gboolean is_abstract = g_object_info_get_abstract ( (GIObjectInfo*) self->info);
1543 return PyBool_FromLong (is_abstract);
1546 static PyObject *
1547 _wrap_g_object_info_get_type_name (PyGIBaseInfo *self)
1549 return _get_info_string (self, g_object_info_get_type_name);
1552 static PyObject *
1553 _wrap_g_object_info_get_type_init (PyGIBaseInfo *self)
1555 return _get_info_string (self, g_object_info_get_type_init);
1558 static PyObject *
1559 _wrap_g_object_info_get_fundamental (PyGIBaseInfo *self)
1561 return PyBool_FromLong (g_object_info_get_fundamental ( (GIObjectInfo*) self->info));
1564 static PyObject *
1565 _wrap_g_object_info_get_class_struct (PyGIBaseInfo *self)
1567 return _get_child_info (self, g_object_info_get_class_struct);
1570 static PyObject *
1571 _wrap_g_object_info_find_vfunc (PyGIBaseInfo *self, PyObject *py_name)
1573 return _get_child_info_by_name (self, py_name, g_object_info_find_vfunc);
1576 static PyObject *
1577 _wrap_g_object_info_get_unref_function (PyGIBaseInfo *self)
1579 return _get_info_string (self, g_object_info_get_unref_function);
1582 static PyObject *
1583 _wrap_g_object_info_get_ref_function (PyGIBaseInfo *self)
1585 return _get_info_string (self, g_object_info_get_ref_function);
1588 static PyObject *
1589 _wrap_g_object_info_get_set_value_function (PyGIBaseInfo *self)
1591 return _get_info_string (self, g_object_info_get_set_value_function);
1594 static PyObject *
1595 _wrap_g_object_info_get_get_value_function (PyGIBaseInfo *self)
1597 return _get_info_string (self, g_object_info_get_get_value_function);
1600 static PyMethodDef _PyGIObjectInfo_methods[] = {
1601 { "get_parent", (PyCFunction) _wrap_g_object_info_get_parent, METH_NOARGS },
1602 { "get_methods", (PyCFunction) _wrap_g_object_info_get_methods, METH_NOARGS },
1603 { "find_method", (PyCFunction) _wrap_g_object_info_find_method, METH_O },
1604 { "get_fields", (PyCFunction) _wrap_g_object_info_get_fields, METH_NOARGS },
1605 { "get_properties", (PyCFunction) _wrap_g_object_info_get_properties, METH_NOARGS },
1606 { "get_signals", (PyCFunction) _wrap_g_object_info_get_signals, METH_NOARGS },
1607 { "get_interfaces", (PyCFunction) _wrap_g_object_info_get_interfaces, METH_NOARGS },
1608 { "get_constants", (PyCFunction) _wrap_g_object_info_get_constants, METH_NOARGS },
1609 { "get_vfuncs", (PyCFunction) _wrap_g_object_info_get_vfuncs, METH_NOARGS },
1610 { "find_vfunc", (PyCFunction) _wrap_g_object_info_find_vfunc, METH_O },
1611 { "get_abstract", (PyCFunction) _wrap_g_object_info_get_abstract, METH_NOARGS },
1612 { "get_type_name", (PyCFunction) _wrap_g_object_info_get_type_name, METH_NOARGS },
1613 { "get_type_init", (PyCFunction) _wrap_g_object_info_get_type_init, METH_NOARGS },
1614 { "get_fundamental", (PyCFunction) _wrap_g_object_info_get_fundamental, METH_NOARGS },
1615 { "get_class_struct", (PyCFunction) _wrap_g_object_info_get_class_struct, METH_NOARGS },
1616 { "get_unref_function", (PyCFunction) _wrap_g_object_info_get_unref_function, METH_NOARGS },
1617 { "get_ref_function", (PyCFunction) _wrap_g_object_info_get_ref_function, METH_NOARGS },
1618 { "get_set_value_function", (PyCFunction) _wrap_g_object_info_get_set_value_function, METH_NOARGS },
1619 { "get_get_value_function", (PyCFunction) _wrap_g_object_info_get_get_value_function, METH_NOARGS },
1620 { NULL, NULL, 0 }
1624 /* GIInterfaceInfo */
1625 PYGLIB_DEFINE_TYPE ("InterfaceInfo", PyGIInterfaceInfo_Type, PyGIBaseInfo);
1627 static PyObject *
1628 _wrap_g_interface_info_get_methods (PyGIBaseInfo *self)
1630 return _make_infos_tuple (self, g_interface_info_get_n_methods, g_interface_info_get_method);
1633 static PyObject *
1634 _wrap_g_interface_info_find_method (PyGIBaseInfo *self, PyObject *py_name)
1636 return _get_child_info_by_name (self, py_name, g_interface_info_find_method);
1639 static PyObject *
1640 _wrap_g_interface_info_get_constants (PyGIBaseInfo *self)
1642 return _make_infos_tuple (self, g_interface_info_get_n_constants, g_interface_info_get_constant);
1645 static PyObject *
1646 _wrap_g_interface_info_get_vfuncs (PyGIBaseInfo *self)
1648 return _make_infos_tuple (self, g_interface_info_get_n_vfuncs, g_interface_info_get_vfunc);
1651 static PyObject *
1652 _wrap_g_interface_info_find_vfunc (PyGIBaseInfo *self, PyObject *py_name)
1654 return _get_child_info_by_name (self, py_name, g_interface_info_find_vfunc);
1657 static PyObject *
1658 _wrap_g_interface_info_get_prerequisites (PyGIBaseInfo *self)
1660 return _make_infos_tuple (self, g_interface_info_get_n_prerequisites, g_interface_info_get_prerequisite);
1663 static PyObject *
1664 _wrap_g_interface_info_get_properties (PyGIBaseInfo *self)
1666 return _make_infos_tuple (self, g_interface_info_get_n_properties, g_interface_info_get_property);
1669 static PyObject *
1670 _wrap_g_interface_info_get_iface_struct (PyGIBaseInfo *self)
1672 return _get_child_info (self, g_interface_info_get_iface_struct);
1675 static PyObject *
1676 _wrap_g_interface_info_get_signals (PyGIBaseInfo *self)
1678 return _make_infos_tuple (self, g_interface_info_get_n_signals, g_interface_info_get_signal);
1681 static PyObject *
1682 _wrap_g_interface_info_find_signal (PyGIBaseInfo *self, PyObject *py_name)
1684 return _get_child_info_by_name (self, py_name, g_interface_info_find_signal);
1687 static PyMethodDef _PyGIInterfaceInfo_methods[] = {
1688 { "get_prerequisites", (PyCFunction) _wrap_g_interface_info_get_prerequisites, METH_NOARGS },
1689 { "get_properties", (PyCFunction) _wrap_g_interface_info_get_properties, METH_NOARGS },
1690 { "get_methods", (PyCFunction) _wrap_g_interface_info_get_methods, METH_NOARGS },
1691 { "find_method", (PyCFunction) _wrap_g_interface_info_find_method, METH_O },
1692 { "get_signals", (PyCFunction) _wrap_g_interface_info_get_signals, METH_NOARGS },
1693 { "find_signal", (PyCFunction) _wrap_g_interface_info_find_signal, METH_O },
1694 { "get_vfuncs", (PyCFunction) _wrap_g_interface_info_get_vfuncs, METH_NOARGS },
1695 { "get_constants", (PyCFunction) _wrap_g_interface_info_get_constants, METH_NOARGS },
1696 { "get_iface_struct", (PyCFunction) _wrap_g_interface_info_get_iface_struct, METH_NOARGS },
1697 { "find_vfunc", (PyCFunction) _wrap_g_interface_info_find_vfunc, METH_O },
1698 { NULL, NULL, 0 }
1701 /* GIConstantInfo */
1702 PYGLIB_DEFINE_TYPE ("gi.ConstantInfo", PyGIConstantInfo_Type, PyGIBaseInfo);
1704 static PyObject *
1705 _wrap_g_constant_info_get_value (PyGIBaseInfo *self)
1707 GITypeInfo *type_info;
1708 GIArgument value;
1709 PyObject *py_value;
1710 gboolean free_array = FALSE;
1712 if (g_constant_info_get_value ( (GIConstantInfo *) self->info, &value) < 0) {
1713 PyErr_SetString (PyExc_RuntimeError, "unable to get value");
1714 return NULL;
1717 type_info = g_constant_info_get_type ( (GIConstantInfo *) self->info);
1719 if (g_type_info_get_tag (type_info) == GI_TYPE_TAG_ARRAY) {
1720 value.v_pointer = _pygi_argument_to_array (&value, NULL, NULL, NULL,
1721 type_info, &free_array);
1724 py_value = _pygi_argument_to_object (&value, type_info, GI_TRANSFER_NOTHING);
1726 if (free_array) {
1727 g_array_free (value.v_pointer, FALSE);
1730 g_constant_info_free_value (self->info, &value);
1731 g_base_info_unref ( (GIBaseInfo *) type_info);
1733 return py_value;
1736 static PyMethodDef _PyGIConstantInfo_methods[] = {
1737 { "get_value", (PyCFunction) _wrap_g_constant_info_get_value, METH_NOARGS },
1738 { NULL, NULL, 0 }
1741 /* GIValueInfo */
1742 PYGLIB_DEFINE_TYPE ("gi.ValueInfo", PyGIValueInfo_Type, PyGIBaseInfo);
1744 static PyObject *
1745 _wrap_g_value_info_get_value (PyGIBaseInfo *self)
1747 glong value;
1749 value = g_value_info_get_value ( (GIValueInfo *) self->info);
1751 return PYGLIB_PyLong_FromLong (value);
1755 static PyMethodDef _PyGIValueInfo_methods[] = {
1756 { "get_value", (PyCFunction) _wrap_g_value_info_get_value, METH_NOARGS },
1757 { NULL, NULL, 0 }
1761 /* GIFieldInfo */
1762 PYGLIB_DEFINE_TYPE ("gi.FieldInfo", PyGIFieldInfo_Type, PyGIBaseInfo);
1764 static gssize
1765 _struct_field_array_length_marshal (gsize length_index,
1766 void *container_ptr,
1767 void *struct_data_ptr)
1769 gssize array_len = -1;
1770 GIFieldInfo *array_len_field = NULL;
1771 GIArgument arg = {0};
1772 GIBaseInfo *container_info = (GIBaseInfo *)container_ptr;
1774 switch (g_base_info_get_type (container_info)) {
1775 case GI_INFO_TYPE_UNION:
1776 array_len_field = g_union_info_get_field ((GIUnionInfo *)container_info, length_index);
1777 break;
1778 case GI_INFO_TYPE_STRUCT:
1779 array_len_field = g_struct_info_get_field ((GIStructInfo *)container_info, length_index);
1780 break;
1781 case GI_INFO_TYPE_OBJECT:
1782 array_len_field = g_object_info_get_field ((GIObjectInfo *)container_info, length_index);
1783 break;
1784 default:
1785 /* Other types don't have fields. */
1786 g_assert_not_reached();
1789 if (array_len_field == NULL) {
1790 return -1;
1793 if (g_field_info_get_field (array_len_field, struct_data_ptr, &arg)) {
1794 GITypeInfo *array_len_type_info;
1796 array_len_type_info = g_field_info_get_type (array_len_field);
1797 if (array_len_type_info == NULL) {
1798 goto out;
1801 if (!pygi_argument_to_gssize (&arg,
1802 g_type_info_get_tag (array_len_type_info),
1803 &array_len)) {
1804 array_len = -1;
1807 g_base_info_unref (array_len_type_info);
1810 out:
1811 g_base_info_unref (array_len_field);
1812 return array_len;
1815 static gint
1816 _pygi_g_registered_type_info_check_object (GIRegisteredTypeInfo *info,
1817 gboolean is_instance,
1818 PyObject *object)
1820 gint retval;
1822 GType g_type;
1823 PyObject *py_type;
1824 gchar *type_name_expected = NULL;
1825 GIInfoType interface_type;
1827 interface_type = g_base_info_get_type (info);
1828 if ( (interface_type == GI_INFO_TYPE_STRUCT) &&
1829 (g_struct_info_is_foreign ( (GIStructInfo*) info))) {
1830 /* TODO: Could we check is the correct foreign type? */
1831 return 1;
1834 g_type = g_registered_type_info_get_g_type (info);
1835 if (g_type != G_TYPE_NONE) {
1836 py_type = _pygi_type_get_from_g_type (g_type);
1837 } else {
1838 py_type = _pygi_type_import_by_gi_info ( (GIBaseInfo *) info);
1841 if (py_type == NULL) {
1842 return 0;
1845 g_assert (PyType_Check (py_type));
1847 if (is_instance) {
1848 retval = PyObject_IsInstance (object, py_type);
1849 if (!retval) {
1850 type_name_expected = _pygi_g_base_info_get_fullname (
1851 (GIBaseInfo *) info);
1853 } else {
1854 if (!PyObject_Type (py_type)) {
1855 type_name_expected = "type";
1856 retval = 0;
1857 } else if (!PyType_IsSubtype ( (PyTypeObject *) object,
1858 (PyTypeObject *) py_type)) {
1859 type_name_expected = _pygi_g_base_info_get_fullname (
1860 (GIBaseInfo *) info);
1861 retval = 0;
1862 } else {
1863 retval = 1;
1867 Py_DECREF (py_type);
1869 if (!retval) {
1870 PyTypeObject *object_type;
1872 if (type_name_expected == NULL) {
1873 return -1;
1876 object_type = (PyTypeObject *) PyObject_Type (object);
1877 if (object_type == NULL) {
1878 return -1;
1881 PyErr_Format (PyExc_TypeError, "Must be %s, not %s",
1882 type_name_expected, object_type->tp_name);
1884 g_free (type_name_expected);
1887 return retval;
1890 static PyObject *
1891 _wrap_g_field_info_get_value (PyGIBaseInfo *self,
1892 PyObject *args)
1894 PyObject *instance;
1895 GIBaseInfo *container_info;
1896 GIInfoType container_info_type;
1897 gpointer pointer;
1898 GITypeInfo *field_type_info;
1899 GIArgument value;
1900 PyObject *py_value = NULL;
1901 gboolean free_array = FALSE;
1903 memset(&value, 0, sizeof(GIArgument));
1905 if (!PyArg_ParseTuple (args, "O:FieldInfo.get_value", &instance)) {
1906 return NULL;
1909 container_info = g_base_info_get_container (self->info);
1910 g_assert (container_info != NULL);
1912 /* Check the instance. */
1913 if (!_pygi_g_registered_type_info_check_object ( (GIRegisteredTypeInfo *) container_info, TRUE, instance)) {
1914 _PyGI_ERROR_PREFIX ("argument 1: ");
1915 return NULL;
1918 /* Get the pointer to the container. */
1919 container_info_type = g_base_info_get_type (container_info);
1920 switch (container_info_type) {
1921 case GI_INFO_TYPE_UNION:
1922 case GI_INFO_TYPE_STRUCT:
1923 pointer = pyg_boxed_get (instance, void);
1924 break;
1925 case GI_INFO_TYPE_OBJECT:
1926 pointer = pygobject_get (instance);
1927 break;
1928 default:
1929 /* Other types don't have fields. */
1930 g_assert_not_reached();
1933 /* Get the field's value. */
1934 field_type_info = g_field_info_get_type ( (GIFieldInfo *) self->info);
1936 /* A few types are not handled by g_field_info_get_field, so do it here. */
1937 if (!g_type_info_is_pointer (field_type_info)
1938 && g_type_info_get_tag (field_type_info) == GI_TYPE_TAG_INTERFACE) {
1939 GIBaseInfo *info;
1940 GIInfoType info_type;
1942 if (! (g_field_info_get_flags ( (GIFieldInfo *) self->info) & GI_FIELD_IS_READABLE)) {
1943 PyErr_SetString (PyExc_RuntimeError, "field is not readable");
1944 goto out;
1947 info = g_type_info_get_interface (field_type_info);
1949 info_type = g_base_info_get_type (info);
1951 g_base_info_unref (info);
1953 switch (info_type) {
1954 case GI_INFO_TYPE_UNION:
1955 PyErr_SetString (PyExc_NotImplementedError, "getting an union is not supported yet");
1956 goto out;
1957 case GI_INFO_TYPE_STRUCT:
1959 gsize offset;
1961 offset = g_field_info_get_offset ( (GIFieldInfo *) self->info);
1963 value.v_pointer = (char*) pointer + offset;
1965 goto argument_to_object;
1967 default:
1968 /* Fallback. */
1969 break;
1973 if (!g_field_info_get_field ( (GIFieldInfo *) self->info, pointer, &value)) {
1974 PyErr_SetString (PyExc_RuntimeError, "unable to get the value");
1975 goto out;
1978 if (g_type_info_get_tag (field_type_info) == GI_TYPE_TAG_ARRAY) {
1979 value.v_pointer = _pygi_argument_to_array (&value,
1980 _struct_field_array_length_marshal,
1981 container_info,
1982 pointer,
1983 field_type_info,
1984 &free_array);
1987 argument_to_object:
1988 py_value = _pygi_argument_to_object (&value, field_type_info, GI_TRANSFER_NOTHING);
1990 if (free_array) {
1991 g_array_free (value.v_pointer, FALSE);
1994 out:
1995 g_base_info_unref ( (GIBaseInfo *) field_type_info);
1997 return py_value;
2000 static PyObject *
2001 _wrap_g_field_info_set_value (PyGIBaseInfo *self,
2002 PyObject *args)
2004 PyObject *instance;
2005 PyObject *py_value;
2006 GIBaseInfo *container_info;
2007 GIInfoType container_info_type;
2008 gpointer pointer;
2009 GITypeInfo *field_type_info;
2010 GIArgument value;
2011 PyObject *retval = NULL;
2013 if (!PyArg_ParseTuple (args, "OO:FieldInfo.set_value", &instance, &py_value)) {
2014 return NULL;
2017 container_info = g_base_info_get_container (self->info);
2018 g_assert (container_info != NULL);
2020 /* Check the instance. */
2021 if (!_pygi_g_registered_type_info_check_object ( (GIRegisteredTypeInfo *) container_info, TRUE, instance)) {
2022 _PyGI_ERROR_PREFIX ("argument 1: ");
2023 return NULL;
2026 /* Get the pointer to the container. */
2027 container_info_type = g_base_info_get_type (container_info);
2028 switch (container_info_type) {
2029 case GI_INFO_TYPE_UNION:
2030 case GI_INFO_TYPE_STRUCT:
2031 pointer = pyg_boxed_get (instance, void);
2032 break;
2033 case GI_INFO_TYPE_OBJECT:
2034 pointer = pygobject_get (instance);
2035 break;
2036 default:
2037 /* Other types don't have fields. */
2038 g_assert_not_reached();
2041 field_type_info = g_field_info_get_type ( (GIFieldInfo *) self->info);
2043 /* Set the field's value. */
2044 /* A few types are not handled by g_field_info_set_field, so do it here. */
2045 if (!g_type_info_is_pointer (field_type_info)
2046 && g_type_info_get_tag (field_type_info) == GI_TYPE_TAG_INTERFACE) {
2047 GIBaseInfo *info;
2048 GIInfoType info_type;
2050 if (! (g_field_info_get_flags ( (GIFieldInfo *) self->info) & GI_FIELD_IS_WRITABLE)) {
2051 PyErr_SetString (PyExc_RuntimeError, "field is not writable");
2052 goto out;
2055 info = g_type_info_get_interface (field_type_info);
2057 info_type = g_base_info_get_type (info);
2059 switch (info_type) {
2060 case GI_INFO_TYPE_UNION:
2061 PyErr_SetString (PyExc_NotImplementedError, "setting an union is not supported yet");
2062 goto out;
2063 case GI_INFO_TYPE_STRUCT:
2065 gboolean is_simple;
2066 gsize offset;
2067 gssize size;
2069 is_simple = pygi_g_struct_info_is_simple ( (GIStructInfo *) info);
2071 if (!is_simple) {
2072 PyErr_SetString (PyExc_TypeError,
2073 "cannot set a structure which has no well-defined ownership transfer rules");
2074 g_base_info_unref (info);
2075 goto out;
2078 value = _pygi_argument_from_object (py_value, field_type_info, GI_TRANSFER_NOTHING);
2079 if (PyErr_Occurred()) {
2080 g_base_info_unref (info);
2081 goto out;
2084 offset = g_field_info_get_offset ( (GIFieldInfo *) self->info);
2085 size = g_struct_info_get_size ( (GIStructInfo *) info);
2086 g_assert (size > 0);
2088 g_memmove ((char*) pointer + offset, value.v_pointer, size);
2090 g_base_info_unref (info);
2092 retval = Py_None;
2093 goto out;
2095 default:
2096 /* Fallback. */
2097 break;
2100 g_base_info_unref (info);
2101 } else if (g_type_info_is_pointer (field_type_info)
2102 && (g_type_info_get_tag (field_type_info) == GI_TYPE_TAG_VOID
2103 || g_type_info_get_tag (field_type_info) == GI_TYPE_TAG_UTF8)) {
2104 int offset;
2105 value = _pygi_argument_from_object (py_value, field_type_info, GI_TRANSFER_NOTHING);
2106 if (PyErr_Occurred()) {
2107 goto out;
2110 offset = g_field_info_get_offset ((GIFieldInfo *) self->info);
2111 G_STRUCT_MEMBER (gpointer, pointer, offset) = (gpointer)value.v_pointer;
2113 retval = Py_None;
2114 goto out;
2117 value = _pygi_argument_from_object (py_value, field_type_info, GI_TRANSFER_EVERYTHING);
2118 if (PyErr_Occurred()) {
2119 goto out;
2122 if (!g_field_info_set_field ( (GIFieldInfo *) self->info, pointer, &value)) {
2123 _pygi_argument_release (&value, field_type_info, GI_TRANSFER_NOTHING, GI_DIRECTION_IN);
2124 PyErr_SetString (PyExc_RuntimeError, "unable to set value for field");
2125 goto out;
2128 retval = Py_None;
2130 out:
2131 g_base_info_unref ( (GIBaseInfo *) field_type_info);
2133 Py_XINCREF (retval);
2134 return retval;
2137 static PyObject *
2138 _wrap_g_field_info_get_flags (PyGIBaseInfo *self)
2140 return PYGLIB_PyLong_FromLong (g_field_info_get_flags (self->info));
2143 static PyObject *
2144 _wrap_g_field_info_get_size (PyGIBaseInfo *self)
2146 return PYGLIB_PyLong_FromLong (g_field_info_get_size (self->info));
2149 static PyObject *
2150 _wrap_g_field_info_get_offset (PyGIBaseInfo *self)
2152 return PYGLIB_PyLong_FromLong (g_field_info_get_offset (self->info));
2155 static PyObject *
2156 _wrap_g_field_info_get_type (PyGIBaseInfo *self)
2158 return _get_child_info (self, g_field_info_get_type);
2161 static PyMethodDef _PyGIFieldInfo_methods[] = {
2162 { "get_value", (PyCFunction) _wrap_g_field_info_get_value, METH_VARARGS },
2163 { "set_value", (PyCFunction) _wrap_g_field_info_set_value, METH_VARARGS },
2164 { "get_flags", (PyCFunction) _wrap_g_field_info_get_flags, METH_VARARGS },
2165 { "get_size", (PyCFunction) _wrap_g_field_info_get_size, METH_VARARGS },
2166 { "get_offset", (PyCFunction) _wrap_g_field_info_get_offset, METH_VARARGS },
2167 { "get_type", (PyCFunction) _wrap_g_field_info_get_type, METH_VARARGS },
2168 { NULL, NULL, 0 }
2172 /* GIUnresolvedInfo */
2173 PYGLIB_DEFINE_TYPE ("gi.UnresolvedInfo", PyGIUnresolvedInfo_Type, PyGIBaseInfo);
2175 static PyMethodDef _PyGIUnresolvedInfo_methods[] = {
2176 { NULL, NULL, 0 }
2179 /* GIVFuncInfo */
2180 PYGLIB_DEFINE_TYPE ("gi.VFuncInfo", PyGIVFuncInfo_Type, PyGICallableInfo);
2182 static PyObject *
2183 _wrap_g_vfunc_info_get_flags (PyGIBaseInfo *self)
2185 return PYGLIB_PyLong_FromLong (g_vfunc_info_get_flags ((GIVFuncInfo *) self->info));
2188 static PyObject *
2189 _wrap_g_vfunc_info_get_offset (PyGIBaseInfo *self)
2191 return PYGLIB_PyLong_FromLong (g_vfunc_info_get_offset ((GIVFuncInfo *) self->info));
2194 static PyObject *
2195 _wrap_g_vfunc_info_get_signal (PyGIBaseInfo *self)
2197 return _get_child_info (self, g_vfunc_info_get_signal);
2200 static PyObject *
2201 _wrap_g_vfunc_info_get_invoker (PyGIBaseInfo *self)
2203 return _get_child_info (self, g_vfunc_info_get_invoker);
2206 static PyMethodDef _PyGIVFuncInfo_methods[] = {
2207 { "get_flags", (PyCFunction) _wrap_g_vfunc_info_get_flags, METH_NOARGS },
2208 { "get_offset", (PyCFunction) _wrap_g_vfunc_info_get_offset, METH_NOARGS },
2209 { "get_signal", (PyCFunction) _wrap_g_vfunc_info_get_signal, METH_NOARGS },
2210 { "get_invoker", (PyCFunction) _wrap_g_vfunc_info_get_invoker, METH_NOARGS },
2211 { NULL, NULL, 0 }
2215 /* GIUnionInfo */
2216 PYGLIB_DEFINE_TYPE ("gi.UnionInfo", PyGIUnionInfo_Type, PyGIBaseInfo);
2218 static PyObject *
2219 _wrap_g_union_info_get_fields (PyGIBaseInfo *self)
2221 return _make_infos_tuple (self, g_union_info_get_n_fields, g_union_info_get_field);
2224 static PyObject *
2225 _wrap_g_union_info_get_methods (PyGIBaseInfo *self)
2227 return _make_infos_tuple (self, g_union_info_get_n_methods, g_union_info_get_method);
2230 static PyObject *
2231 _wrap_g_union_info_get_size (PyGIBaseInfo *self)
2233 return PYGLIB_PyLong_FromSize_t (g_union_info_get_size (self->info));
2236 static PyMethodDef _PyGIUnionInfo_methods[] = {
2237 { "get_fields", (PyCFunction) _wrap_g_union_info_get_fields, METH_NOARGS },
2238 { "get_methods", (PyCFunction) _wrap_g_union_info_get_methods, METH_NOARGS },
2239 { "get_size", (PyCFunction) _wrap_g_union_info_get_size, METH_NOARGS },
2240 { NULL, NULL, 0 }
2243 /* Private */
2245 gchar *
2246 _pygi_g_base_info_get_fullname (GIBaseInfo *info)
2248 GIBaseInfo *container_info;
2249 gchar *fullname;
2251 container_info = g_base_info_get_container (info);
2252 if (container_info != NULL) {
2253 fullname = g_strdup_printf ("%s.%s.%s",
2254 g_base_info_get_namespace (container_info),
2255 _safe_base_info_get_name (container_info),
2256 _safe_base_info_get_name (info));
2257 } else {
2258 fullname = g_strdup_printf ("%s.%s",
2259 g_base_info_get_namespace (info),
2260 _safe_base_info_get_name (info));
2263 if (fullname == NULL) {
2264 PyErr_NoMemory();
2267 return fullname;
2271 void
2272 _pygi_info_register_types (PyObject *m)
2274 #define _PyGI_REGISTER_TYPE(m, type, cname, base) \
2275 Py_TYPE(&type) = &PyType_Type; \
2276 type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE); \
2277 type.tp_weaklistoffset = offsetof(PyGIBaseInfo, inst_weakreflist); \
2278 type.tp_methods = _PyGI##cname##_methods; \
2279 type.tp_base = &base; \
2280 if (PyType_Ready(&type)) \
2281 return; \
2282 if (PyModule_AddObject(m, #cname, (PyObject *)&type)) \
2283 return
2285 Py_TYPE(&PyGIBaseInfo_Type) = &PyType_Type;
2287 PyGIBaseInfo_Type.tp_dealloc = (destructor) _base_info_dealloc;
2288 PyGIBaseInfo_Type.tp_repr = (reprfunc) _base_info_repr;
2289 PyGIBaseInfo_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
2290 PyGIBaseInfo_Type.tp_weaklistoffset = offsetof(PyGIBaseInfo, inst_weakreflist);
2291 PyGIBaseInfo_Type.tp_methods = _PyGIBaseInfo_methods;
2292 PyGIBaseInfo_Type.tp_richcompare = (richcmpfunc)_base_info_richcompare;
2293 PyGIBaseInfo_Type.tp_getset = _base_info_getsets;
2294 PyGIBaseInfo_Type.tp_getattro = (getattrofunc) _base_info_getattro;
2296 if (PyType_Ready(&PyGIBaseInfo_Type))
2297 return;
2298 if (PyModule_AddObject(m, "BaseInfo", (PyObject *)&PyGIBaseInfo_Type))
2299 return;
2301 _PyGI_REGISTER_TYPE (m, PyGICallableInfo_Type, CallableInfo,
2302 PyGIBaseInfo_Type);
2303 PyGICallableInfo_Type.tp_call = (ternaryfunc) _callable_info_call;
2304 PyGICallableInfo_Type.tp_dealloc = (destructor) _callable_info_dealloc;
2306 _PyGI_REGISTER_TYPE (m, PyGIFunctionInfo_Type, FunctionInfo,
2307 PyGICallableInfo_Type);
2308 PyGIFunctionInfo_Type.tp_call = (ternaryfunc) _function_info_call;
2309 PyGIFunctionInfo_Type.tp_descr_get = (descrgetfunc) _function_info_descr_get;
2311 _PyGI_REGISTER_TYPE (m, PyGIVFuncInfo_Type, VFuncInfo,
2312 PyGICallableInfo_Type);
2313 PyGIVFuncInfo_Type.tp_descr_get = (descrgetfunc) _vfunc_info_descr_get;
2315 _PyGI_REGISTER_TYPE (m, PyGISignalInfo_Type, SignalInfo,
2316 PyGICallableInfo_Type);
2318 _PyGI_REGISTER_TYPE (m, PyGIUnresolvedInfo_Type, UnresolvedInfo,
2319 PyGIBaseInfo_Type);
2320 _PyGI_REGISTER_TYPE (m, PyGICallbackInfo_Type, CallbackInfo,
2321 PyGICallableInfo_Type);
2322 _PyGI_REGISTER_TYPE (m, PyGIRegisteredTypeInfo_Type, RegisteredTypeInfo,
2323 PyGIBaseInfo_Type);
2324 _PyGI_REGISTER_TYPE (m, PyGIStructInfo_Type, StructInfo,
2325 PyGIRegisteredTypeInfo_Type);
2326 _PyGI_REGISTER_TYPE (m, PyGIEnumInfo_Type, EnumInfo,
2327 PyGIRegisteredTypeInfo_Type);
2328 _PyGI_REGISTER_TYPE (m, PyGIObjectInfo_Type, ObjectInfo,
2329 PyGIRegisteredTypeInfo_Type);
2330 _PyGI_REGISTER_TYPE (m, PyGIInterfaceInfo_Type, InterfaceInfo,
2331 PyGIRegisteredTypeInfo_Type);
2332 _PyGI_REGISTER_TYPE (m, PyGIConstantInfo_Type, ConstantInfo,
2333 PyGIBaseInfo_Type);
2334 _PyGI_REGISTER_TYPE (m, PyGIValueInfo_Type, ValueInfo,
2335 PyGIBaseInfo_Type);
2336 _PyGI_REGISTER_TYPE (m, PyGIFieldInfo_Type, FieldInfo,
2337 PyGIBaseInfo_Type);
2338 _PyGI_REGISTER_TYPE (m, PyGIUnionInfo_Type, UnionInfo,
2339 PyGIRegisteredTypeInfo_Type);
2340 _PyGI_REGISTER_TYPE (m, PyGIErrorDomainInfo_Type, ErrorDomainInfo,
2341 PyGIBaseInfo_Type);
2342 _PyGI_REGISTER_TYPE (m, PyGIPropertyInfo_Type, PropertyInfo,
2343 PyGIBaseInfo_Type);
2344 _PyGI_REGISTER_TYPE (m, PyGIArgInfo_Type, ArgInfo,
2345 PyGIBaseInfo_Type);
2346 _PyGI_REGISTER_TYPE (m, PyGITypeInfo_Type, TypeInfo,
2347 PyGIBaseInfo_Type);
2349 #undef _PyGI_REGISTER_TYPE
2351 #define _PyGI_ENUM_BEGIN(name) \
2353 const char *__enum_name = #name; \
2354 PyObject *__enum_value = NULL; \
2355 PyObject *__new_enum_cls = NULL; \
2356 PyObject *__enum_instance_dict = PyDict_New(); \
2357 PyObject *__module_name = PyObject_GetAttrString (m, "__name__"); \
2358 PyDict_SetItemString (__enum_instance_dict, "__module__", __module_name); \
2359 Py_DECREF (__module_name);
2361 #define _PyGI_ENUM_ADD_VALUE(prefix, name) \
2362 __enum_value = PYGLIB_PyLong_FromLong (prefix##_##name); \
2363 if (PyDict_SetItemString(__enum_instance_dict, #name, __enum_value)) { \
2364 Py_DECREF (__enum_instance_dict); \
2365 Py_DECREF (__enum_value); \
2366 return; \
2368 Py_DECREF (__enum_value);
2370 #define _PyGI_ENUM_END \
2371 __new_enum_cls = PyObject_CallFunction ((PyObject *)&PyType_Type, "s(O)O", \
2372 __enum_name, (PyObject *)&PyType_Type, \
2373 __enum_instance_dict); \
2374 Py_DECREF (__enum_instance_dict); \
2375 PyModule_AddObject (m, __enum_name, __new_enum_cls); /* steals ref */ \
2379 /* GIDirection */
2380 _PyGI_ENUM_BEGIN (Direction)
2381 _PyGI_ENUM_ADD_VALUE (GI_DIRECTION, IN)
2382 _PyGI_ENUM_ADD_VALUE (GI_DIRECTION, OUT)
2383 _PyGI_ENUM_ADD_VALUE (GI_DIRECTION, INOUT)
2384 _PyGI_ENUM_END
2387 /* GITransfer */
2388 _PyGI_ENUM_BEGIN (Transfer)
2389 _PyGI_ENUM_ADD_VALUE (GI_TRANSFER, NOTHING)
2390 _PyGI_ENUM_ADD_VALUE (GI_TRANSFER, CONTAINER)
2391 _PyGI_ENUM_ADD_VALUE (GI_TRANSFER, EVERYTHING)
2392 _PyGI_ENUM_END
2394 /* GIArrayType */
2395 _PyGI_ENUM_BEGIN (ArrayType)
2396 _PyGI_ENUM_ADD_VALUE (GI_ARRAY_TYPE, C)
2397 _PyGI_ENUM_ADD_VALUE (GI_ARRAY_TYPE, ARRAY)
2398 _PyGI_ENUM_ADD_VALUE (GI_ARRAY_TYPE, PTR_ARRAY)
2399 _PyGI_ENUM_ADD_VALUE (GI_ARRAY_TYPE, BYTE_ARRAY)
2400 _PyGI_ENUM_END
2402 /* GIScopeType */
2403 _PyGI_ENUM_BEGIN (ScopeType)
2404 _PyGI_ENUM_ADD_VALUE (GI_SCOPE_TYPE, INVALID)
2405 _PyGI_ENUM_ADD_VALUE (GI_SCOPE_TYPE, CALL)
2406 _PyGI_ENUM_ADD_VALUE (GI_SCOPE_TYPE, ASYNC)
2407 _PyGI_ENUM_ADD_VALUE (GI_SCOPE_TYPE, NOTIFIED)
2408 _PyGI_ENUM_END
2410 /* GIVFuncInfoFlags */
2411 _PyGI_ENUM_BEGIN (VFuncInfoFlags)
2412 _PyGI_ENUM_ADD_VALUE (GI_VFUNC_MUST, CHAIN_UP)
2413 _PyGI_ENUM_ADD_VALUE (GI_VFUNC_MUST, OVERRIDE)
2414 _PyGI_ENUM_ADD_VALUE (GI_VFUNC_MUST, NOT_OVERRIDE)
2415 _PyGI_ENUM_END
2417 /* GIFieldInfoFlags */
2418 _PyGI_ENUM_BEGIN (FieldInfoFlags)
2419 _PyGI_ENUM_ADD_VALUE (GI_FIELD, IS_READABLE)
2420 _PyGI_ENUM_ADD_VALUE (GI_FIELD, IS_WRITABLE)
2421 _PyGI_ENUM_END
2423 /* GIFunctionInfoFlags */
2424 _PyGI_ENUM_BEGIN (FunctionInfoFlags)
2425 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, IS_METHOD)
2426 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, IS_CONSTRUCTOR)
2427 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, IS_GETTER)
2428 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, IS_SETTER)
2429 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, WRAPS_VFUNC)
2430 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, THROWS)
2431 _PyGI_ENUM_END
2433 /* GITypeTag */
2434 _PyGI_ENUM_BEGIN (TypeTag)
2435 /* Basic types */
2436 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, VOID)
2437 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, BOOLEAN)
2438 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INT8)
2439 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UINT8)
2440 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INT16)
2441 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UINT16)
2442 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INT32)
2443 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UINT32)
2444 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INT64)
2445 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UINT64)
2446 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, FLOAT)
2447 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, DOUBLE)
2448 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, GTYPE)
2449 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UTF8)
2450 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, FILENAME)
2452 /* Non-basic types; compare with G_TYPE_TAG_IS_BASIC */
2453 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, ARRAY)
2454 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INTERFACE)
2455 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, GLIST)
2456 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, GSLIST)
2457 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, GHASH)
2458 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, ERROR)
2460 /* Another basic type */
2461 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UNICHAR)
2462 _PyGI_ENUM_END
2464 /* GIInfoType */
2465 _PyGI_ENUM_BEGIN (InfoType)
2466 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, INVALID)
2467 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, FUNCTION)
2468 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, CALLBACK)
2469 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, STRUCT)
2470 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, BOXED)
2471 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, ENUM)
2472 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, FLAGS)
2473 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, OBJECT)
2474 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, INTERFACE)
2475 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, CONSTANT)
2476 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, INVALID_0)
2477 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, UNION)
2478 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, VALUE)
2479 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, SIGNAL)
2480 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, VFUNC)
2481 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, PROPERTY)
2482 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, FIELD)
2483 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, ARG)
2484 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, TYPE)
2485 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, UNRESOLVED)
2486 _PyGI_ENUM_END
2488 #undef _PyGI_ENUM_BEGIN
2489 #undef _PyGI_ENUM_ADD_VALUE
2490 #undef _PyGI_ENUM_END