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) {
299 py_obj = PYGLIB_PyUnicode_FromString (arg->v_string);
304 _pygi_marshal_to_py_filename (PyGIInvokeState *state,
305 PyGICallableCache *callable_cache,
306 PyGIArgCache *arg_cache,
309 gchar *string = NULL;
310 PyObject *py_obj = NULL;
311 GError *error = NULL;
313 if (arg->v_string == NULL) {
317 string = g_filename_to_utf8 (arg->v_string, -1, NULL, NULL, &error);
318 if (string == NULL) {
319 PyErr_SetString (PyExc_Exception, error->message);
320 /* TODO: Convert the error to an exception. */
324 py_obj = PYGLIB_PyUnicode_FromString (string);
331 _pygi_marshal_to_py_array (PyGIInvokeState *state,
332 PyGICallableCache *callable_cache,
333 PyGIArgCache *arg_cache,
337 PyObject *py_obj = NULL;
338 PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;
339 gsize processed_items = 0;
341 /* GArrays make it easier to iterate over arrays
342 * with different element sizes but requires that
343 * we allocate a GArray if the argument was a C array
345 if (seq_cache->array_type == GI_ARRAY_TYPE_C) {
347 if (seq_cache->fixed_size >= 0) {
348 g_assert(arg->v_pointer != NULL);
349 len = seq_cache->fixed_size;
350 } else if (seq_cache->is_zero_terminated) {
351 if (arg->v_pointer == NULL) {
353 } else if (seq_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8) {
354 len = strlen (arg->v_pointer);
356 len = g_strv_length ((gchar **)arg->v_pointer);
359 GIArgument *len_arg = state->args[seq_cache->len_arg_index];
361 if (!gi_argument_to_gsize (len_arg,
363 callable_cache->args_cache[seq_cache->len_arg_index]->type_tag)) {
368 array_ = g_array_new (FALSE,
370 seq_cache->item_size);
371 if (array_ == NULL) {
374 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING && arg->v_pointer != NULL)
375 g_free (arg->v_pointer);
380 if (array_->data != NULL)
381 g_free (array_->data);
382 array_->data = arg->v_pointer;
385 array_ = arg->v_pointer;
388 if (seq_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8) {
389 if (arg->v_pointer == NULL) {
390 py_obj = PYGLIB_PyBytes_FromString ("");
392 py_obj = PYGLIB_PyBytes_FromStringAndSize (array_->data, array_->len);
395 if (arg->v_pointer == NULL) {
396 py_obj = PyList_New (0);
401 PyGIMarshalToPyFunc item_to_py_marshaller;
402 PyGIArgCache *item_arg_cache;
404 py_obj = PyList_New (array_->len);
409 item_arg_cache = seq_cache->item_cache;
410 item_to_py_marshaller = item_arg_cache->to_py_marshaller;
412 item_size = g_array_get_element_size (array_);
414 for (i = 0; i < array_->len; i++) {
418 if (seq_cache->array_type == GI_ARRAY_TYPE_PTR_ARRAY) {
419 item_arg.v_pointer = g_ptr_array_index ( ( GPtrArray *)array_, i);
420 } else if (item_arg_cache->type_tag == GI_TYPE_TAG_INTERFACE) {
421 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *) item_arg_cache;
422 gboolean is_gvariant = iface_cache->g_type == G_TYPE_VARIANT;
424 // FIXME: This probably doesn't work with boxed types or gvalues. See fx. _pygi_marshal_from_py_array()
425 switch (g_base_info_get_type (iface_cache->interface_info)) {
426 case GI_INFO_TYPE_STRUCT:
428 g_assert (item_size == sizeof (gpointer));
429 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
430 item_arg.v_pointer = g_variant_ref_sink (g_array_index (array_, gpointer, i));
432 item_arg.v_pointer = g_array_index (array_, gpointer, i);
433 } else if (arg_cache->transfer == GI_TRANSFER_EVERYTHING && !item_arg_cache->is_pointer &&
434 !g_type_is_a (iface_cache->g_type, G_TYPE_BOXED)) {
435 /* array elements are structs */
436 gpointer *_struct = g_malloc (item_size);
437 memcpy (_struct, array_->data + i * item_size,
439 item_arg.v_pointer = _struct;
440 } else if (item_arg_cache->is_pointer)
441 /* array elements are pointers to values */
442 item_arg.v_pointer = g_array_index (array_, gpointer, i);
444 item_arg.v_pointer = array_->data + i * item_size;
447 item_arg.v_pointer = g_array_index (array_, gpointer, i);
451 memcpy (&item_arg, array_->data + i * item_size, item_size);
454 py_item = item_to_py_marshaller ( state,
459 if (py_item == NULL) {
462 if (seq_cache->array_type == GI_ARRAY_TYPE_C)
463 g_array_unref (array_);
467 PyList_SET_ITEM (py_obj, i, py_item);
473 if (seq_cache->array_type == GI_ARRAY_TYPE_C)
474 g_array_free (array_, FALSE);
479 if (seq_cache->array_type == GI_ARRAY_TYPE_C) {
480 g_array_free (array_, arg_cache->transfer == GI_TRANSFER_EVERYTHING);
482 /* clean up unprocessed items */
483 if (seq_cache->item_cache->to_py_cleanup != NULL) {
485 PyGIMarshalCleanupFunc cleanup_func = seq_cache->item_cache->to_py_cleanup;
486 for (j = processed_items; j < array_->len; j++) {
488 seq_cache->item_cache,
489 g_array_index (array_, gpointer, j),
494 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
495 g_array_free (array_, TRUE);
502 _pygi_marshal_to_py_glist (PyGIInvokeState *state,
503 PyGICallableCache *callable_cache,
504 PyGIArgCache *arg_cache,
511 PyGIMarshalToPyFunc item_to_py_marshaller;
512 PyGIArgCache *item_arg_cache;
513 PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;
515 PyObject *py_obj = NULL;
517 list_ = arg->v_pointer;
518 length = g_list_length (list_);
520 py_obj = PyList_New (length);
524 item_arg_cache = seq_cache->item_cache;
525 item_to_py_marshaller = item_arg_cache->to_py_marshaller;
527 for (i = 0; list_ != NULL; list_ = g_list_next (list_), i++) {
531 item_arg.v_pointer = list_->data;
532 _pygi_hash_pointer_to_arg (&item_arg, item_arg_cache->type_tag);
533 py_item = item_to_py_marshaller (state,
538 if (py_item == NULL) {
540 _PyGI_ERROR_PREFIX ("Item %zu: ", i);
544 PyList_SET_ITEM (py_obj, i, py_item);
551 _pygi_marshal_to_py_gslist (PyGIInvokeState *state,
552 PyGICallableCache *callable_cache,
553 PyGIArgCache *arg_cache,
560 PyGIMarshalToPyFunc item_to_py_marshaller;
561 PyGIArgCache *item_arg_cache;
562 PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;
564 PyObject *py_obj = NULL;
566 list_ = arg->v_pointer;
567 length = g_slist_length (list_);
569 py_obj = PyList_New (length);
573 item_arg_cache = seq_cache->item_cache;
574 item_to_py_marshaller = item_arg_cache->to_py_marshaller;
576 for (i = 0; list_ != NULL; list_ = g_slist_next (list_), i++) {
580 item_arg.v_pointer = list_->data;
581 _pygi_hash_pointer_to_arg (&item_arg, item_arg_cache->type_tag);
582 py_item = item_to_py_marshaller (state,
587 if (py_item == NULL) {
589 _PyGI_ERROR_PREFIX ("Item %zu: ", i);
593 PyList_SET_ITEM (py_obj, i, py_item);
600 _pygi_marshal_to_py_ghash (PyGIInvokeState *state,
601 PyGICallableCache *callable_cache,
602 PyGIArgCache *arg_cache,
606 GHashTableIter hash_table_iter;
608 PyGIMarshalToPyFunc key_to_py_marshaller;
609 PyGIMarshalToPyFunc value_to_py_marshaller;
611 PyGIArgCache *key_arg_cache;
612 PyGIArgCache *value_arg_cache;
613 PyGIHashCache *hash_cache = (PyGIHashCache *)arg_cache;
616 GIArgument value_arg;
618 PyObject *py_obj = NULL;
620 hash_ = arg->v_pointer;
628 py_obj = PyDict_New ();
632 key_arg_cache = hash_cache->key_cache;
633 key_to_py_marshaller = key_arg_cache->to_py_marshaller;
635 value_arg_cache = hash_cache->value_cache;
636 value_to_py_marshaller = value_arg_cache->to_py_marshaller;
638 g_hash_table_iter_init (&hash_table_iter, hash_);
639 while (g_hash_table_iter_next (&hash_table_iter,
641 &value_arg.v_pointer)) {
647 _pygi_hash_pointer_to_arg (&key_arg, hash_cache->key_cache->type_tag);
648 py_key = key_to_py_marshaller ( state,
653 if (py_key == NULL) {
658 _pygi_hash_pointer_to_arg (&value_arg, hash_cache->value_cache->type_tag);
659 py_value = value_to_py_marshaller ( state,
664 if (py_value == NULL) {
670 retval = PyDict_SetItem (py_obj, py_key, py_value);
673 Py_DECREF (py_value);
685 _pygi_marshal_to_py_gerror (PyGIInvokeState *state,
686 PyGICallableCache *callable_cache,
687 PyGIArgCache *arg_cache,
690 GError *error = arg->v_pointer;
691 PyObject *py_obj = NULL;
693 py_obj = pyglib_error_marshal(&error);
695 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING && error != NULL) {
696 g_error_free (error);
699 if (py_obj != NULL) {
707 _pygi_marshal_to_py_interface_callback (PyGIInvokeState *state,
708 PyGICallableCache *callable_cache,
709 PyGIArgCache *arg_cache,
712 PyObject *py_obj = NULL;
714 PyErr_Format (PyExc_NotImplementedError,
715 "Marshalling a callback to PyObject is not supported");
720 _pygi_marshal_to_py_interface_enum (PyGIInvokeState *state,
721 PyGICallableCache *callable_cache,
722 PyGIArgCache *arg_cache,
725 PyObject *py_obj = NULL;
726 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
727 GIBaseInfo *interface;
730 interface = g_type_info_get_interface (arg_cache->type_info);
731 g_assert (g_base_info_get_type (interface) == GI_INFO_TYPE_ENUM);
733 if (!gi_argument_to_c_long(arg, &c_long,
734 g_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
738 if (iface_cache->g_type == G_TYPE_NONE) {
739 py_obj = PyObject_CallFunction (iface_cache->py_type, "l", c_long);
741 py_obj = pyg_enum_from_gtype (iface_cache->g_type, c_long);
747 _pygi_marshal_to_py_interface_flags (PyGIInvokeState *state,
748 PyGICallableCache *callable_cache,
749 PyGIArgCache *arg_cache,
752 PyObject *py_obj = NULL;
753 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
754 GIBaseInfo *interface;
757 interface = g_type_info_get_interface (arg_cache->type_info);
758 g_assert (g_base_info_get_type (interface) == GI_INFO_TYPE_FLAGS);
760 if (!gi_argument_to_c_long(arg, &c_long,
761 g_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
765 if (iface_cache->g_type == G_TYPE_NONE) {
766 /* An enum with a GType of None is an enum without GType */
768 PyObject *py_type = _pygi_type_import_by_gi_info (iface_cache->interface_info);
769 PyObject *py_args = NULL;
774 py_args = PyTuple_New (1);
775 if (PyTuple_SetItem (py_args, 0, PyLong_FromLong (c_long)) != 0) {
781 py_obj = PyObject_CallFunction (py_type, "l", c_long);
786 py_obj = pyg_flags_from_gtype (iface_cache->g_type, c_long);
793 _pygi_marshal_to_py_interface_struct (PyGIInvokeState *state,
794 PyGICallableCache *callable_cache,
795 PyGIArgCache *arg_cache,
798 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
800 return pygi_marshal_to_py_interface_struct (arg,
801 iface_cache->interface_info,
803 iface_cache->py_type,
805 arg_cache->is_caller_allocates,
806 iface_cache->is_foreign);
810 _pygi_marshal_to_py_interface_interface (PyGIInvokeState *state,
811 PyGICallableCache *callable_cache,
812 PyGIArgCache *arg_cache,
815 PyObject *py_obj = NULL;
817 PyErr_Format (PyExc_NotImplementedError,
818 "Marshalling for this type is not implemented yet");
823 _pygi_marshal_to_py_interface_boxed (PyGIInvokeState *state,
824 PyGICallableCache *callable_cache,
825 PyGIArgCache *arg_cache,
828 PyObject *py_obj = NULL;
830 PyErr_Format (PyExc_NotImplementedError,
831 "Marshalling for this type is not implemented yet");
836 _pygi_marshal_to_py_interface_object (PyGIInvokeState *state,
837 PyGICallableCache *callable_cache,
838 PyGIArgCache *arg_cache,
841 return pygi_marshal_to_py_object(arg, arg_cache->transfer);
845 _pygi_marshal_to_py_interface_union (PyGIInvokeState *state,
846 PyGICallableCache *callable_cache,
847 PyGIArgCache *arg_cache,
850 PyObject *py_obj = NULL;
852 PyErr_Format (PyExc_NotImplementedError,
853 "Marshalling for this type is not implemented yet");
858 pygi_marshal_to_py_object (GIArgument *arg, GITransfer transfer) {
861 if (arg->v_pointer == NULL) {
865 } else if (G_IS_PARAM_SPEC(arg->v_pointer)) {
866 pyobj = pyg_param_spec_new (arg->v_pointer);
867 if (transfer == GI_TRANSFER_EVERYTHING)
868 g_param_spec_unref (arg->v_pointer);
871 pyobj = pygobject_new_full (arg->v_pointer,
872 /*steal=*/ transfer == GI_TRANSFER_EVERYTHING,
880 pygi_marshal_to_py_interface_struct (GIArgument *arg,
881 GIInterfaceInfo *interface_info,
885 gboolean is_allocated,
888 PyObject *py_obj = NULL;
890 if (arg->v_pointer == NULL) {
894 if (g_type_is_a (g_type, G_TYPE_VALUE)) {
895 py_obj = pyg_value_as_pyobject (arg->v_pointer, FALSE);
896 } else if (is_foreign) {
897 py_obj = pygi_struct_foreign_convert_from_g_argument (interface_info,
899 } else if (g_type_is_a (g_type, G_TYPE_BOXED)) {
901 py_obj = _pygi_boxed_new ((PyTypeObject *) py_type,
903 transfer == GI_TRANSFER_EVERYTHING || is_allocated,
905 g_struct_info_get_size(interface_info) : 0);
907 } else if (g_type_is_a (g_type, G_TYPE_POINTER)) {
908 if (py_type == NULL ||
909 !PyType_IsSubtype ((PyTypeObject *) py_type, &PyGIStruct_Type)) {
910 g_warn_if_fail (transfer == GI_TRANSFER_NOTHING);
911 py_obj = pyg_pointer_new (g_type, arg->v_pointer);
913 py_obj = _pygi_struct_new ( (PyTypeObject *) py_type,
915 transfer == GI_TRANSFER_EVERYTHING);
917 } else if (g_type_is_a (g_type, G_TYPE_VARIANT)) {
918 /* Note we do not use transfer for the structs free_on_dealloc because
919 * GLib.Variant overrides __del__ to call "g_variant_unref". */
921 g_variant_ref_sink (arg->v_pointer);
922 py_obj = _pygi_struct_new ((PyTypeObject *) py_type,
926 } else if (g_type == G_TYPE_NONE) {
928 py_obj = _pygi_struct_new ((PyTypeObject *) py_type,
930 transfer == GI_TRANSFER_EVERYTHING);
933 PyErr_Format (PyExc_NotImplementedError,
934 "structure type '%s' is not supported yet",
935 g_type_name (g_type));