1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
4 * Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>, Red Hat, Inc.
6 * pygi-marshal-from-py.c: functions for converting C types to PyObject
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.
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.
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
24 #include "pygi-private.h"
30 #include <pygobject.h>
31 #include <pyglib-python-compat.h>
33 #include "pygi-cache.h"
34 #include "pygi-marshal-cleanup.h"
35 #include "pygi-marshal-to-py.h"
38 gi_argument_to_c_long (GIArgument *arg_in,
43 case GI_TYPE_TAG_INT8:
44 *c_long_out = arg_in->v_int8;
46 case GI_TYPE_TAG_UINT8:
47 *c_long_out = arg_in->v_uint8;
49 case GI_TYPE_TAG_INT16:
50 *c_long_out = arg_in->v_int16;
52 case GI_TYPE_TAG_UINT16:
53 *c_long_out = arg_in->v_uint16;
55 case GI_TYPE_TAG_INT32:
56 *c_long_out = arg_in->v_int32;
58 case GI_TYPE_TAG_UINT32:
59 *c_long_out = arg_in->v_uint32;
61 case GI_TYPE_TAG_INT64:
62 *c_long_out = arg_in->v_int64;
64 case GI_TYPE_TAG_UINT64:
65 *c_long_out = arg_in->v_uint64;
68 PyErr_Format (PyExc_TypeError,
69 "Unable to marshal %s to C long",
70 g_type_tag_to_string (type_tag));
76 gi_argument_to_gsize (GIArgument *arg_in,
81 case GI_TYPE_TAG_INT8:
82 *gsize_out = arg_in->v_int8;
84 case GI_TYPE_TAG_UINT8:
85 *gsize_out = arg_in->v_uint8;
87 case GI_TYPE_TAG_INT16:
88 *gsize_out = arg_in->v_int16;
90 case GI_TYPE_TAG_UINT16:
91 *gsize_out = arg_in->v_uint16;
93 case GI_TYPE_TAG_INT32:
94 *gsize_out = arg_in->v_int32;
96 case GI_TYPE_TAG_UINT32:
97 *gsize_out = arg_in->v_uint32;
99 case GI_TYPE_TAG_INT64:
100 *gsize_out = arg_in->v_int64;
102 case GI_TYPE_TAG_UINT64:
103 *gsize_out = arg_in->v_uint64;
106 PyErr_Format (PyExc_TypeError,
107 "Unable to marshal %s to gsize",
108 g_type_tag_to_string (type_tag));
115 _pygi_marshal_to_py_void (PyGIInvokeState *state,
116 PyGICallableCache *callable_cache,
117 PyGIArgCache *arg_cache,
120 PyObject *py_obj = NULL;
121 if (arg_cache->is_pointer)
122 py_obj = arg->v_pointer;
131 _pygi_marshal_to_py_boolean (PyGIInvokeState *state,
132 PyGICallableCache *callable_cache,
133 PyGIArgCache *arg_cache,
136 PyObject *py_obj = PyBool_FromLong (arg->v_boolean);
141 _pygi_marshal_to_py_int8 (PyGIInvokeState *state,
142 PyGICallableCache *callable_cache,
143 PyGIArgCache *arg_cache,
146 PyObject *py_obj = PYGLIB_PyLong_FromLong (arg->v_int8);
151 _pygi_marshal_to_py_uint8 (PyGIInvokeState *state,
152 PyGICallableCache *callable_cache,
153 PyGIArgCache *arg_cache,
156 PyObject *py_obj = PYGLIB_PyLong_FromLong (arg->v_uint8);
162 _pygi_marshal_to_py_int16 (PyGIInvokeState *state,
163 PyGICallableCache *callable_cache,
164 PyGIArgCache *arg_cache,
167 PyObject *py_obj = PYGLIB_PyLong_FromLong (arg->v_int16);
173 _pygi_marshal_to_py_uint16 (PyGIInvokeState *state,
174 PyGICallableCache *callable_cache,
175 PyGIArgCache *arg_cache,
178 PyObject *py_obj = PYGLIB_PyLong_FromLong (arg->v_uint16);
184 _pygi_marshal_to_py_int32 (PyGIInvokeState *state,
185 PyGICallableCache *callable_cache,
186 PyGIArgCache *arg_cache,
189 PyObject *py_obj = PYGLIB_PyLong_FromLong (arg->v_int32);
195 _pygi_marshal_to_py_uint32 (PyGIInvokeState *state,
196 PyGICallableCache *callable_cache,
197 PyGIArgCache *arg_cache,
200 PyObject *py_obj = PyLong_FromLongLong (arg->v_uint32);
206 _pygi_marshal_to_py_int64 (PyGIInvokeState *state,
207 PyGICallableCache *callable_cache,
208 PyGIArgCache *arg_cache,
211 PyObject *py_obj = PyLong_FromLongLong (arg->v_int64);
217 _pygi_marshal_to_py_uint64 (PyGIInvokeState *state,
218 PyGICallableCache *callable_cache,
219 PyGIArgCache *arg_cache,
222 PyObject *py_obj = PyLong_FromUnsignedLongLong (arg->v_uint64);
228 _pygi_marshal_to_py_float (PyGIInvokeState *state,
229 PyGICallableCache *callable_cache,
230 PyGIArgCache *arg_cache,
233 PyObject *py_obj = PyFloat_FromDouble (arg->v_float);
239 _pygi_marshal_to_py_double (PyGIInvokeState *state,
240 PyGICallableCache *callable_cache,
241 PyGIArgCache *arg_cache,
244 PyObject *py_obj = PyFloat_FromDouble (arg->v_double);
250 _pygi_marshal_to_py_unichar (PyGIInvokeState *state,
251 PyGICallableCache *callable_cache,
252 PyGIArgCache *arg_cache,
255 PyObject *py_obj = NULL;
257 /* Preserve the bidirectional mapping between 0 and "" */
258 if (arg->v_uint32 == 0) {
259 py_obj = PYGLIB_PyUnicode_FromString ("");
260 } else if (g_unichar_validate (arg->v_uint32)) {
264 bytes = g_unichar_to_utf8 (arg->v_uint32, utf8);
265 py_obj = PYGLIB_PyUnicode_FromStringAndSize ((char*)utf8, bytes);
267 /* TODO: Convert the error to an exception. */
268 PyErr_Format (PyExc_TypeError,
269 "Invalid unicode codepoint %" G_GUINT32_FORMAT,
277 _pygi_marshal_to_py_gtype (PyGIInvokeState *state,
278 PyGICallableCache *callable_cache,
279 PyGIArgCache *arg_cache,
282 PyObject *py_obj = NULL;
284 py_obj = pyg_type_wrapper_new ( (GType)arg->v_long);
289 _pygi_marshal_to_py_utf8 (PyGIInvokeState *state,
290 PyGICallableCache *callable_cache,
291 PyGIArgCache *arg_cache,
294 PyObject *py_obj = NULL;
295 if (arg->v_string == NULL) {
301 py_obj = PYGLIB_PyUnicode_FromString (arg->v_string);
306 _pygi_marshal_to_py_filename (PyGIInvokeState *state,
307 PyGICallableCache *callable_cache,
308 PyGIArgCache *arg_cache,
312 PyObject *py_obj = NULL;
313 GError *error = NULL;
315 if (arg->v_string == NULL) {
321 string = g_filename_to_utf8 (arg->v_string, -1, NULL, NULL, &error);
322 if (string == NULL) {
323 PyErr_SetString (PyExc_Exception, error->message);
324 /* TODO: Convert the error to an exception. */
328 py_obj = PYGLIB_PyUnicode_FromString (string);
335 _pygi_marshal_to_py_array (PyGIInvokeState *state,
336 PyGICallableCache *callable_cache,
337 PyGIArgCache *arg_cache,
341 PyObject *py_obj = NULL;
342 PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;
343 gsize processed_items = 0;
345 /* GArrays make it easier to iterate over arrays
346 * with different element sizes but requires that
347 * we allocate a GArray if the argument was a C array
349 if (seq_cache->array_type == GI_ARRAY_TYPE_C) {
351 if (seq_cache->fixed_size >= 0) {
352 g_assert(arg->v_pointer != NULL);
353 len = seq_cache->fixed_size;
354 } else if (seq_cache->is_zero_terminated) {
355 if (arg->v_pointer == NULL) {
357 } else if (seq_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8) {
358 len = strlen (arg->v_pointer);
360 len = g_strv_length ((gchar **)arg->v_pointer);
363 GIArgument *len_arg = state->args[seq_cache->len_arg_index];
365 if (!gi_argument_to_gsize (len_arg,
367 callable_cache->args_cache[seq_cache->len_arg_index]->type_tag)) {
372 array_ = g_array_new (FALSE,
374 seq_cache->item_size);
375 if (array_ == NULL) {
378 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING && arg->v_pointer != NULL)
379 g_free (arg->v_pointer);
384 if (array_->data != NULL)
385 g_free (array_->data);
386 array_->data = arg->v_pointer;
389 array_ = arg->v_pointer;
392 if (seq_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8) {
393 if (arg->v_pointer == NULL) {
394 py_obj = PYGLIB_PyBytes_FromString ("");
396 py_obj = PYGLIB_PyBytes_FromStringAndSize (array_->data, array_->len);
399 if (arg->v_pointer == NULL) {
400 py_obj = PyList_New (0);
405 PyGIMarshalToPyFunc item_to_py_marshaller;
406 PyGIArgCache *item_arg_cache;
408 py_obj = PyList_New (array_->len);
413 item_arg_cache = seq_cache->item_cache;
414 item_to_py_marshaller = item_arg_cache->to_py_marshaller;
416 item_size = g_array_get_element_size (array_);
418 for (i = 0; i < array_->len; i++) {
422 if (seq_cache->array_type == GI_ARRAY_TYPE_PTR_ARRAY) {
423 item_arg.v_pointer = g_ptr_array_index ( ( GPtrArray *)array_, i);
424 } else if (item_arg_cache->type_tag == GI_TYPE_TAG_INTERFACE) {
425 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *) item_arg_cache;
426 gboolean is_gvariant = iface_cache->g_type == G_TYPE_VARIANT;
428 // FIXME: This probably doesn't work with boxed types or gvalues. See fx. _pygi_marshal_from_py_array()
429 switch (g_base_info_get_type (iface_cache->interface_info)) {
430 case GI_INFO_TYPE_STRUCT:
432 g_assert (item_size == sizeof (gpointer));
433 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
434 item_arg.v_pointer = g_variant_ref_sink (g_array_index (array_, gpointer, i));
436 item_arg.v_pointer = g_array_index (array_, gpointer, i);
437 } else if (arg_cache->transfer == GI_TRANSFER_EVERYTHING && !item_arg_cache->is_pointer) {
438 /* array elements are structs */
439 gpointer *_struct = g_malloc (item_size);
440 memcpy (_struct, array_->data + i * item_size,
442 item_arg.v_pointer = _struct;
443 } else if (item_arg_cache->is_pointer)
444 /* array elements are pointers to values */
445 item_arg.v_pointer = g_array_index (array_, gpointer, i);
447 item_arg.v_pointer = array_->data + i * item_size;
450 item_arg.v_pointer = g_array_index (array_, gpointer, i);
454 memcpy (&item_arg, array_->data + i * item_size, item_size);
457 py_item = item_to_py_marshaller ( state,
462 if (py_item == NULL) {
465 if (seq_cache->array_type == GI_ARRAY_TYPE_C)
466 g_array_unref (array_);
470 PyList_SET_ITEM (py_obj, i, py_item);
476 if (seq_cache->array_type == GI_ARRAY_TYPE_C)
477 g_array_free (array_, FALSE);
482 if (seq_cache->array_type == GI_ARRAY_TYPE_C) {
483 g_array_free (array_, arg_cache->transfer == GI_TRANSFER_EVERYTHING);
485 /* clean up unprocessed items */
486 if (seq_cache->item_cache->to_py_cleanup != NULL) {
488 PyGIMarshalCleanupFunc cleanup_func = seq_cache->item_cache->to_py_cleanup;
489 for (j = processed_items; j < array_->len; j++) {
491 seq_cache->item_cache,
492 g_array_index (array_, gpointer, j),
497 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
498 g_array_free (array_, TRUE);
505 _pygi_marshal_to_py_glist (PyGIInvokeState *state,
506 PyGICallableCache *callable_cache,
507 PyGIArgCache *arg_cache,
514 PyGIMarshalToPyFunc item_to_py_marshaller;
515 PyGIArgCache *item_arg_cache;
516 PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;
518 PyObject *py_obj = NULL;
520 list_ = arg->v_pointer;
521 length = g_list_length (list_);
523 py_obj = PyList_New (length);
527 item_arg_cache = seq_cache->item_cache;
528 item_to_py_marshaller = item_arg_cache->to_py_marshaller;
530 for (i = 0; list_ != NULL; list_ = g_list_next (list_), i++) {
534 item_arg.v_pointer = list_->data;
535 _pygi_hash_pointer_to_arg (&item_arg, item_arg_cache->type_tag);
536 py_item = item_to_py_marshaller (state,
541 if (py_item == NULL) {
543 _PyGI_ERROR_PREFIX ("Item %zu: ", i);
547 PyList_SET_ITEM (py_obj, i, py_item);
554 _pygi_marshal_to_py_gslist (PyGIInvokeState *state,
555 PyGICallableCache *callable_cache,
556 PyGIArgCache *arg_cache,
563 PyGIMarshalToPyFunc item_to_py_marshaller;
564 PyGIArgCache *item_arg_cache;
565 PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;
567 PyObject *py_obj = NULL;
569 list_ = arg->v_pointer;
570 length = g_slist_length (list_);
572 py_obj = PyList_New (length);
576 item_arg_cache = seq_cache->item_cache;
577 item_to_py_marshaller = item_arg_cache->to_py_marshaller;
579 for (i = 0; list_ != NULL; list_ = g_slist_next (list_), i++) {
583 item_arg.v_pointer = list_->data;
584 _pygi_hash_pointer_to_arg (&item_arg, item_arg_cache->type_tag);
585 py_item = item_to_py_marshaller (state,
590 if (py_item == NULL) {
592 _PyGI_ERROR_PREFIX ("Item %zu: ", i);
596 PyList_SET_ITEM (py_obj, i, py_item);
603 _pygi_marshal_to_py_ghash (PyGIInvokeState *state,
604 PyGICallableCache *callable_cache,
605 PyGIArgCache *arg_cache,
609 GHashTableIter hash_table_iter;
611 PyGIMarshalToPyFunc key_to_py_marshaller;
612 PyGIMarshalToPyFunc value_to_py_marshaller;
614 PyGIArgCache *key_arg_cache;
615 PyGIArgCache *value_arg_cache;
616 PyGIHashCache *hash_cache = (PyGIHashCache *)arg_cache;
619 GIArgument value_arg;
621 PyObject *py_obj = NULL;
623 hash_ = arg->v_pointer;
631 py_obj = PyDict_New ();
635 key_arg_cache = hash_cache->key_cache;
636 key_to_py_marshaller = key_arg_cache->to_py_marshaller;
638 value_arg_cache = hash_cache->value_cache;
639 value_to_py_marshaller = value_arg_cache->to_py_marshaller;
641 g_hash_table_iter_init (&hash_table_iter, hash_);
642 while (g_hash_table_iter_next (&hash_table_iter,
644 &value_arg.v_pointer)) {
650 _pygi_hash_pointer_to_arg (&key_arg, hash_cache->key_cache->type_tag);
651 py_key = key_to_py_marshaller ( state,
656 if (py_key == NULL) {
661 _pygi_hash_pointer_to_arg (&value_arg, hash_cache->value_cache->type_tag);
662 py_value = value_to_py_marshaller ( state,
667 if (py_value == NULL) {
673 retval = PyDict_SetItem (py_obj, py_key, py_value);
676 Py_DECREF (py_value);
688 _pygi_marshal_to_py_gerror (PyGIInvokeState *state,
689 PyGICallableCache *callable_cache,
690 PyGIArgCache *arg_cache,
693 GError *error = arg->v_pointer;
694 PyObject *py_obj = NULL;
696 py_obj = pyglib_error_marshal(&error);
698 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING && error != NULL) {
699 g_error_free (error);
702 if (py_obj != NULL) {
710 _pygi_marshal_to_py_interface_callback (PyGIInvokeState *state,
711 PyGICallableCache *callable_cache,
712 PyGIArgCache *arg_cache,
715 PyObject *py_obj = NULL;
717 PyErr_Format (PyExc_NotImplementedError,
718 "Marshalling a callback to PyObject is not supported");
723 _pygi_marshal_to_py_interface_enum (PyGIInvokeState *state,
724 PyGICallableCache *callable_cache,
725 PyGIArgCache *arg_cache,
728 PyObject *py_obj = NULL;
729 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
730 GIBaseInfo *interface;
733 interface = g_type_info_get_interface (arg_cache->type_info);
734 g_assert (g_base_info_get_type (interface) == GI_INFO_TYPE_ENUM);
736 if (!gi_argument_to_c_long(arg, &c_long,
737 g_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
741 if (iface_cache->g_type == G_TYPE_NONE) {
742 py_obj = PyObject_CallFunction (iface_cache->py_type, "l", c_long);
744 py_obj = pyg_enum_from_gtype (iface_cache->g_type, c_long);
750 _pygi_marshal_to_py_interface_flags (PyGIInvokeState *state,
751 PyGICallableCache *callable_cache,
752 PyGIArgCache *arg_cache,
755 PyObject *py_obj = NULL;
756 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
757 GIBaseInfo *interface;
760 interface = g_type_info_get_interface (arg_cache->type_info);
761 g_assert (g_base_info_get_type (interface) == GI_INFO_TYPE_FLAGS);
763 if (!gi_argument_to_c_long(arg, &c_long,
764 g_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
768 if (iface_cache->g_type == G_TYPE_NONE) {
769 /* An enum with a GType of None is an enum without GType */
771 PyObject *py_type = _pygi_type_import_by_gi_info (iface_cache->interface_info);
772 PyObject *py_args = NULL;
777 py_args = PyTuple_New (1);
778 if (PyTuple_SetItem (py_args, 0, PyLong_FromLong (c_long)) != 0) {
784 py_obj = PyObject_CallFunction (py_type, "l", c_long);
789 py_obj = pyg_flags_from_gtype (iface_cache->g_type, c_long);
796 _pygi_marshal_to_py_interface_struct (PyGIInvokeState *state,
797 PyGICallableCache *callable_cache,
798 PyGIArgCache *arg_cache,
801 PyObject *py_obj = NULL;
802 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
803 GType type = iface_cache->g_type;
805 if (arg->v_pointer == NULL) {
811 if (g_type_is_a (type, G_TYPE_VALUE)) {
812 py_obj = pyg_value_as_pyobject (arg->v_pointer, FALSE);
813 } else if (iface_cache->is_foreign) {
814 py_obj = pygi_struct_foreign_convert_from_g_argument (iface_cache->interface_info,
816 } else if (g_type_is_a (type, G_TYPE_BOXED)) {
817 py_obj = _pygi_boxed_new ( (PyTypeObject *)iface_cache->py_type, arg->v_pointer,
818 arg_cache->transfer == GI_TRANSFER_EVERYTHING || arg_cache->is_caller_allocates);
819 if (arg_cache->is_caller_allocates)
820 ((PyGIBoxed *)py_obj)->slice_allocated = TRUE;
821 } else if (g_type_is_a (type, G_TYPE_POINTER)) {
822 if (iface_cache->py_type == NULL ||
823 !PyType_IsSubtype ( (PyTypeObject *)iface_cache->py_type, &PyGIStruct_Type)) {
824 g_warn_if_fail(arg_cache->transfer == GI_TRANSFER_NOTHING);
825 py_obj = pyg_pointer_new (type, arg->v_pointer);
827 py_obj = _pygi_struct_new ( (PyTypeObject *)iface_cache->py_type, arg->v_pointer,
828 arg_cache->transfer == GI_TRANSFER_EVERYTHING);
830 } else if (g_type_is_a (type, G_TYPE_VARIANT)) {
831 g_variant_ref_sink (arg->v_pointer);
832 py_obj = _pygi_struct_new ( (PyTypeObject *)iface_cache->py_type, arg->v_pointer,
834 } else if (type == G_TYPE_NONE && iface_cache->is_foreign) {
835 py_obj = pygi_struct_foreign_convert_from_g_argument (iface_cache->interface_info, arg->v_pointer);
836 } else if (type == G_TYPE_NONE) {
837 py_obj = _pygi_struct_new ( (PyTypeObject *) iface_cache->py_type, arg->v_pointer,
838 arg_cache->transfer == GI_TRANSFER_EVERYTHING);
840 PyErr_Format (PyExc_NotImplementedError,
841 "structure type '%s' is not supported yet",
849 _pygi_marshal_to_py_interface_interface (PyGIInvokeState *state,
850 PyGICallableCache *callable_cache,
851 PyGIArgCache *arg_cache,
854 PyObject *py_obj = NULL;
856 PyErr_Format (PyExc_NotImplementedError,
857 "Marshalling for this type is not implemented yet");
862 _pygi_marshal_to_py_interface_boxed (PyGIInvokeState *state,
863 PyGICallableCache *callable_cache,
864 PyGIArgCache *arg_cache,
867 PyObject *py_obj = NULL;
869 PyErr_Format (PyExc_NotImplementedError,
870 "Marshalling for this type is not implemented yet");
875 _pygi_marshal_to_py_interface_object (PyGIInvokeState *state,
876 PyGICallableCache *callable_cache,
877 PyGIArgCache *arg_cache,
882 if (arg->v_pointer == NULL) {
888 if (G_IS_PARAM_SPEC(arg->v_pointer))
890 py_obj = pyg_param_spec_new (arg->v_pointer);
891 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
892 g_param_spec_unref (arg->v_pointer);
896 py_obj = pygobject_new (arg->v_pointer);
897 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
898 g_object_unref (arg->v_pointer);
905 _pygi_marshal_to_py_interface_union (PyGIInvokeState *state,
906 PyGICallableCache *callable_cache,
907 PyGIArgCache *arg_cache,
910 PyObject *py_obj = NULL;
912 PyErr_Format (PyExc_NotImplementedError,
913 "Marshalling for this type is not implemented yet");