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"
36 #include "pygi-argument.h"
39 gi_argument_to_c_long (GIArgument *arg_in,
44 case GI_TYPE_TAG_INT8:
45 *c_long_out = arg_in->v_int8;
47 case GI_TYPE_TAG_UINT8:
48 *c_long_out = arg_in->v_uint8;
50 case GI_TYPE_TAG_INT16:
51 *c_long_out = arg_in->v_int16;
53 case GI_TYPE_TAG_UINT16:
54 *c_long_out = arg_in->v_uint16;
56 case GI_TYPE_TAG_INT32:
57 *c_long_out = arg_in->v_int32;
59 case GI_TYPE_TAG_UINT32:
60 *c_long_out = arg_in->v_uint32;
62 case GI_TYPE_TAG_INT64:
63 *c_long_out = arg_in->v_int64;
65 case GI_TYPE_TAG_UINT64:
66 *c_long_out = arg_in->v_uint64;
69 PyErr_Format (PyExc_TypeError,
70 "Unable to marshal %s to C long",
71 g_type_tag_to_string (type_tag));
77 gi_argument_to_gsize (GIArgument *arg_in,
82 case GI_TYPE_TAG_INT8:
83 *gsize_out = arg_in->v_int8;
85 case GI_TYPE_TAG_UINT8:
86 *gsize_out = arg_in->v_uint8;
88 case GI_TYPE_TAG_INT16:
89 *gsize_out = arg_in->v_int16;
91 case GI_TYPE_TAG_UINT16:
92 *gsize_out = arg_in->v_uint16;
94 case GI_TYPE_TAG_INT32:
95 *gsize_out = arg_in->v_int32;
97 case GI_TYPE_TAG_UINT32:
98 *gsize_out = arg_in->v_uint32;
100 case GI_TYPE_TAG_INT64:
101 *gsize_out = arg_in->v_int64;
103 case GI_TYPE_TAG_UINT64:
104 *gsize_out = arg_in->v_uint64;
107 PyErr_Format (PyExc_TypeError,
108 "Unable to marshal %s to gsize",
109 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 /* NOTE: This will change to interpret pointers as integer values
123 * by using the following:
124 * py_obj = PyLong_FromVoidPtr (arg->v_pointer);
125 * See: https://bugzilla.gnome.org/show_bug.cgi?id=688081
127 py_obj = arg->v_pointer;
137 _pygi_marshal_to_py_unichar (GIArgument *arg)
139 PyObject *py_obj = NULL;
141 /* Preserve the bidirectional mapping between 0 and "" */
142 if (arg->v_uint32 == 0) {
143 py_obj = PYGLIB_PyUnicode_FromString ("");
144 } else if (g_unichar_validate (arg->v_uint32)) {
148 bytes = g_unichar_to_utf8 (arg->v_uint32, utf8);
149 py_obj = PYGLIB_PyUnicode_FromStringAndSize ((char*)utf8, bytes);
151 /* TODO: Convert the error to an exception. */
152 PyErr_Format (PyExc_TypeError,
153 "Invalid unicode codepoint %" G_GUINT32_FORMAT,
161 _pygi_marshal_to_py_utf8 (GIArgument *arg)
163 PyObject *py_obj = NULL;
164 if (arg->v_string == NULL) {
168 py_obj = PYGLIB_PyUnicode_FromString (arg->v_string);
173 _pygi_marshal_to_py_filename (GIArgument *arg)
175 gchar *string = NULL;
176 PyObject *py_obj = NULL;
177 GError *error = NULL;
179 if (arg->v_string == NULL) {
183 string = g_filename_to_utf8 (arg->v_string, -1, NULL, NULL, &error);
184 if (string == NULL) {
185 PyErr_SetString (PyExc_Exception, error->message);
186 /* TODO: Convert the error to an exception. */
190 py_obj = PYGLIB_PyUnicode_FromString (string);
198 * _pygi_marshal_to_py_basic_type:
199 * @arg: The argument to convert to an object.
200 * @type_tag: Type tag for @arg
201 * @transfer: Transfer annotation
203 * Convert the given argument to a Python object. This function
204 * is restricted to simple types that only require the GITypeTag
205 * and GITransfer. For a more complete conversion routine, use:
206 * _pygi_argument_to_object.
208 * Returns: A PyObject representing @arg or NULL if it cannot convert
212 _pygi_marshal_to_py_basic_type (GIArgument *arg,
217 case GI_TYPE_TAG_BOOLEAN:
218 return PyBool_FromLong (arg->v_boolean);
220 case GI_TYPE_TAG_INT8:
221 return PYGLIB_PyLong_FromLong (arg->v_int8);
223 case GI_TYPE_TAG_UINT8:
224 return PYGLIB_PyLong_FromLong (arg->v_uint8);
226 case GI_TYPE_TAG_INT16:
227 return PYGLIB_PyLong_FromLong (arg->v_int16);
229 case GI_TYPE_TAG_UINT16:
230 return PYGLIB_PyLong_FromLong (arg->v_uint16);
232 case GI_TYPE_TAG_INT32:
233 return PYGLIB_PyLong_FromLong (arg->v_int32);
235 case GI_TYPE_TAG_UINT32:
236 return PyLong_FromLongLong (arg->v_uint32);
238 case GI_TYPE_TAG_INT64:
239 return PyLong_FromLongLong (arg->v_int64);
241 case GI_TYPE_TAG_UINT64:
242 return PyLong_FromUnsignedLongLong (arg->v_uint64);
244 case GI_TYPE_TAG_FLOAT:
245 return PyFloat_FromDouble (arg->v_float);
247 case GI_TYPE_TAG_DOUBLE:
248 return PyFloat_FromDouble (arg->v_double);
250 case GI_TYPE_TAG_GTYPE:
251 return pyg_type_wrapper_new ( (GType) arg->v_long);
253 case GI_TYPE_TAG_UNICHAR:
254 return _pygi_marshal_to_py_unichar (arg);
256 case GI_TYPE_TAG_UTF8:
257 return _pygi_marshal_to_py_utf8 (arg);
259 case GI_TYPE_TAG_FILENAME:
260 return _pygi_marshal_to_py_filename (arg);
269 _pygi_marshal_to_py_basic_type_cache_adapter (PyGIInvokeState *state,
270 PyGICallableCache *callable_cache,
271 PyGIArgCache *arg_cache,
274 return _pygi_marshal_to_py_basic_type (arg,
276 arg_cache->transfer);
280 _pygi_marshal_to_py_array (PyGIInvokeState *state,
281 PyGICallableCache *callable_cache,
282 PyGIArgCache *arg_cache,
286 PyObject *py_obj = NULL;
287 PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;
288 gsize processed_items = 0;
290 /* GArrays make it easier to iterate over arrays
291 * with different element sizes but requires that
292 * we allocate a GArray if the argument was a C array
294 if (seq_cache->array_type == GI_ARRAY_TYPE_C) {
296 if (seq_cache->fixed_size >= 0) {
297 g_assert(arg->v_pointer != NULL);
298 len = seq_cache->fixed_size;
299 } else if (seq_cache->is_zero_terminated) {
300 if (arg->v_pointer == NULL) {
302 } else if (seq_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8) {
303 len = strlen (arg->v_pointer);
305 len = g_strv_length ((gchar **)arg->v_pointer);
308 GIArgument *len_arg = state->args[seq_cache->len_arg_index];
310 if (!gi_argument_to_gsize (len_arg,
312 callable_cache->args_cache[seq_cache->len_arg_index]->type_tag)) {
317 array_ = g_array_new (FALSE,
319 seq_cache->item_size);
320 if (array_ == NULL) {
323 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING && arg->v_pointer != NULL)
324 g_free (arg->v_pointer);
329 if (array_->data != NULL)
330 g_free (array_->data);
331 array_->data = arg->v_pointer;
334 array_ = arg->v_pointer;
337 if (seq_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8) {
338 if (arg->v_pointer == NULL) {
339 py_obj = PYGLIB_PyBytes_FromString ("");
341 py_obj = PYGLIB_PyBytes_FromStringAndSize (array_->data, array_->len);
344 if (arg->v_pointer == NULL) {
345 py_obj = PyList_New (0);
350 PyGIMarshalToPyFunc item_to_py_marshaller;
351 PyGIArgCache *item_arg_cache;
353 py_obj = PyList_New (array_->len);
358 item_arg_cache = seq_cache->item_cache;
359 item_to_py_marshaller = item_arg_cache->to_py_marshaller;
361 item_size = g_array_get_element_size (array_);
363 for (i = 0; i < array_->len; i++) {
367 if (seq_cache->array_type == GI_ARRAY_TYPE_PTR_ARRAY) {
368 item_arg.v_pointer = g_ptr_array_index ( ( GPtrArray *)array_, i);
369 } else if (item_arg_cache->type_tag == GI_TYPE_TAG_INTERFACE) {
370 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *) item_arg_cache;
371 gboolean is_gvariant = iface_cache->g_type == G_TYPE_VARIANT;
373 // FIXME: This probably doesn't work with boxed types or gvalues. See fx. _pygi_marshal_from_py_array()
374 switch (g_base_info_get_type (iface_cache->interface_info)) {
375 case GI_INFO_TYPE_STRUCT:
377 g_assert (item_size == sizeof (gpointer));
378 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
379 item_arg.v_pointer = g_variant_ref_sink (g_array_index (array_, gpointer, i));
381 item_arg.v_pointer = g_array_index (array_, gpointer, i);
382 } else if (arg_cache->transfer == GI_TRANSFER_EVERYTHING && !item_arg_cache->is_pointer &&
383 !g_type_is_a (iface_cache->g_type, G_TYPE_BOXED)) {
384 /* array elements are structs */
385 gpointer *_struct = g_malloc (item_size);
386 memcpy (_struct, array_->data + i * item_size,
388 item_arg.v_pointer = _struct;
389 } else if (item_arg_cache->is_pointer)
390 /* array elements are pointers to values */
391 item_arg.v_pointer = g_array_index (array_, gpointer, i);
393 item_arg.v_pointer = array_->data + i * item_size;
396 item_arg.v_pointer = g_array_index (array_, gpointer, i);
400 memcpy (&item_arg, array_->data + i * item_size, item_size);
403 py_item = item_to_py_marshaller ( state,
408 if (py_item == NULL) {
411 if (seq_cache->array_type == GI_ARRAY_TYPE_C)
412 g_array_unref (array_);
416 PyList_SET_ITEM (py_obj, i, py_item);
422 if (seq_cache->array_type == GI_ARRAY_TYPE_C)
423 g_array_free (array_, FALSE);
428 if (seq_cache->array_type == GI_ARRAY_TYPE_C) {
429 g_array_free (array_, arg_cache->transfer == GI_TRANSFER_EVERYTHING);
431 /* clean up unprocessed items */
432 if (seq_cache->item_cache->to_py_cleanup != NULL) {
434 PyGIMarshalCleanupFunc cleanup_func = seq_cache->item_cache->to_py_cleanup;
435 for (j = processed_items; j < array_->len; j++) {
437 seq_cache->item_cache,
438 g_array_index (array_, gpointer, j),
443 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
444 g_array_free (array_, TRUE);
451 _pygi_marshal_to_py_glist (PyGIInvokeState *state,
452 PyGICallableCache *callable_cache,
453 PyGIArgCache *arg_cache,
460 PyGIMarshalToPyFunc item_to_py_marshaller;
461 PyGIArgCache *item_arg_cache;
462 PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;
464 PyObject *py_obj = NULL;
466 list_ = arg->v_pointer;
467 length = g_list_length (list_);
469 py_obj = PyList_New (length);
473 item_arg_cache = seq_cache->item_cache;
474 item_to_py_marshaller = item_arg_cache->to_py_marshaller;
476 for (i = 0; list_ != NULL; list_ = g_list_next (list_), i++) {
480 item_arg.v_pointer = list_->data;
481 _pygi_hash_pointer_to_arg (&item_arg, item_arg_cache->type_tag);
482 py_item = item_to_py_marshaller (state,
487 if (py_item == NULL) {
489 _PyGI_ERROR_PREFIX ("Item %zu: ", i);
493 PyList_SET_ITEM (py_obj, i, py_item);
500 _pygi_marshal_to_py_gslist (PyGIInvokeState *state,
501 PyGICallableCache *callable_cache,
502 PyGIArgCache *arg_cache,
509 PyGIMarshalToPyFunc item_to_py_marshaller;
510 PyGIArgCache *item_arg_cache;
511 PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;
513 PyObject *py_obj = NULL;
515 list_ = arg->v_pointer;
516 length = g_slist_length (list_);
518 py_obj = PyList_New (length);
522 item_arg_cache = seq_cache->item_cache;
523 item_to_py_marshaller = item_arg_cache->to_py_marshaller;
525 for (i = 0; list_ != NULL; list_ = g_slist_next (list_), i++) {
529 item_arg.v_pointer = list_->data;
530 _pygi_hash_pointer_to_arg (&item_arg, item_arg_cache->type_tag);
531 py_item = item_to_py_marshaller (state,
536 if (py_item == NULL) {
538 _PyGI_ERROR_PREFIX ("Item %zu: ", i);
542 PyList_SET_ITEM (py_obj, i, py_item);
549 _pygi_marshal_to_py_ghash (PyGIInvokeState *state,
550 PyGICallableCache *callable_cache,
551 PyGIArgCache *arg_cache,
555 GHashTableIter hash_table_iter;
557 PyGIMarshalToPyFunc key_to_py_marshaller;
558 PyGIMarshalToPyFunc value_to_py_marshaller;
560 PyGIArgCache *key_arg_cache;
561 PyGIArgCache *value_arg_cache;
562 PyGIHashCache *hash_cache = (PyGIHashCache *)arg_cache;
565 GIArgument value_arg;
567 PyObject *py_obj = NULL;
569 hash_ = arg->v_pointer;
577 py_obj = PyDict_New ();
581 key_arg_cache = hash_cache->key_cache;
582 key_to_py_marshaller = key_arg_cache->to_py_marshaller;
584 value_arg_cache = hash_cache->value_cache;
585 value_to_py_marshaller = value_arg_cache->to_py_marshaller;
587 g_hash_table_iter_init (&hash_table_iter, hash_);
588 while (g_hash_table_iter_next (&hash_table_iter,
590 &value_arg.v_pointer)) {
596 _pygi_hash_pointer_to_arg (&key_arg, hash_cache->key_cache->type_tag);
597 py_key = key_to_py_marshaller ( state,
602 if (py_key == NULL) {
607 _pygi_hash_pointer_to_arg (&value_arg, hash_cache->value_cache->type_tag);
608 py_value = value_to_py_marshaller ( state,
613 if (py_value == NULL) {
619 retval = PyDict_SetItem (py_obj, py_key, py_value);
622 Py_DECREF (py_value);
634 _pygi_marshal_to_py_gerror (PyGIInvokeState *state,
635 PyGICallableCache *callable_cache,
636 PyGIArgCache *arg_cache,
639 GError *error = arg->v_pointer;
640 PyObject *py_obj = NULL;
642 py_obj = pyglib_error_marshal(&error);
644 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING && error != NULL) {
645 g_error_free (error);
648 if (py_obj != NULL) {
656 _pygi_marshal_to_py_interface_callback (PyGIInvokeState *state,
657 PyGICallableCache *callable_cache,
658 PyGIArgCache *arg_cache,
661 PyObject *py_obj = NULL;
663 PyErr_Format (PyExc_NotImplementedError,
664 "Marshalling a callback to PyObject is not supported");
669 _pygi_marshal_to_py_interface_enum (PyGIInvokeState *state,
670 PyGICallableCache *callable_cache,
671 PyGIArgCache *arg_cache,
674 PyObject *py_obj = NULL;
675 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
676 GIBaseInfo *interface;
679 interface = g_type_info_get_interface (arg_cache->type_info);
680 g_assert (g_base_info_get_type (interface) == GI_INFO_TYPE_ENUM);
682 if (!gi_argument_to_c_long(arg, &c_long,
683 g_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
687 if (iface_cache->g_type == G_TYPE_NONE) {
688 py_obj = PyObject_CallFunction (iface_cache->py_type, "l", c_long);
690 py_obj = pyg_enum_from_gtype (iface_cache->g_type, c_long);
692 g_base_info_unref (interface);
697 _pygi_marshal_to_py_interface_flags (PyGIInvokeState *state,
698 PyGICallableCache *callable_cache,
699 PyGIArgCache *arg_cache,
702 PyObject *py_obj = NULL;
703 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
704 GIBaseInfo *interface;
707 interface = g_type_info_get_interface (arg_cache->type_info);
708 g_assert (g_base_info_get_type (interface) == GI_INFO_TYPE_FLAGS);
710 if (!gi_argument_to_c_long(arg, &c_long,
711 g_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
712 g_base_info_unref (interface);
716 g_base_info_unref (interface);
717 if (iface_cache->g_type == G_TYPE_NONE) {
718 /* An enum with a GType of None is an enum without GType */
720 PyObject *py_type = _pygi_type_import_by_gi_info (iface_cache->interface_info);
721 PyObject *py_args = NULL;
726 py_args = PyTuple_New (1);
727 if (PyTuple_SetItem (py_args, 0, PyLong_FromLong (c_long)) != 0) {
733 py_obj = PyObject_CallFunction (py_type, "l", c_long);
738 py_obj = pyg_flags_from_gtype (iface_cache->g_type, c_long);
745 _pygi_marshal_to_py_interface_struct_cache_adapter (PyGIInvokeState *state,
746 PyGICallableCache *callable_cache,
747 PyGIArgCache *arg_cache,
750 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
752 return _pygi_marshal_to_py_interface_struct (arg,
753 iface_cache->interface_info,
755 iface_cache->py_type,
757 arg_cache->is_caller_allocates,
758 iface_cache->is_foreign);
762 _pygi_marshal_to_py_interface_interface (PyGIInvokeState *state,
763 PyGICallableCache *callable_cache,
764 PyGIArgCache *arg_cache,
767 PyObject *py_obj = NULL;
769 PyErr_Format (PyExc_NotImplementedError,
770 "Marshalling for this type is not implemented yet");
775 _pygi_marshal_to_py_interface_boxed (PyGIInvokeState *state,
776 PyGICallableCache *callable_cache,
777 PyGIArgCache *arg_cache,
780 PyObject *py_obj = NULL;
782 PyErr_Format (PyExc_NotImplementedError,
783 "Marshalling for this type is not implemented yet");
788 _pygi_marshal_to_py_interface_object_cache_adapter (PyGIInvokeState *state,
789 PyGICallableCache *callable_cache,
790 PyGIArgCache *arg_cache,
793 return _pygi_marshal_to_py_object(arg, arg_cache->transfer);
797 _pygi_marshal_to_py_interface_union (PyGIInvokeState *state,
798 PyGICallableCache *callable_cache,
799 PyGIArgCache *arg_cache,
802 PyObject *py_obj = NULL;
804 PyErr_Format (PyExc_NotImplementedError,
805 "Marshalling for this type is not implemented yet");
810 _pygi_marshal_to_py_object (GIArgument *arg, GITransfer transfer) {
813 if (arg->v_pointer == NULL) {
817 } else if (G_IS_PARAM_SPEC(arg->v_pointer)) {
818 pyobj = pyg_param_spec_new (arg->v_pointer);
819 if (transfer == GI_TRANSFER_EVERYTHING)
820 g_param_spec_unref (arg->v_pointer);
823 pyobj = pygobject_new_full (arg->v_pointer,
824 /*steal=*/ transfer == GI_TRANSFER_EVERYTHING,
832 _pygi_marshal_to_py_interface_struct (GIArgument *arg,
833 GIInterfaceInfo *interface_info,
837 gboolean is_allocated,
840 PyObject *py_obj = NULL;
842 if (arg->v_pointer == NULL) {
846 if (g_type_is_a (g_type, G_TYPE_VALUE)) {
847 py_obj = pyg_value_as_pyobject (arg->v_pointer, FALSE);
848 } else if (is_foreign) {
849 py_obj = pygi_struct_foreign_convert_from_g_argument (interface_info,
851 } else if (g_type_is_a (g_type, G_TYPE_BOXED)) {
853 py_obj = _pygi_boxed_new ((PyTypeObject *) py_type,
855 transfer == GI_TRANSFER_EVERYTHING || is_allocated,
857 g_struct_info_get_size(interface_info) : 0);
859 } else if (g_type_is_a (g_type, G_TYPE_POINTER)) {
860 if (py_type == NULL ||
861 !PyType_IsSubtype ((PyTypeObject *) py_type, &PyGIStruct_Type)) {
862 g_warn_if_fail (transfer == GI_TRANSFER_NOTHING);
863 py_obj = pyg_pointer_new (g_type, arg->v_pointer);
865 py_obj = _pygi_struct_new ( (PyTypeObject *) py_type,
867 transfer == GI_TRANSFER_EVERYTHING);
869 } else if (g_type_is_a (g_type, G_TYPE_VARIANT)) {
870 /* Note we do not use transfer for the structs free_on_dealloc because
871 * GLib.Variant overrides __del__ to call "g_variant_unref". */
873 g_variant_ref_sink (arg->v_pointer);
874 py_obj = _pygi_struct_new ((PyTypeObject *) py_type,
878 } else if (g_type == G_TYPE_NONE) {
880 py_obj = _pygi_struct_new ((PyTypeObject *) py_type,
882 transfer == GI_TRANSFER_EVERYTHING);
885 PyErr_Format (PyExc_NotImplementedError,
886 "structure type '%s' is not supported yet",
887 g_type_name (g_type));