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 to convert PyObjects to C types.
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"
28 #include <pygobject.h>
29 #include <pyglib-python-compat.h>
31 #include "pygi-cache.h"
32 #include "pygi-marshal-cleanup.h"
33 #include "pygi-marshal-from-py.h"
36 * _is_union_member - check to see if the py_arg is actually a member of the
40 _is_union_member (PyGIInterfaceCache *iface_cache, PyObject *py_arg) {
43 GIUnionInfo *union_info;
45 gboolean is_member = FALSE;
47 info_type = g_base_info_get_type (iface_cache->interface_info);
49 if (info_type != GI_INFO_TYPE_UNION)
52 union_info = (GIUnionInfo *) iface_cache->interface_info;
53 n_fields = g_union_info_get_n_fields (union_info);
55 for (i = 0; i < n_fields; i++) {
56 GIFieldInfo *field_info;
57 GITypeInfo *field_type_info;
59 field_info = g_union_info_get_field (union_info, i);
60 field_type_info = g_field_info_get_type (field_info);
62 /* we can only check if the members are interfaces */
63 if (g_type_info_get_tag (field_type_info) == GI_TYPE_TAG_INTERFACE) {
64 GIInterfaceInfo *field_iface_info;
67 field_iface_info = g_type_info_get_interface (field_type_info);
68 py_type = _pygi_type_import_by_gi_info ((GIBaseInfo *) field_iface_info);
70 if (py_type != NULL && PyObject_IsInstance (py_arg, py_type)) {
75 g_base_info_unref ( ( GIBaseInfo *) field_iface_info);
78 g_base_info_unref ( ( GIBaseInfo *) field_type_info);
79 g_base_info_unref ( ( GIBaseInfo *) field_info);
89 _pygi_marshal_from_py_void (PyGIInvokeState *state,
90 PyGICallableCache *callable_cache,
91 PyGIArgCache *arg_cache,
95 g_warn_if_fail (arg_cache->transfer == GI_TRANSFER_NOTHING);
97 arg->v_pointer = py_arg;
103 _pygi_marshal_from_py_boolean (PyGIInvokeState *state,
104 PyGICallableCache *callable_cache,
105 PyGIArgCache *arg_cache,
109 arg->v_boolean = PyObject_IsTrue (py_arg);
115 _pygi_marshal_from_py_int8 (PyGIInvokeState *state,
116 PyGICallableCache *callable_cache,
117 PyGIArgCache *arg_cache,
124 if (!PyNumber_Check (py_arg)) {
125 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
126 py_arg->ob_type->tp_name);
130 py_long = PYGLIB_PyNumber_Long (py_arg);
134 long_ = PYGLIB_PyLong_AsLong (py_long);
137 if (PyErr_Occurred ()) {
139 PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, -128, 127);
143 if (long_ < -128 || long_ > 127) {
144 PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, -128, 127);
154 _pygi_marshal_from_py_uint8 (PyGIInvokeState *state,
155 PyGICallableCache *callable_cache,
156 PyGIArgCache *arg_cache,
162 if (PYGLIB_PyBytes_Check (py_arg)) {
164 if (PYGLIB_PyBytes_Size (py_arg) != 1) {
165 PyErr_Format (PyExc_TypeError, "Must be a single character");
169 long_ = (unsigned char)(PYGLIB_PyBytes_AsString (py_arg)[0]);
171 } else if (PyNumber_Check (py_arg)) {
173 py_long = PYGLIB_PyNumber_Long (py_arg);
177 long_ = PYGLIB_PyLong_AsLong (py_long);
180 if (PyErr_Occurred ()) {
183 PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, 0, 255);
187 PyErr_Format (PyExc_TypeError, "Must be number or single byte string, not %s",
188 py_arg->ob_type->tp_name);
192 if (long_ < 0 || long_ > 255) {
193 PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, 0, 255);
197 arg->v_uint8 = long_;
203 _pygi_marshal_from_py_int16 (PyGIInvokeState *state,
204 PyGICallableCache *callable_cache,
205 PyGIArgCache *arg_cache,
212 if (!PyNumber_Check (py_arg)) {
213 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
214 py_arg->ob_type->tp_name);
218 py_long = PYGLIB_PyNumber_Long (py_arg);
222 long_ = PYGLIB_PyLong_AsLong (py_long);
225 if (PyErr_Occurred ()) {
227 PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, -32768, 32767);
231 if (long_ < -32768 || long_ > 32767) {
232 PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, -32768, 32767);
236 arg->v_int16 = long_;
242 _pygi_marshal_from_py_uint16 (PyGIInvokeState *state,
243 PyGICallableCache *callable_cache,
244 PyGIArgCache *arg_cache,
251 if (!PyNumber_Check (py_arg)) {
252 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
253 py_arg->ob_type->tp_name);
257 py_long = PYGLIB_PyNumber_Long (py_arg);
261 long_ = PYGLIB_PyLong_AsLong (py_long);
264 if (PyErr_Occurred ()) {
266 PyErr_Format (PyExc_ValueError, "%li not in range %d to %d", long_, 0, 65535);
270 if (long_ < 0 || long_ > 65535) {
271 PyErr_Format (PyExc_ValueError, "%li not in range %d to %d", long_, 0, 65535);
275 arg->v_uint16 = long_;
281 _pygi_marshal_from_py_int32 (PyGIInvokeState *state,
282 PyGICallableCache *callable_cache,
283 PyGIArgCache *arg_cache,
290 if (!PyNumber_Check (py_arg)) {
291 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
292 py_arg->ob_type->tp_name);
296 py_long = PYGLIB_PyNumber_Long (py_arg);
300 long_ = PYGLIB_PyLong_AsLong (py_long);
303 if (PyErr_Occurred ()) {
305 PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, G_MININT32, G_MAXINT32);
309 if (long_ < G_MININT32 || long_ > G_MAXINT32) {
310 PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, G_MININT32, G_MAXINT32);
314 arg->v_int32 = long_;
320 _pygi_marshal_from_py_uint32 (PyGIInvokeState *state,
321 PyGICallableCache *callable_cache,
322 PyGIArgCache *arg_cache,
329 if (!PyNumber_Check (py_arg)) {
330 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
331 py_arg->ob_type->tp_name);
335 py_long = PYGLIB_PyNumber_Long (py_arg);
339 #if PY_VERSION_HEX < 0x03000000
340 if (PyInt_Check (py_long))
341 long_ = PyInt_AsLong (py_long);
344 long_ = PyLong_AsLongLong (py_long);
348 if (PyErr_Occurred ()) {
350 PyErr_Format (PyExc_ValueError, "%lli not in range %i to %u", long_, 0, G_MAXUINT32);
354 if (long_ < 0 || long_ > G_MAXUINT32) {
355 PyErr_Format (PyExc_ValueError, "%lli not in range %i to %u", long_, 0, G_MAXUINT32);
359 arg->v_uint32 = long_;
365 _pygi_marshal_from_py_int64 (PyGIInvokeState *state,
366 PyGICallableCache *callable_cache,
367 PyGIArgCache *arg_cache,
374 if (!PyNumber_Check (py_arg)) {
375 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
376 py_arg->ob_type->tp_name);
380 py_long = PYGLIB_PyNumber_Long (py_arg);
384 #if PY_VERSION_HEX < 0x03000000
385 if (PyInt_Check (py_long))
386 long_ = PyInt_AS_LONG (py_long);
389 long_ = PyLong_AsLongLong (py_long);
393 if (PyErr_Occurred ()) {
394 /* OverflowError occured but range errors should be returned as ValueError */
400 py_str = PyObject_Str (py_long);
402 if (PyUnicode_Check (py_str)) {
403 PyObject *py_bytes = PyUnicode_AsUTF8String (py_str);
404 if (py_bytes == NULL)
407 long_str = g_strdup (PYGLIB_PyBytes_AsString (py_bytes));
408 if (long_str == NULL) {
413 Py_DECREF (py_bytes);
415 long_str = g_strdup (PYGLIB_PyBytes_AsString(py_str));
419 PyErr_Format (PyExc_ValueError, "%s not in range %ld to %ld",
420 long_str, G_MININT64, G_MAXINT64);
426 if (long_ < G_MININT64 || long_ > G_MAXINT64) {
427 PyErr_Format (PyExc_ValueError, "%lld not in range %ld to %ld", long_, G_MININT64, G_MAXINT64);
431 arg->v_int64 = long_;
437 _pygi_marshal_from_py_uint64 (PyGIInvokeState *state,
438 PyGICallableCache *callable_cache,
439 PyGIArgCache *arg_cache,
446 if (!PyNumber_Check (py_arg)) {
447 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
448 py_arg->ob_type->tp_name);
452 py_long = PYGLIB_PyNumber_Long (py_arg);
456 #if PY_VERSION_HEX < 0x03000000
457 if (PyInt_Check (py_long)) {
458 long long_ = PyInt_AsLong (py_long);
460 PyErr_Format (PyExc_ValueError, "%ld not in range %d to %lu",
461 long_, 0, G_MAXUINT64);
467 ulong_ = PyLong_AsUnsignedLongLong (py_long);
471 if (PyErr_Occurred ()) {
472 /* OverflowError occured but range errors should be returned as ValueError */
478 py_str = PyObject_Str (py_long);
480 if (PyUnicode_Check (py_str)) {
481 PyObject *py_bytes = PyUnicode_AsUTF8String (py_str);
482 if (py_bytes == NULL)
485 long_str = g_strdup (PYGLIB_PyBytes_AsString (py_bytes));
486 if (long_str == NULL) {
491 Py_DECREF (py_bytes);
493 long_str = g_strdup (PYGLIB_PyBytes_AsString (py_str));
498 PyErr_Format (PyExc_ValueError, "%s not in range %d to %lu",
499 long_str, 0, G_MAXUINT64);
505 if (ulong_ > G_MAXUINT64) {
506 PyErr_Format (PyExc_ValueError, "%lu not in range %d to %lu", ulong_, 0, G_MAXUINT64);
510 arg->v_uint64 = ulong_;
516 _pygi_marshal_from_py_float (PyGIInvokeState *state,
517 PyGICallableCache *callable_cache,
518 PyGIArgCache *arg_cache,
525 if (!PyNumber_Check (py_arg)) {
526 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
527 py_arg->ob_type->tp_name);
531 py_float = PyNumber_Float (py_arg);
535 double_ = PyFloat_AsDouble (py_float);
536 Py_DECREF (py_float);
538 if (PyErr_Occurred ()) {
540 PyErr_Format (PyExc_ValueError, "%f not in range %f to %f", double_, -G_MAXFLOAT, G_MAXFLOAT);
544 if (double_ < -G_MAXFLOAT || double_ > G_MAXFLOAT) {
545 PyErr_Format (PyExc_ValueError, "%f not in range %f to %f", double_, -G_MAXFLOAT, G_MAXFLOAT);
549 arg->v_float = double_;
555 _pygi_marshal_from_py_double (PyGIInvokeState *state,
556 PyGICallableCache *callable_cache,
557 PyGIArgCache *arg_cache,
564 if (!PyNumber_Check (py_arg)) {
565 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
566 py_arg->ob_type->tp_name);
570 py_float = PyNumber_Float (py_arg);
574 double_ = PyFloat_AsDouble (py_float);
575 Py_DECREF (py_float);
577 if (PyErr_Occurred ()) {
579 PyErr_Format (PyExc_ValueError, "%f not in range %f to %f", double_, -G_MAXDOUBLE, G_MAXDOUBLE);
583 if (double_ < -G_MAXDOUBLE || double_ > G_MAXDOUBLE) {
584 PyErr_Format (PyExc_ValueError, "%f not in range %f to %f", double_, -G_MAXDOUBLE, G_MAXDOUBLE);
588 arg->v_double = double_;
594 _pygi_marshal_from_py_unichar (PyGIInvokeState *state,
595 PyGICallableCache *callable_cache,
596 PyGIArgCache *arg_cache,
603 if (PyUnicode_Check (py_arg)) {
606 size = PyUnicode_GET_SIZE (py_arg);
607 py_bytes = PyUnicode_AsUTF8String (py_arg);
608 string_ = strdup(PYGLIB_PyBytes_AsString (py_bytes));
609 Py_DECREF (py_bytes);
611 #if PY_VERSION_HEX < 0x03000000
612 } else if (PyString_Check (py_arg)) {
613 PyObject *pyuni = PyUnicode_FromEncodedObject (py_arg, "UTF-8", "strict");
617 size = PyUnicode_GET_SIZE (pyuni);
618 string_ = g_strdup (PyString_AsString(py_arg));
622 PyErr_Format (PyExc_TypeError, "Must be string, not %s",
623 py_arg->ob_type->tp_name);
628 PyErr_Format (PyExc_TypeError, "Must be a one character string, not %ld characters",
634 arg->v_uint32 = g_utf8_get_char (string_);
640 _pygi_marshal_from_py_gtype (PyGIInvokeState *state,
641 PyGICallableCache *callable_cache,
642 PyGIArgCache *arg_cache,
646 long type_ = pyg_type_from_object (py_arg);
649 PyErr_Format (PyExc_TypeError, "Must be gobject.GType, not %s",
650 py_arg->ob_type->tp_name);
658 _pygi_marshal_from_py_utf8 (PyGIInvokeState *state,
659 PyGICallableCache *callable_cache,
660 PyGIArgCache *arg_cache,
666 if (py_arg == Py_None) {
667 arg->v_pointer = NULL;
671 if (PyUnicode_Check (py_arg)) {
672 PyObject *pystr_obj = PyUnicode_AsUTF8String (py_arg);
676 string_ = g_strdup (PYGLIB_PyBytes_AsString (pystr_obj));
677 Py_DECREF (pystr_obj);
679 #if PY_VERSION_HEX < 0x03000000
680 else if (PyString_Check (py_arg)) {
681 string_ = g_strdup (PyString_AsString (py_arg));
685 PyErr_Format (PyExc_TypeError, "Must be string, not %s",
686 py_arg->ob_type->tp_name);
690 arg->v_string = string_;
695 _pygi_marshal_from_py_filename (PyGIInvokeState *state,
696 PyGICallableCache *callable_cache,
697 PyGIArgCache *arg_cache,
702 GError *error = NULL;
704 if (PyUnicode_Check (py_arg)) {
705 PyObject *pystr_obj = PyUnicode_AsUTF8String (py_arg);
709 string_ = g_strdup (PYGLIB_PyBytes_AsString (pystr_obj));
710 Py_DECREF (pystr_obj);
712 #if PY_VERSION_HEX < 0x03000000
713 else if (PyString_Check (py_arg)) {
714 string_ = g_strdup (PyString_AsString (py_arg));
718 PyErr_Format (PyExc_TypeError, "Must be string, not %s",
719 py_arg->ob_type->tp_name);
723 arg->v_string = g_filename_from_utf8 (string_, -1, NULL, NULL, &error);
726 if (arg->v_string == NULL) {
727 PyErr_SetString (PyExc_Exception, error->message);
728 g_error_free (error);
729 /* TODO: Convert the error to an exception. */
737 _pygi_marshal_from_py_array (PyGIInvokeState *state,
738 PyGICallableCache *callable_cache,
739 PyGIArgCache *arg_cache,
743 PyGIMarshalFromPyFunc from_py_marshaller;
747 gboolean is_ptr_array;
748 GArray *array_ = NULL;
749 PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache;
752 if (py_arg == Py_None) {
753 arg->v_pointer = NULL;
757 if (!PySequence_Check (py_arg)) {
758 PyErr_Format (PyExc_TypeError, "Must be sequence, not %s",
759 py_arg->ob_type->tp_name);
763 length = PySequence_Length (py_arg);
767 if (sequence_cache->fixed_size >= 0 &&
768 sequence_cache->fixed_size != length) {
769 PyErr_Format (PyExc_ValueError, "Must contain %zd items, not %zd",
770 sequence_cache->fixed_size, length);
775 item_size = sequence_cache->item_size;
776 is_ptr_array = (sequence_cache->array_type == GI_ARRAY_TYPE_PTR_ARRAY);
778 array_ = (GArray *)g_ptr_array_new ();
780 array_ = g_array_sized_new (sequence_cache->is_zero_terminated,
786 if (array_ == NULL) {
791 if (sequence_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8 &&
792 PYGLIB_PyBytes_Check (py_arg)) {
793 memcpy(array_->data, PYGLIB_PyBytes_AsString (py_arg), length);
794 if (sequence_cache->is_zero_terminated) {
795 /* If array_ has been created with zero_termination, space for the
796 * terminator is properly allocated, so we're not off-by-one here. */
797 array_->data[length] = '\0';
802 from_py_marshaller = sequence_cache->item_cache->from_py_marshaller;
803 for (i = 0; i < length; i++) {
805 PyObject *py_item = PySequence_GetItem (py_arg, i);
809 if (!from_py_marshaller ( state,
811 sequence_cache->item_cache,
816 /* FIXME: it is much more efficent to have seperate marshaller
817 * for ptr arrays than doing the evaluation
818 * and casting each loop iteration
821 g_ptr_array_add((GPtrArray *)array_, item.v_pointer);
822 } else if (sequence_cache->item_cache->type_tag == GI_TYPE_TAG_INTERFACE) {
823 PyGIInterfaceCache *item_iface_cache = (PyGIInterfaceCache *) sequence_cache->item_cache;
824 GIBaseInfo *base_info = (GIBaseInfo *) item_iface_cache->interface_info;
825 GIInfoType info_type = g_base_info_get_type (base_info);
828 case GI_INFO_TYPE_UNION:
829 case GI_INFO_TYPE_STRUCT:
831 PyGIArgCache *item_arg_cache = (PyGIArgCache *)item_iface_cache;
832 PyGIMarshalCleanupFunc from_py_cleanup = item_arg_cache->from_py_cleanup;
833 gboolean is_boxed = g_type_is_a (item_iface_cache->g_type, G_TYPE_BOXED);
834 gboolean is_gvalue = item_iface_cache->g_type == G_TYPE_VALUE;
835 gboolean is_gvariant = item_iface_cache->g_type == G_TYPE_VARIANT;
838 /* Item size will always be that of a pointer,
839 * since GVariants are opaque hence always passed by ref */
840 g_assert (item_size == sizeof (item.v_pointer));
841 g_array_insert_val (array_, i, item.v_pointer);
842 } else if (is_gvalue) {
843 GValue* dest = (GValue*) (array_->data + (i * item_size));
844 memset (dest, 0, item_size);
845 if (item.v_pointer != NULL) {
846 g_value_init (dest, G_VALUE_TYPE ((GValue*) item.v_pointer));
847 g_value_copy ((GValue*) item.v_pointer, dest);
850 if (from_py_cleanup) {
851 from_py_cleanup (state, item_arg_cache, item.v_pointer, TRUE);
852 /* we freed the original copy already, the new one is a
853 * struct in an array. _pygi_marshal_cleanup_from_py_array()
854 * must not free it again */
855 item_arg_cache->from_py_cleanup = NULL;
857 } else if (!is_boxed) {
858 /* HACK: Gdk.Atom is merely an integer wrapped in a pointer,
859 * so we must not dereference it; just copy the pointer
860 * value, and don't attempt to free it. TODO: find out
861 * if there are other data types with similar behaviour
863 if (g_strcmp0 (item_iface_cache->type_name, "Gdk.Atom") == 0) {
864 g_assert (item_size == sizeof (item.v_pointer));
865 memcpy (array_->data + (i * item_size), &item.v_pointer, item_size);
867 memcpy (array_->data + (i * item_size), item.v_pointer, item_size);
870 from_py_cleanup (state, item_arg_cache, item.v_pointer, TRUE);
873 g_array_insert_val (array_, i, item);
878 g_array_insert_val (array_, i, item);
881 g_array_insert_val (array_, i, item);
885 if (sequence_cache->item_cache->from_py_cleanup != NULL) {
887 PyGIMarshalCleanupFunc cleanup_func =
888 sequence_cache->item_cache->from_py_cleanup;
890 for(j = 0; j < i; j++) {
892 sequence_cache->item_cache,
893 g_array_index (array_, gpointer, j),
899 g_ptr_array_free ( ( GPtrArray *)array_, TRUE);
901 g_array_free (array_, TRUE);
902 _PyGI_ERROR_PREFIX ("Item %i: ", i);
907 if (sequence_cache->len_arg_index >= 0) {
908 /* we have an child arg to handle */
909 PyGIArgCache *child_cache =
910 callable_cache->args_cache[sequence_cache->len_arg_index];
912 if (child_cache->direction == PYGI_DIRECTION_BIDIRECTIONAL) {
913 gint *len_arg = (gint *)state->in_args[child_cache->c_arg_index].v_pointer;
914 /* if we are not setup yet just set the in arg */
916 state->in_args[child_cache->c_arg_index].v_long = length;
920 state->in_args[child_cache->c_arg_index].v_long = length;
924 if (sequence_cache->array_type == GI_ARRAY_TYPE_C) {
925 arg->v_pointer = array_->data;
926 g_array_free (array_, FALSE);
928 arg->v_pointer = array_;
935 _pygi_marshal_from_py_glist (PyGIInvokeState *state,
936 PyGICallableCache *callable_cache,
937 PyGIArgCache *arg_cache,
941 PyGIMarshalFromPyFunc from_py_marshaller;
945 PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache;
948 if (py_arg == Py_None) {
949 arg->v_pointer = NULL;
953 if (!PySequence_Check (py_arg)) {
954 PyErr_Format (PyExc_TypeError, "Must be sequence, not %s",
955 py_arg->ob_type->tp_name);
959 length = PySequence_Length (py_arg);
963 if (sequence_cache->fixed_size >= 0 &&
964 sequence_cache->fixed_size != length) {
965 PyErr_Format (PyExc_ValueError, "Must contain %zd items, not %zd",
966 sequence_cache->fixed_size, length);
971 from_py_marshaller = sequence_cache->item_cache->from_py_marshaller;
972 for (i = 0; i < length; i++) {
974 PyObject *py_item = PySequence_GetItem (py_arg, i);
978 if (!from_py_marshaller ( state,
980 sequence_cache->item_cache,
985 list_ = g_list_prepend (list_, item.v_pointer);
988 /* FIXME: clean up list
989 if (sequence_cache->item_cache->from_py_cleanup != NULL) {
990 PyGIMarshalCleanupFunc cleanup = sequence_cache->item_cache->from_py_cleanup;
994 _PyGI_ERROR_PREFIX ("Item %i: ", i);
998 arg->v_pointer = g_list_reverse (list_);
1003 _pygi_marshal_from_py_gslist (PyGIInvokeState *state,
1004 PyGICallableCache *callable_cache,
1005 PyGIArgCache *arg_cache,
1009 PyGIMarshalFromPyFunc from_py_marshaller;
1012 GSList *list_ = NULL;
1013 PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache;
1015 if (py_arg == Py_None) {
1016 arg->v_pointer = NULL;
1020 if (!PySequence_Check (py_arg)) {
1021 PyErr_Format (PyExc_TypeError, "Must be sequence, not %s",
1022 py_arg->ob_type->tp_name);
1026 length = PySequence_Length (py_arg);
1030 if (sequence_cache->fixed_size >= 0 &&
1031 sequence_cache->fixed_size != length) {
1032 PyErr_Format (PyExc_ValueError, "Must contain %zd items, not %zd",
1033 sequence_cache->fixed_size, length);
1038 from_py_marshaller = sequence_cache->item_cache->from_py_marshaller;
1039 for (i = 0; i < length; i++) {
1041 PyObject *py_item = PySequence_GetItem (py_arg, i);
1042 if (py_item == NULL)
1045 if (!from_py_marshaller ( state,
1047 sequence_cache->item_cache,
1052 list_ = g_slist_prepend (list_, item.v_pointer);
1055 /* FIXME: Clean up list
1056 if (sequence_cache->item_cache->from_py_cleanup != NULL) {
1057 PyGIMarshalCleanupFunc cleanup = sequence_cache->item_cache->from_py_cleanup;
1061 g_slist_free (list_);
1062 _PyGI_ERROR_PREFIX ("Item %i: ", i);
1066 arg->v_pointer = g_slist_reverse (list_);
1071 _pygi_arg_to_hash_pointer (const GIArgument *arg,
1075 case GI_TYPE_TAG_INT32:
1076 return GINT_TO_POINTER(arg->v_int32);
1077 case GI_TYPE_TAG_UTF8:
1078 case GI_TYPE_TAG_FILENAME:
1079 case GI_TYPE_TAG_INTERFACE:
1080 return arg->v_pointer;
1082 g_critical("Unsupported type %s", g_type_tag_to_string(type_tag));
1083 return arg->v_pointer;
1088 _pygi_marshal_from_py_ghash (PyGIInvokeState *state,
1089 PyGICallableCache *callable_cache,
1090 PyGIArgCache *arg_cache,
1094 PyGIMarshalFromPyFunc key_from_py_marshaller;
1095 PyGIMarshalFromPyFunc value_from_py_marshaller;
1099 PyObject *py_keys, *py_values;
1101 GHashFunc hash_func;
1102 GEqualFunc equal_func;
1104 GHashTable *hash_ = NULL;
1105 PyGIHashCache *hash_cache = (PyGIHashCache *)arg_cache;
1107 if (py_arg == Py_None) {
1108 arg->v_pointer = NULL;
1112 py_keys = PyMapping_Keys (py_arg);
1113 if (py_keys == NULL) {
1114 PyErr_Format (PyExc_TypeError, "Must be mapping, not %s",
1115 py_arg->ob_type->tp_name);
1119 length = PyMapping_Length (py_arg);
1121 Py_DECREF (py_keys);
1125 py_values = PyMapping_Values (py_arg);
1126 if (py_values == NULL) {
1127 Py_DECREF (py_keys);
1131 key_from_py_marshaller = hash_cache->key_cache->from_py_marshaller;
1132 value_from_py_marshaller = hash_cache->value_cache->from_py_marshaller;
1134 switch (hash_cache->key_cache->type_tag) {
1135 case GI_TYPE_TAG_UTF8:
1136 case GI_TYPE_TAG_FILENAME:
1137 hash_func = g_str_hash;
1138 equal_func = g_str_equal;
1145 hash_ = g_hash_table_new (hash_func, equal_func);
1146 if (hash_ == NULL) {
1148 Py_DECREF (py_keys);
1149 Py_DECREF (py_values);
1153 for (i = 0; i < length; i++) {
1154 GIArgument key, value;
1155 PyObject *py_key = PyList_GET_ITEM (py_keys, i);
1156 PyObject *py_value = PyList_GET_ITEM (py_values, i);
1157 if (py_key == NULL || py_value == NULL)
1160 if (!key_from_py_marshaller ( state,
1162 hash_cache->key_cache,
1167 if (!value_from_py_marshaller ( state,
1169 hash_cache->value_cache,
1174 g_hash_table_insert (hash_,
1175 _pygi_arg_to_hash_pointer (&key, hash_cache->key_cache->type_tag),
1176 _pygi_arg_to_hash_pointer (&value, hash_cache->value_cache->type_tag));
1179 /* FIXME: cleanup hash keys and values */
1180 Py_XDECREF (py_key);
1181 Py_XDECREF (py_value);
1182 Py_DECREF (py_keys);
1183 Py_DECREF (py_values);
1184 g_hash_table_unref (hash_);
1185 _PyGI_ERROR_PREFIX ("Item %i: ", i);
1189 arg->v_pointer = hash_;
1194 _pygi_marshal_from_py_gerror (PyGIInvokeState *state,
1195 PyGICallableCache *callable_cache,
1196 PyGIArgCache *arg_cache,
1200 PyErr_Format (PyExc_NotImplementedError,
1201 "Marshalling for GErrors is not implemented");
1206 _pygi_marshal_from_py_interface_callback (PyGIInvokeState *state,
1207 PyGICallableCache *callable_cache,
1208 PyGIArgCache *arg_cache,
1212 GICallableInfo *callable_info;
1213 PyGICClosure *closure;
1214 PyGIArgCache *user_data_cache = NULL;
1215 PyGIArgCache *destroy_cache = NULL;
1216 PyGICallbackCache *callback_cache;
1217 PyObject *py_user_data = NULL;
1219 callback_cache = (PyGICallbackCache *)arg_cache;
1221 if (callback_cache->user_data_index > 0) {
1222 user_data_cache = callable_cache->args_cache[callback_cache->user_data_index];
1223 if (user_data_cache->py_arg_index < state->n_py_in_args) {
1224 py_user_data = PyTuple_GetItem (state->py_in_args, user_data_cache->py_arg_index);
1228 py_user_data = Py_None;
1229 Py_INCREF (Py_None);
1233 if (py_arg == Py_None && !(py_user_data == Py_None || py_user_data == NULL)) {
1234 Py_DECREF (py_user_data);
1235 PyErr_Format (PyExc_TypeError,
1236 "When passing None for a callback userdata must also be None");
1241 if (py_arg == Py_None) {
1242 Py_XDECREF (py_user_data);
1246 if (!PyCallable_Check (py_arg)) {
1247 Py_XDECREF (py_user_data);
1248 PyErr_Format (PyExc_TypeError,
1249 "Callback needs to be a function or method not %s",
1250 py_arg->ob_type->tp_name);
1255 if (callback_cache->destroy_notify_index > 0)
1256 destroy_cache = callable_cache->args_cache[callback_cache->destroy_notify_index];
1258 callable_info = (GICallableInfo *)callback_cache->interface_info;
1260 closure = _pygi_make_native_closure (callable_info, callback_cache->scope, py_arg, py_user_data);
1261 arg->v_pointer = closure->closure;
1262 if (user_data_cache != NULL) {
1263 state->in_args[user_data_cache->c_arg_index].v_pointer = closure;
1266 if (destroy_cache) {
1267 PyGICClosure *destroy_notify = _pygi_destroy_notify_create ();
1268 state->in_args[destroy_cache->c_arg_index].v_pointer = destroy_notify->closure;
1275 _pygi_marshal_from_py_interface_enum (PyGIInvokeState *state,
1276 PyGICallableCache *callable_cache,
1277 PyGIArgCache *arg_cache,
1283 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
1285 is_instance = PyObject_IsInstance (py_arg, iface_cache->py_type);
1287 int_ = PYGLIB_PyNumber_Long (py_arg);
1293 arg->v_long = PYGLIB_PyLong_AsLong (int_);
1296 /* If this is not an instance of the Enum type that we want
1297 * we need to check if the value is equivilant to one of the
1298 * Enum's memebers */
1301 gboolean is_found = FALSE;
1303 for (i = 0; i < g_enum_info_get_n_values (iface_cache->interface_info); i++) {
1304 GIValueInfo *value_info =
1305 g_enum_info_get_value (iface_cache->interface_info, i);
1306 glong enum_value = g_value_info_get_value (value_info);
1307 g_base_info_unref ( (GIBaseInfo *)value_info);
1308 if (arg->v_long == enum_value) {
1321 PyErr_Format (PyExc_TypeError, "Expected a %s, but got %s",
1322 iface_cache->type_name, py_arg->ob_type->tp_name);
1327 _pygi_marshal_from_py_interface_flags (PyGIInvokeState *state,
1328 PyGICallableCache *callable_cache,
1329 PyGIArgCache *arg_cache,
1335 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
1337 is_instance = PyObject_IsInstance (py_arg, iface_cache->py_type);
1339 int_ = PYGLIB_PyNumber_Long (py_arg);
1345 arg->v_long = PYGLIB_PyLong_AsLong (int_);
1348 /* only 0 or argument of type Flag is allowed */
1349 if (!is_instance && arg->v_long != 0)
1355 PyErr_Format (PyExc_TypeError, "Expected a %s, but got %s",
1356 iface_cache->type_name, py_arg->ob_type->tp_name);
1362 _pygi_marshal_from_py_interface_struct (PyGIInvokeState *state,
1363 PyGICallableCache *callable_cache,
1364 PyGIArgCache *arg_cache,
1368 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
1370 if (py_arg == Py_None) {
1371 arg->v_pointer = NULL;
1375 /* FIXME: handle this large if statement in the cache
1376 * and set the correct marshaller
1379 if (iface_cache->g_type == G_TYPE_CLOSURE) {
1381 GType object_gtype = pyg_type_from_object_strict (py_arg, FALSE);
1383 if ( !(PyCallable_Check(py_arg) ||
1384 g_type_is_a (object_gtype, G_TYPE_CLOSURE))) {
1385 PyErr_Format (PyExc_TypeError, "Must be callable, not %s",
1386 py_arg->ob_type->tp_name);
1390 if (g_type_is_a (object_gtype, G_TYPE_CLOSURE))
1391 closure = (GClosure *)pyg_boxed_get (py_arg, void);
1393 closure = pyg_closure_new (py_arg, NULL, NULL);
1395 if (closure == NULL) {
1396 PyErr_SetString (PyExc_RuntimeError, "PyObject conversion to GClosure failed");
1400 arg->v_pointer = closure;
1402 } else if (iface_cache->g_type == G_TYPE_VALUE) {
1406 object_type = pyg_type_from_object_strict ( (PyObject *) py_arg->ob_type, FALSE);
1407 if (object_type == G_TYPE_INVALID) {
1408 PyErr_SetString (PyExc_RuntimeError, "unable to retrieve object's GType");
1412 /* if already a gvalue, use that, else marshal into gvalue */
1413 if (object_type == G_TYPE_VALUE) {
1414 value = (GValue *)( (PyGObject *)py_arg)->obj;
1416 value = g_slice_new0 (GValue);
1417 g_value_init (value, object_type);
1418 if (pyg_value_from_pyobject (value, py_arg) < 0) {
1419 g_slice_free (GValue, value);
1420 PyErr_SetString (PyExc_RuntimeError, "PyObject conversion to GValue failed");
1425 arg->v_pointer = value;
1427 } else if (iface_cache->is_foreign) {
1429 success = pygi_struct_foreign_convert_to_g_argument (py_arg,
1430 iface_cache->interface_info,
1431 arg_cache->transfer,
1434 return (success == Py_None);
1435 } else if (!PyObject_IsInstance (py_arg, iface_cache->py_type)) {
1436 /* first check to see if this is a member of the expected union */
1437 if (!_is_union_member (iface_cache, py_arg)) {
1438 if (!PyErr_Occurred())
1439 PyErr_Format (PyExc_TypeError, "Expected %s, but got %s",
1440 iface_cache->type_name,
1441 iface_cache->py_type->ob_type->tp_name);
1447 if (g_type_is_a (iface_cache->g_type, G_TYPE_BOXED)) {
1448 arg->v_pointer = pyg_boxed_get (py_arg, void);
1449 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING) {
1450 arg->v_pointer = g_boxed_copy (iface_cache->g_type, arg->v_pointer);
1452 } else if (g_type_is_a (iface_cache->g_type, G_TYPE_POINTER) ||
1453 g_type_is_a (iface_cache->g_type, G_TYPE_VARIANT) ||
1454 iface_cache->g_type == G_TYPE_NONE) {
1455 arg->v_pointer = pyg_pointer_get (py_arg, void);
1457 PyErr_Format (PyExc_NotImplementedError,
1458 "structure type '%s' is not supported yet",
1459 g_type_name(iface_cache->g_type));
1466 _pygi_marshal_from_py_interface_boxed (PyGIInvokeState *state,
1467 PyGICallableCache *callable_cache,
1468 PyGIArgCache *arg_cache,
1472 PyErr_Format (PyExc_NotImplementedError,
1473 "Marshalling for this type is not implemented yet");
1478 _pygi_marshal_from_py_interface_object (PyGIInvokeState *state,
1479 PyGICallableCache *callable_cache,
1480 PyGIArgCache *arg_cache,
1484 if (py_arg == Py_None) {
1485 arg->v_pointer = NULL;
1489 if (!PyObject_IsInstance (py_arg, ( (PyGIInterfaceCache *)arg_cache)->py_type)) {
1490 PyErr_Format (PyExc_TypeError, "Expected %s, but got %s",
1491 ( (PyGIInterfaceCache *)arg_cache)->type_name,
1492 ( (PyGIInterfaceCache *)arg_cache)->py_type->ob_type->tp_name);
1496 arg->v_pointer = pygobject_get(py_arg);
1497 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
1498 g_object_ref (arg->v_pointer);
1504 _pygi_marshal_from_py_interface_union (PyGIInvokeState *state,
1505 PyGICallableCache *callable_cache,
1506 PyGIArgCache *arg_cache,
1510 PyErr_Format(PyExc_NotImplementedError,
1511 "Marshalling for this type is not implemented yet");
1515 gboolean _pygi_marshal_from_py_interface_instance (PyGIInvokeState *state,
1516 PyGICallableCache *callable_cache,
1517 PyGIArgCache *arg_cache,
1521 GIInfoType info_type;
1522 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
1524 info_type = g_base_info_get_type (iface_cache->interface_info);
1525 switch (info_type) {
1526 case GI_INFO_TYPE_UNION:
1527 case GI_INFO_TYPE_STRUCT:
1529 GType type = iface_cache->g_type;
1531 if (!PyObject_IsInstance (py_arg, iface_cache->py_type)) {
1532 /* wait, we might be a member of a union so manually check */
1533 if (!_is_union_member (iface_cache, py_arg)) {
1534 if (!PyErr_Occurred())
1535 PyErr_Format (PyExc_TypeError,
1536 "Expected a %s, but got %s",
1537 iface_cache->type_name,
1538 py_arg->ob_type->tp_name);
1544 if (g_type_is_a (type, G_TYPE_BOXED)) {
1545 arg->v_pointer = pyg_boxed_get (py_arg, void);
1546 } else if (g_type_is_a (type, G_TYPE_POINTER) ||
1547 g_type_is_a (type, G_TYPE_VARIANT) ||
1548 type == G_TYPE_NONE) {
1549 arg->v_pointer = pyg_pointer_get (py_arg, void);
1551 PyErr_Format (PyExc_TypeError, "unable to convert an instance of '%s'", g_type_name (type));
1557 case GI_INFO_TYPE_OBJECT:
1558 case GI_INFO_TYPE_INTERFACE:
1559 arg->v_pointer = pygobject_get (py_arg);
1560 if (arg->v_pointer != NULL) {
1561 GType obj_type = G_OBJECT_TYPE (( GObject *)arg->v_pointer);
1562 GType expected_type = iface_cache->g_type;
1564 if (!g_type_is_a (obj_type, expected_type)) {
1565 PyErr_Format (PyExc_TypeError, "Expected a %s, but got %s",
1566 iface_cache->type_name,
1567 py_arg->ob_type->tp_name);
1573 /* Other types don't have methods. */
1574 g_assert_not_reached ();