Debian Codegraph @

gi/pygi-argument.c

raw
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
3 *
4 * Copyright (C) 2005-2009 Johan Dahlin <[email protected]>
5 *
6 * pygi-argument.c: GIArgument - PyObject conversion functions.
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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <string.h>
23 #include <time.h>
24
25 #include "pygenum.h"
26 #include "pygflags.h"
27
28 #include "pygi-type.h"
29
30 #include "pygi-argument.h"
31 #include "pygi-basictype.h"
32 #include "pygi-boxed.h"
33 #include "pygi-error.h"
34 #include "pygi-foreign.h"
35 #include "pygi-info.h"
36 #include "pygi-util.h"
37 #include "pygi-value.h"
38 #include "pygi-cache-private.h"
39
40 GIArgument
41 pygi_argument_from_py (GITypeInfo *type_info, PyObject *object,
42 GITransfer transfer,
43 PyGIArgumentFromPyCleanupData *arg_cleanup)
44 {
45 GIArgument arg = PYGI_ARG_INIT;
46
47 PyGIArgCache *cache = pygi_arg_cache_new (
48 type_info, /*arg_info=*/NULL, transfer, PYGI_DIRECTION_FROM_PYTHON,
49 /*callable_cache=*/NULL, 0, 0);
50
51 if (!cache->from_py_marshaller (&arg_cleanup->state,
52 /*callable_cache=*/NULL, cache, object,
53 &arg, &arg_cleanup->cleanup_data)) {
54 if (!PyErr_Occurred ())
55 PyErr_Format (PyExc_TypeError,
56 "Unable to convert argument %R to its C equivalent",
57 object);
58 }
59
60 arg_cleanup->cache = cache;
61 arg_cleanup->object = object;
62
63 return arg;
64 }
65
66 void
67 pygi_argument_from_py_cleanup (PyGIArgumentFromPyCleanupData *arg_cleanup)
68 {
69 PyGIArgCache *cache = (PyGIArgCache *)arg_cleanup->cache;
70
71 if (cache) {
72 if (cache->from_py_cleanup != NULL
73 && arg_cleanup->cleanup_data != NULL)
74 cache->from_py_cleanup (&arg_cleanup->state, cache,
75 arg_cleanup->object,
76 arg_cleanup->cleanup_data, TRUE);
77 pygi_arg_cache_free (cache);
78 memset (arg_cleanup, 0, sizeof (PyGIArgumentFromPyCleanupData));
79 }
80 }
81
82 PyObject *
83 pygi_argument_to_py (GITypeInfo *type_info, GIArgument arg,
84 GITransfer transfer)
85 {
86 PyGIInvokeState state = { 0 };
87 gpointer cleanup_data = NULL;
88 PyObject *object;
89
90 PyGIArgCache *cache = pygi_arg_cache_new (
91 type_info, /*arg_info=*/NULL, transfer, PYGI_DIRECTION_TO_PYTHON,
92 /*callable_cache=*/NULL, 0, 0);
93
94 object = cache->to_py_marshaller (&state, /*callable_cache=*/NULL, cache,
95 &arg, &cleanup_data);
96
97 if (cache->to_py_cleanup && arg.v_pointer)
98 cache->to_py_cleanup (&state, cache, cleanup_data, arg.v_pointer,
99 TRUE);
100
101 pygi_arg_cache_free (cache);
102
103 return object;
104 }
105
106 PyObject *
107 pygi_argument_to_py_with_array_length (GITypeInfo *type_info, GIArgument arg,
108 GITransfer transfer, gsize array_length)
109 {
110 PyGIInvokeState state = { 0 };
111 PyGIInvokeArgState arg_state = { 0 };
112 gpointer cleanup_data = NULL;
113 PyObject *object;
114
115 PyGIArgCache *cache = pygi_arg_garray_new_from_info (
116 type_info, /*arg_info=*/NULL, transfer, PYGI_DIRECTION_TO_PYTHON,
117 /*callable_cache=*/NULL, 0, NULL);
118 arg_state.arg_value = (GIArgument){ .v_size = array_length };
119 state.args = &arg_state;
120 ((PyGIArgGArray *)cache)->has_len_arg = TRUE;
121
122 object = cache->to_py_marshaller (&state, /*callable_cache=*/NULL, cache,
123 &arg, &cleanup_data);
124
125 if (cache->to_py_cleanup && arg.v_pointer)
126 cache->to_py_cleanup (&state, cache, cleanup_data, arg.v_pointer,
127 TRUE);
128
129 pygi_arg_cache_free (cache);
130
131 return object;
132 }
133
134 gboolean
135 pygi_argument_to_gsize (GIArgument arg, GITypeTag type_tag, gsize *gsize_out)
136 {
137 switch (type_tag) {
138 case GI_TYPE_TAG_INT8:
139 *gsize_out = arg.v_int8;
140 return TRUE;
141 case GI_TYPE_TAG_UINT8:
142 *gsize_out = arg.v_uint8;
143 return TRUE;
144 case GI_TYPE_TAG_INT16:
145 *gsize_out = arg.v_int16;
146 return TRUE;
147 case GI_TYPE_TAG_UINT16:
148 *gsize_out = arg.v_uint16;
149 return TRUE;
150 case GI_TYPE_TAG_INT32:
151 *gsize_out = arg.v_int32;
152 return TRUE;
153 case GI_TYPE_TAG_UINT32:
154 case GI_TYPE_TAG_UNICHAR:
155 *gsize_out = arg.v_uint32;
156 return TRUE;
157 case GI_TYPE_TAG_INT64:
158 if (arg.v_uint64 > G_MAXSIZE) {
159 PyErr_Format (PyExc_TypeError, "Unable to marshal %s to gsize",
160 gi_type_tag_to_string (type_tag));
161 return FALSE;
162 }
163 *gsize_out = (gsize)arg.v_int64;
164 return TRUE;
165 case GI_TYPE_TAG_UINT64:
166 if (arg.v_uint64 > G_MAXSIZE) {
167 PyErr_Format (PyExc_TypeError, "Unable to marshal %s to gsize",
168 gi_type_tag_to_string (type_tag));
169 return FALSE;
170 }
171 *gsize_out = (gsize)arg.v_uint64;
172 return TRUE;
173 case GI_TYPE_TAG_VOID:
174 case GI_TYPE_TAG_BOOLEAN:
175 case GI_TYPE_TAG_FLOAT:
176 case GI_TYPE_TAG_DOUBLE:
177 case GI_TYPE_TAG_GTYPE:
178 case GI_TYPE_TAG_UTF8:
179 case GI_TYPE_TAG_FILENAME:
180 case GI_TYPE_TAG_ARRAY:
181 case GI_TYPE_TAG_INTERFACE:
182 case GI_TYPE_TAG_GLIST:
183 case GI_TYPE_TAG_GSLIST:
184 case GI_TYPE_TAG_GHASH:
185 case GI_TYPE_TAG_ERROR:
186 PyErr_Format (PyExc_TypeError, "Unable to marshal %s to gsize",
187 gi_type_tag_to_string (type_tag));
188 return FALSE;
189 default:
190 g_assert_not_reached ();
191 }
192 }
193
194 static GITypeTag
195 _pygi_get_storage_type (GITypeInfo *type_info)
196 {
197 GITypeTag type_tag = gi_type_info_get_tag (type_info);
198
199 if (type_tag == GI_TYPE_TAG_INTERFACE) {
200 GIBaseInfo *interface = gi_type_info_get_interface (type_info);
201 if (GI_IS_ENUM_INFO (interface))
202 type_tag = gi_enum_info_get_storage_type ((GIEnumInfo *)interface);
203 /* FIXME: we might have something to do for other types */
204 gi_base_info_unref (interface);
205 }
206 return type_tag;
207 }
208
209 void
210 _pygi_hash_pointer_to_arg_in_place (GIArgument *arg, GITypeInfo *type_info)
211 {
212 GITypeTag type_tag = _pygi_get_storage_type (type_info);
213
214 switch (type_tag) {
215 case GI_TYPE_TAG_INT8:
216 arg->v_int8 = (gint8)GPOINTER_TO_INT (arg->v_pointer);
217 break;
218 case GI_TYPE_TAG_INT16:
219 arg->v_int16 = (gint16)GPOINTER_TO_INT (arg->v_pointer);
220 break;
221 case GI_TYPE_TAG_INT32:
222 arg->v_int32 = (gint32)GPOINTER_TO_INT (arg->v_pointer);
223 break;
224 case GI_TYPE_TAG_INT64:
225 arg->v_int64 = (gint64)GPOINTER_TO_INT (arg->v_pointer);
226 break;
227 case GI_TYPE_TAG_UINT8:
228 arg->v_uint8 = (guint8)GPOINTER_TO_UINT (arg->v_pointer);
229 break;
230 case GI_TYPE_TAG_UINT16:
231 arg->v_uint16 = (guint16)GPOINTER_TO_UINT (arg->v_pointer);
232 break;
233 case GI_TYPE_TAG_UINT32:
234 case GI_TYPE_TAG_UNICHAR:
235 arg->v_uint32 = (guint32)GPOINTER_TO_UINT (arg->v_pointer);
236 break;
237 case GI_TYPE_TAG_UINT64:
238 arg->v_uint64 = (guint64)GPOINTER_TO_UINT (arg->v_pointer);
239 break;
240 case GI_TYPE_TAG_GTYPE:
241 arg->v_size = GPOINTER_TO_SIZE (arg->v_pointer);
242 break;
243 case GI_TYPE_TAG_VOID:
244 case GI_TYPE_TAG_BOOLEAN:
245 case GI_TYPE_TAG_FLOAT:
246 case GI_TYPE_TAG_DOUBLE:
247 case GI_TYPE_TAG_UTF8:
248 case GI_TYPE_TAG_FILENAME:
249 case GI_TYPE_TAG_INTERFACE:
250 case GI_TYPE_TAG_ARRAY:
251 case GI_TYPE_TAG_GLIST:
252 case GI_TYPE_TAG_GSLIST:
253 case GI_TYPE_TAG_GHASH:
254 case GI_TYPE_TAG_ERROR:
255 break;
256 default:
257 g_assert_not_reached ();
258 }
259 }
260
261 gpointer
262 _pygi_arg_to_hash_pointer (const GIArgument arg, GITypeInfo *type_info)
263 {
264 GITypeTag type_tag = _pygi_get_storage_type (type_info);
265
266 switch (type_tag) {
267 case GI_TYPE_TAG_INT8:
268 return GINT_TO_POINTER (arg.v_int8);
269 case GI_TYPE_TAG_UINT8:
270 return GUINT_TO_POINTER (arg.v_uint8);
271 case GI_TYPE_TAG_INT16:
272 return GINT_TO_POINTER (arg.v_int16);
273 case GI_TYPE_TAG_UINT16:
274 return GUINT_TO_POINTER (arg.v_uint16);
275 case GI_TYPE_TAG_INT32:
276 return GINT_TO_POINTER (arg.v_int32);
277 case GI_TYPE_TAG_UINT32:
278 case GI_TYPE_TAG_UNICHAR:
279 return GUINT_TO_POINTER (arg.v_uint32);
280 case GI_TYPE_TAG_INT64:
281 return GINT_TO_POINTER (arg.v_int64);
282 case GI_TYPE_TAG_UINT64:
283 return GUINT_TO_POINTER (arg.v_uint64);
284 case GI_TYPE_TAG_GTYPE:
285 return GSIZE_TO_POINTER (arg.v_size);
286 case GI_TYPE_TAG_UTF8:
287 case GI_TYPE_TAG_FILENAME:
288 case GI_TYPE_TAG_INTERFACE:
289 case GI_TYPE_TAG_ARRAY:
290 return arg.v_pointer;
291 case GI_TYPE_TAG_VOID:
292 case GI_TYPE_TAG_BOOLEAN:
293 case GI_TYPE_TAG_FLOAT:
294 case GI_TYPE_TAG_DOUBLE:
295 case GI_TYPE_TAG_GLIST:
296 case GI_TYPE_TAG_GSLIST:
297 case GI_TYPE_TAG_GHASH:
298 case GI_TYPE_TAG_ERROR:
299 g_critical ("Unsupported type %s", gi_type_tag_to_string (type_tag));
300 return arg.v_pointer;
301 default:
302 g_assert_not_reached ();
303 }
304 }