Rename pyglib-python-compat.h to pygi-python-compat.h
[pygobject.git] / gi / pygi-info.c
blobd7338f43a69c94bd92ea25beb9bc545fa885618b
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 "pygtype.h"
32 /* _generate_doc_string
34 * C wrapper to call Python implemented "gi.docstring.generate_doc_string"
36 static PyObject *
37 _generate_doc_string(PyGIBaseInfo *self)
39 static PyObject *_py_generate_doc_string = NULL;
41 if (_py_generate_doc_string == NULL) {
42 PyObject *mod = PYGLIB_PyImport_ImportModule ("gi.docstring");
43 if (!mod)
44 return NULL;
46 _py_generate_doc_string = PyObject_GetAttrString (mod, "generate_doc_string");
47 if (_py_generate_doc_string == NULL) {
48 Py_DECREF (mod);
49 return NULL;
51 Py_DECREF (mod);
54 return PyObject_CallFunctionObjArgs (_py_generate_doc_string, self, NULL);
57 static PyObject *
58 _get_info_string (PyGIBaseInfo *self,
59 const gchar* (*get_info_string)(GIBaseInfo*))
61 const gchar *value = get_info_string ((GIBaseInfo*)self->info);
62 if (value == NULL) {
63 Py_RETURN_NONE;
65 return PYGLIB_PyUnicode_FromString (value);
68 static PyObject *
69 _get_child_info (PyGIBaseInfo *self,
70 GIBaseInfo* (*get_child_info)(GIBaseInfo*))
72 GIBaseInfo *info;
73 PyObject *py_info;
75 info = get_child_info ((GIBaseInfo*)self->info);
76 if (info == NULL) {
77 Py_RETURN_NONE;
80 py_info = _pygi_info_new (info);
81 g_base_info_unref (info);
82 return py_info;
86 static PyObject *
87 _get_child_info_by_name (PyGIBaseInfo *self, PyObject *py_name,
88 GIBaseInfo* (*get_child_info_by_name)(GIBaseInfo*, const gchar*))
90 GIBaseInfo *info;
91 PyObject *py_info;
92 char *name;
94 if (!PYGLIB_PyUnicode_Check (py_name)) {
95 PyErr_SetString (PyExc_TypeError, "expected string name");
96 return NULL;
99 name = PYGLIB_PyUnicode_AsString (py_name);
100 info = get_child_info_by_name ((GIObjectInfo*)self->info, name);
101 if (info == NULL) {
102 Py_RETURN_NONE;
105 py_info = _pygi_info_new (info);
106 g_base_info_unref (info);
107 return py_info;
111 /* _make_infos_tuple
113 * Build a tuple from the common API pattern in GI of having a
114 * function which returns a count and an indexed GIBaseInfo
115 * in the range of 0 to count;
117 static PyObject *
118 _make_infos_tuple (PyGIBaseInfo *self,
119 gint (*get_n_infos)(GIBaseInfo*),
120 GIBaseInfo* (*get_info)(GIBaseInfo*, gint))
122 gint n_infos;
123 PyObject *infos;
124 gint i;
126 n_infos = get_n_infos ( (GIBaseInfo *) self->info);
128 infos = PyTuple_New (n_infos);
129 if (infos == NULL) {
130 return NULL;
133 for (i = 0; i < n_infos; i++) {
134 GIBaseInfo *info;
135 PyObject *py_info;
137 info = (GIBaseInfo *) get_info (self->info, i);
138 g_assert (info != NULL);
140 py_info = _pygi_info_new (info);
142 g_base_info_unref (info);
144 if (py_info == NULL) {
145 Py_CLEAR (infos);
146 break;
149 PyTuple_SET_ITEM (infos, i, py_info);
152 return infos;
156 /* BaseInfo */
158 /* We need to be careful about calling g_base_info_get_name because
159 * calling it with a GI_INFO_TYPE_TYPE will crash.
160 * See: https://bugzilla.gnome.org/show_bug.cgi?id=709456
162 static const char *
163 _safe_base_info_get_name (GIBaseInfo *info)
165 if (g_base_info_get_type (info) == GI_INFO_TYPE_TYPE) {
166 return "type_type_instance";
167 } else {
168 return g_base_info_get_name (info);
172 static void
173 _base_info_dealloc (PyGIBaseInfo *self)
175 if (self->inst_weakreflist != NULL)
176 PyObject_ClearWeakRefs ( (PyObject *) self);
178 g_base_info_unref (self->info);
180 if (self->cache != NULL)
181 pygi_callable_cache_free ( (PyGICallableCache *) self->cache);
183 Py_TYPE (self)->tp_free ((PyObject *)self);
186 static PyObject *
187 _base_info_repr (PyGIBaseInfo *self)
190 return PYGLIB_PyUnicode_FromFormat ("%s(%s)",
191 Py_TYPE( (PyObject *) self)->tp_name,
192 _safe_base_info_get_name (self->info));
195 static PyObject *
196 _wrap_g_base_info_equal (PyGIBaseInfo *self, PyObject *other)
198 GIBaseInfo *other_info;
200 if (!PyObject_TypeCheck (other, &PyGIBaseInfo_Type)) {
201 Py_INCREF (Py_NotImplemented);
202 return Py_NotImplemented;
205 other_info = ((PyGIBaseInfo *)other)->info;
206 if (g_base_info_equal (self->info, other_info)) {
207 Py_RETURN_TRUE;
208 } else {
209 Py_RETURN_FALSE;
213 static PyObject *
214 _base_info_richcompare (PyGIBaseInfo *self, PyObject *other, int op)
216 PyObject *res;
218 switch (op) {
219 case Py_EQ:
220 return _wrap_g_base_info_equal (self, other);
221 case Py_NE:
222 res = _wrap_g_base_info_equal (self, other);
223 if (res == Py_True) {
224 Py_DECREF (res);
225 Py_RETURN_FALSE;
226 } else {
227 Py_DECREF (res);
228 Py_RETURN_TRUE;
230 default:
231 res = Py_NotImplemented;
232 break;
234 Py_INCREF(res);
235 return res;
238 PYGLIB_DEFINE_TYPE("gi.BaseInfo", PyGIBaseInfo_Type, PyGIBaseInfo);
240 gboolean
241 _pygi_is_python_keyword (const gchar *name)
243 /* It may be better to use keyword.iskeyword(); keep in sync with
244 * python -c 'import keyword; print(keyword.kwlist)' */
245 #if PY_VERSION_HEX < 0x03000000
246 /* Python 2.x */
247 static const gchar* keywords[] = {"and", "as", "assert", "break", "class",
248 "continue", "def", "del", "elif", "else", "except", "exec", "finally",
249 "for", "from", "global", "if", "import", "in", "is", "lambda", "not",
250 "or", "pass", "print", "raise", "return", "try", "while", "with",
251 "yield", NULL};
252 #elif PY_VERSION_HEX < 0x04000000
253 /* Python 3.x; note that we explicitly keep "print"; it is not a keyword
254 * any more, but we do not want to break API between Python versions */
255 static const gchar* keywords[] = {"False", "None", "True", "and", "as",
256 "assert", "break", "class", "continue", "def", "del", "elif", "else",
257 "except", "finally", "for", "from", "global", "if", "import", "in",
258 "is", "lambda", "nonlocal", "not", "or", "pass", "raise", "return",
259 "try", "while", "with", "yield",
260 "print", NULL};
261 #else
262 #error Need keyword list for this major Python version
263 #endif
265 const gchar **i;
267 for (i = keywords; *i != NULL; ++i) {
268 if (strcmp (name, *i) == 0) {
269 return TRUE;
273 return FALSE;
276 static PyObject *
277 _wrap_g_base_info_get_type (PyGIBaseInfo *self)
279 return PYGLIB_PyLong_FromLong (g_base_info_get_type (self->info));
282 static PyObject *
283 _wrap_g_base_info_get_name (PyGIBaseInfo *self)
285 const gchar *name;
287 name = _safe_base_info_get_name (self->info);
289 /* escape keywords */
290 if (_pygi_is_python_keyword (name)) {
291 gchar *escaped = g_strconcat (name, "_", NULL);
292 PyObject *obj = PYGLIB_PyUnicode_FromString (escaped);
293 g_free (escaped);
294 return obj;
297 return PYGLIB_PyUnicode_FromString (name);
300 static PyObject *
301 _wrap_g_base_info_get_name_unescaped (PyGIBaseInfo *self)
303 return _get_info_string (self, _safe_base_info_get_name);
306 static PyObject *
307 _wrap_g_base_info_get_namespace (PyGIBaseInfo *self)
309 return _get_info_string (self, g_base_info_get_namespace);
312 static PyObject *
313 _wrap_g_base_info_is_deprecated (PyGIBaseInfo *self)
315 if (g_base_info_is_deprecated (self->info))
316 Py_RETURN_TRUE;
317 else
318 Py_RETURN_FALSE;
321 static PyObject *
322 _wrap_g_base_info_get_attribute (PyGIBaseInfo *self, PyObject *arg)
324 char *name;
325 const char *value;
327 if (!PYGLIB_PyUnicode_Check (arg)) {
328 PyErr_SetString (PyExc_TypeError, "expected string name");
329 return NULL;
332 name = PYGLIB_PyUnicode_AsString (arg);
333 value = g_base_info_get_attribute (self->info, name);
334 if (value == NULL) {
335 Py_RETURN_NONE;
337 return PYGLIB_PyUnicode_FromString (value);
340 static PyObject *
341 _wrap_g_base_info_get_container (PyGIBaseInfo *self)
343 /* Note: don't use _get_child_info because g_base_info_get_container
344 * is marked as [transfer none] and therefore returns a borrowed ref.
346 GIBaseInfo *info;
348 info = g_base_info_get_container (self->info);
350 if (info == NULL) {
351 Py_RETURN_NONE;
354 return _pygi_info_new (info);
358 static PyMethodDef _PyGIBaseInfo_methods[] = {
359 { "get_type", (PyCFunction) _wrap_g_base_info_get_type, METH_NOARGS },
360 { "get_name", (PyCFunction) _wrap_g_base_info_get_name, METH_NOARGS },
361 { "get_name_unescaped", (PyCFunction) _wrap_g_base_info_get_name_unescaped, METH_NOARGS },
362 { "get_namespace", (PyCFunction) _wrap_g_base_info_get_namespace, METH_NOARGS },
363 { "is_deprecated", (PyCFunction) _wrap_g_base_info_is_deprecated, METH_NOARGS },
364 { "get_attribute", (PyCFunction) _wrap_g_base_info_get_attribute, METH_O },
365 { "get_container", (PyCFunction) _wrap_g_base_info_get_container, METH_NOARGS },
366 { "equal", (PyCFunction) _wrap_g_base_info_equal, METH_O },
367 { NULL, NULL, 0 }
370 /* _base_info_getattro:
372 * The usage of __getattr__ is needed because the get/set method table
373 * does not work for __doc__.
375 static PyObject *
376 _base_info_getattro(PyGIBaseInfo *self, PyObject *name)
378 PyObject *result;
380 static PyObject *docstr;
381 if (docstr == NULL) {
382 docstr= PYGLIB_PyUnicode_InternFromString("__doc__");
383 if (docstr == NULL)
384 return NULL;
387 Py_INCREF (name);
388 PYGLIB_PyUnicode_InternInPlace (&name);
390 if (name == docstr) {
391 result = _generate_doc_string (self);
392 } else {
393 result = PyObject_GenericGetAttr ((PyObject *)self, name);
396 Py_DECREF (name);
397 return result;
400 static PyObject *
401 _base_info_attr_name(PyGIBaseInfo *self, void *closure)
403 return _wrap_g_base_info_get_name (self);
406 static PyObject *
407 _base_info_attr_module(PyGIBaseInfo *self, void *closure)
409 return PYGLIB_PyUnicode_FromFormat ("gi.repository.%s",
410 g_base_info_get_namespace (self->info));
413 static PyGetSetDef _base_info_getsets[] = {
414 { "__name__", (getter)_base_info_attr_name, (setter)0, "Name", NULL},
415 { "__module__", (getter)_base_info_attr_module, (setter)0, "Module name", NULL},
416 { NULL, 0, 0 }
419 PyObject *
420 _pygi_info_new (GIBaseInfo *info)
422 GIInfoType info_type;
423 PyTypeObject *type = NULL;
424 PyGIBaseInfo *self;
426 info_type = g_base_info_get_type (info);
428 switch (info_type)
430 case GI_INFO_TYPE_INVALID:
431 PyErr_SetString (PyExc_RuntimeError, "Invalid info type");
432 return NULL;
433 case GI_INFO_TYPE_FUNCTION:
434 type = &PyGIFunctionInfo_Type;
435 break;
436 case GI_INFO_TYPE_CALLBACK:
437 type = &PyGICallbackInfo_Type;
438 break;
439 case GI_INFO_TYPE_STRUCT:
440 case GI_INFO_TYPE_BOXED:
441 type = &PyGIStructInfo_Type;
442 break;
443 case GI_INFO_TYPE_ENUM:
444 case GI_INFO_TYPE_FLAGS:
445 type = &PyGIEnumInfo_Type;
446 break;
447 case GI_INFO_TYPE_OBJECT:
448 type = &PyGIObjectInfo_Type;
449 break;
450 case GI_INFO_TYPE_INTERFACE:
451 type = &PyGIInterfaceInfo_Type;
452 break;
453 case GI_INFO_TYPE_CONSTANT:
454 type = &PyGIConstantInfo_Type;
455 break;
456 case GI_INFO_TYPE_UNION:
457 type = &PyGIUnionInfo_Type;
458 break;
459 case GI_INFO_TYPE_VALUE:
460 type = &PyGIValueInfo_Type;
461 break;
462 case GI_INFO_TYPE_SIGNAL:
463 type = &PyGISignalInfo_Type;
464 break;
465 case GI_INFO_TYPE_VFUNC:
466 type = &PyGIVFuncInfo_Type;
467 break;
468 case GI_INFO_TYPE_PROPERTY:
469 type = &PyGIPropertyInfo_Type;
470 break;
471 case GI_INFO_TYPE_FIELD:
472 type = &PyGIFieldInfo_Type;
473 break;
474 case GI_INFO_TYPE_ARG:
475 type = &PyGIArgInfo_Type;
476 break;
477 case GI_INFO_TYPE_TYPE:
478 type = &PyGITypeInfo_Type;
479 break;
480 case GI_INFO_TYPE_UNRESOLVED:
481 type = &PyGIUnresolvedInfo_Type;
482 break;
483 default:
484 g_assert_not_reached();
485 break;
488 self = (PyGIBaseInfo *) type->tp_alloc (type, 0);
489 if (self == NULL) {
490 return NULL;
493 self->info = g_base_info_ref (info);
494 self->inst_weakreflist = NULL;
495 self->cache = NULL;
497 return (PyObject *) self;
500 GIBaseInfo *
501 _pygi_object_get_gi_info (PyObject *object,
502 PyTypeObject *type)
504 PyObject *py_info;
505 GIBaseInfo *info = NULL;
507 py_info = PyObject_GetAttrString (object, "__info__");
508 if (py_info == NULL) {
509 return NULL;
511 if (!PyObject_TypeCheck (py_info, type)) {
512 PyErr_Format (PyExc_TypeError, "attribute '__info__' must be %s, not %s",
513 type->tp_name, Py_TYPE(py_info)->tp_name);
514 goto out;
517 info = ( (PyGIBaseInfo *) py_info)->info;
518 g_base_info_ref (info);
520 out:
521 Py_DECREF (py_info);
523 return info;
527 /* CallableInfo */
528 PYGLIB_DEFINE_TYPE ("gi.CallableInfo", PyGICallableInfo_Type, PyGICallableInfo);
530 /* _callable_info_call:
532 * Shared wrapper for invoke which can be bound (instance method or class constructor)
533 * or unbound (function or static method).
535 static PyObject *
536 _callable_info_call (PyGICallableInfo *self, PyObject *args, PyObject *kwargs)
538 /* Insert the bound arg at the beginning of the invoke method args. */
539 if (self->py_bound_arg) {
540 int i;
541 PyObject *result;
542 Py_ssize_t argcount = PyTuple_Size (args);
543 PyObject *newargs = PyTuple_New (argcount + 1);
544 if (newargs == NULL)
545 return NULL;
547 Py_INCREF (self->py_bound_arg);
548 PyTuple_SET_ITEM (newargs, 0, self->py_bound_arg);
550 for (i = 0; i < argcount; i++) {
551 PyObject *v = PyTuple_GET_ITEM (args, i);
552 Py_XINCREF (v);
553 PyTuple_SET_ITEM (newargs, i+1, v);
556 /* Invoke with the original GI info struct this wrapper was based upon.
557 * This is necessary to maintain the same cache for all bound versions.
559 result = _wrap_g_callable_info_invoke ((PyGIBaseInfo *)self->py_unbound_info,
560 newargs, kwargs);
561 Py_DECREF (newargs);
562 return result;
564 } else {
565 /* We should never have an unbound info when calling when calling invoke
566 * at this point because the descriptor implementation on sub-classes
567 * should return "self" not a copy when there is no bound arg.
569 g_assert (self->py_unbound_info == NULL);
570 return _wrap_g_callable_info_invoke ((PyGIBaseInfo *)self, args, kwargs);
575 /* _function_info_call:
577 * Specialization of _callable_info_call for GIFunctionInfo which
578 * handles constructor error conditions.
580 static PyObject *
581 _function_info_call (PyGICallableInfo *self, PyObject *args, PyObject *kwargs)
583 if (self->py_bound_arg) {
584 GIFunctionInfoFlags flags;
586 /* Ensure constructors are only called as class methods on the class
587 * implementing the constructor and not on sub-classes.
589 flags = g_function_info_get_flags ( (GIFunctionInfo*) self->base.info);
590 if (flags & GI_FUNCTION_IS_CONSTRUCTOR) {
591 PyObject *py_str_name;
592 const gchar *str_name;
593 GIBaseInfo *container_info = g_base_info_get_container (self->base.info);
594 g_assert (container_info != NULL);
596 py_str_name = PyObject_GetAttrString (self->py_bound_arg, "__name__");
597 if (py_str_name == NULL)
598 return NULL;
600 if (PyUnicode_Check (py_str_name) ) {
601 PyObject *tmp = PyUnicode_AsUTF8String (py_str_name);
602 Py_DECREF (py_str_name);
603 py_str_name = tmp;
606 #if PY_VERSION_HEX < 0x03000000
607 str_name = PyString_AsString (py_str_name);
608 #else
609 str_name = PyBytes_AsString (py_str_name);
610 #endif
612 if (strcmp (str_name, _safe_base_info_get_name (container_info))) {
613 PyErr_Format (PyExc_TypeError,
614 "%s constructor cannot be used to create instances of "
615 "a subclass %s",
616 _safe_base_info_get_name (container_info),
617 str_name);
618 Py_DECREF (py_str_name);
619 return NULL;
621 Py_DECREF (py_str_name);
625 return _callable_info_call (self, args, kwargs);
628 /* _new_bound_callable_info
630 * Utility function for sub-classes to create a bound version of themself.
632 static PyGICallableInfo *
633 _new_bound_callable_info (PyGICallableInfo *self, PyObject *bound_arg)
635 PyGICallableInfo *new_self;
637 /* Return self if this is already bound or there is nothing passed to bind. */
638 if (self->py_bound_arg != NULL || bound_arg == NULL || bound_arg == Py_None) {
639 Py_INCREF ((PyObject *)self);
640 return self;
643 new_self = (PyGICallableInfo *)_pygi_info_new (self->base.info);
644 if (new_self == NULL)
645 return NULL;
647 Py_INCREF ((PyObject *)self);
648 new_self->py_unbound_info = (struct PyGICallableInfo *)self;
650 Py_INCREF (bound_arg);
651 new_self->py_bound_arg = bound_arg;
653 return new_self;
656 /* _function_info_descr_get
658 * Descriptor protocol implementation for functions, methods, and constructors.
660 static PyObject *
661 _function_info_descr_get (PyGICallableInfo *self, PyObject *obj, PyObject *type) {
662 GIFunctionInfoFlags flags;
663 PyObject *bound_arg = NULL;
665 flags = g_function_info_get_flags ( (GIFunctionInfo*) self->base.info);
666 if (flags & GI_FUNCTION_IS_CONSTRUCTOR) {
667 if (type == NULL)
668 bound_arg = (PyObject *)(Py_TYPE(obj));
669 else
670 bound_arg = type;
671 } else if (flags & GI_FUNCTION_IS_METHOD) {
672 bound_arg = obj;
675 return (PyObject *)_new_bound_callable_info (self, bound_arg);
678 /* _vfunc_info_descr_get
680 * Descriptor protocol implementation for virtual functions.
682 static PyObject *
683 _vfunc_info_descr_get (PyGICallableInfo *self, PyObject *obj, PyObject *type) {
684 PyObject *result;
685 PyObject *bound_arg = NULL;
687 bound_arg = PyObject_GetAttrString (type, "__gtype__");
688 if (bound_arg == NULL)
689 return NULL;
691 /* _new_bound_callable_info adds its own ref so free the one from GetAttrString */
692 result = (PyObject *)_new_bound_callable_info (self, bound_arg);
693 Py_DECREF (bound_arg);
694 return result;
697 static void
698 _callable_info_dealloc (PyGICallableInfo *self)
700 Py_CLEAR (self->py_unbound_info);
701 Py_CLEAR (self->py_bound_arg);
703 PyGIBaseInfo_Type.tp_dealloc ((PyObject *) self);
706 static PyObject *
707 _wrap_g_callable_info_get_arguments (PyGIBaseInfo *self)
709 return _make_infos_tuple (self, g_callable_info_get_n_args, g_callable_info_get_arg);
712 static PyObject *
713 _wrap_g_callable_info_get_return_type (PyGIBaseInfo *self)
715 return _get_child_info (self, g_callable_info_get_return_type);
718 static PyObject *
719 _wrap_g_callable_info_get_caller_owns (PyGIBaseInfo *self)
721 return PYGLIB_PyLong_FromLong (
722 g_callable_info_get_caller_owns (self->info) );
725 static PyObject *
726 _wrap_g_callable_info_may_return_null (PyGIBaseInfo *self)
728 return PyBool_FromLong (
729 g_callable_info_may_return_null (self->info) );
732 static PyObject *
733 _wrap_g_callable_info_skip_return (PyGIBaseInfo *self)
735 return PyBool_FromLong (g_callable_info_skip_return (self->info));
738 static PyObject *
739 _wrap_g_callable_info_get_return_attribute (PyGIBaseInfo *self, PyObject *py_name)
741 gchar *name;
742 const gchar *attr;
744 if (!PYGLIB_PyUnicode_Check (py_name)) {
745 PyErr_SetString (PyExc_TypeError, "expected string name");
746 return NULL;
749 name = PYGLIB_PyUnicode_AsString (py_name);
750 attr = g_callable_info_get_return_attribute (self->info, name);
751 if (attr) {
752 return PYGLIB_PyUnicode_FromString (
753 g_callable_info_get_return_attribute (self->info, name));
754 } else {
755 PyErr_Format(PyExc_AttributeError, "return attribute %s not found", name);
756 return NULL;
760 static PyObject *
761 _wrap_g_callable_info_can_throw_gerror (PyGIBaseInfo *self)
763 if (g_callable_info_can_throw_gerror (self->info))
764 Py_RETURN_TRUE;
765 else
766 Py_RETURN_FALSE;
769 static PyMethodDef _PyGICallableInfo_methods[] = {
770 { "invoke", (PyCFunction) _wrap_g_callable_info_invoke, METH_VARARGS | METH_KEYWORDS },
771 { "get_arguments", (PyCFunction) _wrap_g_callable_info_get_arguments, METH_NOARGS },
772 { "get_return_type", (PyCFunction) _wrap_g_callable_info_get_return_type, METH_NOARGS },
773 { "get_caller_owns", (PyCFunction) _wrap_g_callable_info_get_caller_owns, METH_NOARGS },
774 { "may_return_null", (PyCFunction) _wrap_g_callable_info_may_return_null, METH_NOARGS },
775 { "skip_return", (PyCFunction) _wrap_g_callable_info_skip_return, METH_NOARGS },
776 { "get_return_attribute", (PyCFunction) _wrap_g_callable_info_get_return_attribute, METH_O },
777 { "can_throw_gerror", (PyCFunction) _wrap_g_callable_info_can_throw_gerror, METH_NOARGS },
778 { NULL, NULL, 0 }
781 /* CallbackInfo */
782 PYGLIB_DEFINE_TYPE ("gi.CallbackInfo", PyGICallbackInfo_Type, PyGICallableInfo);
784 static PyMethodDef _PyGICallbackInfo_methods[] = {
785 { NULL, NULL, 0 }
788 /* ErrorDomainInfo */
789 PYGLIB_DEFINE_TYPE ("gi.ErrorDomainInfo", PyGIErrorDomainInfo_Type, PyGIBaseInfo);
791 static PyMethodDef _PyGIErrorDomainInfo_methods[] = {
792 { NULL, NULL, 0 }
795 /* SignalInfo */
796 PYGLIB_DEFINE_TYPE ("gi.SignalInfo", PyGISignalInfo_Type, PyGICallableInfo);
798 static PyObject *
799 _wrap_g_signal_info_get_flags (PyGIBaseInfo *self)
801 return PYGLIB_PyLong_FromLong (
802 g_signal_info_get_flags ((GISignalInfo *)self->info) );
805 static PyObject *
806 _wrap_g_signal_info_get_class_closure (PyGIBaseInfo *self)
808 return _get_child_info (self, g_signal_info_get_class_closure);
811 static PyObject *
812 _wrap_g_signal_info_true_stops_emit (PyGIBaseInfo *self)
814 return PyBool_FromLong (
815 g_signal_info_true_stops_emit ((GISignalInfo *)self->info) );
818 static PyMethodDef _PyGISignalInfo_methods[] = {
819 { "get_flags", (PyCFunction) _wrap_g_signal_info_get_flags, METH_NOARGS },
820 { "get_class_closure", (PyCFunction) _wrap_g_signal_info_get_class_closure, METH_NOARGS },
821 { "true_stops_emit", (PyCFunction) _wrap_g_signal_info_true_stops_emit, METH_NOARGS },
822 { NULL, NULL, 0 }
825 /* PropertyInfo */
826 PYGLIB_DEFINE_TYPE ("gi.PropertyInfo", PyGIPropertyInfo_Type, PyGIBaseInfo);
828 static PyObject *
829 _wrap_g_property_info_get_flags (PyGIBaseInfo *self)
831 return PYGLIB_PyLong_FromLong (
832 g_property_info_get_flags ((GIPropertyInfo *)self->info) );
835 static PyObject *
836 _wrap_g_property_info_get_type (PyGIBaseInfo *self)
838 return _get_child_info (self, g_property_info_get_type);
841 static PyObject *
842 _wrap_g_property_info_get_ownership_transfer (PyGIBaseInfo *self)
844 return PYGLIB_PyLong_FromLong (
845 g_property_info_get_ownership_transfer ((GIPropertyInfo *)self->info) );
848 static PyMethodDef _PyGIPropertyInfo_methods[] = {
849 { "get_flags", (PyCFunction) _wrap_g_property_info_get_flags, METH_NOARGS },
850 { "get_type", (PyCFunction) _wrap_g_property_info_get_type, METH_NOARGS },
851 { "get_ownership_transfer", (PyCFunction) _wrap_g_property_info_get_ownership_transfer, METH_NOARGS },
852 { NULL, NULL, 0 }
856 /* ArgInfo */
857 PYGLIB_DEFINE_TYPE ("gi.ArgInfo", PyGIArgInfo_Type, PyGIBaseInfo);
859 static PyObject *
860 _wrap_g_arg_info_get_direction (PyGIBaseInfo *self)
862 return PYGLIB_PyLong_FromLong (
863 g_arg_info_get_direction ((GIArgInfo*)self->info) );
866 static PyObject *
867 _wrap_g_arg_info_is_caller_allocates (PyGIBaseInfo *self)
869 return PyBool_FromLong (
870 g_arg_info_is_caller_allocates ((GIArgInfo*)self->info) );
873 static PyObject *
874 _wrap_g_arg_info_is_return_value (PyGIBaseInfo *self)
876 return PyBool_FromLong (
877 g_arg_info_is_return_value ((GIArgInfo*)self->info) );
880 static PyObject *
881 _wrap_g_arg_info_is_optional (PyGIBaseInfo *self)
883 return PyBool_FromLong (
884 g_arg_info_is_optional ((GIArgInfo*)self->info) );
887 static PyObject *
888 _wrap_g_arg_info_may_be_null (PyGIBaseInfo *self)
890 return PyBool_FromLong (
891 g_arg_info_may_be_null ((GIArgInfo*)self->info) );
894 static PyObject *
895 _wrap_g_arg_info_get_ownership_transfer (PyGIBaseInfo *self)
897 return PYGLIB_PyLong_FromLong (
898 g_arg_info_get_ownership_transfer ((GIArgInfo *)self->info) );
901 static PyObject *
902 _wrap_g_arg_info_get_scope (PyGIBaseInfo *self)
904 return PYGLIB_PyLong_FromLong (
905 g_arg_info_get_scope ((GIArgInfo *)self->info) );
908 static PyObject *
909 _wrap_g_arg_info_get_closure (PyGIBaseInfo *self)
911 return PYGLIB_PyLong_FromLong (
912 g_arg_info_get_closure ((GIArgInfo *)self->info) );
915 static PyObject *
916 _wrap_g_arg_info_get_destroy (PyGIBaseInfo *self)
918 return PYGLIB_PyLong_FromLong (
919 g_arg_info_get_destroy ((GIArgInfo *)self->info) );
922 static PyObject *
923 _wrap_g_arg_info_get_type (PyGIBaseInfo *self)
925 return _get_child_info (self, g_arg_info_get_type);
928 static PyMethodDef _PyGIArgInfo_methods[] = {
929 { "get_direction", (PyCFunction) _wrap_g_arg_info_get_direction, METH_NOARGS },
930 { "is_caller_allocates", (PyCFunction) _wrap_g_arg_info_is_caller_allocates, METH_NOARGS },
931 { "is_return_value", (PyCFunction) _wrap_g_arg_info_is_return_value, METH_NOARGS },
932 { "is_optional", (PyCFunction) _wrap_g_arg_info_is_optional, METH_NOARGS },
933 { "may_be_null", (PyCFunction) _wrap_g_arg_info_may_be_null, METH_NOARGS },
934 { "get_ownership_transfer", (PyCFunction) _wrap_g_arg_info_get_ownership_transfer, METH_NOARGS },
935 { "get_scope", (PyCFunction) _wrap_g_arg_info_get_scope, METH_NOARGS },
936 { "get_closure", (PyCFunction) _wrap_g_arg_info_get_closure, METH_NOARGS },
937 { "get_destroy", (PyCFunction) _wrap_g_arg_info_get_destroy, METH_NOARGS },
938 { "get_type", (PyCFunction) _wrap_g_arg_info_get_type, METH_NOARGS },
939 { NULL, NULL, 0 }
943 /* TypeInfo */
944 PYGLIB_DEFINE_TYPE ("gi.TypeInfo", PyGITypeInfo_Type, PyGIBaseInfo);
946 static PyObject *
947 _wrap_g_type_info_is_pointer (PyGIBaseInfo *self)
949 return PyBool_FromLong (g_type_info_is_pointer (self->info));
952 static PyObject *
953 _wrap_g_type_info_get_tag (PyGIBaseInfo *self)
955 return PYGLIB_PyLong_FromLong (g_type_info_get_tag (self->info));
958 static PyObject *
959 _wrap_g_type_info_get_tag_as_string (PyGIBaseInfo *self)
961 GITypeTag tag = g_type_info_get_tag (self->info);
962 return PYGLIB_PyUnicode_FromString (g_type_tag_to_string(tag));
965 static PyObject *
966 _wrap_g_type_info_get_param_type (PyGIBaseInfo *self, PyObject *py_n)
968 GIBaseInfo *info;
969 PyObject *py_info;
970 gint n;
972 if (!PYGLIB_PyLong_Check (py_n)) {
973 PyErr_SetString(PyExc_TypeError, "expected integer value");
974 return NULL;
977 n = PYGLIB_PyLong_AsLong (py_n);
978 info = (GIBaseInfo *) g_type_info_get_param_type ( (GITypeInfo *) self->info, n);
979 if (info == NULL) {
980 Py_RETURN_NONE;
983 py_info = _pygi_info_new (info);
984 g_base_info_unref (info);
985 return py_info;
988 static PyObject *
989 _wrap_g_type_info_get_interface (PyGIBaseInfo *self)
991 return _get_child_info (self, g_type_info_get_interface);
994 static PyObject *
995 _wrap_g_type_info_get_array_length (PyGIBaseInfo *self)
997 return PYGLIB_PyLong_FromLong (g_type_info_get_array_length (self->info));
1000 static PyObject *
1001 _wrap_g_type_info_get_array_fixed_size (PyGIBaseInfo *self)
1003 return PYGLIB_PyLong_FromLong (g_type_info_get_array_fixed_size (self->info));
1006 static PyObject *
1007 _wrap_g_type_info_is_zero_terminated (PyGIBaseInfo *self)
1009 return PyBool_FromLong (g_type_info_is_zero_terminated (self->info));
1012 static PyObject *
1013 _wrap_g_type_info_get_array_type (PyGIBaseInfo *self)
1015 return PYGLIB_PyLong_FromLong (g_type_info_get_array_type (self->info));
1018 static PyMethodDef _PyGITypeInfo_methods[] = {
1019 { "is_pointer", (PyCFunction) _wrap_g_type_info_is_pointer, METH_NOARGS },
1020 { "get_tag", (PyCFunction) _wrap_g_type_info_get_tag, METH_NOARGS },
1021 { "get_tag_as_string", (PyCFunction) _wrap_g_type_info_get_tag_as_string, METH_NOARGS },
1022 { "get_param_type", (PyCFunction) _wrap_g_type_info_get_param_type, METH_O },
1023 { "get_interface", (PyCFunction) _wrap_g_type_info_get_interface, METH_NOARGS },
1024 { "get_array_length", (PyCFunction) _wrap_g_type_info_get_array_length, METH_NOARGS },
1025 { "get_array_fixed_size", (PyCFunction) _wrap_g_type_info_get_array_fixed_size, METH_NOARGS },
1026 { "is_zero_terminated", (PyCFunction) _wrap_g_type_info_is_zero_terminated, METH_NOARGS },
1027 { "get_array_type", (PyCFunction) _wrap_g_type_info_get_array_type, METH_NOARGS },
1028 { NULL, NULL, 0 }
1032 /* FunctionInfo */
1033 PYGLIB_DEFINE_TYPE ("gi.FunctionInfo", PyGIFunctionInfo_Type, PyGICallableInfo);
1035 static PyObject *
1036 _wrap_g_function_info_is_constructor (PyGIBaseInfo *self)
1038 GIFunctionInfoFlags flags;
1039 gboolean is_constructor;
1041 flags = g_function_info_get_flags ( (GIFunctionInfo*) self->info);
1042 is_constructor = flags & GI_FUNCTION_IS_CONSTRUCTOR;
1044 return PyBool_FromLong (is_constructor);
1047 static PyObject *
1048 _wrap_g_function_info_is_method (PyGIBaseInfo *self)
1050 GIFunctionInfoFlags flags;
1051 gboolean is_method;
1053 flags = g_function_info_get_flags ( (GIFunctionInfo*) self->info);
1054 is_method = flags & GI_FUNCTION_IS_METHOD;
1056 return PyBool_FromLong (is_method);
1059 gsize
1060 _pygi_g_type_tag_size (GITypeTag type_tag)
1062 gsize size = 0;
1064 switch (type_tag) {
1065 case GI_TYPE_TAG_BOOLEAN:
1066 size = sizeof (gboolean);
1067 break;
1068 case GI_TYPE_TAG_INT8:
1069 case GI_TYPE_TAG_UINT8:
1070 size = sizeof (gint8);
1071 break;
1072 case GI_TYPE_TAG_INT16:
1073 case GI_TYPE_TAG_UINT16:
1074 size = sizeof (gint16);
1075 break;
1076 case GI_TYPE_TAG_INT32:
1077 case GI_TYPE_TAG_UINT32:
1078 size = sizeof (gint32);
1079 break;
1080 case GI_TYPE_TAG_INT64:
1081 case GI_TYPE_TAG_UINT64:
1082 size = sizeof (gint64);
1083 break;
1084 case GI_TYPE_TAG_FLOAT:
1085 size = sizeof (gfloat);
1086 break;
1087 case GI_TYPE_TAG_DOUBLE:
1088 size = sizeof (gdouble);
1089 break;
1090 case GI_TYPE_TAG_GTYPE:
1091 size = sizeof (GType);
1092 break;
1093 case GI_TYPE_TAG_UNICHAR:
1094 size = sizeof (gunichar);
1095 break;
1096 case GI_TYPE_TAG_VOID:
1097 case GI_TYPE_TAG_UTF8:
1098 case GI_TYPE_TAG_FILENAME:
1099 case GI_TYPE_TAG_ARRAY:
1100 case GI_TYPE_TAG_INTERFACE:
1101 case GI_TYPE_TAG_GLIST:
1102 case GI_TYPE_TAG_GSLIST:
1103 case GI_TYPE_TAG_GHASH:
1104 case GI_TYPE_TAG_ERROR:
1105 PyErr_Format (PyExc_TypeError,
1106 "Unable to know the size (assuming %s is not a pointer)",
1107 g_type_tag_to_string (type_tag));
1108 break;
1109 default:
1110 break;
1113 return size;
1116 gsize
1117 _pygi_g_type_info_size (GITypeInfo *type_info)
1119 gsize size = 0;
1121 GITypeTag type_tag;
1123 type_tag = g_type_info_get_tag (type_info);
1124 switch (type_tag) {
1125 case GI_TYPE_TAG_BOOLEAN:
1126 case GI_TYPE_TAG_INT8:
1127 case GI_TYPE_TAG_UINT8:
1128 case GI_TYPE_TAG_INT16:
1129 case GI_TYPE_TAG_UINT16:
1130 case GI_TYPE_TAG_INT32:
1131 case GI_TYPE_TAG_UINT32:
1132 case GI_TYPE_TAG_INT64:
1133 case GI_TYPE_TAG_UINT64:
1134 case GI_TYPE_TAG_FLOAT:
1135 case GI_TYPE_TAG_DOUBLE:
1136 case GI_TYPE_TAG_GTYPE:
1137 case GI_TYPE_TAG_UNICHAR:
1138 size = _pygi_g_type_tag_size (type_tag);
1139 g_assert (size > 0);
1140 break;
1141 case GI_TYPE_TAG_INTERFACE:
1143 GIBaseInfo *info;
1144 GIInfoType info_type;
1146 info = g_type_info_get_interface (type_info);
1147 info_type = g_base_info_get_type (info);
1149 switch (info_type) {
1150 case GI_INFO_TYPE_STRUCT:
1151 if (g_type_info_is_pointer (type_info)) {
1152 size = sizeof (gpointer);
1153 } else {
1154 size = g_struct_info_get_size ( (GIStructInfo *) info);
1156 break;
1157 case GI_INFO_TYPE_UNION:
1158 if (g_type_info_is_pointer (type_info)) {
1159 size = sizeof (gpointer);
1160 } else {
1161 size = g_union_info_get_size ( (GIUnionInfo *) info);
1163 break;
1164 case GI_INFO_TYPE_ENUM:
1165 case GI_INFO_TYPE_FLAGS:
1166 if (g_type_info_is_pointer (type_info)) {
1167 size = sizeof (gpointer);
1168 } else {
1169 GITypeTag enum_type_tag;
1171 enum_type_tag = g_enum_info_get_storage_type ( (GIEnumInfo *) info);
1172 size = _pygi_g_type_tag_size (enum_type_tag);
1174 break;
1175 case GI_INFO_TYPE_BOXED:
1176 case GI_INFO_TYPE_OBJECT:
1177 case GI_INFO_TYPE_INTERFACE:
1178 case GI_INFO_TYPE_CALLBACK:
1179 size = sizeof (gpointer);
1180 break;
1181 case GI_INFO_TYPE_VFUNC:
1182 case GI_INFO_TYPE_INVALID:
1183 case GI_INFO_TYPE_FUNCTION:
1184 case GI_INFO_TYPE_CONSTANT:
1185 case GI_INFO_TYPE_VALUE:
1186 case GI_INFO_TYPE_SIGNAL:
1187 case GI_INFO_TYPE_PROPERTY:
1188 case GI_INFO_TYPE_FIELD:
1189 case GI_INFO_TYPE_ARG:
1190 case GI_INFO_TYPE_TYPE:
1191 case GI_INFO_TYPE_UNRESOLVED:
1192 default:
1193 g_assert_not_reached();
1194 break;
1197 g_base_info_unref (info);
1198 break;
1200 case GI_TYPE_TAG_ARRAY:
1201 case GI_TYPE_TAG_VOID:
1202 case GI_TYPE_TAG_UTF8:
1203 case GI_TYPE_TAG_FILENAME:
1204 case GI_TYPE_TAG_GLIST:
1205 case GI_TYPE_TAG_GSLIST:
1206 case GI_TYPE_TAG_GHASH:
1207 case GI_TYPE_TAG_ERROR:
1208 size = sizeof (gpointer);
1209 break;
1210 default:
1211 break;
1214 return size;
1217 static PyObject *
1218 _wrap_g_function_info_get_symbol (PyGIBaseInfo *self)
1220 return _get_info_string (self, g_function_info_get_symbol);
1223 static PyObject *
1224 _wrap_g_function_info_get_flags (PyGIBaseInfo *self)
1226 return PYGLIB_PyLong_FromLong (g_function_info_get_flags (self->info));
1229 static PyObject *
1230 _wrap_g_function_info_get_property (PyGIBaseInfo *self)
1232 return _get_child_info (self, g_function_info_get_property);
1235 static PyObject *
1236 _wrap_g_function_info_get_vfunc (PyGIBaseInfo *self)
1238 return _get_child_info (self, g_function_info_get_vfunc);
1241 static PyMethodDef _PyGIFunctionInfo_methods[] = {
1242 { "is_constructor", (PyCFunction) _wrap_g_function_info_is_constructor, METH_NOARGS },
1243 { "is_method", (PyCFunction) _wrap_g_function_info_is_method, METH_NOARGS },
1244 { "get_symbol", (PyCFunction) _wrap_g_function_info_get_symbol, METH_NOARGS },
1245 { "get_flags", (PyCFunction) _wrap_g_function_info_get_flags, METH_NOARGS },
1246 { "get_property", (PyCFunction) _wrap_g_function_info_get_property, METH_NOARGS },
1247 { "get_vfunc", (PyCFunction) _wrap_g_function_info_get_vfunc, METH_NOARGS },
1248 { NULL, NULL, 0 }
1251 /* RegisteredTypeInfo */
1252 PYGLIB_DEFINE_TYPE ("gi.RegisteredTypeInfo", PyGIRegisteredTypeInfo_Type, PyGIBaseInfo);
1254 static PyObject *
1255 _wrap_g_registered_type_info_get_type_name (PyGIBaseInfo *self)
1257 return _get_info_string (self, g_registered_type_info_get_type_name);
1260 static PyObject *
1261 _wrap_g_registered_type_info_get_type_init (PyGIBaseInfo *self)
1263 return _get_info_string (self, g_registered_type_info_get_type_init);
1266 static PyObject *
1267 _wrap_g_registered_type_info_get_g_type (PyGIBaseInfo *self)
1269 GType type;
1271 type = g_registered_type_info_get_g_type ( (GIRegisteredTypeInfo *) self->info);
1273 return pyg_type_wrapper_new (type);
1276 static PyMethodDef _PyGIRegisteredTypeInfo_methods[] = {
1277 { "get_type_name", (PyCFunction) _wrap_g_registered_type_info_get_type_name, METH_NOARGS },
1278 { "get_type_init", (PyCFunction) _wrap_g_registered_type_info_get_type_init, METH_NOARGS },
1279 { "get_g_type", (PyCFunction) _wrap_g_registered_type_info_get_g_type, METH_NOARGS },
1280 { NULL, NULL, 0 }
1284 /* GIStructInfo */
1285 PYGLIB_DEFINE_TYPE ("StructInfo", PyGIStructInfo_Type, PyGIBaseInfo);
1287 static PyObject *
1288 _wrap_g_struct_info_get_fields (PyGIBaseInfo *self)
1290 return _make_infos_tuple (self, g_struct_info_get_n_fields, g_struct_info_get_field);
1293 static PyObject *
1294 _wrap_g_struct_info_get_methods (PyGIBaseInfo *self)
1296 return _make_infos_tuple (self, g_struct_info_get_n_methods, g_struct_info_get_method);
1299 static PyObject *
1300 _wrap_g_struct_info_get_size (PyGIBaseInfo *self)
1302 return PYGLIB_PyLong_FromSize_t (g_struct_info_get_size (self->info));
1305 static PyObject *
1306 _wrap_g_struct_info_get_alignment (PyGIBaseInfo *self)
1308 return PYGLIB_PyLong_FromSize_t (g_struct_info_get_alignment (self->info));
1311 static PyObject *
1312 _wrap_g_struct_info_is_gtype_struct (PyGIBaseInfo *self)
1314 return PyBool_FromLong (g_struct_info_is_gtype_struct (self->info));
1317 static PyObject *
1318 _wrap_g_struct_info_is_foreign (PyGIBaseInfo *self)
1320 return PyBool_FromLong (g_struct_info_is_foreign (self->info));
1323 static PyMethodDef _PyGIStructInfo_methods[] = {
1324 { "get_fields", (PyCFunction) _wrap_g_struct_info_get_fields, METH_NOARGS },
1325 { "get_methods", (PyCFunction) _wrap_g_struct_info_get_methods, METH_NOARGS },
1326 { "get_size", (PyCFunction) _wrap_g_struct_info_get_size, METH_NOARGS },
1327 { "get_alignment", (PyCFunction) _wrap_g_struct_info_get_alignment, METH_NOARGS },
1328 { "is_gtype_struct", (PyCFunction) _wrap_g_struct_info_is_gtype_struct, METH_NOARGS },
1329 { "is_foreign", (PyCFunction) _wrap_g_struct_info_is_foreign, METH_NOARGS },
1330 { NULL, NULL, 0 }
1333 gboolean
1334 pygi_g_struct_info_is_simple (GIStructInfo *struct_info)
1336 gboolean is_simple;
1337 gsize n_field_infos;
1338 gsize i;
1340 is_simple = TRUE;
1342 n_field_infos = g_struct_info_get_n_fields (struct_info);
1344 for (i = 0; i < n_field_infos && is_simple; i++) {
1345 GIFieldInfo *field_info;
1346 GITypeInfo *field_type_info;
1347 GITypeTag field_type_tag;
1349 field_info = g_struct_info_get_field (struct_info, i);
1350 field_type_info = g_field_info_get_type (field_info);
1353 field_type_tag = g_type_info_get_tag (field_type_info);
1355 switch (field_type_tag) {
1356 case GI_TYPE_TAG_BOOLEAN:
1357 case GI_TYPE_TAG_INT8:
1358 case GI_TYPE_TAG_UINT8:
1359 case GI_TYPE_TAG_INT16:
1360 case GI_TYPE_TAG_UINT16:
1361 case GI_TYPE_TAG_INT32:
1362 case GI_TYPE_TAG_UINT32:
1363 case GI_TYPE_TAG_INT64:
1364 case GI_TYPE_TAG_UINT64:
1365 case GI_TYPE_TAG_FLOAT:
1366 case GI_TYPE_TAG_DOUBLE:
1367 case GI_TYPE_TAG_UNICHAR:
1368 if (g_type_info_is_pointer (field_type_info)) {
1369 is_simple = FALSE;
1371 break;
1372 case GI_TYPE_TAG_VOID:
1373 case GI_TYPE_TAG_GTYPE:
1374 case GI_TYPE_TAG_ERROR:
1375 case GI_TYPE_TAG_UTF8:
1376 case GI_TYPE_TAG_FILENAME:
1377 case GI_TYPE_TAG_ARRAY:
1378 case GI_TYPE_TAG_GLIST:
1379 case GI_TYPE_TAG_GSLIST:
1380 case GI_TYPE_TAG_GHASH:
1381 is_simple = FALSE;
1382 break;
1383 case GI_TYPE_TAG_INTERFACE:
1385 GIBaseInfo *info;
1386 GIInfoType info_type;
1388 info = g_type_info_get_interface (field_type_info);
1389 info_type = g_base_info_get_type (info);
1391 switch (info_type) {
1392 case GI_INFO_TYPE_STRUCT:
1393 if (g_type_info_is_pointer (field_type_info)) {
1394 is_simple = FALSE;
1395 } else {
1396 is_simple = pygi_g_struct_info_is_simple ( (GIStructInfo *) info);
1398 break;
1399 case GI_INFO_TYPE_UNION:
1400 /* TODO */
1401 is_simple = FALSE;
1402 break;
1403 case GI_INFO_TYPE_ENUM:
1404 case GI_INFO_TYPE_FLAGS:
1405 if (g_type_info_is_pointer (field_type_info)) {
1406 is_simple = FALSE;
1408 break;
1409 case GI_INFO_TYPE_BOXED:
1410 case GI_INFO_TYPE_OBJECT:
1411 case GI_INFO_TYPE_CALLBACK:
1412 case GI_INFO_TYPE_INTERFACE:
1413 is_simple = FALSE;
1414 break;
1415 case GI_INFO_TYPE_VFUNC:
1416 case GI_INFO_TYPE_INVALID:
1417 case GI_INFO_TYPE_FUNCTION:
1418 case GI_INFO_TYPE_CONSTANT:
1419 case GI_INFO_TYPE_VALUE:
1420 case GI_INFO_TYPE_SIGNAL:
1421 case GI_INFO_TYPE_PROPERTY:
1422 case GI_INFO_TYPE_FIELD:
1423 case GI_INFO_TYPE_ARG:
1424 case GI_INFO_TYPE_TYPE:
1425 case GI_INFO_TYPE_UNRESOLVED:
1426 default:
1427 g_assert_not_reached();
1428 break;
1431 g_base_info_unref (info);
1432 break;
1434 default:
1435 g_assert_not_reached();
1436 break;
1439 g_base_info_unref ( (GIBaseInfo *) field_type_info);
1440 g_base_info_unref ( (GIBaseInfo *) field_info);
1443 return is_simple;
1447 /* EnumInfo */
1448 PYGLIB_DEFINE_TYPE ("gi.EnumInfo", PyGIEnumInfo_Type, PyGIBaseInfo);
1450 static PyObject *
1451 _wrap_g_enum_info_get_values (PyGIBaseInfo *self)
1453 return _make_infos_tuple (self, g_enum_info_get_n_values, g_enum_info_get_value);
1456 static PyObject *
1457 _wrap_g_enum_info_is_flags (PyGIBaseInfo *self)
1459 GIInfoType info_type = g_base_info_get_type ((GIBaseInfo *) self->info);
1461 if (info_type == GI_INFO_TYPE_ENUM) {
1462 Py_RETURN_FALSE;
1463 } else if (info_type == GI_INFO_TYPE_FLAGS) {
1464 Py_RETURN_TRUE;
1465 } else {
1466 g_assert_not_reached();
1470 static PyObject *
1471 _wrap_g_enum_info_get_methods (PyGIBaseInfo *self)
1473 return _make_infos_tuple (self, g_enum_info_get_n_methods, g_enum_info_get_method);
1476 static PyObject *
1477 _wrap_g_enum_info_get_storage_type (PyGIBaseInfo *self)
1479 return PYGLIB_PyLong_FromLong (g_enum_info_get_storage_type ((GIBaseInfo *) self->info));
1482 static PyMethodDef _PyGIEnumInfo_methods[] = {
1483 { "get_values", (PyCFunction) _wrap_g_enum_info_get_values, METH_NOARGS },
1484 { "is_flags", (PyCFunction) _wrap_g_enum_info_is_flags, METH_NOARGS },
1485 { "get_methods", (PyCFunction) _wrap_g_enum_info_get_methods, METH_NOARGS },
1486 { "get_storage_type", (PyCFunction) _wrap_g_enum_info_get_storage_type, METH_NOARGS },
1487 { NULL, NULL, 0 }
1491 /* ObjectInfo */
1492 PYGLIB_DEFINE_TYPE ("ObjectInfo", PyGIObjectInfo_Type, PyGIBaseInfo);
1494 static PyObject *
1495 _wrap_g_object_info_get_parent (PyGIBaseInfo *self)
1497 return _get_child_info (self, g_object_info_get_parent);
1500 static PyObject *
1501 _wrap_g_object_info_get_methods (PyGIBaseInfo *self)
1503 return _make_infos_tuple (self, g_object_info_get_n_methods, g_object_info_get_method);
1506 static PyObject *
1507 _wrap_g_object_info_find_method (PyGIBaseInfo *self, PyObject *py_name)
1509 return _get_child_info_by_name (self, py_name, g_object_info_find_method);
1512 static PyObject *
1513 _wrap_g_object_info_get_fields (PyGIBaseInfo *self)
1515 return _make_infos_tuple (self, g_object_info_get_n_fields, g_object_info_get_field);
1518 static PyObject *
1519 _wrap_g_object_info_get_properties (PyGIBaseInfo *self)
1521 return _make_infos_tuple (self, g_object_info_get_n_properties, g_object_info_get_property);
1524 static PyObject *
1525 _wrap_g_object_info_get_signals (PyGIBaseInfo *self)
1527 return _make_infos_tuple (self, g_object_info_get_n_signals, g_object_info_get_signal);
1530 static PyObject *
1531 _wrap_g_object_info_get_interfaces (PyGIBaseInfo *self)
1533 return _make_infos_tuple (self, g_object_info_get_n_interfaces, g_object_info_get_interface);
1536 static PyObject *
1537 _wrap_g_object_info_get_constants (PyGIBaseInfo *self)
1539 return _make_infos_tuple (self, g_object_info_get_n_constants, g_object_info_get_constant);
1542 static PyObject *
1543 _wrap_g_object_info_get_vfuncs (PyGIBaseInfo *self)
1545 return _make_infos_tuple (self, g_object_info_get_n_vfuncs, g_object_info_get_vfunc);
1548 static PyObject *
1549 _wrap_g_object_info_get_abstract (PyGIBaseInfo *self)
1551 gboolean is_abstract = g_object_info_get_abstract ( (GIObjectInfo*) self->info);
1552 return PyBool_FromLong (is_abstract);
1555 static PyObject *
1556 _wrap_g_object_info_get_type_name (PyGIBaseInfo *self)
1558 return _get_info_string (self, g_object_info_get_type_name);
1561 static PyObject *
1562 _wrap_g_object_info_get_type_init (PyGIBaseInfo *self)
1564 return _get_info_string (self, g_object_info_get_type_init);
1567 static PyObject *
1568 _wrap_g_object_info_get_fundamental (PyGIBaseInfo *self)
1570 return PyBool_FromLong (g_object_info_get_fundamental ( (GIObjectInfo*) self->info));
1573 static PyObject *
1574 _wrap_g_object_info_get_class_struct (PyGIBaseInfo *self)
1576 return _get_child_info (self, g_object_info_get_class_struct);
1579 static PyObject *
1580 _wrap_g_object_info_find_vfunc (PyGIBaseInfo *self, PyObject *py_name)
1582 return _get_child_info_by_name (self, py_name, g_object_info_find_vfunc);
1585 static PyObject *
1586 _wrap_g_object_info_get_unref_function (PyGIBaseInfo *self)
1588 return _get_info_string (self, g_object_info_get_unref_function);
1591 static PyObject *
1592 _wrap_g_object_info_get_ref_function (PyGIBaseInfo *self)
1594 return _get_info_string (self, g_object_info_get_ref_function);
1597 static PyObject *
1598 _wrap_g_object_info_get_set_value_function (PyGIBaseInfo *self)
1600 return _get_info_string (self, g_object_info_get_set_value_function);
1603 static PyObject *
1604 _wrap_g_object_info_get_get_value_function (PyGIBaseInfo *self)
1606 return _get_info_string (self, g_object_info_get_get_value_function);
1609 static PyMethodDef _PyGIObjectInfo_methods[] = {
1610 { "get_parent", (PyCFunction) _wrap_g_object_info_get_parent, METH_NOARGS },
1611 { "get_methods", (PyCFunction) _wrap_g_object_info_get_methods, METH_NOARGS },
1612 { "find_method", (PyCFunction) _wrap_g_object_info_find_method, METH_O },
1613 { "get_fields", (PyCFunction) _wrap_g_object_info_get_fields, METH_NOARGS },
1614 { "get_properties", (PyCFunction) _wrap_g_object_info_get_properties, METH_NOARGS },
1615 { "get_signals", (PyCFunction) _wrap_g_object_info_get_signals, METH_NOARGS },
1616 { "get_interfaces", (PyCFunction) _wrap_g_object_info_get_interfaces, METH_NOARGS },
1617 { "get_constants", (PyCFunction) _wrap_g_object_info_get_constants, METH_NOARGS },
1618 { "get_vfuncs", (PyCFunction) _wrap_g_object_info_get_vfuncs, METH_NOARGS },
1619 { "find_vfunc", (PyCFunction) _wrap_g_object_info_find_vfunc, METH_O },
1620 { "get_abstract", (PyCFunction) _wrap_g_object_info_get_abstract, METH_NOARGS },
1621 { "get_type_name", (PyCFunction) _wrap_g_object_info_get_type_name, METH_NOARGS },
1622 { "get_type_init", (PyCFunction) _wrap_g_object_info_get_type_init, METH_NOARGS },
1623 { "get_fundamental", (PyCFunction) _wrap_g_object_info_get_fundamental, METH_NOARGS },
1624 { "get_class_struct", (PyCFunction) _wrap_g_object_info_get_class_struct, METH_NOARGS },
1625 { "get_unref_function", (PyCFunction) _wrap_g_object_info_get_unref_function, METH_NOARGS },
1626 { "get_ref_function", (PyCFunction) _wrap_g_object_info_get_ref_function, METH_NOARGS },
1627 { "get_set_value_function", (PyCFunction) _wrap_g_object_info_get_set_value_function, METH_NOARGS },
1628 { "get_get_value_function", (PyCFunction) _wrap_g_object_info_get_get_value_function, METH_NOARGS },
1629 { NULL, NULL, 0 }
1633 /* GIInterfaceInfo */
1634 PYGLIB_DEFINE_TYPE ("InterfaceInfo", PyGIInterfaceInfo_Type, PyGIBaseInfo);
1636 static PyObject *
1637 _wrap_g_interface_info_get_methods (PyGIBaseInfo *self)
1639 return _make_infos_tuple (self, g_interface_info_get_n_methods, g_interface_info_get_method);
1642 static PyObject *
1643 _wrap_g_interface_info_find_method (PyGIBaseInfo *self, PyObject *py_name)
1645 return _get_child_info_by_name (self, py_name, g_interface_info_find_method);
1648 static PyObject *
1649 _wrap_g_interface_info_get_constants (PyGIBaseInfo *self)
1651 return _make_infos_tuple (self, g_interface_info_get_n_constants, g_interface_info_get_constant);
1654 static PyObject *
1655 _wrap_g_interface_info_get_vfuncs (PyGIBaseInfo *self)
1657 return _make_infos_tuple (self, g_interface_info_get_n_vfuncs, g_interface_info_get_vfunc);
1660 static PyObject *
1661 _wrap_g_interface_info_find_vfunc (PyGIBaseInfo *self, PyObject *py_name)
1663 return _get_child_info_by_name (self, py_name, g_interface_info_find_vfunc);
1666 static PyObject *
1667 _wrap_g_interface_info_get_prerequisites (PyGIBaseInfo *self)
1669 return _make_infos_tuple (self, g_interface_info_get_n_prerequisites, g_interface_info_get_prerequisite);
1672 static PyObject *
1673 _wrap_g_interface_info_get_properties (PyGIBaseInfo *self)
1675 return _make_infos_tuple (self, g_interface_info_get_n_properties, g_interface_info_get_property);
1678 static PyObject *
1679 _wrap_g_interface_info_get_iface_struct (PyGIBaseInfo *self)
1681 return _get_child_info (self, g_interface_info_get_iface_struct);
1684 static PyObject *
1685 _wrap_g_interface_info_get_signals (PyGIBaseInfo *self)
1687 return _make_infos_tuple (self, g_interface_info_get_n_signals, g_interface_info_get_signal);
1690 static PyObject *
1691 _wrap_g_interface_info_find_signal (PyGIBaseInfo *self, PyObject *py_name)
1693 return _get_child_info_by_name (self, py_name, g_interface_info_find_signal);
1696 static PyMethodDef _PyGIInterfaceInfo_methods[] = {
1697 { "get_prerequisites", (PyCFunction) _wrap_g_interface_info_get_prerequisites, METH_NOARGS },
1698 { "get_properties", (PyCFunction) _wrap_g_interface_info_get_properties, METH_NOARGS },
1699 { "get_methods", (PyCFunction) _wrap_g_interface_info_get_methods, METH_NOARGS },
1700 { "find_method", (PyCFunction) _wrap_g_interface_info_find_method, METH_O },
1701 { "get_signals", (PyCFunction) _wrap_g_interface_info_get_signals, METH_NOARGS },
1702 { "find_signal", (PyCFunction) _wrap_g_interface_info_find_signal, METH_O },
1703 { "get_vfuncs", (PyCFunction) _wrap_g_interface_info_get_vfuncs, METH_NOARGS },
1704 { "get_constants", (PyCFunction) _wrap_g_interface_info_get_constants, METH_NOARGS },
1705 { "get_iface_struct", (PyCFunction) _wrap_g_interface_info_get_iface_struct, METH_NOARGS },
1706 { "find_vfunc", (PyCFunction) _wrap_g_interface_info_find_vfunc, METH_O },
1707 { NULL, NULL, 0 }
1710 /* GIConstantInfo */
1711 PYGLIB_DEFINE_TYPE ("gi.ConstantInfo", PyGIConstantInfo_Type, PyGIBaseInfo);
1713 static PyObject *
1714 _wrap_g_constant_info_get_value (PyGIBaseInfo *self)
1716 GITypeInfo *type_info;
1717 GIArgument value = {0};
1718 PyObject *py_value;
1719 gboolean free_array = FALSE;
1721 if (g_constant_info_get_value ( (GIConstantInfo *) self->info, &value) < 0) {
1722 PyErr_SetString (PyExc_RuntimeError, "unable to get value");
1723 return NULL;
1726 type_info = g_constant_info_get_type ( (GIConstantInfo *) self->info);
1728 if (g_type_info_get_tag (type_info) == GI_TYPE_TAG_ARRAY) {
1729 value.v_pointer = _pygi_argument_to_array (&value, NULL, NULL, NULL,
1730 type_info, &free_array);
1733 py_value = _pygi_argument_to_object (&value, type_info, GI_TRANSFER_NOTHING);
1735 if (free_array) {
1736 g_array_free (value.v_pointer, FALSE);
1739 g_constant_info_free_value (self->info, &value);
1740 g_base_info_unref ( (GIBaseInfo *) type_info);
1742 return py_value;
1745 static PyMethodDef _PyGIConstantInfo_methods[] = {
1746 { "get_value", (PyCFunction) _wrap_g_constant_info_get_value, METH_NOARGS },
1747 { NULL, NULL, 0 }
1750 /* GIValueInfo */
1751 PYGLIB_DEFINE_TYPE ("gi.ValueInfo", PyGIValueInfo_Type, PyGIBaseInfo);
1753 static PyObject *
1754 _wrap_g_value_info_get_value (PyGIBaseInfo *self)
1756 glong value;
1758 value = g_value_info_get_value ( (GIValueInfo *) self->info);
1760 return PYGLIB_PyLong_FromLong (value);
1764 static PyMethodDef _PyGIValueInfo_methods[] = {
1765 { "get_value", (PyCFunction) _wrap_g_value_info_get_value, METH_NOARGS },
1766 { NULL, NULL, 0 }
1770 /* GIFieldInfo */
1771 PYGLIB_DEFINE_TYPE ("gi.FieldInfo", PyGIFieldInfo_Type, PyGIBaseInfo);
1773 static gssize
1774 _struct_field_array_length_marshal (gsize length_index,
1775 void *container_ptr,
1776 void *struct_data_ptr)
1778 gssize array_len = -1;
1779 GIFieldInfo *array_len_field = NULL;
1780 GIArgument arg = {0};
1781 GIBaseInfo *container_info = (GIBaseInfo *)container_ptr;
1783 switch (g_base_info_get_type (container_info)) {
1784 case GI_INFO_TYPE_UNION:
1785 array_len_field = g_union_info_get_field ((GIUnionInfo *)container_info, length_index);
1786 break;
1787 case GI_INFO_TYPE_STRUCT:
1788 array_len_field = g_struct_info_get_field ((GIStructInfo *)container_info, length_index);
1789 break;
1790 case GI_INFO_TYPE_OBJECT:
1791 array_len_field = g_object_info_get_field ((GIObjectInfo *)container_info, length_index);
1792 break;
1793 default:
1794 /* Other types don't have fields. */
1795 g_assert_not_reached();
1798 if (array_len_field == NULL) {
1799 return -1;
1802 if (g_field_info_get_field (array_len_field, struct_data_ptr, &arg)) {
1803 GITypeInfo *array_len_type_info;
1805 array_len_type_info = g_field_info_get_type (array_len_field);
1806 if (array_len_type_info == NULL) {
1807 goto out;
1810 if (!pygi_argument_to_gssize (&arg,
1811 g_type_info_get_tag (array_len_type_info),
1812 &array_len)) {
1813 array_len = -1;
1816 g_base_info_unref (array_len_type_info);
1819 out:
1820 g_base_info_unref (array_len_field);
1821 return array_len;
1824 static gint
1825 _pygi_g_registered_type_info_check_object (GIRegisteredTypeInfo *info,
1826 gboolean is_instance,
1827 PyObject *object)
1829 gint retval;
1831 GType g_type;
1832 PyObject *py_type;
1833 gchar *type_name_expected = NULL;
1834 GIInfoType interface_type;
1836 interface_type = g_base_info_get_type (info);
1837 if ( (interface_type == GI_INFO_TYPE_STRUCT) &&
1838 (g_struct_info_is_foreign ( (GIStructInfo*) info))) {
1839 /* TODO: Could we check is the correct foreign type? */
1840 return 1;
1843 g_type = g_registered_type_info_get_g_type (info);
1844 if (g_type != G_TYPE_NONE) {
1845 py_type = _pygi_type_get_from_g_type (g_type);
1846 } else {
1847 py_type = _pygi_type_import_by_gi_info ( (GIBaseInfo *) info);
1850 if (py_type == NULL) {
1851 return 0;
1854 g_assert (PyType_Check (py_type));
1856 if (is_instance) {
1857 retval = PyObject_IsInstance (object, py_type);
1858 if (!retval) {
1859 type_name_expected = _pygi_g_base_info_get_fullname (
1860 (GIBaseInfo *) info);
1862 } else {
1863 if (!PyObject_Type (py_type)) {
1864 type_name_expected = "type";
1865 retval = 0;
1866 } else if (!PyType_IsSubtype ( (PyTypeObject *) object,
1867 (PyTypeObject *) py_type)) {
1868 type_name_expected = _pygi_g_base_info_get_fullname (
1869 (GIBaseInfo *) info);
1870 retval = 0;
1871 } else {
1872 retval = 1;
1876 Py_DECREF (py_type);
1878 if (!retval) {
1879 PyTypeObject *object_type;
1881 if (type_name_expected == NULL) {
1882 return -1;
1885 object_type = (PyTypeObject *) PyObject_Type (object);
1886 if (object_type == NULL) {
1887 return -1;
1890 PyErr_Format (PyExc_TypeError, "Must be %s, not %s",
1891 type_name_expected, object_type->tp_name);
1893 g_free (type_name_expected);
1896 return retval;
1899 static PyObject *
1900 _wrap_g_field_info_get_value (PyGIBaseInfo *self,
1901 PyObject *args)
1903 PyObject *instance;
1904 GIBaseInfo *container_info;
1905 GIInfoType container_info_type;
1906 gpointer pointer;
1907 GITypeInfo *field_type_info;
1908 GIArgument value;
1909 PyObject *py_value = NULL;
1910 gboolean free_array = FALSE;
1912 memset(&value, 0, sizeof(GIArgument));
1914 if (!PyArg_ParseTuple (args, "O:FieldInfo.get_value", &instance)) {
1915 return NULL;
1918 container_info = g_base_info_get_container (self->info);
1919 g_assert (container_info != NULL);
1921 /* Check the instance. */
1922 if (!_pygi_g_registered_type_info_check_object ( (GIRegisteredTypeInfo *) container_info, TRUE, instance)) {
1923 _PyGI_ERROR_PREFIX ("argument 1: ");
1924 return NULL;
1927 /* Get the pointer to the container. */
1928 container_info_type = g_base_info_get_type (container_info);
1929 switch (container_info_type) {
1930 case GI_INFO_TYPE_UNION:
1931 case GI_INFO_TYPE_STRUCT:
1932 pointer = pyg_boxed_get (instance, void);
1933 break;
1934 case GI_INFO_TYPE_OBJECT:
1935 pointer = pygobject_get (instance);
1936 break;
1937 default:
1938 /* Other types don't have fields. */
1939 g_assert_not_reached();
1942 /* Get the field's value. */
1943 field_type_info = g_field_info_get_type ( (GIFieldInfo *) self->info);
1945 /* A few types are not handled by g_field_info_get_field, so do it here. */
1946 if (!g_type_info_is_pointer (field_type_info)
1947 && g_type_info_get_tag (field_type_info) == GI_TYPE_TAG_INTERFACE) {
1948 GIBaseInfo *info;
1949 GIInfoType info_type;
1951 if (! (g_field_info_get_flags ( (GIFieldInfo *) self->info) & GI_FIELD_IS_READABLE)) {
1952 PyErr_SetString (PyExc_RuntimeError, "field is not readable");
1953 goto out;
1956 info = g_type_info_get_interface (field_type_info);
1958 info_type = g_base_info_get_type (info);
1960 g_base_info_unref (info);
1962 switch (info_type) {
1963 case GI_INFO_TYPE_UNION:
1964 PyErr_SetString (PyExc_NotImplementedError, "getting an union is not supported yet");
1965 goto out;
1966 case GI_INFO_TYPE_STRUCT:
1968 gsize offset;
1970 offset = g_field_info_get_offset ( (GIFieldInfo *) self->info);
1972 value.v_pointer = (char*) pointer + offset;
1974 goto argument_to_object;
1976 default:
1977 /* Fallback. */
1978 break;
1982 if (!g_field_info_get_field ( (GIFieldInfo *) self->info, pointer, &value)) {
1983 PyErr_SetString (PyExc_RuntimeError, "unable to get the value");
1984 goto out;
1987 if (g_type_info_get_tag (field_type_info) == GI_TYPE_TAG_ARRAY) {
1988 value.v_pointer = _pygi_argument_to_array (&value,
1989 _struct_field_array_length_marshal,
1990 container_info,
1991 pointer,
1992 field_type_info,
1993 &free_array);
1996 argument_to_object:
1997 py_value = _pygi_argument_to_object (&value, field_type_info, GI_TRANSFER_NOTHING);
1999 if (free_array) {
2000 g_array_free (value.v_pointer, FALSE);
2003 out:
2004 g_base_info_unref ( (GIBaseInfo *) field_type_info);
2006 return py_value;
2009 static PyObject *
2010 _wrap_g_field_info_set_value (PyGIBaseInfo *self,
2011 PyObject *args)
2013 PyObject *instance;
2014 PyObject *py_value;
2015 GIBaseInfo *container_info;
2016 GIInfoType container_info_type;
2017 gpointer pointer;
2018 GITypeInfo *field_type_info;
2019 GIArgument value;
2020 PyObject *retval = NULL;
2022 if (!PyArg_ParseTuple (args, "OO:FieldInfo.set_value", &instance, &py_value)) {
2023 return NULL;
2026 container_info = g_base_info_get_container (self->info);
2027 g_assert (container_info != NULL);
2029 /* Check the instance. */
2030 if (!_pygi_g_registered_type_info_check_object ( (GIRegisteredTypeInfo *) container_info, TRUE, instance)) {
2031 _PyGI_ERROR_PREFIX ("argument 1: ");
2032 return NULL;
2035 /* Get the pointer to the container. */
2036 container_info_type = g_base_info_get_type (container_info);
2037 switch (container_info_type) {
2038 case GI_INFO_TYPE_UNION:
2039 case GI_INFO_TYPE_STRUCT:
2040 pointer = pyg_boxed_get (instance, void);
2041 break;
2042 case GI_INFO_TYPE_OBJECT:
2043 pointer = pygobject_get (instance);
2044 break;
2045 default:
2046 /* Other types don't have fields. */
2047 g_assert_not_reached();
2050 field_type_info = g_field_info_get_type ( (GIFieldInfo *) self->info);
2052 /* Set the field's value. */
2053 /* A few types are not handled by g_field_info_set_field, so do it here. */
2054 if (!g_type_info_is_pointer (field_type_info)
2055 && g_type_info_get_tag (field_type_info) == GI_TYPE_TAG_INTERFACE) {
2056 GIBaseInfo *info;
2057 GIInfoType info_type;
2059 if (! (g_field_info_get_flags ( (GIFieldInfo *) self->info) & GI_FIELD_IS_WRITABLE)) {
2060 PyErr_SetString (PyExc_RuntimeError, "field is not writable");
2061 goto out;
2064 info = g_type_info_get_interface (field_type_info);
2066 info_type = g_base_info_get_type (info);
2068 switch (info_type) {
2069 case GI_INFO_TYPE_UNION:
2070 PyErr_SetString (PyExc_NotImplementedError, "setting an union is not supported yet");
2071 goto out;
2072 case GI_INFO_TYPE_STRUCT:
2074 gboolean is_simple;
2075 gsize offset;
2076 gssize size;
2078 is_simple = pygi_g_struct_info_is_simple ( (GIStructInfo *) info);
2080 if (!is_simple) {
2081 PyErr_SetString (PyExc_TypeError,
2082 "cannot set a structure which has no well-defined ownership transfer rules");
2083 g_base_info_unref (info);
2084 goto out;
2087 value = _pygi_argument_from_object (py_value, field_type_info, GI_TRANSFER_NOTHING);
2088 if (PyErr_Occurred()) {
2089 g_base_info_unref (info);
2090 goto out;
2093 offset = g_field_info_get_offset ( (GIFieldInfo *) self->info);
2094 size = g_struct_info_get_size ( (GIStructInfo *) info);
2095 g_assert (size > 0);
2097 g_memmove ((char*) pointer + offset, value.v_pointer, size);
2099 g_base_info_unref (info);
2101 retval = Py_None;
2102 goto out;
2104 default:
2105 /* Fallback. */
2106 break;
2109 g_base_info_unref (info);
2110 } else if (g_type_info_is_pointer (field_type_info)
2111 && (g_type_info_get_tag (field_type_info) == GI_TYPE_TAG_VOID
2112 || g_type_info_get_tag (field_type_info) == GI_TYPE_TAG_UTF8)) {
2113 int offset;
2114 value = _pygi_argument_from_object (py_value, field_type_info, GI_TRANSFER_NOTHING);
2115 if (PyErr_Occurred()) {
2116 goto out;
2119 offset = g_field_info_get_offset ((GIFieldInfo *) self->info);
2120 G_STRUCT_MEMBER (gpointer, pointer, offset) = (gpointer)value.v_pointer;
2122 retval = Py_None;
2123 goto out;
2126 value = _pygi_argument_from_object (py_value, field_type_info, GI_TRANSFER_EVERYTHING);
2127 if (PyErr_Occurred()) {
2128 goto out;
2131 if (!g_field_info_set_field ( (GIFieldInfo *) self->info, pointer, &value)) {
2132 _pygi_argument_release (&value, field_type_info, GI_TRANSFER_NOTHING, GI_DIRECTION_IN);
2133 PyErr_SetString (PyExc_RuntimeError, "unable to set value for field");
2134 goto out;
2137 retval = Py_None;
2139 out:
2140 g_base_info_unref ( (GIBaseInfo *) field_type_info);
2142 Py_XINCREF (retval);
2143 return retval;
2146 static PyObject *
2147 _wrap_g_field_info_get_flags (PyGIBaseInfo *self)
2149 return PYGLIB_PyLong_FromLong (g_field_info_get_flags (self->info));
2152 static PyObject *
2153 _wrap_g_field_info_get_size (PyGIBaseInfo *self)
2155 return PYGLIB_PyLong_FromLong (g_field_info_get_size (self->info));
2158 static PyObject *
2159 _wrap_g_field_info_get_offset (PyGIBaseInfo *self)
2161 return PYGLIB_PyLong_FromLong (g_field_info_get_offset (self->info));
2164 static PyObject *
2165 _wrap_g_field_info_get_type (PyGIBaseInfo *self)
2167 return _get_child_info (self, g_field_info_get_type);
2170 static PyMethodDef _PyGIFieldInfo_methods[] = {
2171 { "get_value", (PyCFunction) _wrap_g_field_info_get_value, METH_VARARGS },
2172 { "set_value", (PyCFunction) _wrap_g_field_info_set_value, METH_VARARGS },
2173 { "get_flags", (PyCFunction) _wrap_g_field_info_get_flags, METH_VARARGS },
2174 { "get_size", (PyCFunction) _wrap_g_field_info_get_size, METH_VARARGS },
2175 { "get_offset", (PyCFunction) _wrap_g_field_info_get_offset, METH_VARARGS },
2176 { "get_type", (PyCFunction) _wrap_g_field_info_get_type, METH_VARARGS },
2177 { NULL, NULL, 0 }
2181 /* GIUnresolvedInfo */
2182 PYGLIB_DEFINE_TYPE ("gi.UnresolvedInfo", PyGIUnresolvedInfo_Type, PyGIBaseInfo);
2184 static PyMethodDef _PyGIUnresolvedInfo_methods[] = {
2185 { NULL, NULL, 0 }
2188 /* GIVFuncInfo */
2189 PYGLIB_DEFINE_TYPE ("gi.VFuncInfo", PyGIVFuncInfo_Type, PyGICallableInfo);
2191 static PyObject *
2192 _wrap_g_vfunc_info_get_flags (PyGIBaseInfo *self)
2194 return PYGLIB_PyLong_FromLong (g_vfunc_info_get_flags ((GIVFuncInfo *) self->info));
2197 static PyObject *
2198 _wrap_g_vfunc_info_get_offset (PyGIBaseInfo *self)
2200 return PYGLIB_PyLong_FromLong (g_vfunc_info_get_offset ((GIVFuncInfo *) self->info));
2203 static PyObject *
2204 _wrap_g_vfunc_info_get_signal (PyGIBaseInfo *self)
2206 return _get_child_info (self, g_vfunc_info_get_signal);
2209 static PyObject *
2210 _wrap_g_vfunc_info_get_invoker (PyGIBaseInfo *self)
2212 return _get_child_info (self, g_vfunc_info_get_invoker);
2215 static PyMethodDef _PyGIVFuncInfo_methods[] = {
2216 { "get_flags", (PyCFunction) _wrap_g_vfunc_info_get_flags, METH_NOARGS },
2217 { "get_offset", (PyCFunction) _wrap_g_vfunc_info_get_offset, METH_NOARGS },
2218 { "get_signal", (PyCFunction) _wrap_g_vfunc_info_get_signal, METH_NOARGS },
2219 { "get_invoker", (PyCFunction) _wrap_g_vfunc_info_get_invoker, METH_NOARGS },
2220 { NULL, NULL, 0 }
2224 /* GIUnionInfo */
2225 PYGLIB_DEFINE_TYPE ("gi.UnionInfo", PyGIUnionInfo_Type, PyGIBaseInfo);
2227 static PyObject *
2228 _wrap_g_union_info_get_fields (PyGIBaseInfo *self)
2230 return _make_infos_tuple (self, g_union_info_get_n_fields, g_union_info_get_field);
2233 static PyObject *
2234 _wrap_g_union_info_get_methods (PyGIBaseInfo *self)
2236 return _make_infos_tuple (self, g_union_info_get_n_methods, g_union_info_get_method);
2239 static PyObject *
2240 _wrap_g_union_info_get_size (PyGIBaseInfo *self)
2242 return PYGLIB_PyLong_FromSize_t (g_union_info_get_size (self->info));
2245 static PyMethodDef _PyGIUnionInfo_methods[] = {
2246 { "get_fields", (PyCFunction) _wrap_g_union_info_get_fields, METH_NOARGS },
2247 { "get_methods", (PyCFunction) _wrap_g_union_info_get_methods, METH_NOARGS },
2248 { "get_size", (PyCFunction) _wrap_g_union_info_get_size, METH_NOARGS },
2249 { NULL, NULL, 0 }
2252 /* Private */
2254 gchar *
2255 _pygi_g_base_info_get_fullname (GIBaseInfo *info)
2257 GIBaseInfo *container_info;
2258 gchar *fullname;
2260 container_info = g_base_info_get_container (info);
2261 if (container_info != NULL) {
2262 fullname = g_strdup_printf ("%s.%s.%s",
2263 g_base_info_get_namespace (container_info),
2264 _safe_base_info_get_name (container_info),
2265 _safe_base_info_get_name (info));
2266 } else {
2267 fullname = g_strdup_printf ("%s.%s",
2268 g_base_info_get_namespace (info),
2269 _safe_base_info_get_name (info));
2272 if (fullname == NULL) {
2273 PyErr_NoMemory();
2276 return fullname;
2280 void
2281 _pygi_info_register_types (PyObject *m)
2283 #define _PyGI_REGISTER_TYPE(m, type, cname, base) \
2284 Py_TYPE(&type) = &PyType_Type; \
2285 type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE); \
2286 type.tp_weaklistoffset = offsetof(PyGIBaseInfo, inst_weakreflist); \
2287 type.tp_methods = _PyGI##cname##_methods; \
2288 type.tp_base = &base; \
2289 if (PyType_Ready(&type)) \
2290 return; \
2291 if (PyModule_AddObject(m, #cname, (PyObject *)&type)) \
2292 return
2294 Py_TYPE(&PyGIBaseInfo_Type) = &PyType_Type;
2296 PyGIBaseInfo_Type.tp_dealloc = (destructor) _base_info_dealloc;
2297 PyGIBaseInfo_Type.tp_repr = (reprfunc) _base_info_repr;
2298 PyGIBaseInfo_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
2299 PyGIBaseInfo_Type.tp_weaklistoffset = offsetof(PyGIBaseInfo, inst_weakreflist);
2300 PyGIBaseInfo_Type.tp_methods = _PyGIBaseInfo_methods;
2301 PyGIBaseInfo_Type.tp_richcompare = (richcmpfunc)_base_info_richcompare;
2302 PyGIBaseInfo_Type.tp_getset = _base_info_getsets;
2303 PyGIBaseInfo_Type.tp_getattro = (getattrofunc) _base_info_getattro;
2305 if (PyType_Ready(&PyGIBaseInfo_Type))
2306 return;
2307 if (PyModule_AddObject(m, "BaseInfo", (PyObject *)&PyGIBaseInfo_Type))
2308 return;
2310 _PyGI_REGISTER_TYPE (m, PyGICallableInfo_Type, CallableInfo,
2311 PyGIBaseInfo_Type);
2312 PyGICallableInfo_Type.tp_call = (ternaryfunc) _callable_info_call;
2313 PyGICallableInfo_Type.tp_dealloc = (destructor) _callable_info_dealloc;
2315 _PyGI_REGISTER_TYPE (m, PyGIFunctionInfo_Type, FunctionInfo,
2316 PyGICallableInfo_Type);
2317 PyGIFunctionInfo_Type.tp_call = (ternaryfunc) _function_info_call;
2318 PyGIFunctionInfo_Type.tp_descr_get = (descrgetfunc) _function_info_descr_get;
2320 _PyGI_REGISTER_TYPE (m, PyGIVFuncInfo_Type, VFuncInfo,
2321 PyGICallableInfo_Type);
2322 PyGIVFuncInfo_Type.tp_descr_get = (descrgetfunc) _vfunc_info_descr_get;
2324 _PyGI_REGISTER_TYPE (m, PyGISignalInfo_Type, SignalInfo,
2325 PyGICallableInfo_Type);
2327 _PyGI_REGISTER_TYPE (m, PyGIUnresolvedInfo_Type, UnresolvedInfo,
2328 PyGIBaseInfo_Type);
2329 _PyGI_REGISTER_TYPE (m, PyGICallbackInfo_Type, CallbackInfo,
2330 PyGICallableInfo_Type);
2331 _PyGI_REGISTER_TYPE (m, PyGIRegisteredTypeInfo_Type, RegisteredTypeInfo,
2332 PyGIBaseInfo_Type);
2333 _PyGI_REGISTER_TYPE (m, PyGIStructInfo_Type, StructInfo,
2334 PyGIRegisteredTypeInfo_Type);
2335 _PyGI_REGISTER_TYPE (m, PyGIEnumInfo_Type, EnumInfo,
2336 PyGIRegisteredTypeInfo_Type);
2337 _PyGI_REGISTER_TYPE (m, PyGIObjectInfo_Type, ObjectInfo,
2338 PyGIRegisteredTypeInfo_Type);
2339 _PyGI_REGISTER_TYPE (m, PyGIInterfaceInfo_Type, InterfaceInfo,
2340 PyGIRegisteredTypeInfo_Type);
2341 _PyGI_REGISTER_TYPE (m, PyGIConstantInfo_Type, ConstantInfo,
2342 PyGIBaseInfo_Type);
2343 _PyGI_REGISTER_TYPE (m, PyGIValueInfo_Type, ValueInfo,
2344 PyGIBaseInfo_Type);
2345 _PyGI_REGISTER_TYPE (m, PyGIFieldInfo_Type, FieldInfo,
2346 PyGIBaseInfo_Type);
2347 _PyGI_REGISTER_TYPE (m, PyGIUnionInfo_Type, UnionInfo,
2348 PyGIRegisteredTypeInfo_Type);
2349 _PyGI_REGISTER_TYPE (m, PyGIErrorDomainInfo_Type, ErrorDomainInfo,
2350 PyGIBaseInfo_Type);
2351 _PyGI_REGISTER_TYPE (m, PyGIPropertyInfo_Type, PropertyInfo,
2352 PyGIBaseInfo_Type);
2353 _PyGI_REGISTER_TYPE (m, PyGIArgInfo_Type, ArgInfo,
2354 PyGIBaseInfo_Type);
2355 _PyGI_REGISTER_TYPE (m, PyGITypeInfo_Type, TypeInfo,
2356 PyGIBaseInfo_Type);
2358 #undef _PyGI_REGISTER_TYPE
2360 #define _PyGI_ENUM_BEGIN(name) \
2362 const char *__enum_name = #name; \
2363 PyObject *__enum_value = NULL; \
2364 PyObject *__new_enum_cls = NULL; \
2365 PyObject *__enum_instance_dict = PyDict_New(); \
2366 PyObject *__module_name = PyObject_GetAttrString (m, "__name__"); \
2367 PyDict_SetItemString (__enum_instance_dict, "__module__", __module_name); \
2368 Py_DECREF (__module_name);
2370 #define _PyGI_ENUM_ADD_VALUE(prefix, name) \
2371 __enum_value = PYGLIB_PyLong_FromLong (prefix##_##name); \
2372 if (PyDict_SetItemString(__enum_instance_dict, #name, __enum_value)) { \
2373 Py_DECREF (__enum_instance_dict); \
2374 Py_DECREF (__enum_value); \
2375 return; \
2377 Py_DECREF (__enum_value);
2379 #define _PyGI_ENUM_END \
2380 __new_enum_cls = PyObject_CallFunction ((PyObject *)&PyType_Type, "s(O)O", \
2381 __enum_name, (PyObject *)&PyType_Type, \
2382 __enum_instance_dict); \
2383 Py_DECREF (__enum_instance_dict); \
2384 PyModule_AddObject (m, __enum_name, __new_enum_cls); /* steals ref */ \
2388 /* GIDirection */
2389 _PyGI_ENUM_BEGIN (Direction)
2390 _PyGI_ENUM_ADD_VALUE (GI_DIRECTION, IN)
2391 _PyGI_ENUM_ADD_VALUE (GI_DIRECTION, OUT)
2392 _PyGI_ENUM_ADD_VALUE (GI_DIRECTION, INOUT)
2393 _PyGI_ENUM_END
2396 /* GITransfer */
2397 _PyGI_ENUM_BEGIN (Transfer)
2398 _PyGI_ENUM_ADD_VALUE (GI_TRANSFER, NOTHING)
2399 _PyGI_ENUM_ADD_VALUE (GI_TRANSFER, CONTAINER)
2400 _PyGI_ENUM_ADD_VALUE (GI_TRANSFER, EVERYTHING)
2401 _PyGI_ENUM_END
2403 /* GIArrayType */
2404 _PyGI_ENUM_BEGIN (ArrayType)
2405 _PyGI_ENUM_ADD_VALUE (GI_ARRAY_TYPE, C)
2406 _PyGI_ENUM_ADD_VALUE (GI_ARRAY_TYPE, ARRAY)
2407 _PyGI_ENUM_ADD_VALUE (GI_ARRAY_TYPE, PTR_ARRAY)
2408 _PyGI_ENUM_ADD_VALUE (GI_ARRAY_TYPE, BYTE_ARRAY)
2409 _PyGI_ENUM_END
2411 /* GIScopeType */
2412 _PyGI_ENUM_BEGIN (ScopeType)
2413 _PyGI_ENUM_ADD_VALUE (GI_SCOPE_TYPE, INVALID)
2414 _PyGI_ENUM_ADD_VALUE (GI_SCOPE_TYPE, CALL)
2415 _PyGI_ENUM_ADD_VALUE (GI_SCOPE_TYPE, ASYNC)
2416 _PyGI_ENUM_ADD_VALUE (GI_SCOPE_TYPE, NOTIFIED)
2417 _PyGI_ENUM_END
2419 /* GIVFuncInfoFlags */
2420 _PyGI_ENUM_BEGIN (VFuncInfoFlags)
2421 _PyGI_ENUM_ADD_VALUE (GI_VFUNC_MUST, CHAIN_UP)
2422 _PyGI_ENUM_ADD_VALUE (GI_VFUNC_MUST, OVERRIDE)
2423 _PyGI_ENUM_ADD_VALUE (GI_VFUNC_MUST, NOT_OVERRIDE)
2424 _PyGI_ENUM_END
2426 /* GIFieldInfoFlags */
2427 _PyGI_ENUM_BEGIN (FieldInfoFlags)
2428 _PyGI_ENUM_ADD_VALUE (GI_FIELD, IS_READABLE)
2429 _PyGI_ENUM_ADD_VALUE (GI_FIELD, IS_WRITABLE)
2430 _PyGI_ENUM_END
2432 /* GIFunctionInfoFlags */
2433 _PyGI_ENUM_BEGIN (FunctionInfoFlags)
2434 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, IS_METHOD)
2435 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, IS_CONSTRUCTOR)
2436 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, IS_GETTER)
2437 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, IS_SETTER)
2438 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, WRAPS_VFUNC)
2439 _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, THROWS)
2440 _PyGI_ENUM_END
2442 /* GITypeTag */
2443 _PyGI_ENUM_BEGIN (TypeTag)
2444 /* Basic types */
2445 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, VOID)
2446 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, BOOLEAN)
2447 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INT8)
2448 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UINT8)
2449 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INT16)
2450 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UINT16)
2451 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INT32)
2452 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UINT32)
2453 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INT64)
2454 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UINT64)
2455 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, FLOAT)
2456 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, DOUBLE)
2457 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, GTYPE)
2458 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UTF8)
2459 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, FILENAME)
2461 /* Non-basic types; compare with G_TYPE_TAG_IS_BASIC */
2462 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, ARRAY)
2463 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INTERFACE)
2464 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, GLIST)
2465 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, GSLIST)
2466 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, GHASH)
2467 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, ERROR)
2469 /* Another basic type */
2470 _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UNICHAR)
2471 _PyGI_ENUM_END
2473 /* GIInfoType */
2474 _PyGI_ENUM_BEGIN (InfoType)
2475 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, INVALID)
2476 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, FUNCTION)
2477 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, CALLBACK)
2478 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, STRUCT)
2479 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, BOXED)
2480 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, ENUM)
2481 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, FLAGS)
2482 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, OBJECT)
2483 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, INTERFACE)
2484 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, CONSTANT)
2485 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, INVALID_0)
2486 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, UNION)
2487 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, VALUE)
2488 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, SIGNAL)
2489 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, VFUNC)
2490 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, PROPERTY)
2491 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, FIELD)
2492 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, ARG)
2493 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, TYPE)
2494 _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, UNRESOLVED)
2495 _PyGI_ENUM_END
2497 #undef _PyGI_ENUM_BEGIN
2498 #undef _PyGI_ENUM_ADD_VALUE
2499 #undef _PyGI_ENUM_END