summaryrefslogtreecommitdiff
path: root/gi/pygi-type.c
blob: dfaadb038bc6c094a56ae80dd2a8d4e1140d62a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* -*- Mode: C; c-basic-offset: 4 -*-
 * vim: tabstop=4 shiftwidth=4 expandtab
 *
 * Copyright (C) 2009 Simon van der Linden <svdlinden@src.gnome.org>
 *
 *   pygi-type.c: helpers to lookup Python wrappers from GType and GIBaseInfo.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
 * USA
 */

#include "pygi-private.h"

#include <pyglib-python-compat.h>


PyObject *
_pygi_type_import_by_name (const char *namespace_,
                           const char *name)
{
    gchar *module_name;
    PyObject *py_module;
    PyObject *py_object;

    module_name = g_strconcat ("gi.repository.", namespace_, NULL);

    py_module = PyImport_ImportModule (module_name);

    g_free (module_name);

    if (py_module == NULL) {
        return NULL;
    }

    py_object = PyObject_GetAttrString (py_module, name);

    Py_DECREF (py_module);

    return py_object;
}

PyObject *
pygi_type_import_by_g_type_real (GType g_type)
{
    GIRepository *repository;
    GIBaseInfo *info;
    PyObject *type;

    repository = g_irepository_get_default();

    info = g_irepository_find_by_gtype (repository, g_type);
    if (info == NULL) {
        return NULL;
    }

    type = _pygi_type_import_by_gi_info (info);
    g_base_info_unref (info);

    return type;
}

PyObject *
_pygi_type_import_by_gi_info (GIBaseInfo *info)
{
    return _pygi_type_import_by_name (g_base_info_get_namespace (info),
                                      g_base_info_get_name (info));
}

PyObject *
_pygi_type_get_from_g_type (GType g_type)
{
    PyObject *py_g_type;
    PyObject *py_type;

    py_g_type = pyg_type_wrapper_new (g_type);
    if (py_g_type == NULL) {
        return NULL;
    }

    py_type = PyObject_GetAttrString (py_g_type, "pytype");
    if (py_type == Py_None) {
        py_type = pygi_type_import_by_g_type_real (g_type);
    }

    Py_DECREF (py_g_type);

    return py_type;
}

/* _pygi_get_py_type_hint
 *
 * This gives a hint to what python type might be used as
 * a particular gi type.
 */
PyObject *
_pygi_get_py_type_hint(GITypeTag type_tag)
{
    PyObject *type = Py_None;

    switch (type_tag) {
        case GI_TYPE_TAG_BOOLEAN:
            type = (PyObject *) &PyBool_Type;
            break;

        case GI_TYPE_TAG_INT8:
        case GI_TYPE_TAG_UINT8:
        case GI_TYPE_TAG_INT16:
        case GI_TYPE_TAG_UINT16:
        case GI_TYPE_TAG_INT32:
        case GI_TYPE_TAG_UINT32:
        case GI_TYPE_TAG_INT64:
        case GI_TYPE_TAG_UINT64:
            type = (PyObject *) &PYGLIB_PyLong_Type;
            break;

        case GI_TYPE_TAG_FLOAT:
        case GI_TYPE_TAG_DOUBLE:
            type = (PyObject *) &PyFloat_Type;
            break;

        case GI_TYPE_TAG_GLIST:
        case GI_TYPE_TAG_GSLIST:
        case GI_TYPE_TAG_ARRAY:
            type = (PyObject *) &PyList_Type;
            break;

        case GI_TYPE_TAG_GHASH:
            type = (PyObject *) &PyDict_Type;
            break;

        case GI_TYPE_TAG_UTF8:
        case GI_TYPE_TAG_FILENAME:
        case GI_TYPE_TAG_UNICHAR:
            type = (PyObject *) &PYGLIB_PyUnicode_Type;
            break;

        case GI_TYPE_TAG_INTERFACE:
        case GI_TYPE_TAG_GTYPE:
        case GI_TYPE_TAG_ERROR:
        case GI_TYPE_TAG_VOID:
            break;
    }

    Py_INCREF(type);
    return type;
}