dfaadb038bc6c094a56ae80dd2a8d4e1140d62a6
[platform/upstream/pygobject2.git] / gi / pygi-type.c
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2  * vim: tabstop=4 shiftwidth=4 expandtab
3  *
4  * Copyright (C) 2009 Simon van der Linden <svdlinden@src.gnome.org>
5  *
6  *   pygi-type.c: helpers to lookup Python wrappers from GType and GIBaseInfo.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
21  * USA
22  */
23
24 #include "pygi-private.h"
25
26 #include <pyglib-python-compat.h>
27
28
29 PyObject *
30 _pygi_type_import_by_name (const char *namespace_,
31                            const char *name)
32 {
33     gchar *module_name;
34     PyObject *py_module;
35     PyObject *py_object;
36
37     module_name = g_strconcat ("gi.repository.", namespace_, NULL);
38
39     py_module = PyImport_ImportModule (module_name);
40
41     g_free (module_name);
42
43     if (py_module == NULL) {
44         return NULL;
45     }
46
47     py_object = PyObject_GetAttrString (py_module, name);
48
49     Py_DECREF (py_module);
50
51     return py_object;
52 }
53
54 PyObject *
55 pygi_type_import_by_g_type_real (GType g_type)
56 {
57     GIRepository *repository;
58     GIBaseInfo *info;
59     PyObject *type;
60
61     repository = g_irepository_get_default();
62
63     info = g_irepository_find_by_gtype (repository, g_type);
64     if (info == NULL) {
65         return NULL;
66     }
67
68     type = _pygi_type_import_by_gi_info (info);
69     g_base_info_unref (info);
70
71     return type;
72 }
73
74 PyObject *
75 _pygi_type_import_by_gi_info (GIBaseInfo *info)
76 {
77     return _pygi_type_import_by_name (g_base_info_get_namespace (info),
78                                       g_base_info_get_name (info));
79 }
80
81 PyObject *
82 _pygi_type_get_from_g_type (GType g_type)
83 {
84     PyObject *py_g_type;
85     PyObject *py_type;
86
87     py_g_type = pyg_type_wrapper_new (g_type);
88     if (py_g_type == NULL) {
89         return NULL;
90     }
91
92     py_type = PyObject_GetAttrString (py_g_type, "pytype");
93     if (py_type == Py_None) {
94         py_type = pygi_type_import_by_g_type_real (g_type);
95     }
96
97     Py_DECREF (py_g_type);
98
99     return py_type;
100 }
101
102 /* _pygi_get_py_type_hint
103  *
104  * This gives a hint to what python type might be used as
105  * a particular gi type.
106  */
107 PyObject *
108 _pygi_get_py_type_hint(GITypeTag type_tag)
109 {
110     PyObject *type = Py_None;
111
112     switch (type_tag) {
113         case GI_TYPE_TAG_BOOLEAN:
114             type = (PyObject *) &PyBool_Type;
115             break;
116
117         case GI_TYPE_TAG_INT8:
118         case GI_TYPE_TAG_UINT8:
119         case GI_TYPE_TAG_INT16:
120         case GI_TYPE_TAG_UINT16:
121         case GI_TYPE_TAG_INT32:
122         case GI_TYPE_TAG_UINT32:
123         case GI_TYPE_TAG_INT64:
124         case GI_TYPE_TAG_UINT64:
125             type = (PyObject *) &PYGLIB_PyLong_Type;
126             break;
127
128         case GI_TYPE_TAG_FLOAT:
129         case GI_TYPE_TAG_DOUBLE:
130             type = (PyObject *) &PyFloat_Type;
131             break;
132
133         case GI_TYPE_TAG_GLIST:
134         case GI_TYPE_TAG_GSLIST:
135         case GI_TYPE_TAG_ARRAY:
136             type = (PyObject *) &PyList_Type;
137             break;
138
139         case GI_TYPE_TAG_GHASH:
140             type = (PyObject *) &PyDict_Type;
141             break;
142
143         case GI_TYPE_TAG_UTF8:
144         case GI_TYPE_TAG_FILENAME:
145         case GI_TYPE_TAG_UNICHAR:
146             type = (PyObject *) &PYGLIB_PyUnicode_Type;
147             break;
148
149         case GI_TYPE_TAG_INTERFACE:
150         case GI_TYPE_TAG_GTYPE:
151         case GI_TYPE_TAG_ERROR:
152         case GI_TYPE_TAG_VOID:
153             break;
154     }
155
156     Py_INCREF(type);
157     return type;
158 }
159