Debian Codegraph @

gi/pygi-cache-list.c

raw
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
3 *
4 * Copyright (C) 2011 John (J5) Palmieri <[email protected]>
5 * Copyright (C) 2014 Simon Feltman <[email protected]>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "pygi-argument.h"
22 #include "pygi-util.h"
23 #include "pygi-cache-private.h"
24
25 typedef PyGISequenceCache PyGIArgGList;
26
27 /*
28 * GList and GSList from Python
29 */
30 static gboolean
31 _pygi_marshal_from_py_glist (PyGIInvokeState *state,
32 PyGICallableCache *callable_cache,
33 PyGIArgCache *arg_cache, PyObject *py_arg,
34 GIArgument *arg, gpointer *cleanup_data)
35 {
36 PyGIMarshalFromPyFunc from_py_marshaller;
37 int i;
38 Py_ssize_t length;
39 GList *list_ = NULL;
40 PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache;
41
42
43 if (Py_IsNone (py_arg)) {
44 arg->v_pointer = NULL;
45 return TRUE;
46 }
47
48 if (!PySequence_Check (py_arg)) {
49 PyErr_Format (PyExc_TypeError, "Must be sequence, not %s",
50 Py_TYPE (py_arg)->tp_name);
51 return FALSE;
52 }
53
54 length = PySequence_Length (py_arg);
55 if (length < 0) return FALSE;
56
57 from_py_marshaller = sequence_cache->item_cache->from_py_marshaller;
58 for (i = 0; i < length; i++) {
59 GIArgument item = PYGI_ARG_INIT;
60 gpointer item_cleanup_data = NULL;
61 PyObject *py_item = PySequence_GetItem (py_arg, i);
62 if (py_item == NULL) goto err;
63
64 if (!from_py_marshaller (state, callable_cache,
65 sequence_cache->item_cache, py_item, &item,
66 &item_cleanup_data))
67 goto err;
68
69 Py_DECREF (py_item);
70 list_ = g_list_prepend (
71 list_, _pygi_arg_to_hash_pointer (
72 item, sequence_cache->item_cache->type_info));
73 continue;
74 err:
75 /* FIXME: clean up list
76 if (sequence_cache->item_cache->from_py_cleanup != NULL) {
77 PyGIMarshalFromPyCleanupFunc cleanup = sequence_cache->item_cache->from_py_cleanup;
78 }
79 */
80 Py_XDECREF (py_item);
81 g_list_free (list_);
82 _PyGI_ERROR_PREFIX ("Item %i: ", i);
83 return FALSE;
84 }
85
86 arg->v_pointer = g_list_reverse (list_);
87
88 if (arg_cache->transfer == GI_TRANSFER_NOTHING) {
89 /* Free everything in cleanup. */
90 *cleanup_data = arg->v_pointer;
91 } else if (arg_cache->transfer == GI_TRANSFER_CONTAINER) {
92 /* Make a shallow copy so we can free the elements later in cleanup
93 * because it is possible invoke will free the list before our cleanup. */
94 *cleanup_data = g_list_copy (arg->v_pointer);
95 } else { /* GI_TRANSFER_EVERYTHING */
96 /* No cleanup, everything is given to the callee. */
97 *cleanup_data = NULL;
98 }
99 return TRUE;
100 }
101
102
103 static gboolean
104 _pygi_marshal_from_py_gslist (PyGIInvokeState *state,
105 PyGICallableCache *callable_cache,
106 PyGIArgCache *arg_cache, PyObject *py_arg,
107 GIArgument *arg, gpointer *cleanup_data)
108 {
109 PyGIMarshalFromPyFunc from_py_marshaller;
110 int i;
111 Py_ssize_t length;
112 GSList *list_ = NULL;
113 PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache;
114
115 if (Py_IsNone (py_arg)) {
116 arg->v_pointer = NULL;
117 return TRUE;
118 }
119
120 if (!PySequence_Check (py_arg)) {
121 PyErr_Format (PyExc_TypeError, "Must be sequence, not %s",
122 Py_TYPE (py_arg)->tp_name);
123 return FALSE;
124 }
125
126 length = PySequence_Length (py_arg);
127 if (length < 0) return FALSE;
128
129 from_py_marshaller = sequence_cache->item_cache->from_py_marshaller;
130 for (i = 0; i < length; i++) {
131 GIArgument item = { 0 };
132 gpointer item_cleanup_data = NULL;
133 PyObject *py_item = PySequence_GetItem (py_arg, i);
134 if (py_item == NULL) goto err;
135
136 if (!from_py_marshaller (state, callable_cache,
137 sequence_cache->item_cache, py_item, &item,
138 &item_cleanup_data))
139 goto err;
140
141 Py_DECREF (py_item);
142 list_ = g_slist_prepend (
143 list_, _pygi_arg_to_hash_pointer (
144 item, sequence_cache->item_cache->type_info));
145 continue;
146 err:
147 /* FIXME: Clean up list
148 if (sequence_cache->item_cache->from_py_cleanup != NULL) {
149 PyGIMarshalFromPyCleanupFunc cleanup = sequence_cache->item_cache->from_py_cleanup;
150 }
151 */
152
153 Py_XDECREF (py_item);
154 g_slist_free (list_);
155 _PyGI_ERROR_PREFIX ("Item %i: ", i);
156 return FALSE;
157 }
158
159 arg->v_pointer = g_slist_reverse (list_);
160
161 if (arg_cache->transfer == GI_TRANSFER_NOTHING) {
162 /* Free everything in cleanup. */
163 *cleanup_data = arg->v_pointer;
164 } else if (arg_cache->transfer == GI_TRANSFER_CONTAINER) {
165 /* Make a shallow copy so we can free the elements later in cleanup
166 * because it is possible invoke will free the list before our cleanup. */
167 *cleanup_data = g_slist_copy (arg->v_pointer);
168 } else { /* GI_TRANSFER_EVERYTHING */
169 /* No cleanup, everything is given to the callee. */
170 *cleanup_data = NULL;
171 }
172
173 return TRUE;
174 }
175
176 static void
177 _pygi_marshal_cleanup_from_py_glist (PyGIInvokeState *state,
178 PyGIArgCache *arg_cache, PyObject *py_arg,
179 gpointer data, gboolean was_processed)
180 {
181 if (was_processed) {
182 GSList *list_;
183 PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache;
184
185 list_ = (GSList *)data;
186
187 /* clean up items first */
188 if (sequence_cache->item_cache->from_py_cleanup != NULL) {
189 PyGIMarshalFromPyCleanupFunc cleanup_func =
190 sequence_cache->item_cache->from_py_cleanup;
191 GSList *node = list_;
192 gsize i = 0;
193 while (node != NULL) {
194 PyObject *py_item = PySequence_GetItem (py_arg, i);
195 cleanup_func (state, sequence_cache->item_cache, py_item,
196 node->data, TRUE);
197 Py_XDECREF (py_item);
198 node = node->next;
199 i++;
200 }
201 }
202
203 if (arg_cache->type_tag == GI_TYPE_TAG_GLIST) {
204 g_list_free ((GList *)list_);
205 } else if (arg_cache->type_tag == GI_TYPE_TAG_GSLIST) {
206 g_slist_free (list_);
207 } else {
208 g_assert_not_reached ();
209 }
210 }
211 }
212
213
214 /*
215 * GList and GSList to Python
216 */
217 static PyObject *
218 _pygi_marshal_to_py_glist (PyGIInvokeState *state,
219 PyGICallableCache *callable_cache,
220 PyGIArgCache *arg_cache, GIArgument *arg,
221 gpointer *cleanup_data)
222 {
223 GList *list_;
224 guint length;
225 guint i;
226 GPtrArray *item_cleanups;
227
228 PyGIMarshalToPyFunc item_to_py_marshaller;
229 PyGIArgCache *item_arg_cache;
230 PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;
231
232 PyObject *py_obj = NULL;
233
234 list_ = arg->v_pointer;
235 length = g_list_length (list_);
236
237 py_obj = PyList_New (length);
238 if (py_obj == NULL) return NULL;
239
240 item_cleanups = g_ptr_array_sized_new (length);
241 *cleanup_data = item_cleanups;
242
243 item_arg_cache = seq_cache->item_cache;
244 item_to_py_marshaller = item_arg_cache->to_py_marshaller;
245
246 for (i = 0; list_ != NULL; list_ = g_list_next (list_), i++) {
247 GIArgument item_arg;
248 PyObject *py_item;
249 gpointer item_cleanup_data = NULL;
250
251 item_arg.v_pointer = list_->data;
252 _pygi_hash_pointer_to_arg_in_place (&item_arg,
253 item_arg_cache->type_info);
254 py_item = item_to_py_marshaller (state, callable_cache, item_arg_cache,
255 &item_arg, &item_cleanup_data);
256
257 g_ptr_array_index (item_cleanups, i) = item_cleanup_data;
258
259 if (py_item == NULL) {
260 Py_CLEAR (py_obj);
261 _PyGI_ERROR_PREFIX ("Item %u: ", i);
262 g_ptr_array_unref (item_cleanups);
263 return NULL;
264 }
265
266 PyList_SET_ITEM (py_obj, i, py_item);
267 }
268
269 return py_obj;
270 }
271
272 static PyObject *
273 _pygi_marshal_to_py_gslist (PyGIInvokeState *state,
274 PyGICallableCache *callable_cache,
275 PyGIArgCache *arg_cache, GIArgument *arg,
276 gpointer *cleanup_data)
277 {
278 GSList *list_;
279 guint length;
280 guint i;
281 GPtrArray *item_cleanups;
282
283 PyGIMarshalToPyFunc item_to_py_marshaller;
284 PyGIArgCache *item_arg_cache;
285 PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;
286
287 PyObject *py_obj = NULL;
288
289 list_ = arg->v_pointer;
290 length = g_slist_length (list_);
291
292 py_obj = PyList_New (length);
293 if (py_obj == NULL) return NULL;
294
295 item_cleanups = g_ptr_array_sized_new (length);
296 *cleanup_data = item_cleanups;
297
298 item_arg_cache = seq_cache->item_cache;
299 item_to_py_marshaller = item_arg_cache->to_py_marshaller;
300
301 for (i = 0; list_ != NULL; list_ = g_slist_next (list_), i++) {
302 GIArgument item_arg;
303 PyObject *py_item;
304 gpointer item_cleanup_data = NULL;
305
306 item_arg.v_pointer = list_->data;
307 _pygi_hash_pointer_to_arg_in_place (&item_arg,
308 item_arg_cache->type_info);
309 py_item = item_to_py_marshaller (state, callable_cache, item_arg_cache,
310 &item_arg, &item_cleanup_data);
311
312 g_ptr_array_index (item_cleanups, i) = item_cleanup_data;
313 if (py_item == NULL) {
314 Py_CLEAR (py_obj);
315 _PyGI_ERROR_PREFIX ("Item %u: ", i);
316 g_ptr_array_unref (item_cleanups);
317 return NULL;
318 }
319
320 PyList_SET_ITEM (py_obj, i, py_item);
321 }
322
323 return py_obj;
324 }
325
326 static void
327 _pygi_marshal_cleanup_to_py_glist (PyGIInvokeState *state,
328 PyGIArgCache *arg_cache,
329 gpointer cleanup_data, gpointer data,
330 gboolean was_processed)
331 {
332 GPtrArray *item_cleanups = (GPtrArray *)cleanup_data;
333 PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache;
334 GSList *list_ = (GSList *)data;
335
336 if (sequence_cache->item_cache->to_py_cleanup != NULL) {
337 PyGIMarshalToPyCleanupFunc cleanup_func =
338 sequence_cache->item_cache->to_py_cleanup;
339 GSList *node = list_;
340 guint i = 0;
341
342 while (node != NULL) {
343 cleanup_func (state, sequence_cache->item_cache,
344 g_ptr_array_index (item_cleanups, i), node->data,
345 was_processed);
346 node = node->next;
347 i++;
348 }
349 }
350
351 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING
352 || arg_cache->transfer == GI_TRANSFER_CONTAINER) {
353 if (arg_cache->type_tag == GI_TYPE_TAG_GLIST) {
354 g_list_free ((GList *)list_);
355 } else if (arg_cache->type_tag == GI_TYPE_TAG_GSLIST) {
356 g_slist_free (list_);
357 } else {
358 g_assert_not_reached ();
359 }
360 }
361
362 g_ptr_array_unref (item_cleanups);
363 }
364
365 static void
366 _arg_cache_from_py_glist_setup (PyGIArgCache *arg_cache, GITransfer transfer)
367 {
368 arg_cache->from_py_marshaller = _pygi_marshal_from_py_glist;
369 arg_cache->from_py_cleanup = _pygi_marshal_cleanup_from_py_glist;
370 }
371
372 static void
373 _arg_cache_to_py_glist_setup (PyGIArgCache *arg_cache, GITransfer transfer)
374 {
375 arg_cache->to_py_marshaller = _pygi_marshal_to_py_glist;
376 arg_cache->to_py_cleanup = _pygi_marshal_cleanup_to_py_glist;
377 }
378
379 static void
380 _arg_cache_from_py_gslist_setup (PyGIArgCache *arg_cache, GITransfer transfer)
381 {
382 arg_cache->from_py_marshaller = _pygi_marshal_from_py_gslist;
383 arg_cache->from_py_cleanup = _pygi_marshal_cleanup_from_py_glist;
384 }
385
386 static void
387 _arg_cache_to_py_gslist_setup (PyGIArgCache *arg_cache, GITransfer transfer)
388 {
389 arg_cache->to_py_marshaller = _pygi_marshal_to_py_gslist;
390 arg_cache->to_py_cleanup = _pygi_marshal_cleanup_to_py_glist;
391 }
392
393
394 /*
395 * GList/GSList Interface
396 */
397
398 PyGIArgCache *
399 pygi_arg_glist_new_from_info (GITypeInfo *type_info, GIArgInfo *arg_info,
400 GITransfer transfer, PyGIDirection direction,
401 PyGICallableCache *callable_cache)
402 {
403 PyGIArgCache *arg_cache = (PyGIArgCache *)g_slice_new0 (PyGIArgGList);
404 if (arg_cache == NULL) return NULL;
405
406 if (!pygi_arg_sequence_setup ((PyGISequenceCache *)arg_cache, type_info,
407 arg_info, transfer, direction,
408 callable_cache)) {
409 pygi_arg_cache_free (arg_cache);
410 return NULL;
411 }
412
413 if (direction & PYGI_DIRECTION_FROM_PYTHON)
414 _arg_cache_from_py_glist_setup (arg_cache, transfer);
415
416 if (direction & PYGI_DIRECTION_TO_PYTHON)
417 _arg_cache_to_py_glist_setup (arg_cache, transfer);
418
419 return arg_cache;
420 }
421
422 PyGIArgCache *
423 pygi_arg_gslist_new_from_info (GITypeInfo *type_info, GIArgInfo *arg_info,
424 GITransfer transfer, PyGIDirection direction,
425 PyGICallableCache *callable_cache)
426 {
427 PyGIArgCache *arg_cache = (PyGIArgCache *)g_slice_new0 (PyGIArgGList);
428 if (arg_cache == NULL) return NULL;
429
430 if (!pygi_arg_sequence_setup ((PyGISequenceCache *)arg_cache, type_info,
431 arg_info, transfer, direction,
432 callable_cache)) {
433 pygi_arg_cache_free (arg_cache);
434 return NULL;
435 }
436
437 if (direction & PYGI_DIRECTION_FROM_PYTHON)
438 _arg_cache_from_py_gslist_setup (arg_cache, transfer);
439
440 if (direction & PYGI_DIRECTION_TO_PYTHON)
441 _arg_cache_to_py_gslist_setup (arg_cache, transfer);
442
443 return arg_cache;
444 }