Rename PYGLIB_PyImport_ImportModule to pygi_import_module and move to utils
[pygobject.git] / gi / pygi-info.c
blob8a0deaa1215e661a61c3b728a8c10157396563c9
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-python-compat.h"
24 #include "pygi-info.h"
25 #include "pygi-cache.h"
26 #include "pygi-invoke.h"
27 #include "pygi-type.h"
28 #include "pygi-argument.h"
29 #include "pygi-util.h"
30 #include "pygi-basictype.h"
31 #include "pygi-type.h"
33 /* _generate_doc_string
35 * C wrapper to call Python implemented "gi.docstring.generate_doc_string"
37 static PyObject *
38 _generate_doc_string(PyGIBaseInfo *self)
40 static PyObject *_py_generate_doc_string = NULL;
42 if (_py_generate_doc_string == NULL) {
43 PyObject *mod = pygi_import_module ("gi.docstring");
44 if (!mod)
45 return NULL;
47 _py_generate_doc_string = PyObject_GetAttrString (mod, "generate_doc_string");
48 if (_py_generate_doc_string == NULL) {
49 Py_DECREF (mod);
50 return NULL;
52 Py_DECREF (mod);
55 return PyObject_CallFunctionObjArgs (_py_generate_doc_string, self, NULL);
58 static PyObject *
59 _get_info_string (PyGIBaseInfo *self,
60 const gchar* (*get_info_string)(GIBaseInfo*))
62 const gchar *value = get_info_string ((GIBaseInfo*)self->info);
63 if (value == NULL) {
64 Py_RETURN_NONE;
66 return pygi_utf8_to_py (value);
69 static PyObject *
70 _get_child_info (PyGIBaseInfo *self,
71 GIBaseInfo* (*get_child_info)(GIBaseInfo*))
73 GIBaseInfo *info;
74 PyObject *py_info;
76 info = get_child_info ((GIBaseInfo*)self->info);
77 if (info == NULL) {
78 Py_RETURN_NONE;
81 py_info = _pygi_info_new (info);
82 g_base_info_unref (info);
83 return py_info;
87 static PyObject *
88 _get_child_info_by_name (PyGIBaseInfo *self, PyObject *py_name,
89 GIBaseInfo* (*get_child_info_by_name)(GIBaseInfo*, const gchar*))
91 GIBaseInfo *info;
92 PyObject *py_info;
93 char *name;
95 if (!pygi_utf8_from_py (py_name, &name))
96 return NULL;
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 pygi_guint_to_py (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 = pygi_utf8_to_py (escaped);
291 g_free (escaped);
292 return obj;
295 return pygi_utf8_to_py (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 (!pygi_utf8_from_py (arg, &name))
326 return NULL;
328 value = g_base_info_get_attribute (self->info, name);
329 if (value == NULL) {
330 Py_RETURN_NONE;
332 return pygi_utf8_to_py (value);
335 static PyObject *
336 _wrap_g_base_info_get_container (PyGIBaseInfo *self)
338 /* Note: don't use _get_child_info because g_base_info_get_container
339 * is marked as [transfer none] and therefore returns a borrowed ref.
341 GIBaseInfo *info;
343 info = g_base_info_get_container (self->info);
345 if (info == NULL) {
346 Py_RETURN_NONE;
349 return _pygi_info_new (info);
353 static PyMethodDef _PyGIBaseInfo_methods[] = {
354 { "get_type", (PyCFunction) _wrap_g_base_info_get_type, METH_NOARGS },
355 { "get_name", (PyCFunction) _wrap_g_base_info_get_name, METH_NOARGS },
356 { "get_name_unescaped", (PyCFunction) _wrap_g_base_info_get_name_unescaped, METH_NOARGS },
357 { "get_namespace", (PyCFunction) _wrap_g_base_info_get_namespace, METH_NOARGS },
358 { "is_deprecated", (PyCFunction) _wrap_g_base_info_is_deprecated, METH_NOARGS },
359 { "get_attribute", (PyCFunction) _wrap_g_base_info_get_attribute, METH_O },
360 { "get_container", (PyCFunction) _wrap_g_base_info_get_container, METH_NOARGS },
361 { "equal", (PyCFunction) _wrap_g_base_info_equal, METH_O },
362 { NULL, NULL, 0 }
365 /* _base_info_getattro:
367 * The usage of __getattr__ is needed because the get/set method table
368 * does not work for __doc__.
370 static PyObject *
371 _base_info_getattro(PyGIBaseInfo *self, PyObject *name)
373 PyObject *result;
375 static PyObject *docstr;
376 if (docstr == NULL) {
377 docstr= PYGLIB_PyUnicode_InternFromString("__doc__");
378 if (docstr == NULL)
379 return NULL;
382 Py_INCREF (name);
383 PYGLIB_PyUnicode_InternInPlace (&name);
385 if (name == docstr) {
386 result = _generate_doc_string (self);
387 } else {
388 result = PyObject_GenericGetAttr ((PyObject *)self, name);
391 Py_DECREF (name);
392 return result;
395 static PyObject *
396 _base_info_attr_name(PyGIBaseInfo *self, void *closure)
398 return _wrap_g_base_info_get_name (self);
401 static PyObject *
402 _base_info_attr_module(PyGIBaseInfo *self, void *closure)
404 return PYGLIB_PyUnicode_FromFormat ("gi.repository.%s",
405 g_base_info_get_namespace (self->info));
408 static PyGetSetDef _base_info_getsets[] = {
409 { "__name__", (getter)_base_info_attr_name, (setter)0, "Name", NULL},
410 { "__module__", (getter)_base_info_attr_module, (setter)0, "Module name", NULL},
411 { NULL, 0, 0 }
414 PyObject *
415 _pygi_info_new (GIBaseInfo *info)
417 GIInfoType info_type;
418 PyTypeObject *type = NULL;
419 PyGIBaseInfo *self;
421 info_type = g_base_info_get_type (info);
423 switch (info_type)
425 case GI_INFO_TYPE_INVALID:
426 PyErr_SetString (PyExc_RuntimeError, "Invalid info type");
427 return NULL;
428 case GI_INFO_TYPE_FUNCTION:
429 type = &PyGIFunctionInfo_Type;
430 break;
431 case GI_INFO_TYPE_CALLBACK:
432 type = &PyGICallbackInfo_Type;
433 break;
434 case GI_INFO_TYPE_STRUCT:
435 case GI_INFO_TYPE_BOXED:
436 type = &PyGIStructInfo_Type;
437 break;
438 case GI_INFO_TYPE_ENUM:
439 case GI_INFO_TYPE_FLAGS:
440 type = &PyGIEnumInfo_Type;
441 break;
442 case GI_INFO_TYPE_OBJECT:
443 type = &PyGIObjectInfo_Type;
444 break;
445 case GI_INFO_TYPE_INTERFACE:
446 type = &PyGIInterfaceInfo_Type;
447 break;
448 case GI_INFO_TYPE_CONSTANT:
449 type = &PyGIConstantInfo_Type;
450 break;
451 case GI_INFO_TYPE_UNION:
452 type = &PyGIUnionInfo_Type;
453 break;
454 case GI_INFO_TYPE_VALUE:
455 type = &PyGIValueInfo_Type;
456 break;
457 case GI_INFO_TYPE_SIGNAL:
458 type = &PyGISignalInfo_Type;
459 break;
460 case GI_INFO_TYPE_VFUNC:
461 type = &PyGIVFuncInfo_Type;
462 break;
463 case GI_INFO_TYPE_PROPERTY:
464 type = &PyGIPropertyInfo_Type;
465 break;
466 case GI_INFO_TYPE_FIELD:
467 type = &PyGIFieldInfo_Type;
468 break;
469 case GI_INFO_TYPE_ARG:
470 type = &PyGIArgInfo_Type;
471 break;
472 case GI_INFO_TYPE_TYPE:
473 type = &PyGITypeInfo_Type;
474 break;
475 case GI_INFO_TYPE_UNRESOLVED:
476 type = &PyGIUnresolvedInfo_Type;
477 break;
478 default:
479 g_assert_not_reached();
480 break;
483 self = (PyGIBaseInfo *) type->tp_alloc (type, 0);
484 if (self == NULL) {
485 return NULL;
488 self->info = g_base_info_ref (info);
489 self->inst_weakreflist = NULL;
490 self->cache = NULL;
492 return (PyObject *) self;
495 GIBaseInfo *
496 _pygi_object_get_gi_info (PyObject *object,
497 PyTypeObject *type)
499 PyObject *py_info;
500 GIBaseInfo *info = NULL;
502 py_info = PyObject_GetAttrString (object, "__info__");
503 if (py_info == NULL) {
504 return NULL;
506 if (!PyObject_TypeCheck (py_info, type)) {
507 PyErr_Format (PyExc_TypeError, "attribute '__info__' must be %s, not %s",
508 type->tp_name, Py_TYPE(py_info)->tp_name);
509 goto out;
512 info = ( (PyGIBaseInfo *) py_info)->info;
513 g_base_info_ref (info);
515 out:
516 Py_DECREF (py_info);
518 return info;
522 /* CallableInfo */
523 PYGLIB_DEFINE_TYPE ("gi.CallableInfo", PyGICallableInfo_Type, PyGICallableInfo);
525 /* _callable_info_call:
527 * Shared wrapper for invoke which can be bound (instance method or class constructor)
528 * or unbound (function or static method).
530 static PyObject *
531 _callable_info_call (PyGICallableInfo *self, PyObject *args, PyObject *kwargs)
533 /* Insert the bound arg at the beginning of the invoke method args. */
534 if (self->py_bound_arg) {
535 int i;
536 PyObject *result;
537 Py_ssize_t argcount = PyTuple_Size (args);
538 PyObject *newargs = PyTuple_New (argcount + 1);
539 if (newargs == NULL)
540 return NULL;
542 Py_INCREF (self->py_bound_arg);
543 PyTuple_SET_ITEM (newargs, 0, self->py_bound_arg);
545 for (i = 0; i < argcount; i++) {
546 PyObject *v = PyTuple_GET_ITEM (args, i);
547 Py_XINCREF (v);
548 PyTuple_SET_ITEM (newargs, i+1, v);
551 /* Invoke with the original GI info struct this wrapper was based upon.
552 * This is necessary to maintain the same cache for all bound versions.
554 result = _wrap_g_callable_info_invoke ((PyGIBaseInfo *)self->py_unbound_info,
555 newargs, kwargs);
556 Py_DECREF (newargs);
557 return result;
559 } else {
560 /* We should never have an unbound info when calling when calling invoke
561 * at this point because the descriptor implementation on sub-classes
562 * should return "self" not a copy when there is no bound arg.
564 g_assert (self->py_unbound_info == NULL);
565 return _wrap_g_callable_info_invoke ((PyGIBaseInfo *)self, args, kwargs);
570 /* _function_info_call:
572 * Specialization of _callable_info_call for GIFunctionInfo which
573 * handles constructor error conditions.
575 static PyObject *
576 _function_info_call (PyGICallableInfo *self, PyObject *args, PyObject *kwargs)
578 if (self->py_bound_arg) {
579 GIFunctionInfoFlags flags;
581 /* Ensure constructors are only called as class methods on the class
582 * implementing the constructor and not on sub-classes.
584 flags = g_function_info_get_flags ( (GIFunctionInfo*) self->base.info);
585 if (flags & GI_FUNCTION_IS_CONSTRUCTOR) {
586 PyObject *py_str_name;
587 const gchar *str_name;
588 GIBaseInfo *container_info = g_base_info_get_container (self->base.info);
589 g_assert (container_info != NULL);
591 py_str_name = PyObject_GetAttrString (self->py_bound_arg, "__name__");
592 if (py_str_name == NULL)
593 return NULL;
595 if (PyUnicode_Check (py_str_name) ) {
596 PyObject *tmp = PyUnicode_AsUTF8String (py_str_name);
597 Py_DECREF (py_str_name);
598 py_str_name = tmp;
601 #if PY_VERSION_HEX < 0x03000000
602 str_name = PyString_AsString (py_str_name);
603 #else
604 str_name = PyBytes_AsString (py_str_name);
605 #endif
607 if (strcmp (str_name, _safe_base_info_get_name (container_info))) {
608 PyErr_Format (PyExc_TypeError,
609 "%s constructor cannot be used to create instances of "
610 "a subclass %s",
611 _safe_base_info_get_name (container_info),
612 str_name);
613 Py_DECREF (py_str_name);
614 return NULL;
616 Py_DECREF (py_str_name);
620 return _callable_info_call (self, args, kwargs);
623 /* _new_bound_callable_info
625 * Utility function for sub-classes to create a bound version of themself.
627 static PyGICallableInfo *
628 _new_bound_callable_info (PyGICallableInfo *self, PyObject *bound_arg)
630 PyGICallableInfo *new_self;
632 /* Return self if this is already bound or there is nothing passed to bind. */
633 if (self->py_bound_arg != NULL || bound_arg == NULL || bound_arg == Py_None) {
634 Py_INCREF ((PyObject *)self);
635 return self;
638 new_self = (PyGICallableInfo *)_pygi_info_new (self->base.info);
639 if (new_self == NULL)
640 return NULL;
642 Py_INCREF ((PyObject *)self);
643 new_self->py_unbound_info = (struct PyGICallableInfo *)self;
645 Py_INCREF (bound_arg);
646 new_self->py_bound_arg = bound_arg;
648 return new_self;
651 /* _function_info_descr_get
653 * Descriptor protocol implementation for functions, methods, and constructors.
655 static PyObject *
656 _function_info_descr_get (PyGICallableInfo *self, PyObject *obj, PyObject *type) {
657 GIFunctionInfoFlags flags;
658 PyObject *bound_arg = NULL;
660 flags = g_function_info_get_flags ( (GIFunctionInfo*) self->base.info);
661 if (flags & GI_FUNCTION_IS_CONSTRUCTOR) {
662 if (type == NULL)
663 bound_arg = (PyObject *)(Py_TYPE(obj));
664 else
665 bound_arg = type;
666 } else if (flags & GI_FUNCTION_IS_METHOD) {
667 bound_arg = obj;
670 return (PyObject *)_new_bound_callable_info (self, bound_arg);
673 /* _vfunc_info_descr_get
675 * Descriptor protocol implementation for virtual functions.
677 static PyObject *
678 _vfunc_info_descr_get (PyGICallableInfo *self, PyObject *obj, PyObject *type) {
679 PyObject *result;
680 PyObject *bound_arg = NULL;
682 bound_arg = PyObject_GetAttrString (type, "__gtype__");
683 if (bound_arg == NULL)
684 return NULL;
686 /* _new_bound_callable_info adds its own ref so free the one from GetAttrString */
687 result = (PyObject *)_new_bound_callable_info (self, bound_arg);
688 Py_DECREF (bound_arg);
689 return result;
692 static void
693 _callable_info_dealloc (PyGICallableInfo *self)
695 Py_CLEAR (self->py_unbound_info);
696 Py_CLEAR (self->py_bound_arg);
698 PyGIBaseInfo_Type.tp_dealloc ((PyObject *) self);
701 static PyObject *
702 _wrap_g_callable_info_get_arguments (PyGIBaseInfo *self)
704 return _make_infos_tuple (self, g_callable_info_get_n_args, g_callable_info_get_arg);
707 static PyObject *
708 _wrap_g_callable_info_get_return_type (PyGIBaseInfo *self)
710 return _get_child_info (self, g_callable_info_get_return_type);
713 static PyObject *
714 _wrap_g_callable_info_get_caller_owns (PyGIBaseInfo *self)
716 return pygi_guint_to_py (
717 g_callable_info_get_caller_owns (self->info) );
720 static PyObject *
721 _wrap_g_callable_info_may_return_null (PyGIBaseInfo *self)
723 return pygi_gboolean_to_py (
724 g_callable_info_may_return_null (self->info) );
727 static PyObject *
728 _wrap_g_callable_info_skip_return (PyGIBaseInfo *self)
730 return pygi_gboolean_to_py (g_callable_info_skip_return (self->info));
733 static PyObject *
734 _wrap_g_callable_info_get_return_attribute (PyGIBaseInfo *self, PyObject *py_name)
736 gchar *name;
737 const gchar *attr;
739 if (!pygi_utf8_from_py (py_name, &name))
740 return NULL;
742 attr = g_callable_info_get_return_attribute (self->info, name);
743 if (attr) {
744 return pygi_utf8_to_py (
745 g_callable_info_get_return_attribute (self->info, name));
746 } else {
747 PyErr_Format(PyExc_AttributeError, "return attribute %s not found", name);
748 return NULL;
752 static PyObject *
753 _wrap_g_callable_info_can_throw_gerror (PyGIBaseInfo *self)
755 if (g_callable_info_can_throw_gerror (self->info))
756 Py_RETURN_TRUE;
757 else
758 Py_RETURN_FALSE;
761 static PyMethodDef _PyGICallableInfo_methods[] = {
762 { "invoke", (PyCFunction) _wrap_g_callable_info_invoke, METH_VARARGS | METH_KEYWORDS },
763 { "get_arguments", (PyCFunction) _wrap_g_callable_info_get_arguments, METH_NOARGS },
764 { "get_return_type", (PyCFunction) _wrap_g_callable_info_get_return_type, METH_NOARGS },
765 { "get_caller_owns", (PyCFunction) _wrap_g_callable_info_get_caller_owns, METH_NOARGS },
766 { "may_return_null", (PyCFunction) _wrap_g_callable_info_may_return_null, METH_NOARGS },
767 { "skip_return", (PyCFunction) _wrap_g_callable_info_skip_return, METH_NOARGS },
768 { "get_return_attribute", (PyCFunction) _wrap_g_callable_info_get_return_attribute, METH_O },
769 { "can_throw_gerror", (PyCFunction) _wrap_g_callable_info_can_throw_gerror, METH_NOARGS },
770 { NULL, NULL, 0 }
773 /* CallbackInfo */
774 PYGLIB_DEFINE_TYPE ("gi.CallbackInfo", PyGICallbackInfo_Type, PyGICallableInfo);
776 static PyMethodDef _PyGICallbackInfo_methods[] = {
777 { NULL, NULL, 0 }
780 /* ErrorDomainInfo */
781 PYGLIB_DEFINE_TYPE ("gi.ErrorDomainInfo", PyGIErrorDomainInfo_Type, PyGIBaseInfo);
783 static PyMethodDef _PyGIErrorDomainInfo_methods[] = {
784 { NULL, NULL, 0 }
787 /* SignalInfo */
788 PYGLIB_DEFINE_TYPE ("gi.SignalInfo", PyGISignalInfo_Type, PyGICallableInfo);
790 static PyObject *
791 _wrap_g_signal_info_get_flags (PyGIBaseInfo *self)
793 return pygi_guint_to_py (
794 g_signal_info_get_flags ((GISignalInfo *)self->info) );
797 static PyObject *
798 _wrap_g_signal_info_get_class_closure (PyGIBaseInfo *self)
800 return _get_child_info (self, g_signal_info_get_class_closure);
803 static PyObject *
804 _wrap_g_signal_info_true_stops_emit (PyGIBaseInfo *self)
806 return pygi_gboolean_to_py (
807 g_signal_info_true_stops_emit ((GISignalInfo *)self->info) );
810 static PyMethodDef _PyGISignalInfo_methods[] = {
811 { "get_flags", (PyCFunction) _wrap_g_signal_info_get_flags, METH_NOARGS },
812 { "get_class_closure", (PyCFunction) _wrap_g_signal_info_get_class_closure, METH_NOARGS },
813 { "true_stops_emit", (PyCFunction) _wrap_g_signal_info_true_stops_emit, METH_NOARGS },
814 { NULL, NULL, 0 }
817 /* PropertyInfo */
818 PYGLIB_DEFINE_TYPE ("gi.PropertyInfo", PyGIPropertyInfo_Type, PyGIBaseInfo);
820 static PyObject *
821 _wrap_g_property_info_get_flags (PyGIBaseInfo *self)
823 return pygi_guint_to_py (
824 g_property_info_get_flags ((GIPropertyInfo *)self->info) );
827 static PyObject *
828 _wrap_g_property_info_get_type (PyGIBaseInfo *self)
830 return _get_child_info (self, g_property_info_get_type);
833 static PyObject *
834 _wrap_g_property_info_get_ownership_transfer (PyGIBaseInfo *self)
836 return pygi_guint_to_py (
837 g_property_info_get_ownership_transfer ((GIPropertyInfo *)self->info) );
840 static PyMethodDef _PyGIPropertyInfo_methods[] = {
841 { "get_flags", (PyCFunction) _wrap_g_property_info_get_flags, METH_NOARGS },
842 { "get_type", (PyCFunction) _wrap_g_property_info_get_type, METH_NOARGS },
843 { "get_ownership_transfer", (PyCFunction) _wrap_g_property_info_get_ownership_transfer, METH_NOARGS },
844 { NULL, NULL, 0 }
848 /* ArgInfo */
849 PYGLIB_DEFINE_TYPE ("gi.ArgInfo", PyGIArgInfo_Type, PyGIBaseInfo);
851 static PyObject *
852 _wrap_g_arg_info_get_direction (PyGIBaseInfo *self)
854 return pygi_guint_to_py (
855 g_arg_info_get_direction ((GIArgInfo*)self->info) );
858 static PyObject *
859 _wrap_g_arg_info_is_caller_allocates (PyGIBaseInfo *self)
861 return pygi_gboolean_to_py (
862 g_arg_info_is_caller_allocates ((GIArgInfo*)self->info) );
865 static PyObject *
866 _wrap_g_arg_info_is_return_value (PyGIBaseInfo *self)
868 return pygi_gboolean_to_py (
869 g_arg_info_is_return_value ((GIArgInfo*)self->info) );
872 static PyObject *
873 _wrap_g_arg_info_is_optional (PyGIBaseInfo *self)
875 return pygi_gboolean_to_py (
876 g_arg_info_is_optional ((GIArgInfo*)self->info) );
879 static PyObject *
880 _wrap_g_arg_info_may_be_null (PyGIBaseInfo *self)
882 return pygi_gboolean_to_py (
883 g_arg_info_may_be_null ((GIArgInfo*)self->info) );
886 static PyObject *
887 _wrap_g_arg_info_get_ownership_transfer (PyGIBaseInfo *self)
889 return pygi_guint_to_py (
890 g_arg_info_get_ownership_transfer ((GIArgInfo *)self->info) );
893 static PyObject *
894 _wrap_g_arg_info_get_scope (PyGIBaseInfo *self)
896 return pygi_guint_to_py (
897 g_arg_info_get_scope ((GIArgInfo *)self->info) );
900 static PyObject *
901 _wrap_g_arg_info_get_closure (PyGIBaseInfo *self)
903 return pygi_gint_to_py (
904 g_arg_info_get_closure ((GIArgInfo *)self->info) );
907 static PyObject *
908 _wrap_g_arg_info_get_destroy (PyGIBaseInfo *self)
910 return pygi_gint_to_py (
911 g_arg_info_get_destroy ((GIArgInfo *)self->info) );
914 static PyObject *
915 _wrap_g_arg_info_get_type (PyGIBaseInfo *self)
917 return _get_child_info (self, g_arg_info_get_type);
920 static PyMethodDef _PyGIArgInfo_methods[] = {
921 { "get_direction", (PyCFunction) _wrap_g_arg_info_get_direction, METH_NOARGS },
922 { "is_caller_allocates", (PyCFunction) _wrap_g_arg_info_is_caller_allocates, METH_NOARGS },
923 { "is_return_value", (PyCFunction) _wrap_g_arg_info_is_return_value, METH_NOARGS },
924 { "is_optional", (PyCFunction) _wrap_g_arg_info_is_optional, METH_NOARGS },
925 { "may_be_null", (PyCFunction) _wrap_g_arg_info_may_be_null, METH_NOARGS },
926 { "get_ownership_transfer", (PyCFunction) _wrap_g_arg_info_get_ownership_transfer, METH_NOARGS },
927 { "get_scope", (PyCFunction) _wrap_g_arg_info_get_scope, METH_NOARGS },
928 { "get_closure", (PyCFunction) _wrap_g_arg_info_get_closure, METH_NOARGS },
929 { "get_destroy", (PyCFunction) _wrap_g_arg_info_get_destroy, METH_NOARGS },
930 { "get_type", (PyCFunction) _wrap_g_arg_info_get_type, METH_NOARGS },
931 { NULL, NULL, 0 }
935 /* TypeInfo */
936 PYGLIB_DEFINE_TYPE ("gi.TypeInfo", PyGITypeInfo_Type, PyGIBaseInfo);
938 static PyObject *
939 _wrap_g_type_info_is_pointer (PyGIBaseInfo *self)
941 return pygi_gboolean_to_py (g_type_info_is_pointer (self->info));
944 static PyObject *
945 _wrap_g_type_info_get_tag (PyGIBaseInfo *self)
947 return pygi_guint_to_py (g_type_info_get_tag (self->info));
950 static PyObject *
951 _wrap_g_type_info_get_tag_as_string (PyGIBaseInfo *self)
953 GITypeTag tag = g_type_info_get_tag (self->info);
954 return pygi_utf8_to_py (g_type_tag_to_string(tag));
957 static PyObject *
958 _wrap_g_type_info_get_param_type (PyGIBaseInfo *self, PyObject *py_n)
960 GIBaseInfo *info;
961 PyObject *py_info;
962 gint n;
964 if (!pygi_gint_from_py (py_n, &n))
965 return NULL;
967 info = (GIBaseInfo *) g_type_info_get_param_type ( (GITypeInfo *) self->info, n);
968 if (info == NULL) {
969 Py_RETURN_NONE;
972 py_info = _pygi_info_new (info);
973 g_base_info_unref (info);
974 return py_info;
977 static PyObject *
978 _wrap_g_type_info_get_interface (PyGIBaseInfo *self)
980 return _get_child_info (self, g_type_info_get_interface);
983 static PyObject *
984 _wrap_g_type_info_get_array_length (PyGIBaseInfo *self)
986 return pygi_gint_to_py (g_type_info_get_array_length (self->info));
989 static PyObject *
990 _wrap_g_type_info_get_array_fixed_size (PyGIBaseInfo *self)
992 return pygi_gint_to_py (g_type_info_get_array_fixed_size (self->info));
995 static PyObject *
996 _wrap_g_type_info_is_zero_terminated (PyGIBaseInfo *self)
998 return pygi_gboolean_to_py (g_type_info_is_zero_terminated (self->info));
1001 static PyObject *
1002 _wrap_g_type_info_get_array_type (PyGIBaseInfo *self)
1004 return pygi_guint_to_py (g_type_info_get_array_type (self->info));
1007 static PyMethodDef _PyGITypeInfo_methods[] = {
1008 { "is_pointer", (PyCFunction) _wrap_g_type_info_is_pointer, METH_NOARGS },
1009 { "get_tag", (PyCFunction) _wrap_g_type_info_get_tag, METH_NOARGS },
1010 { "get_tag_as_string", (PyCFunction) _wrap_g_type_info_get_tag_as_string, METH_NOARGS },
1011 { "get_param_type", (PyCFunction) _wrap_g_type_info_get_param_type, METH_O },
1012 { "get_interface", (PyCFunction) _wrap_g_type_info_get_interface, METH_NOARGS },
1013 { "get_array_length", (PyCFunction) _wrap_g_type_info_get_array_length, METH_NOARGS },
1014 { "get_array_fixed_size", (PyCFunction) _wrap_g_type_info_get_array_fixed_size, METH_NOARGS },
1015 { "is_zero_terminated", (PyCFunction) _wrap_g_type_info_is_zero_terminated, METH_NOARGS },
1016 { "get_array_type", (PyCFunction) _wrap_g_type_info_get_array_type, METH_NOARGS },
1017 { NULL, NULL, 0 }
1021 /* FunctionInfo */
1022 PYGLIB_DEFINE_TYPE ("gi.FunctionInfo", PyGIFunctionInfo_Type, PyGICallableInfo);
1024 static PyObject *
1025 _wrap_g_function_info_is_constructor (PyGIBaseInfo *self)
1027 GIFunctionInfoFlags flags;
1028 gboolean is_constructor;
1030 flags = g_function_info_get_flags ( (GIFunctionInfo*) self->info);
1031 is_constructor = flags & GI_FUNCTION_IS_CONSTRUCTOR;
1033 return pygi_gboolean_to_py (is_constructor);
1036 static PyObject *
1037 _wrap_g_function_info_is_method (PyGIBaseInfo *self)
1039 GIFunctionInfoFlags flags;
1040 gboolean is_method;
1042 flags = g_function_info_get_flags ( (GIFunctionInfo*) self->info);
1043 is_method = flags & GI_FUNCTION_IS_METHOD;
1045 return pygi_gboolean_to_py (is_method);
1048 gsize
1049 _pygi_g_type_tag_size (GITypeTag type_tag)
1051 gsize size = 0;
1053 switch (type_tag) {
1054 case GI_TYPE_TAG_BOOLEAN:
1055 size = sizeof (gboolean);
1056 break;
1057 case GI_TYPE_TAG_INT8:
1058 case GI_TYPE_TAG_UINT8:
1059 size = sizeof (gint8);
1060 break;
1061 case GI_TYPE_TAG_INT16:
1062 case GI_TYPE_TAG_UINT16:
1063 size = sizeof (gint16);
1064 break;
1065 case GI_TYPE_TAG_INT32:
1066 case GI_TYPE_TAG_UINT32:
1067 size = sizeof (gint32);
1068 break;
1069 case GI_TYPE_TAG_INT64:
1070 case GI_TYPE_TAG_UINT64:
1071 size = sizeof (gint64);
1072 break;
1073 case GI_TYPE_TAG_FLOAT:
1074 size = sizeof (gfloat);
1075 break;
1076 case GI_TYPE_TAG_DOUBLE:
1077 size = sizeof (gdouble);
1078 break;
1079 case GI_TYPE_TAG_GTYPE:
1080 size = sizeof (GType);
1081 break;
1082 case GI_TYPE_TAG_UNICHAR:
1083 size = sizeof (gunichar);
1084 break;
1085 case GI_TYPE_TAG_VOID:
1086 case GI_TYPE_TAG_UTF8:
1087 case GI_TYPE_TAG_FILENAME:
1088 case GI_TYPE_TAG_ARRAY:
1089 case GI_TYPE_TAG_INTERFACE:
1090 case GI_TYPE_TAG_GLIST:
1091 case GI_TYPE_TAG_GSLIST:
1092 case GI_TYPE_TAG_GHASH:
1093 case GI_TYPE_TAG_ERROR:
1094 PyErr_Format (PyExc_TypeError,
1095 "Unable to know the size (assuming %s is not a pointer)",
1096 g_type_tag_to_string (type_tag));
1097 break;
1098 default:
1099 break;
1102 return size;
1105 gsize
1106 _pygi_g_type_info_size (GITypeInfo *type_info)
1108 gsize size = 0;
1110 GITypeTag type_tag;
1112 type_tag = g_type_info_get_tag (type_info);
1113 switch (type_tag) {
1114 case GI_TYPE_TAG_BOOLEAN:
1115 case GI_TYPE_TAG_INT8:
1116 case GI_TYPE_TAG_UINT8:
1117 case GI_TYPE_TAG_INT16:
1118 case GI_TYPE_TAG_UINT16:
1119 case GI_TYPE_TAG_INT32:
1120 case GI_TYPE_TAG_UINT32:
1121 case GI_TYPE_TAG_INT64:
1122 case GI_TYPE_TAG_UINT64:
1123 case GI_TYPE_TAG_FLOAT:
1124 case GI_TYPE_TAG_DOUBLE:
1125 case GI_TYPE_TAG_GTYPE:
1126 case GI_TYPE_TAG_UNICHAR:
1127 size = _pygi_g_type_tag_size (type_tag);
1128 g_assert (size > 0);
1129 break;
1130 case GI_TYPE_TAG_INTERFACE:
1132 GIBaseInfo *info;
1133 GIInfoType info_type;
1135 info = g_type_info_get_interface (type_info);
1136 info_type = g_base_info_get_type (info);
1138 switch (info_type) {
1139 case GI_INFO_TYPE_STRUCT:
1140 if (g_type_info_is_pointer (type_info)) {
1141 size = sizeof (gpointer);
1142 } else {
1143 size = g_struct_info_get_size ( (GIStructInfo *) info);
1145 break;
1146 case GI_INFO_TYPE_UNION:
1147 if (g_type_info_is_pointer (type_info)) {
1148 size = sizeof (gpointer);
1149 } else {
1150 size = g_union_info_get_size ( (GIUnionInfo *) info);
1152 break;
1153 case GI_INFO_TYPE_ENUM:
1154 case GI_INFO_TYPE_FLAGS:
1155 if (g_type_info_is_pointer (type_info)) {
1156 size = sizeof (gpointer);
1157 } else {
1158 GITypeTag enum_type_tag;
1160 enum_type_tag = g_enum_info_get_storage_type ( (GIEnumInfo *) info);
1161 size = _pygi_g_type_tag_size (enum_type_tag);
1163 break;
1164 case GI_INFO_TYPE_BOXED:
1165 case GI_INFO_TYPE_OBJECT:
1166 case GI_INFO_TYPE_INTERFACE:
1167 case GI_INFO_TYPE_CALLBACK:
1168 size = sizeof (gpointer);
1169 break;
1170 case GI_INFO_TYPE_VFUNC:
1171 case GI_INFO_TYPE_INVALID:
1172 case GI_INFO_TYPE_FUNCTION:
1173 case GI_INFO_TYPE_CONSTANT:
1174 case GI_INFO_TYPE_VALUE:
1175 case GI_INFO_TYPE_SIGNAL:
1176 case GI_INFO_TYPE_PROPERTY:
1177 case GI_INFO_TYPE_FIELD:
1178 case GI_INFO_TYPE_ARG:
1179 case GI_INFO_TYPE_TYPE:
1180 case GI_INFO_TYPE_UNRESOLVED:
1181 default:
1182 g_assert_not_reached();
1183 break;
1186 g_base_info_unref (info);
1187 break;
1189 case GI_TYPE_TAG_ARRAY:
1190 case GI_TYPE_TAG_VOID:
1191 case GI_TYPE_TAG_UTF8:
1192 case GI_TYPE_TAG_FILENAME:
1193 case GI_TYPE_TAG_GLIST:
1194 case GI_TYPE_TAG_GSLIST:
1195 case GI_TYPE_TAG_GHASH:
1196 case GI_TYPE_TAG_ERROR:
1197 size = sizeof (gpointer);
1198 break;
1199 default:
1200 break;
1203 return size;
1206 static PyObject *
1207 _wrap_g_function_info_get_symbol (PyGIBaseInfo *self)
1209 return _get_info_string (self, g_function_info_get_symbol);
1212 static PyObject *
1213 _wrap_g_function_info_get_flags (PyGIBaseInfo *self)
1215 return pygi_guint_to_py (g_function_info_get_flags (self->info));
1218 static PyObject *
1219 _wrap_g_function_info_get_property (PyGIBaseInfo *self)
1221 return _get_child_info (self, g_function_info_get_property);
1224 static PyObject *
1225 _wrap_g_function_info_get_vfunc (PyGIBaseInfo *self)
1227 return _get_child_info (self, g_function_info_get_vfunc);
1230 static PyMethodDef _PyGIFunctionInfo_methods[] = {
1231 { "is_constructor", (PyCFunction) _wrap_g_function_info_is_constructor, METH_NOARGS },
1232 { "is_method", (PyCFunction) _wrap_g_function_info_is_method, METH_NOARGS },
1233 { "get_symbol", (PyCFunction) _wrap_g_function_info_get_symbol, METH_NOARGS },
1234 { "get_flags", (PyCFunction) _wrap_g_function_info_get_flags, METH_NOARGS },
1235 { "get_property", (PyCFunction) _wrap_g_function_info_get_property, METH_NOARGS },
1236 { "get_vfunc", (PyCFunction) _wrap_g_function_info_get_vfunc, METH_NOARGS },
1237 { NULL, NULL, 0 }
1240 /* RegisteredTypeInfo */
1241 PYGLIB_DEFINE_TYPE ("gi.RegisteredTypeInfo", PyGIRegisteredTypeInfo_Type, PyGIBaseInfo);
1243 static PyObject *
1244 _wrap_g_registered_type_info_get_type_name (PyGIBaseInfo *self)
1246 return _get_info_string (self, g_registered_type_info_get_type_name);
1249 static PyObject *
1250 _wrap_g_registered_type_info_get_type_init (PyGIBaseInfo *self)
1252 return _get_info_string (self, g_registered_type_info_get_type_init);
1255 static PyObject *
1256 _wrap_g_registered_type_info_get_g_type (PyGIBaseInfo *self)
1258 GType type;
1260 type = g_registered_type_info_get_g_type ( (GIRegisteredTypeInfo *) self->info);
1262 return pyg_type_wrapper_new (type);
1265 static PyMethodDef _PyGIRegisteredTypeInfo_methods[] = {
1266 { "get_type_name", (PyCFunction) _wrap_g_registered_type_info_get_type_name, METH_NOARGS },
1267 { "get_type_init", (PyCFunction) _wrap_g_registered_type_info_get_type_init, METH_NOARGS },
1268 { "get_g_type", (PyCFunction) _wrap_g_registered_type_info_get_g_type, METH_NOARGS },
1269 { NULL, NULL, 0 }
1273 /* GIStructInfo */
1274 PYGLIB_DEFINE_TYPE ("StructInfo", PyGIStructInfo_Type, PyGIBaseInfo);
1276 static PyObject *
1277 _wrap_g_struct_info_get_fields (PyGIBaseInfo *self)
1279 return _make_infos_tuple (self, g_struct_info_get_n_fields, g_struct_info_get_field);
1282 static PyObject *
1283 _wrap_g_struct_info_get_methods (PyGIBaseInfo *self)
1285 return _make_infos_tuple (self, g_struct_info_get_n_methods, g_struct_info_get_method);
1288 static PyObject *
1289 _wrap_g_struct_info_get_size (PyGIBaseInfo *self)
1291 return pygi_gsize_to_py (g_struct_info_get_size (self->info));
1294 static PyObject *
1295 _wrap_g_struct_info_get_alignment (PyGIBaseInfo *self)
1297 return pygi_gsize_to_py (g_struct_info_get_alignment (self->info));
1300 static PyObject *
1301 _wrap_g_struct_info_is_gtype_struct (PyGIBaseInfo *self)
1303 return pygi_gboolean_to_py (g_struct_info_is_gtype_struct (self->info));
1306 static PyObject *
1307 _wrap_g_struct_info_is_foreign (PyGIBaseInfo *self)
1309 return pygi_gboolean_to_py (g_struct_info_is_foreign (self->info));
1312 static PyMethodDef _PyGIStructInfo_methods[] = {
1313 { "get_fields", (PyCFunction) _wrap_g_struct_info_get_fields, METH_NOARGS },
1314 { "get_methods", (PyCFunction) _wrap_g_struct_info_get_methods, METH_NOARGS },
1315 { "get_size", (PyCFunction) _wrap_g_struct_info_get_size, METH_NOARGS },
1316 { "get_alignment", (PyCFunction) _wrap_g_struct_info_get_alignment, METH_NOARGS },
1317 { "is_gtype_struct", (PyCFunction) _wrap_g_struct_info_is_gtype_struct, METH_NOARGS },
1318 { "is_foreign", (PyCFunction) _wrap_g_struct_info_is_foreign, METH_NOARGS },
1319 { NULL, NULL, 0 }
1322 gboolean
1323 pygi_g_struct_info_is_simple (GIStructInfo *struct_info)
1325 gboolean is_simple;
1326 gsize n_field_infos;
1327 gsize i;
1329 is_simple = TRUE;
1331 n_field_infos = g_struct_info_get_n_fields (struct_info);
1333 for (i = 0; i < n_field_infos && is_simple; i++) {
1334 GIFieldInfo *field_info;
1335 GITypeInfo *field_type_info;
1336 GITypeTag field_type_tag;
1338 field_info = g_struct_info_get_field (struct_info, i);
1339 field_type_info = g_field_info_get_type (field_info);
1342 field_type_tag = g_type_info_get_tag (field_type_info);
1344 switch (field_type_tag) {
1345 case GI_TYPE_TAG_BOOLEAN:
1346 case GI_TYPE_TAG_INT8:
1347 case GI_TYPE_TAG_UINT8:
1348 case GI_TYPE_TAG_INT16:
1349 case GI_TYPE_TAG_UINT16:
1350 case GI_TYPE_TAG_INT32:
1351 case GI_TYPE_TAG_UINT32:
1352 case GI_TYPE_TAG_INT64:
1353 case GI_TYPE_TAG_UINT64:
1354 case GI_TYPE_TAG_FLOAT:
1355 case GI_TYPE_TAG_DOUBLE:
1356 case GI_TYPE_TAG_UNICHAR:
1357 if (g_type_info_is_pointer (field_type_info)) {
1358 is_simple = FALSE;
1360 break;
1361 case GI_TYPE_TAG_VOID:
1362 case GI_TYPE_TAG_GTYPE:
1363 case GI_TYPE_TAG_ERROR:
1364 case GI_TYPE_TAG_UTF8:
1365 case GI_TYPE_TAG_FILENAME:
1366 case GI_TYPE_TAG_ARRAY:
1367 case GI_TYPE_TAG_GLIST:
1368 case GI_TYPE_TAG_GSLIST:
1369 case GI_TYPE_TAG_GHASH:
1370 is_simple = FALSE;
1371 break;
1372 case GI_TYPE_TAG_INTERFACE:
1374 GIBaseInfo *info;
1375 GIInfoType info_type;
1377 info = g_type_info_get_interface (field_type_info);
1378 info_type = g_base_info_get_type (info);
1380 switch (info_type) {
1381 case GI_INFO_TYPE_STRUCT:
1382 if (g_type_info_is_pointer (field_type_info)) {
1383 is_simple = FALSE;
1384 } else {
1385 is_simple = pygi_g_struct_info_is_simple ( (GIStructInfo *) info);
1387 break;
1388 case GI_INFO_TYPE_UNION:
1389 /* TODO */
1390 is_simple = FALSE;
1391 break;
1392 case GI_INFO_TYPE_ENUM:
1393 case GI_INFO_TYPE_FLAGS:
1394 if (g_type_info_is_pointer (field_type_info)) {
1395 is_simple = FALSE;
1397 break;
1398 case GI_INFO_TYPE_BOXED:
1399 case GI_INFO_TYPE_OBJECT:
1400 case GI_INFO_TYPE_CALLBACK:
1401 case GI_INFO_TYPE_INTERFACE:
1402 is_simple = FALSE;
1403 break;
1404 case GI_INFO_TYPE_VFUNC:
1405 case GI_INFO_TYPE_INVALID:
1406 case GI_INFO_TYPE_FUNCTION:
1407 case GI_INFO_TYPE_CONSTANT:
1408 case GI_INFO_TYPE_VALUE:
1409 case GI_INFO_TYPE_SIGNAL:
1410 case GI_INFO_TYPE_PROPERTY:
1411 case GI_INFO_TYPE_FIELD:
1412 case GI_INFO_TYPE_ARG:
1413 case GI_INFO_TYPE_TYPE:
1414 case GI_INFO_TYPE_UNRESOLVED:
1415 default:
1416 g_assert_not_reached();
1417 break;
1420 g_base_info_unref (info);
1421 break;
1423 default:
1424 g_assert_not_reached();
1425 break;
1428 g_base_info_unref ( (GIBaseInfo *) field_type_info);
1429 g_base_info_unref ( (GIBaseInfo *) field_info);
1432 return is_simple;
1436 /* EnumInfo */
1437 PYGLIB_DEFINE_TYPE ("gi.EnumInfo", PyGIEnumInfo_Type, PyGIBaseInfo);
1439 static PyObject *
1440 _wrap_g_enum_info_get_values (PyGIBaseInfo *self)
1442 return _make_infos_tuple (self, g_enum_info_get_n_values, g_enum_info_get_value);
1445 static PyObject *
1446 _wrap_g_enum_info_is_flags (PyGIBaseInfo *self)
1448 GIInfoType info_type = g_base_info_get_type ((GIBaseInfo *) self->info);
1450 if (info_type == GI_INFO_TYPE_ENUM) {
1451 Py_RETURN_FALSE;
1452 } else if (info_type == GI_INFO_TYPE_FLAGS) {
1453 Py_RETURN_TRUE;
1454 } else {
1455 g_assert_not_reached();
1459 static PyObject *
1460 _wrap_g_enum_info_get_methods (PyGIBaseInfo *self)
1462 return _make_infos_tuple (self, g_enum_info_get_n_methods, g_enum_info_get_method);
1465 static PyObject *
1466 _wrap_g_enum_info_get_storage_type (PyGIBaseInfo *self)
1468 return pygi_guint_to_py (g_enum_info_get_storage_type ((GIBaseInfo *) self->info));
1471 static PyMethodDef _PyGIEnumInfo_methods[] = {
1472 { "get_values", (PyCFunction) _wrap_g_enum_info_get_values, METH_NOARGS },
1473 { "is_flags", (PyCFunction) _wrap_g_enum_info_is_flags, METH_NOARGS },
1474 { "get_methods", (PyCFunction) _wrap_g_enum_info_get_methods, METH_NOARGS },
1475 { "get_storage_type", (PyCFunction) _wrap_g_enum_info_get_storage_type, METH_NOARGS },
1476 { NULL, NULL, 0 }
1480 /* ObjectInfo */
1481 PYGLIB_DEFINE_TYPE ("ObjectInfo", PyGIObjectInfo_Type, PyGIBaseInfo);
1483 static PyObject *
1484 _wrap_g_object_info_get_parent (PyGIBaseInfo *self)
1486 return _get_child_info (self, g_object_info_get_parent);
1489 static PyObject *
1490 _wrap_g_object_info_get_methods (PyGIBaseInfo *self)
1492 return _make_infos_tuple (self, g_object_info_get_n_methods, g_object_info_get_method);
1495 static PyObject *
1496 _wrap_g_object_info_find_method (PyGIBaseInfo *self, PyObject *py_name)
1498 return _get_child_info_by_name (self, py_name, g_object_info_find_method);
1501 static PyObject *
1502 _wrap_g_object_info_get_fields (PyGIBaseInfo *self)
1504 return _make_infos_tuple (self, g_object_info_get_n_fields, g_object_info_get_field);
1507 static PyObject *
1508 _wrap_g_object_info_get_properties (PyGIBaseInfo *self)
1510 return _make_infos_tuple (self, g_object_info_get_n_properties, g_object_info_get_property);
1513 static PyObject *
1514 _wrap_g_object_info_get_signals (PyGIBaseInfo *self)
1516 return _make_infos_tuple (self, g_object_info_get_n_signals, g_object_info_get_signal);
1519 static PyObject *
1520 _wrap_g_object_info_get_interfaces (PyGIBaseInfo *self)
1522 return _make_infos_tuple (self, g_object_info_get_n_interfaces, g_object_info_get_interface);
1525 static PyObject *
1526 _wrap_g_object_info_get_constants (PyGIBaseInfo *self)
1528 return _make_infos_tuple (self, g_object_info_get_n_constants, g_object_info_get_constant);
1531 static PyObject *
1532 _wrap_g_object_info_get_vfuncs (PyGIBaseInfo *self)
1534 return _make_infos_tuple (self, g_object_info_get_n_vfuncs, g_object_info_get_vfunc);
1537 static PyObject *
1538 _wrap_g_object_info_get_abstract (PyGIBaseInfo *self)
1540 gboolean is_abstract = g_object_info_get_abstract ( (GIObjectInfo*) self->info);
1541 return pygi_gboolean_to_py (is_abstract);
1544 static PyObject *
1545 _wrap_g_object_info_get_type_name (PyGIBaseInfo *self)
1547 return _get_info_string (self, g_object_info_get_type_name);
1550 static PyObject *
1551 _wrap_g_object_info_get_type_init (PyGIBaseInfo *self)
1553 return _get_info_string (self, g_object_info_get_type_init);
1556 static PyObject *
1557 _wrap_g_object_info_get_fundamental (PyGIBaseInfo *self)
1559 return pygi_gboolean_to_py (g_object_info_get_fundamental ( (GIObjectInfo*) self->info));
1562 static PyObject *
1563 _wrap_g_object_info_get_class_struct (PyGIBaseInfo *self)
1565 return _get_child_info (self, g_object_info_get_class_struct);
1568 static PyObject *
1569 _wrap_g_object_info_find_vfunc (PyGIBaseInfo *self, PyObject *py_name)
1571 return _get_child_info_by_name (self, py_name, g_object_info_find_vfunc);
1574 static PyObject *
1575 _wrap_g_object_info_get_unref_function (PyGIBaseInfo *self)
1577 return _get_info_string (self, g_object_info_get_unref_function);
1580 static PyObject *
1581 _wrap_g_object_info_get_ref_function (PyGIBaseInfo *self)
1583 return _get_info_string (self, g_object_info_get_ref_function);
1586 static PyObject *
1587 _wrap_g_object_info_get_set_value_function (PyGIBaseInfo *self)
1589 return _get_info_string (self, g_object_info_get_set_value_function);
1592 static PyObject *
1593 _wrap_g_object_info_get_get_value_function (PyGIBaseInfo *self)
1595 return _get_info_string (self, g_object_info_get_get_value_function);
1598 static PyMethodDef _PyGIObjectInfo_methods[] = {
1599 { "get_parent", (PyCFunction) _wrap_g_object_info_get_parent, METH_NOARGS },
1600 { "get_methods", (PyCFunction) _wrap_g_object_info_get_methods, METH_NOARGS },
1601 { "find_method", (PyCFunction) _wrap_g_object_info_find_method, METH_O },
1602 { "get_fields", (PyCFunction) _wrap_g_object_info_get_fields, METH_NOARGS },
1603 { "get_properties", (PyCFunction) _wrap_g_object_info_get_properties, METH_NOARGS },
1604 { "get_signals", (PyCFunction) _wrap_g_object_info_get_signals, METH_NOARGS },
1605 { "get_interfaces", (PyCFunction) _wrap_g_object_info_get_interfaces, METH_NOARGS },
1606 { "get_constants", (PyCFunction) _wrap_g_object_info_get_constants, METH_NOARGS },
1607 { "get_vfuncs", (PyCFunction) _wrap_g_object_info_get_vfuncs, METH_NOARGS },
1608 { "find_vfunc", (PyCFunction) _wrap_g_object_info_find_vfunc, METH_O },
1609 { "get_abstract", (PyCFunction) _wrap_g_object_info_get_abstract, METH_NOARGS },
1610 { "get_type_name", (PyCFunction) _wrap_g_object_info_get_type_name, METH_NOARGS },
1611 { "get_type_init", (PyCFunction) _wrap_g_object_info_get_type_init, METH_NOARGS },
1612 { "get_fundamental", (PyCFunction) _wrap_g_object_info_get_fundamental, METH_NOARGS },
1613 { "get_class_struct", (PyCFunction) _wrap_g_object_info_get_class_struct, METH_NOARGS },
1614 { "get_unref_function", (PyCFunction) _wrap_g_object_info_get_unref_function, METH_NOARGS },
1615 { "get_ref_function", (PyCFunction) _wrap_g_object_info_get_ref_function, METH_NOARGS },
1616 { "get_set_value_function", (PyCFunction) _wrap_g_object_info_get_set_value_function, METH_NOARGS },
1617 { "get_get_value_function", (PyCFunction) _wrap_g_object_info_get_get_value_function, METH_NOARGS },
1618 { NULL, NULL, 0 }
1622 /* GIInterfaceInfo */
1623 PYGLIB_DEFINE_TYPE ("InterfaceInfo", PyGIInterfaceInfo_Type, PyGIBaseInfo);
1625 static PyObject *
1626 _wrap_g_interface_info_get_methods (PyGIBaseInfo *self)
1628 return _make_infos_tuple (self, g_interface_info_get_n_methods, g_interface_info_get_method);
1631 static PyObject *
1632 _wrap_g_interface_info_find_method (PyGIBaseInfo *self, PyObject *py_name)
1634 return _get_child_info_by_name (self, py_name, g_interface_info_find_method);
1637 static PyObject *
1638 _wrap_g_interface_info_get_constants (PyGIBaseInfo *self)
1640 return _make_infos_tuple (self, g_interface_info_get_n_constants, g_interface_info_get_constant);
1643 static PyObject *
1644 _wrap_g_interface_info_get_vfuncs (PyGIBaseInfo *self)
1646 return _make_infos_tuple (self, g_interface_info_get_n_vfuncs, g_interface_info_get_vfunc);
1649 static PyObject *
1650 _wrap_g_interface_info_find_vfunc (PyGIBaseInfo *self, PyObject *py_name)
1652 return _get_child_info_by_name (self, py_name, g_interface_info_find_vfunc);
1655 static PyObject *
1656 _wrap_g_interface_info_get_prerequisites (PyGIBaseInfo *self)
1658 return _make_infos_tuple (self, g_interface_info_get_n_prerequisites, g_interface_info_get_prerequisite);
1661 static PyObject *
1662 _wrap_g_interface_info_get_properties (PyGIBaseInfo *self)
1664 return _make_infos_tuple (self, g_interface_info_get_n_properties, g_interface_info_get_property);
1667 static PyObject *
1668 _wrap_g_interface_info_get_iface_struct (PyGIBaseInfo *self)
1670 return _get_child_info (self, g_interface_info_get_iface_struct);
1673 static PyObject *
1674 _wrap_g_interface_info_get_signals (PyGIBaseInfo *self)
1676 return _make_infos_tuple (self, g_interface_info_get_n_signals, g_interface_info_get_signal);
1679 static PyObject *
1680 _wrap_g_interface_info_find_signal (PyGIBaseInfo *self, PyObject *py_name)
1682 return _get_child_info_by_name (self, py_name, g_interface_info_find_signal);
1685 static PyMethodDef _PyGIInterfaceInfo_methods[] = {
1686 { "get_prerequisites", (PyCFunction) _wrap_g_interface_info_get_prerequisites, METH_NOARGS },
1687 { "get_properties", (PyCFunction) _wrap_g_interface_info_get_properties, METH_NOARGS },
1688 { "get_methods", (PyCFunction) _wrap_g_interface_info_get_methods, METH_NOARGS },
1689 { "find_method", (PyCFunction) _wrap_g_interface_info_find_method, METH_O },
1690 { "get_signals", (PyCFunction) _wrap_g_interface_info_get_signals, METH_NOARGS },
1691 { "find_signal", (PyCFunction) _wrap_g_interface_info_find_signal, METH_O },
1692 { "get_vfuncs", (PyCFunction) _wrap_g_interface_info_get_vfuncs, METH_NOARGS },
1693 { "get_constants", (PyCFunction) _wrap_g_interface_info_get_constants, METH_NOARGS },
1694 { "get_iface_struct", (PyCFunction) _wrap_g_interface_info_get_iface_struct, METH_NOARGS },
1695 { "find_vfunc", (PyCFunction) _wrap_g_interface_info_find_vfunc, METH_O },
1696 { NULL, NULL, 0 }
1699 /* GIConstantInfo */
1700 PYGLIB_DEFINE_TYPE ("gi.ConstantInfo", PyGIConstantInfo_Type, PyGIBaseInfo);
1702 static PyObject *
1703 _wrap_g_constant_info_get_value (PyGIBaseInfo *self)
1705 GITypeInfo *type_info;
1706 GIArgument value = {0};
1707 PyObject *py_value;
1708 gboolean free_array = FALSE;
1710 if (g_constant_info_get_value ( (GIConstantInfo *) self->info, &value) < 0) {
1711 PyErr_SetString (PyExc_RuntimeError, "unable to get value");
1712 return NULL;
1715 type_info = g_constant_info_get_type ( (GIConstantInfo *) self->info);
1717 if (g_type_info_get_tag (type_info) == GI_TYPE_TAG_ARRAY) {
1718 value.v_pointer = _pygi_argument_to_array (&value, NULL, NULL, NULL,
1719 type_info, &free_array);
1722 py_value = _pygi_argument_to_object (&value, type_info, GI_TRANSFER_NOTHING);
1724 if (free_array) {
1725 g_array_free (value.v_pointer, FALSE);
1728 g_constant_info_free_value (self->info, &value);
1729 g_base_info_unref ( (GIBaseInfo *) type_info);
1731 return py_value;
1734 static PyMethodDef _PyGIConstantInfo_methods[] = {
1735 { "get_value", (PyCFunction) _wrap_g_constant_info_get_value, METH_NOARGS },
1736 { NULL, NULL, 0 }
1739 /* GIValueInfo */
1740 PYGLIB_DEFINE_TYPE ("gi.ValueInfo", PyGIValueInfo_Type, PyGIBaseInfo);
1742 static PyObject *
1743 _wrap_g_value_info_get_value (PyGIBaseInfo *self)
1745 glong value;
1747 value = g_value_info_get_value ( (GIValueInfo *) self->info);
1749 return pygi_gint64_to_py (value);
1753 static PyMethodDef _PyGIValueInfo_methods[] = {
1754 { "get_value", (PyCFunction) _wrap_g_value_info_get_value, METH_NOARGS },
1755 { NULL, NULL, 0 }
1759 /* GIFieldInfo */
1760 PYGLIB_DEFINE_TYPE ("gi.FieldInfo", PyGIFieldInfo_Type, PyGIBaseInfo);
1762 static gssize
1763 _struct_field_array_length_marshal (gsize length_index,
1764 void *container_ptr,
1765 void *struct_data_ptr)
1767 gssize array_len = -1;
1768 GIFieldInfo *array_len_field = NULL;
1769 GIArgument arg = {0};
1770 GIBaseInfo *container_info = (GIBaseInfo *)container_ptr;
1772 switch (g_base_info_get_type (container_info)) {
1773 case GI_INFO_TYPE_UNION:
1774 array_len_field = g_union_info_get_field ((GIUnionInfo *)container_info, length_index);
1775 break;
1776 case GI_INFO_TYPE_STRUCT:
1777 array_len_field = g_struct_info_get_field ((GIStructInfo *)container_info, length_index);
1778 break;
1779 case GI_INFO_TYPE_OBJECT:
1780 array_len_field = g_object_info_get_field ((GIObjectInfo *)container_info, length_index);
1781 break;
1782 default:
1783 /* Other types don't have fields. */
1784 g_assert_not_reached();
1787 if (array_len_field == NULL) {
1788 return -1;
1791 if (g_field_info_get_field (array_len_field, struct_data_ptr, &arg)) {
1792 GITypeInfo *array_len_type_info;
1794 array_len_type_info = g_field_info_get_type (array_len_field);
1795 if (array_len_type_info == NULL) {
1796 goto out;
1799 if (!pygi_argument_to_gssize (&arg,
1800 g_type_info_get_tag (array_len_type_info),
1801 &array_len)) {
1802 array_len = -1;
1805 g_base_info_unref (array_len_type_info);
1808 out:
1809 g_base_info_unref (array_len_field);
1810 return array_len;
1813 static gint
1814 _pygi_g_registered_type_info_check_object (GIRegisteredTypeInfo *info,
1815 gboolean is_instance,
1816 PyObject *object)
1818 gint retval;
1820 GType g_type;
1821 PyObject *py_type;
1822 gchar *type_name_expected = NULL;
1823 GIInfoType interface_type;
1825 interface_type = g_base_info_get_type (info);
1826 if ( (interface_type == GI_INFO_TYPE_STRUCT) &&
1827 (g_struct_info_is_foreign ( (GIStructInfo*) info))) {
1828 /* TODO: Could we check is the correct foreign type? */
1829 return 1;
1832 g_type = g_registered_type_info_get_g_type (info);
1833 if (g_type != G_TYPE_NONE) {
1834 py_type = pygi_type_get_from_g_type (g_type);
1835 } else {
1836 py_type = pygi_type_import_by_gi_info ( (GIBaseInfo *) info);
1839 if (py_type == NULL) {
1840 return 0;
1843 g_assert (PyType_Check (py_type));
1845 if (is_instance) {
1846 retval = PyObject_IsInstance (object, py_type);
1847 if (!retval) {
1848 type_name_expected = _pygi_g_base_info_get_fullname (
1849 (GIBaseInfo *) info);
1851 } else {
1852 if (!PyObject_Type (py_type)) {
1853 type_name_expected = "type";
1854 retval = 0;
1855 } else if (!PyType_IsSubtype ( (PyTypeObject *) object,
1856 (PyTypeObject *) py_type)) {
1857 type_name_expected = _pygi_g_base_info_get_fullname (
1858 (GIBaseInfo *) info);
1859 retval = 0;
1860 } else {
1861 retval = 1;
1865 Py_DECREF (py_type);
1867 if (!retval) {
1868 PyTypeObject *object_type;
1870 if (type_name_expected == NULL) {
1871 return -1;
1874 object_type = (PyTypeObject *) PyObject_Type (object);
1875 if (object_type == NULL) {
1876 return -1;
1879 PyErr_Format (PyExc_TypeError, "Must be %s, not %s",
1880 type_name_expected, object_type->tp_name);
1882 g_free (type_name_expected);
1885 return retval;
1888 static PyObject *
1889 _wrap_g_field_info_get_value (PyGIBaseInfo *self,
1890 PyObject *args)
1892 PyObject *instance;
1893 GIBaseInfo *container_info;
1894 GIInfoType container_info_type;
1895 gpointer pointer;
1896 GITypeInfo *field_type_info;
1897 GIArgument value;
1898 PyObject *py_value = NULL;
1899 gboolean free_array = FALSE;
1901 memset(&value, 0, sizeof(GIArgument));
1903 if (!PyArg_ParseTuple (args, "O:FieldInfo.get_value", &instance)) {
1904 return NULL;
1907 container_info = g_base_info_get_container (self->info);
1908 g_assert (container_info != NULL);
1910 /* Check the instance. */
1911 if (!_pygi_g_registered_type_info_check_object ( (GIRegisteredTypeInfo *) container_info, TRUE, instance)) {
1912 _PyGI_ERROR_PREFIX ("argument 1: ");
1913 return NULL;
1916 /* Get the pointer to the container. */
1917 container_info_type = g_base_info_get_type (container_info);
1918 switch (container_info_type) {
1919 case GI_INFO_TYPE_UNION:
1920 case GI_INFO_TYPE_STRUCT:
1921 pointer = pyg_boxed_get (instance, void);
1922 break;
1923 case GI_INFO_TYPE_OBJECT:
1924 pointer = pygobject_get (instance);
1925 break;
1926 default:
1927 /* Other types don't have fields. */
1928 g_assert_not_reached();
1931 /* Get the field's value. */
1932 field_type_info = g_field_info_get_type ( (GIFieldInfo *) self->info);
1934 /* A few types are not handled by g_field_info_get_field, so do it here. */
1935 if (!g_type_info_is_pointer (field_type_info)
1936 && g_type_info_get_tag (field_type_info) == GI_TYPE_TAG_INTERFACE) {
1937 GIBaseInfo *info;
1938 GIInfoType info_type;
1940 if (! (g_field_info_get_flags ( (GIFieldInfo *) self->info) & GI_FIELD_IS_READABLE)) {
1941 PyErr_SetString (PyExc_RuntimeError, "field is not readable");
1942 goto out;
1945 info = g_type_info_get_interface (field_type_info);
1947 info_type = g_base_info_get_type (info);
1949 g_base_info_unref (info);
1951 switch (info_type) {
1952 case GI_INFO_TYPE_UNION:
1953 PyErr_SetString (PyExc_NotImplementedError, "getting an union is not supported yet");
1954 goto out;
1955 case GI_INFO_TYPE_STRUCT:
1957 gsize offset;
1959 offset = g_field_info_get_offset ( (GIFieldInfo *) self->info);
1961 value.v_pointer = (char*) pointer + offset;
1963 goto argument_to_object;
1965 default:
1966 /* Fallback. */
1967 break;
1971 if (!g_field_info_get_field ( (GIFieldInfo *) self->info, pointer, &value)) {
1972 PyErr_SetString (PyExc_RuntimeError, "unable to get the value");
1973 goto out;
1976 if (g_type_info_get_tag (field_type_info) == GI_TYPE_TAG_ARRAY) {
1977 value.v_pointer = _pygi_argument_to_array (&value,
1978 _struct_field_array_length_marshal,
1979 container_info,
1980 pointer,
1981 field_type_info,
1982 &free_array);
1985 argument_to_object:
1986 py_value = _pygi_argument_to_object (&value, field_type_info, GI_TRANSFER_NOTHING);
1988 if (free_array) {
1989 g_array_free (value.v_pointer, FALSE);
1992 out:
1993 g_base_info_unref ( (GIBaseInfo *) field_type_info);
1995 return py_value;
1998 static PyObject *
1999 _wrap_g_field_info_set_value (PyGIBaseInfo *self,
2000 PyObject *args)
2002 PyObject *instance;
2003 PyObject *py_value;
2004 GIBaseInfo *container_info;
2005 GIInfoType container_info_type;
2006 gpointer pointer;
2007 GITypeInfo *field_type_info;
2008 GIArgument value;
2009 PyObject *retval = NULL;
2011 if (!PyArg_ParseTuple (args, "OO:FieldInfo.set_value", &instance, &py_value)) {
2012 return NULL;
2015 container_info = g_base_info_get_container (self->info);
2016 g_assert (container_info != NULL);
2018 /* Check the instance. */
2019 if (!_pygi_g_registered_type_info_check_object ( (GIRegisteredTypeInfo *) container_info, TRUE, instance)) {
2020 _PyGI_ERROR_PREFIX ("argument 1: ");
2021 return NULL;
2024 /* Get the pointer to the container. */
2025 container_info_type = g_base_info_get_type (container_info);
2026 switch (container_info_type) {
2027 case GI_INFO_TYPE_UNION:
2028 case GI_INFO_TYPE_STRUCT:
2029 pointer = pyg_boxed_get (instance, void);
2030 break;
2031 case GI_INFO_TYPE_OBJECT:
2032 pointer = pygobject_get (instance);
2033 break;
2034 default:
2035 /* Other types don't have fields. */
2036 g_assert_not_reached();
2039 field_type_info = g_field_info_get_type ( (GIFieldInfo *) self->info);
2041 /* Set the field's value. */
2042 /* A few types are not handled by g_field_info_set_field, so do it here. */
2043 if (!g_type_info_is_pointer (field_type_info)
2044 && g_type_info_get_tag (field_type_info) == GI_TYPE_TAG_INTERFACE) {
2045 GIBaseInfo *info;
2046 GIInfoType info_type;
2048 if (! (g_field_info_get_flags ( (GIFieldInfo *) self->info) & GI_FIELD_IS_WRITABLE)) {
2049 PyErr_SetString (PyExc_RuntimeError, "field is not writable");
2050 goto out;
2053 info = g_type_info_get_interface (field_type_info);
2055 info_type = g_base_info_get_type (info);
2057 switch (info_type) {
2058 case GI_INFO_TYPE_UNION:
2059 PyErr_SetString (PyExc_NotImplementedError, "setting an union is not supported yet");
2060 goto out;
2061 case GI_INFO_TYPE_STRUCT:
2063 gboolean is_simple;
2064 gsize offset;
2065 gssize size;
2067 is_simple = pygi_g_struct_info_is_simple ( (GIStructInfo *) info);
2069 if (!is_simple) {
2070 PyErr_SetString (PyExc_TypeError,
2071 "cannot set a structure which has no well-defined ownership transfer rules");
2072 g_base_info_unref (info);
2073 goto out;
2076 value = _pygi_argument_from_object (py_value, field_type_info, GI_TRANSFER_NOTHING);
2077 if (PyErr_Occurred()) {
2078 g_base_info_unref (info);
2079 goto out;
2082 offset = g_field_info_get_offset ( (GIFieldInfo *) self->info);
2083 size = g_struct_info_get_size ( (GIStructInfo *) info);
2084 g_assert (size > 0);
2086 g_memmove ((char*) pointer + offset, value.v_pointer, size);
2088 g_base_info_unref (info);
2090 retval = Py_None;
2091 goto out;
2093 default:
2094 /* Fallback. */
2095 break;
2098 g_base_info_unref (info);
2099 } else if (g_type_info_is_pointer (field_type_info)
2100 && (g_type_info_get_tag (field_type_info) == GI_TYPE_TAG_VOID
2101 || g_type_info_get_tag (field_type_info) == GI_TYPE_TAG_UTF8)) {
2102 int offset;
2103 value = _pygi_argument_from_object (py_value, field_type_info, GI_TRANSFER_NOTHING);
2104 if (PyErr_Occurred()) {
2105 goto out;
2108 offset = g_field_info_get_offset ((GIFieldInfo *) self->info);
2109 G_STRUCT_MEMBER (gpointer, pointer, offset) = (gpointer)value.v_pointer;
2111 retval = Py_None;
2112 goto out;
2115 value = _pygi_argument_from_object (py_value, field_type_info, GI_TRANSFER_EVERYTHING);
2116 if (PyErr_Occurred()) {
2117 goto out;
2120 if (!g_field_info_set_field ( (GIFieldInfo *) self->info, pointer, &value)) {
2121 _pygi_argument_release (&value, field_type_info, GI_TRANSFER_NOTHING, GI_DIRECTION_IN);
2122 PyErr_SetString (PyExc_RuntimeError, "unable to set value for field");
2123 goto out;
2126 retval = Py_None;
2128 out:
2129 g_base_info_unref ( (GIBaseInfo *) field_type_info);
2131 Py_XINCREF (retval);
2132 return retval;
2135 static PyObject *
2136 _wrap_g_field_info_get_flags (PyGIBaseInfo *self)
2138 return pygi_guint_to_py (g_field_info_get_flags (self->info));
2141 static PyObject *
2142 _wrap_g_field_info_get_size (PyGIBaseInfo *self)
2144 return pygi_gint_to_py (g_field_info_get_size (self->info));
2147 static PyObject *
2148 _wrap_g_field_info_get_offset (PyGIBaseInfo *self)
2150 return pygi_gint_to_py (g_field_info_get_offset (self->info));
2153 static PyObject *
2154 _wrap_g_field_info_get_type (PyGIBaseInfo *self)
2156 return _get_child_info (self, g_field_info_get_type);
2159 static PyMethodDef _PyGIFieldInfo_methods[] = {
2160 { "get_value", (PyCFunction) _wrap_g_field_info_get_value, METH_VARARGS },
2161 { "set_value", (PyCFunction) _wrap_g_field_info_set_value, METH_VARARGS },
2162 { "get_flags", (PyCFunction) _wrap_g_field_info_get_flags, METH_VARARGS },
2163 { "get_size", (PyCFunction) _wrap_g_field_info_get_size, METH_VARARGS },
2164 { "get_offset", (PyCFunction) _wrap_g_field_info_get_offset, METH_VARARGS },
2165 { "get_type", (PyCFunction) _wrap_g_field_info_get_type, METH_VARARGS },
2166 { NULL, NULL, 0 }
2170 /* GIUnresolvedInfo */
2171 PYGLIB_DEFINE_TYPE ("gi.UnresolvedInfo", PyGIUnresolvedInfo_Type, PyGIBaseInfo);
2173 static PyMethodDef _PyGIUnresolvedInfo_methods[] = {
2174 { NULL, NULL, 0 }
2177 /* GIVFuncInfo */
2178 PYGLIB_DEFINE_TYPE ("gi.VFuncInfo", PyGIVFuncInfo_Type, PyGICallableInfo);
2180 static PyObject *
2181 _wrap_g_vfunc_info_get_flags (PyGIBaseInfo *self)
2183 return pygi_guint_to_py (g_vfunc_info_get_flags ((GIVFuncInfo *) self->info));
2186 static PyObject *
2187 _wrap_g_vfunc_info_get_offset (PyGIBaseInfo *self)
2189 return pygi_gint_to_py (g_vfunc_info_get_offset ((GIVFuncInfo *) self->info));
2192 static PyObject *
2193 _wrap_g_vfunc_info_get_signal (PyGIBaseInfo *self)
2195 return _get_child_info (self, g_vfunc_info_get_signal);
2198 static PyObject *
2199 _wrap_g_vfunc_info_get_invoker (PyGIBaseInfo *self)
2201 return _get_child_info (self, g_vfunc_info_get_invoker);
2204 static PyMethodDef _PyGIVFuncInfo_methods[] = {
2205 { "get_flags", (PyCFunction) _wrap_g_vfunc_info_get_flags, METH_NOARGS },
2206 { "get_offset", (PyCFunction) _wrap_g_vfunc_info_get_offset, METH_NOARGS },
2207 { "get_signal", (PyCFunction) _wrap_g_vfunc_info_get_signal, METH_NOARGS },
2208 { "get_invoker", (PyCFunction) _wrap_g_vfunc_info_get_invoker, METH_NOARGS },
2209 { NULL, NULL, 0 }
2213 /* GIUnionInfo */
2214 PYGLIB_DEFINE_TYPE ("gi.UnionInfo", PyGIUnionInfo_Type, PyGIBaseInfo);
2216 static PyObject *
2217 _wrap_g_union_info_get_fields (PyGIBaseInfo *self)
2219 return _make_infos_tuple (self, g_union_info_get_n_fields, g_union_info_get_field);
2222 static PyObject *
2223 _wrap_g_union_info_get_methods (PyGIBaseInfo *self)
2225 return _make_infos_tuple (self, g_union_info_get_n_methods, g_union_info_get_method);
2228 static PyObject *
2229 _wrap_g_union_info_get_size (PyGIBaseInfo *self)
2231 return pygi_gsize_to_py (g_union_info_get_size (self->info));
2234 static PyMethodDef _PyGIUnionInfo_methods[] = {
2235 { "get_fields", (PyCFunction) _wrap_g_union_info_get_fields, METH_NOARGS },
2236 { "get_methods", (PyCFunction) _wrap_g_union_info_get_methods, METH_NOARGS },
2237 { "get_size", (PyCFunction) _wrap_g_union_info_get_size, METH_NOARGS },
2238 { NULL, NULL, 0 }
2241 /* Private */
2243 gchar *
2244 _pygi_g_base_info_get_fullname (GIBaseInfo *info)
2246 GIBaseInfo *container_info;
2247 gchar *fullname;
2249 container_info = g_base_info_get_container (info);
2250 if (container_info != NULL) {
2251 fullname = g_strdup_printf ("%s.%s.%s",
2252 g_base_info_get_namespace (container_info),
2253 _safe_base_info_get_name (container_info),
2254 _safe_base_info_get_name (info));
2255 } else {
2256 fullname = g_strdup_printf ("%s.%s",
2257 g_base_info_get_namespace (info),
2258 _safe_base_info_get_name (info));
2261 if (fullname == NULL) {
2262 PyErr_NoMemory();
2265 return fullname;
2270 * Returns 0 on success, or -1 and sets an exception.
2273 pygi_info_register_types (PyObject *m)
2275 #define _PyGI_REGISTER_TYPE(m, type, cname, base) \
2276 Py_TYPE(&type) = &PyType_Type; \
2277 type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE); \
2278 type.tp_weaklistoffset = offsetof(PyGIBaseInfo, inst_weakreflist); \
2279 type.tp_methods = _PyGI##cname##_methods; \
2280 type.tp_base = &base; \
2281 if (PyType_Ready(&type) < 0) \
2282 return -1; \
2283 Py_INCREF ((PyObject *)&type); \
2284 if (PyModule_AddObject(m, #cname, (PyObject *)&type) < 0) { \
2285 Py_DECREF ((PyObject *)&type); \
2286 return -1; \
2289 Py_TYPE(&PyGIBaseInfo_Type) = &PyType_Type;
2291 PyGIBaseInfo_Type.tp_dealloc = (destructor) _base_info_dealloc;
2292 PyGIBaseInfo_Type.tp_repr = (reprfunc) _base_info_repr;
2293 PyGIBaseInfo_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
2294 PyGIBaseInfo_Type.tp_weaklistoffset = offsetof(PyGIBaseInfo, inst_weakreflist);
2295 PyGIBaseInfo_Type.tp_methods = _PyGIBaseInfo_methods;
2296 PyGIBaseInfo_Type.tp_richcompare = (richcmpfunc)_base_info_richcompare;
2297 PyGIBaseInfo_Type.tp_getset = _base_info_getsets;
2298 PyGIBaseInfo_Type.tp_getattro = (getattrofunc) _base_info_getattro;
2300 if (PyType_Ready(&PyGIBaseInfo_Type) < 0)
2301 return -1;
2302 Py_INCREF ((PyObject *)&PyGIBaseInfo_Type);
2303 if (PyModule_AddObject(m, "BaseInfo", (PyObject *)&PyGIBaseInfo_Type) < 0) {
2304 Py_DECREF ((PyObject *)&PyGIBaseInfo_Type);
2305 return -1;
2308 PyGICallableInfo_Type.tp_call = (ternaryfunc) _callable_info_call;
2309 PyGICallableInfo_Type.tp_dealloc = (destructor) _callable_info_dealloc;
2310 _PyGI_REGISTER_TYPE (m, PyGICallableInfo_Type, CallableInfo,
2311 PyGIBaseInfo_Type);
2313 PyGIFunctionInfo_Type.tp_call = (ternaryfunc) _function_info_call;
2314 PyGIFunctionInfo_Type.tp_descr_get = (descrgetfunc) _function_info_descr_get;
2315 _PyGI_REGISTER_TYPE (m, PyGIFunctionInfo_Type, FunctionInfo,
2316 PyGICallableInfo_Type);
2318 PyGIVFuncInfo_Type.tp_descr_get = (descrgetfunc) _vfunc_info_descr_get;
2319 _PyGI_REGISTER_TYPE (m, PyGIVFuncInfo_Type, VFuncInfo,
2320 PyGICallableInfo_Type);
2322 _PyGI_REGISTER_TYPE (m, PyGISignalInfo_Type, SignalInfo,
2323 PyGICallableInfo_Type);
2325 _PyGI_REGISTER_TYPE (m, PyGIUnresolvedInfo_Type, UnresolvedInfo,
2326 PyGIBaseInfo_Type);
2327 _PyGI_REGISTER_TYPE (m, PyGICallbackInfo_Type, CallbackInfo,
2328 PyGICallableInfo_Type);
2329 _PyGI_REGISTER_TYPE (m, PyGIRegisteredTypeInfo_Type, RegisteredTypeInfo,
2330 PyGIBaseInfo_Type);
2331 _PyGI_REGISTER_TYPE (m, PyGIStructInfo_Type, StructInfo,
2332 PyGIRegisteredTypeInfo_Type);
2333 _PyGI_REGISTER_TYPE (m, PyGIEnumInfo_Type, EnumInfo,
2334 PyGIRegisteredTypeInfo_Type);
2335 _PyGI_REGISTER_TYPE (m, PyGIObjectInfo_Type, ObjectInfo,
2336 PyGIRegisteredTypeInfo_Type);
2337 _PyGI_REGISTER_TYPE (m, PyGIInterfaceInfo_Type, InterfaceInfo,
2338 PyGIRegisteredTypeInfo_Type);
2339 _PyGI_REGISTER_TYPE (m, PyGIConstantInfo_Type, ConstantInfo,
2340 PyGIBaseInfo_Type);
2341 _PyGI_REGISTER_TYPE (m, PyGIValueInfo_Type, ValueInfo,
2342 PyGIBaseInfo_Type);
2343 _PyGI_REGISTER_TYPE (m, PyGIFieldInfo_Type, FieldInfo,
2344 PyGIBaseInfo_Type);
2345 _PyGI_REGISTER_TYPE (m, PyGIUnionInfo_Type, UnionInfo,
2346 PyGIRegisteredTypeInfo_Type);
2347 _PyGI_REGISTER_TYPE (m, PyGIErrorDomainInfo_Type, ErrorDomainInfo,
2348 PyGIBaseInfo_Type);
2349 _PyGI_REGISTER_TYPE (m, PyGIPropertyInfo_Type, PropertyInfo,
2350 PyGIBaseInfo_Type);
2351 _PyGI_REGISTER_TYPE (m, PyGIArgInfo_Type, ArgInfo,
2352 PyGIBaseInfo_Type);
2353 _PyGI_REGISTER_TYPE (m, PyGITypeInfo_Type, TypeInfo,
2354 PyGIBaseInfo_Type);
2356 #undef _PyGI_REGISTER_TYPE
2358 #define _PyGI_ENUM_BEGIN(name) \
2360 const char *__enum_name = #name; \
2361 PyObject *__enum_value = NULL; \
2362 PyObject *__new_enum_cls = NULL; \
2363 PyObject *__enum_instance_dict = PyDict_New(); \
2364 PyObject *__module_name = PyObject_GetAttrString (m, "__name__"); \
2365 PyDict_SetItemString (__enum_instance_dict, "__module__", __module_name); \
2366 Py_DECREF (__module_name);
2368 #define _PyGI_ENUM_ADD_VALUE(prefix, name) \
2369 __enum_value = pygi_guint_to_py (prefix##_##name); \
2370 if (PyDict_SetItemString(__enum_instance_dict, #name, __enum_value) < 0) { \
2371 Py_DECREF (__enum_instance_dict); \
2372 Py_DECREF (__enum_value); \
2373 return -1; \
2375 Py_DECREF (__enum_value);
2377 #define _PyGI_ENUM_END \
2378 __new_enum_cls = PyObject_CallFunction ((PyObject *)&PyType_Type, "s(O)O", \
2379 __enum_name, (PyObject *)&PyType_Type, \
2380 __enum_instance_dict); \
2381 Py_DECREF (__enum_instance_dict); \
2382 PyModule_AddObject (m, __enum_name, __new_enum_cls); /* steals ref */ \
2386 /* GIDirection */
2387 _PyGI_ENUM_BEGIN (Direction)
2388 _PyGI_ENUM_ADD_VALUE (GI_DIRECTION, IN)
2389 _PyGI_ENUM_ADD_VALUE (GI_DIRECTION, OUT)
2390 _PyGI_ENUM_ADD_VALUE (GI_DIRECTION, INOUT)
2391 _PyGI_ENUM_END
2394 /* GITransfer */
2395 _PyGI_ENUM_BEGIN (Transfer)
2396 _PyGI_ENUM_ADD_VALUE (GI_TRANSFER, NOTHING)
2397 _PyGI_ENUM_ADD_VALUE (GI_TRANSFER, CONTAINER)
2398 _PyGI_ENUM_ADD_VALUE (GI_TRANSFER, EVERYTHING)
2399 _PyGI_ENUM_END
2401 /* GIArrayType */
2402 _PyGI_ENUM_BEGIN (ArrayType)
2403 _PyGI_ENUM_ADD_VALUE (GI_ARRAY_TYPE, C)
2404 _PyGI_ENUM_ADD_VALUE (GI_ARRAY_TYPE, ARRAY)
2405 _PyGI_ENUM_ADD_VALUE (GI_ARRAY_TYPE, PTR_ARRAY)
2406 _PyGI_ENUM_ADD_VALUE (GI_ARRAY_TYPE, BYTE_ARRAY)
2407 _PyGI_ENUM_END
2409 /* GIScopeType */
2410 _PyGI_ENUM_BEGIN (ScopeType)
2411 _PyGI_ENUM_ADD_VALUE (GI_SCOPE_TYPE, INVALID)
2412 _PyGI_ENUM_ADD_VALUE (GI_SCOPE_TYPE, CALL)
2413 _PyGI_ENUM_ADD_VALUE (GI_SCOPE_TYPE, ASYNC)
2414 _PyGI_ENUM_ADD_VALUE (GI_SCOPE_TYPE, NOTIFIED)
2415 _PyGI_ENUM_END
2417 /* GIVFuncInfoFlags */
2418 _PyGI_ENUM_BEGIN (VFuncInfoFlags)
2419 _PyGI_ENUM_ADD_VALUE (GI_VFUNC_MUST, CHAIN_UP)
2420 _PyGI_ENUM_ADD_VALUE (GI_VFUNC_MUST, OVERRIDE)
2421 _PyGI_ENUM_ADD_VALUE (GI_VFUNC_MUST, NOT_OVERRIDE)
2422 _PyGI_ENUM_END
2424 /* GIFieldInfoFlags */
2425 _PyGI_ENUM_BEGIN (FieldInfoFlags)
2426 _PyGI_ENUM_ADD_VALUE (GI_FIELD, IS_READABLE)
2427 _PyGI_ENUM_ADD_VALUE (GI_FIELD, IS_WRITABLE)
2428 _PyGI_ENUM_END
2430 /* GIFunctionInfoFlags */
2431 _PyGI_ENUM_BEGIN (FunctionInfoFlags)
2432 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, IS_METHOD)
2433 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, IS_CONSTRUCTOR)
2434 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, IS_GETTER)
2435 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, IS_SETTER)
2436 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, WRAPS_VFUNC)
2437 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, THROWS)
2438 _PyGI_ENUM_END
2440 /* GITypeTag */
2441 _PyGI_ENUM_BEGIN (TypeTag)
2442 /* Basic types */
2443 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, VOID)
2444 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, BOOLEAN)
2445 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INT8)
2446 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UINT8)
2447 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INT16)
2448 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UINT16)
2449 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INT32)
2450 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UINT32)
2451 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INT64)
2452 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UINT64)
2453 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, FLOAT)
2454 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, DOUBLE)
2455 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, GTYPE)
2456 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UTF8)
2457 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, FILENAME)
2459 /* Non-basic types; compare with G_TYPE_TAG_IS_BASIC */
2460 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, ARRAY)
2461 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INTERFACE)
2462 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, GLIST)
2463 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, GSLIST)
2464 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, GHASH)
2465 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, ERROR)
2467 /* Another basic type */
2468 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UNICHAR)
2469 _PyGI_ENUM_END
2471 /* GIInfoType */
2472 _PyGI_ENUM_BEGIN (InfoType)
2473 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, INVALID)
2474 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, FUNCTION)
2475 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, CALLBACK)
2476 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, STRUCT)
2477 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, BOXED)
2478 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, ENUM)
2479 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, FLAGS)
2480 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, OBJECT)
2481 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, INTERFACE)
2482 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, CONSTANT)
2483 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, INVALID_0)
2484 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, UNION)
2485 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, VALUE)
2486 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, SIGNAL)
2487 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, VFUNC)
2488 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, PROPERTY)
2489 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, FIELD)
2490 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, ARG)
2491 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, TYPE)
2492 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, UNRESOLVED)
2493 _PyGI_ENUM_END
2495 #undef _PyGI_ENUM_BEGIN
2496 #undef _PyGI_ENUM_ADD_VALUE
2497 #undef _PyGI_ENUM_END
2499 return 0;