Debian Codegraph @

gi/pygi-foreign.c

raw
1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 /*
3 * Copyright (c) 2010 litl, LLC
4 * Copyright (c) 2010 Collabora Ltd. <http://www.collabora.co.uk/>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #include "pygobject-types.h"
26
27 #include "pygi-foreign.h"
28
29 typedef struct {
30 const char *namespace;
31 const char *name;
32 PyGIArgOverrideToGIArgumentFunc to_func;
33 PyGIArgOverrideFromGIArgumentFunc from_func;
34 PyGIArgOverrideReleaseFunc release_func;
35 } PyGIForeignStruct;
36
37 static GPtrArray *foreign_structs = NULL;
38
39 static void
40 init_foreign_structs (void)
41 {
42 foreign_structs = g_ptr_array_new ();
43 }
44
45 static PyGIForeignStruct *
46 do_lookup (const gchar *namespace, const gchar *name)
47 {
48 guint i;
49 for (i = 0; i < foreign_structs->len; i++) {
50 PyGIForeignStruct *foreign_struct =
51 g_ptr_array_index (foreign_structs, i);
52
53 if ((strcmp (namespace, foreign_struct->namespace) == 0)
54 && (strcmp (name, foreign_struct->name) == 0)) {
55 return foreign_struct;
56 }
57 }
58 return NULL;
59 }
60
61 static PyObject *
62 pygi_struct_foreign_load_module (const char *namespace)
63 {
64 gchar *module_name = g_strconcat ("gi._gi_", namespace, NULL);
65 PyObject *module = PyImport_ImportModule (module_name);
66 g_free (module_name);
67 return module;
68 }
69
70 static PyGIForeignStruct *
71 pygi_struct_foreign_lookup_by_name (const char *namespace, const char *name)
72 {
73 PyGIForeignStruct *result;
74
75 result = do_lookup (namespace, name);
76
77 if (result == NULL) {
78 PyObject *module = pygi_struct_foreign_load_module (namespace);
79
80 if (module == NULL)
81 PyErr_Clear ();
82 else {
83 Py_DECREF (module);
84 result = do_lookup (namespace, name);
85 }
86 }
87
88 if (result == NULL) {
89 PyErr_Format (PyExc_TypeError,
90 "Couldn't find foreign struct converter for '%s.%s'",
91 namespace, name);
92 }
93
94 return result;
95 }
96
97 static PyGIForeignStruct *
98 pygi_struct_foreign_lookup (GIBaseInfo *base_info)
99 {
100 const gchar *namespace = gi_base_info_get_namespace (base_info);
101 const gchar *name = gi_base_info_get_name (base_info);
102
103 return pygi_struct_foreign_lookup_by_name (namespace, name);
104 }
105
106 PyObject *
107 pygi_struct_foreign_convert_to_g_argument (
108 PyObject *value, GIRegisteredTypeInfo *interface_info, GITransfer transfer,
109 GIArgument *arg)
110 {
111 PyObject *result;
112
113 GIBaseInfo *base_info = GI_BASE_INFO (interface_info);
114 PyGIForeignStruct *foreign_struct = pygi_struct_foreign_lookup (base_info);
115
116 if (foreign_struct == NULL) {
117 PyErr_Format (PyExc_KeyError, "could not find foreign type %s",
118 gi_base_info_get_name (base_info));
119 return FALSE;
120 }
121
122 result = foreign_struct->to_func (value, interface_info, transfer, arg);
123 return result;
124 }
125
126 PyObject *
127 pygi_struct_foreign_convert_from_g_argument (
128 GIRegisteredTypeInfo *interface_info, GITransfer transfer, GIArgument *arg)
129 {
130 GIBaseInfo *base_info = GI_BASE_INFO (interface_info);
131 PyGIForeignStruct *foreign_struct = pygi_struct_foreign_lookup (base_info);
132
133 if (foreign_struct == NULL) return NULL;
134
135 return foreign_struct->from_func (interface_info, transfer, arg);
136 }
137
138 PyObject *
139 pygi_struct_foreign_release (GIBaseInfo *base_info, gpointer struct_)
140 {
141 PyGIForeignStruct *foreign_struct = pygi_struct_foreign_lookup (base_info);
142
143 if (foreign_struct == NULL) return NULL;
144
145 if (!foreign_struct->release_func) Py_RETURN_NONE;
146
147 return foreign_struct->release_func (base_info, struct_);
148 }
149
150 void
151 pygi_register_foreign_struct (const char *namespace_, const char *name,
152 PyGIArgOverrideToGIArgumentFunc to_func,
153 PyGIArgOverrideFromGIArgumentFunc from_func,
154 PyGIArgOverrideReleaseFunc release_func)
155 {
156 PyGIForeignStruct *new_struct = g_slice_new (PyGIForeignStruct);
157 new_struct->namespace = namespace_;
158 new_struct->name = name;
159 new_struct->to_func = to_func;
160 new_struct->from_func = from_func;
161 new_struct->release_func = release_func;
162
163 g_ptr_array_add (foreign_structs, new_struct);
164 }
165
166 PyObject *
167 pygi_require_foreign (PyObject *self, PyObject *args, PyObject *kwargs)
168 {
169 static char *kwlist[] = { "namespace", "symbol", NULL };
170 const char *namespace = NULL;
171 const char *symbol = NULL;
172
173 if (!PyArg_ParseTupleAndKeywords (args, kwargs, "s|z:require_foreign",
174 kwlist, &namespace, &symbol)) {
175 return NULL;
176 }
177
178 if (symbol) {
179 PyGIForeignStruct *foreign;
180 foreign = pygi_struct_foreign_lookup_by_name (namespace, symbol);
181 if (foreign == NULL) {
182 return NULL;
183 }
184 } else {
185 PyObject *module = pygi_struct_foreign_load_module (namespace);
186 if (module) {
187 Py_DECREF (module);
188 } else {
189 return NULL;
190 }
191 }
192
193 Py_RETURN_NONE;
194 }
195
196 /**
197 * Returns 0 on success, or -1 and sets an exception.
198 */
199 int
200 pygi_foreign_init (void)
201 {
202 if (foreign_structs == NULL) {
203 init_foreign_structs ();
204 }
205
206 return 0;
207 }
208
209
210 PyObject *
211 pygi_register_foreign (PyObject *self, PyObject *args)
212 {
213 /* We need to try loading the foreign modules upfront so the GType
214 * converters are registered:
215 * https://gitlab.gnome.org/GNOME/pygobject/issues/260
216 */
217 PyObject *mod = pygi_struct_foreign_load_module ("cairo");
218 if (mod == NULL)
219 PyErr_Clear ();
220 else
221 Py_DECREF (mod);
222
223 Py_RETURN_NONE;
224 }