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"
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-from-py.h"
38 _pygi_marshal_from_py_void (PyGIInvokeState *state,
39 PyGICallableCache *callable_cache,
40 PyGIArgCache *arg_cache,
44 g_warn_if_fail (arg_cache->transfer == GI_TRANSFER_NOTHING);
46 arg->v_pointer = py_arg;
52 _pygi_marshal_from_py_boolean (PyGIInvokeState *state,
53 PyGICallableCache *callable_cache,
54 PyGIArgCache *arg_cache,
58 arg->v_boolean = PyObject_IsTrue (py_arg);
64 _pygi_marshal_from_py_int8 (PyGIInvokeState *state,
65 PyGICallableCache *callable_cache,
66 PyGIArgCache *arg_cache,
73 if (!PyNumber_Check (py_arg)) {
74 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
75 py_arg->ob_type->tp_name);
79 py_long = PYGLIB_PyNumber_Long (py_arg);
83 long_ = PYGLIB_PyLong_AsLong (py_long);
86 if (PyErr_Occurred ()) {
88 PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, -128, 127);
92 if (long_ < -128 || long_ > 127) {
93 PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, -128, 127);
103 _pygi_marshal_from_py_uint8 (PyGIInvokeState *state,
104 PyGICallableCache *callable_cache,
105 PyGIArgCache *arg_cache,
111 if (PYGLIB_PyBytes_Check (py_arg)) {
113 if (PYGLIB_PyBytes_Size (py_arg) != 1) {
114 PyErr_Format (PyExc_TypeError, "Must be a single character");
118 long_ = (unsigned char)(PYGLIB_PyBytes_AsString (py_arg)[0]);
120 } else if (PyNumber_Check (py_arg)) {
122 py_long = PYGLIB_PyNumber_Long (py_arg);
126 long_ = PYGLIB_PyLong_AsLong (py_long);
129 if (PyErr_Occurred ()) {
132 PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, 0, 255);
136 PyErr_Format (PyExc_TypeError, "Must be number or single byte string, not %s",
137 py_arg->ob_type->tp_name);
141 if (long_ < 0 || long_ > 255) {
142 PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, 0, 255);
152 _pygi_marshal_from_py_int16 (PyGIInvokeState *state,
153 PyGICallableCache *callable_cache,
154 PyGIArgCache *arg_cache,
161 if (!PyNumber_Check (py_arg)) {
162 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
163 py_arg->ob_type->tp_name);
167 py_long = PYGLIB_PyNumber_Long (py_arg);
171 long_ = PYGLIB_PyLong_AsLong (py_long);
174 if (PyErr_Occurred ()) {
176 PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, -32768, 32767);
180 if (long_ < -32768 || long_ > 32767) {
181 PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, -32768, 32767);
191 _pygi_marshal_from_py_uint16 (PyGIInvokeState *state,
192 PyGICallableCache *callable_cache,
193 PyGIArgCache *arg_cache,
200 if (!PyNumber_Check (py_arg)) {
201 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
202 py_arg->ob_type->tp_name);
206 py_long = PYGLIB_PyNumber_Long (py_arg);
210 long_ = PYGLIB_PyLong_AsLong (py_long);
213 if (PyErr_Occurred ()) {
215 PyErr_Format (PyExc_ValueError, "%li not in range %d to %d", long_, 0, 65535);
219 if (long_ < 0 || long_ > 65535) {
220 PyErr_Format (PyExc_ValueError, "%li not in range %d to %d", long_, 0, 65535);
230 _pygi_marshal_from_py_int32 (PyGIInvokeState *state,
231 PyGICallableCache *callable_cache,
232 PyGIArgCache *arg_cache,
239 if (!PyNumber_Check (py_arg)) {
240 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
241 py_arg->ob_type->tp_name);
245 py_long = PYGLIB_PyNumber_Long (py_arg);
249 long_ = PYGLIB_PyLong_AsLong (py_long);
252 if (PyErr_Occurred ()) {
254 PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, G_MININT32, G_MAXINT32);
258 if (long_ < G_MININT32 || long_ > G_MAXINT32) {
259 PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, G_MININT32, G_MAXINT32);
269 _pygi_marshal_from_py_uint32 (PyGIInvokeState *state,
270 PyGICallableCache *callable_cache,
271 PyGIArgCache *arg_cache,
278 if (!PyNumber_Check (py_arg)) {
279 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
280 py_arg->ob_type->tp_name);
284 py_long = PYGLIB_PyNumber_Long (py_arg);
288 #if PY_VERSION_HEX < 0x03000000
289 if (PyInt_Check (py_long))
290 long_ = PyInt_AsLong (py_long);
293 long_ = PyLong_AsLongLong (py_long);
297 if (PyErr_Occurred ()) {
299 PyErr_Format (PyExc_ValueError, "%lli not in range %i to %u", long_, 0, G_MAXUINT32);
303 if (long_ < 0 || long_ > G_MAXUINT32) {
304 PyErr_Format (PyExc_ValueError, "%lli not in range %i to %u", long_, 0, G_MAXUINT32);
308 arg->v_uint64 = long_;
314 _pygi_marshal_from_py_int64 (PyGIInvokeState *state,
315 PyGICallableCache *callable_cache,
316 PyGIArgCache *arg_cache,
323 if (!PyNumber_Check (py_arg)) {
324 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
325 py_arg->ob_type->tp_name);
329 py_long = PYGLIB_PyNumber_Long (py_arg);
333 #if PY_VERSION_HEX < 0x03000000
334 if (PyInt_Check (py_long))
335 long_ = PyInt_AS_LONG (py_long);
338 long_ = PyLong_AsLongLong (py_long);
342 if (PyErr_Occurred ()) {
343 /* OverflowError occured but range errors should be returned as ValueError */
349 py_str = PyObject_Str (py_long);
351 if (PyUnicode_Check (py_str)) {
352 PyObject *py_bytes = PyUnicode_AsUTF8String (py_str);
353 if (py_bytes == NULL)
356 long_str = g_strdup (PYGLIB_PyBytes_AsString (py_bytes));
357 if (long_str == NULL) {
362 Py_DECREF (py_bytes);
364 long_str = g_strdup (PYGLIB_PyBytes_AsString(py_str));
368 PyErr_Format (PyExc_ValueError, "%s not in range %ld to %ld",
369 long_str, G_MININT64, G_MAXINT64);
375 if (long_ < G_MININT64 || long_ > G_MAXINT64) {
376 PyErr_Format (PyExc_ValueError, "%lld not in range %ld to %ld", long_, G_MININT64, G_MAXINT64);
380 arg->v_int64 = long_;
386 _pygi_marshal_from_py_uint64 (PyGIInvokeState *state,
387 PyGICallableCache *callable_cache,
388 PyGIArgCache *arg_cache,
395 if (!PyNumber_Check (py_arg)) {
396 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
397 py_arg->ob_type->tp_name);
401 py_long = PYGLIB_PyNumber_Long (py_arg);
405 #if PY_VERSION_HEX < 0x03000000
406 if (PyInt_Check (py_long)) {
407 long long_ = PyInt_AsLong (py_long);
409 PyErr_Format (PyExc_ValueError, "%ld not in range %d to %llu",
410 long_, 0, G_MAXUINT64);
416 ulong_ = PyLong_AsUnsignedLongLong (py_long);
420 if (PyErr_Occurred ()) {
421 /* OverflowError occured but range errors should be returned as ValueError */
427 py_str = PyObject_Str (py_long);
429 if (PyUnicode_Check (py_str)) {
430 PyObject *py_bytes = PyUnicode_AsUTF8String (py_str);
431 if (py_bytes == NULL)
434 long_str = g_strdup (PYGLIB_PyBytes_AsString (py_bytes));
435 if (long_str == NULL) {
440 Py_DECREF (py_bytes);
442 long_str = g_strdup (PYGLIB_PyBytes_AsString (py_str));
447 PyErr_Format (PyExc_ValueError, "%s not in range %d to %llu",
448 long_str, 0, G_MAXUINT64);
454 if (ulong_ > G_MAXUINT64) {
455 PyErr_Format (PyExc_ValueError, "%llu not in range %d to %llu", ulong_, 0, G_MAXUINT64);
459 arg->v_uint64 = ulong_;
465 _pygi_marshal_from_py_float (PyGIInvokeState *state,
466 PyGICallableCache *callable_cache,
467 PyGIArgCache *arg_cache,
474 if (!PyNumber_Check (py_arg)) {
475 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
476 py_arg->ob_type->tp_name);
480 py_float = PyNumber_Float (py_arg);
484 double_ = PyFloat_AsDouble (py_float);
485 Py_DECREF (py_float);
487 if (PyErr_Occurred ()) {
489 PyErr_Format (PyExc_ValueError, "%f not in range %f to %f", double_, -G_MAXFLOAT, G_MAXFLOAT);
493 if (double_ < -G_MAXFLOAT || double_ > G_MAXFLOAT) {
494 PyErr_Format (PyExc_ValueError, "%f not in range %f to %f", double_, -G_MAXFLOAT, G_MAXFLOAT);
498 arg->v_float = double_;
504 _pygi_marshal_from_py_double (PyGIInvokeState *state,
505 PyGICallableCache *callable_cache,
506 PyGIArgCache *arg_cache,
513 if (!PyNumber_Check (py_arg)) {
514 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
515 py_arg->ob_type->tp_name);
519 py_float = PyNumber_Float (py_arg);
523 double_ = PyFloat_AsDouble (py_float);
524 Py_DECREF (py_float);
526 if (PyErr_Occurred ()) {
528 PyErr_Format (PyExc_ValueError, "%f not in range %f to %f", double_, -G_MAXDOUBLE, G_MAXDOUBLE);
532 if (double_ < -G_MAXDOUBLE || double_ > G_MAXDOUBLE) {
533 PyErr_Format (PyExc_ValueError, "%f not in range %f to %f", double_, -G_MAXDOUBLE, G_MAXDOUBLE);
537 arg->v_double = double_;
543 _pygi_marshal_from_py_unichar (PyGIInvokeState *state,
544 PyGICallableCache *callable_cache,
545 PyGIArgCache *arg_cache,
552 if (PyUnicode_Check (py_arg)) {
555 size = PyUnicode_GET_SIZE (py_arg);
556 py_bytes = PyUnicode_AsUTF8String (py_arg);
557 string_ = strdup(PYGLIB_PyBytes_AsString (py_bytes));
558 Py_DECREF (py_bytes);
560 #if PY_VERSION_HEX < 0x03000000
561 } else if (PyString_Check (py_arg)) {
562 PyObject *pyuni = PyUnicode_FromEncodedObject (py_arg, "UTF-8", "strict");
566 size = PyUnicode_GET_SIZE (pyuni);
567 string_ = g_strdup (PyString_AsString(py_arg));
571 PyErr_Format (PyExc_TypeError, "Must be string, not %s",
572 py_arg->ob_type->tp_name);
577 PyErr_Format (PyExc_TypeError, "Must be a one character string, not %ld characters",
583 arg->v_uint32 = g_utf8_get_char (string_);
589 _pygi_marshal_from_py_gtype (PyGIInvokeState *state,
590 PyGICallableCache *callable_cache,
591 PyGIArgCache *arg_cache,
595 long type_ = pyg_type_from_object (py_arg);
598 PyErr_Format (PyExc_TypeError, "Must be gobject.GType, not %s",
599 py_arg->ob_type->tp_name);
607 _pygi_marshal_from_py_utf8 (PyGIInvokeState *state,
608 PyGICallableCache *callable_cache,
609 PyGIArgCache *arg_cache,
615 if (py_arg == Py_None) {
616 arg->v_pointer = NULL;
620 if (PyUnicode_Check (py_arg)) {
621 PyObject *pystr_obj = PyUnicode_AsUTF8String (py_arg);
625 string_ = g_strdup (PYGLIB_PyBytes_AsString (pystr_obj));
626 Py_DECREF (pystr_obj);
628 #if PY_VERSION_HEX < 0x03000000
629 else if (PyString_Check (py_arg)) {
630 string_ = g_strdup (PyString_AsString (py_arg));
634 PyErr_Format (PyExc_TypeError, "Must be string, not %s",
635 py_arg->ob_type->tp_name);
639 arg->v_string = string_;
644 _pygi_marshal_from_py_filename (PyGIInvokeState *state,
645 PyGICallableCache *callable_cache,
646 PyGIArgCache *arg_cache,
651 GError *error = NULL;
653 if (PyUnicode_Check (py_arg)) {
654 PyObject *pystr_obj = PyUnicode_AsUTF8String (py_arg);
658 string_ = g_strdup (PYGLIB_PyBytes_AsString (pystr_obj));
659 Py_DECREF (pystr_obj);
661 #if PY_VERSION_HEX < 0x03000000
662 else if (PyString_Check (py_arg)) {
663 string_ = g_strdup (PyString_AsString (py_arg));
667 PyErr_Format (PyExc_TypeError, "Must be string, not %s",
668 py_arg->ob_type->tp_name);
672 arg->v_string = g_filename_from_utf8 (string_, -1, NULL, NULL, &error);
675 if (arg->v_string == NULL) {
676 PyErr_SetString (PyExc_Exception, error->message);
677 g_error_free (error);
678 /* TODO: Convert the error to an exception. */
686 _pygi_marshal_from_py_array (PyGIInvokeState *state,
687 PyGICallableCache *callable_cache,
688 PyGIArgCache *arg_cache,
692 PyGIMarshalFromPyFunc from_py_marshaller;
696 gboolean is_ptr_array;
697 GArray *array_ = NULL;
698 PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache;
701 if (py_arg == Py_None) {
702 arg->v_pointer = NULL;
706 if (!PySequence_Check (py_arg)) {
707 PyErr_Format (PyExc_TypeError, "Must be sequence, not %s",
708 py_arg->ob_type->tp_name);
712 length = PySequence_Length (py_arg);
716 if (sequence_cache->fixed_size >= 0 &&
717 sequence_cache->fixed_size != length) {
718 PyErr_Format (PyExc_ValueError, "Must contain %zd items, not %zd",
719 sequence_cache->fixed_size, length);
724 item_size = sequence_cache->item_size;
725 is_ptr_array = (sequence_cache->array_type == GI_ARRAY_TYPE_PTR_ARRAY);
727 array_ = (GArray *)g_ptr_array_new ();
729 array_ = g_array_sized_new (sequence_cache->is_zero_terminated,
735 if (array_ == NULL) {
740 if (sequence_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8 &&
741 PYGLIB_PyBytes_Check (py_arg)) {
742 memcpy(array_->data, PYGLIB_PyBytes_AsString (py_arg), length);
747 from_py_marshaller = sequence_cache->item_cache->from_py_marshaller;
748 for (i = 0; i < length; i++) {
750 PyObject *py_item = PySequence_GetItem (py_arg, i);
754 if (!from_py_marshaller ( state,
756 sequence_cache->item_cache,
761 /* FIXME: it is much more efficent to have seperate marshaller
762 * for ptr arrays than doing the evaluation
763 * and casting each loop iteration
766 g_ptr_array_add((GPtrArray *)array_, item.v_pointer);
767 } else if (sequence_cache->item_cache->type_tag == GI_TYPE_TAG_INTERFACE) {
768 PyGIInterfaceCache *item_iface_cache = (PyGIInterfaceCache *) sequence_cache->item_cache;
769 GIBaseInfo *base_info = (GIBaseInfo *) item_iface_cache->interface_info;
770 GIInfoType info_type = g_base_info_get_type (base_info);
773 case GI_INFO_TYPE_UNION:
774 case GI_INFO_TYPE_STRUCT:
776 PyGIArgCache *item_arg_cache = (PyGIArgCache *)item_iface_cache;
777 PyGIMarshalCleanupFunc from_py_cleanup = item_arg_cache->from_py_cleanup;
778 gboolean is_boxed = g_type_is_a (item_iface_cache->g_type, G_TYPE_BOXED);
779 gboolean is_gvalue = item_iface_cache->g_type == G_TYPE_VALUE;
781 if (!is_boxed || is_gvalue) {
782 memcpy (array_->data + (i * item_size), item.v_pointer, item_size);
784 from_py_cleanup (state, item_arg_cache, item.v_pointer, TRUE);
786 g_array_insert_val (array_, i, item);
791 g_array_insert_val (array_, i, item);
794 g_array_insert_val (array_, i, item);
798 if (sequence_cache->item_cache->from_py_cleanup != NULL) {
800 PyGIMarshalCleanupFunc cleanup_func =
801 sequence_cache->item_cache->from_py_cleanup;
803 for(j = 0; j < i; j++) {
805 sequence_cache->item_cache,
806 g_array_index (array_, gpointer, j),
812 g_ptr_array_free ( ( GPtrArray *)array_, TRUE);
814 g_array_free (array_, TRUE);
815 _PyGI_ERROR_PREFIX ("Item %i: ", i);
820 if (sequence_cache->len_arg_index >= 0) {
821 /* we have an child arg to handle */
822 PyGIArgCache *child_cache =
823 callable_cache->args_cache[sequence_cache->len_arg_index];
825 if (child_cache->direction == PYGI_DIRECTION_BIDIRECTIONAL) {
826 gint *len_arg = (gint *)state->in_args[child_cache->c_arg_index].v_pointer;
827 /* if we are not setup yet just set the in arg */
829 state->in_args[child_cache->c_arg_index].v_long = length;
833 state->in_args[child_cache->c_arg_index].v_long = length;
837 if (sequence_cache->array_type == GI_ARRAY_TYPE_C) {
838 arg->v_pointer = array_->data;
839 g_array_free (array_, FALSE);
841 arg->v_pointer = array_;
848 _pygi_marshal_from_py_glist (PyGIInvokeState *state,
849 PyGICallableCache *callable_cache,
850 PyGIArgCache *arg_cache,
854 PyGIMarshalFromPyFunc from_py_marshaller;
858 PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache;
861 if (py_arg == Py_None) {
862 arg->v_pointer = NULL;
866 if (!PySequence_Check (py_arg)) {
867 PyErr_Format (PyExc_TypeError, "Must be sequence, not %s",
868 py_arg->ob_type->tp_name);
872 length = PySequence_Length (py_arg);
876 if (sequence_cache->fixed_size >= 0 &&
877 sequence_cache->fixed_size != length) {
878 PyErr_Format (PyExc_ValueError, "Must contain %zd items, not %zd",
879 sequence_cache->fixed_size, length);
884 from_py_marshaller = sequence_cache->item_cache->from_py_marshaller;
885 for (i = 0; i < length; i++) {
887 PyObject *py_item = PySequence_GetItem (py_arg, i);
891 if (!from_py_marshaller ( state,
893 sequence_cache->item_cache,
898 list_ = g_list_append (list_, item.v_pointer);
901 if (sequence_cache->item_cache->from_py_cleanup != NULL) {
902 PyGIMarshalCleanupFunc cleanup = sequence_cache->item_cache->from_py_cleanup;
906 _PyGI_ERROR_PREFIX ("Item %i: ", i);
910 arg->v_pointer = list_;
915 _pygi_marshal_from_py_gslist (PyGIInvokeState *state,
916 PyGICallableCache *callable_cache,
917 PyGIArgCache *arg_cache,
921 PyGIMarshalFromPyFunc from_py_marshaller;
924 GSList *list_ = NULL;
925 PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache;
927 if (py_arg == Py_None) {
928 arg->v_pointer = NULL;
932 if (!PySequence_Check (py_arg)) {
933 PyErr_Format (PyExc_TypeError, "Must be sequence, not %s",
934 py_arg->ob_type->tp_name);
938 length = PySequence_Length (py_arg);
942 if (sequence_cache->fixed_size >= 0 &&
943 sequence_cache->fixed_size != length) {
944 PyErr_Format (PyExc_ValueError, "Must contain %zd items, not %zd",
945 sequence_cache->fixed_size, length);
950 from_py_marshaller = sequence_cache->item_cache->from_py_marshaller;
951 for (i = 0; i < length; i++) {
953 PyObject *py_item = PySequence_GetItem (py_arg, i);
957 if (!from_py_marshaller ( state,
959 sequence_cache->item_cache,
964 list_ = g_slist_append (list_, item.v_pointer);
967 if (sequence_cache->item_cache->from_py_cleanup != NULL) {
968 PyGIMarshalCleanupFunc cleanup = sequence_cache->item_cache->from_py_cleanup;
971 g_slist_free (list_);
972 _PyGI_ERROR_PREFIX ("Item %i: ", i);
976 arg->v_pointer = list_;
981 _pygi_marshal_from_py_ghash (PyGIInvokeState *state,
982 PyGICallableCache *callable_cache,
983 PyGIArgCache *arg_cache,
987 PyGIMarshalFromPyFunc key_from_py_marshaller;
988 PyGIMarshalFromPyFunc value_from_py_marshaller;
992 PyObject *py_keys, *py_values;
995 GEqualFunc equal_func;
997 GHashTable *hash_ = NULL;
998 PyGIHashCache *hash_cache = (PyGIHashCache *)arg_cache;
1000 if (py_arg == Py_None) {
1001 arg->v_pointer = NULL;
1005 py_keys = PyMapping_Keys (py_arg);
1006 if (py_keys == NULL) {
1007 PyErr_Format (PyExc_TypeError, "Must be mapping, not %s",
1008 py_arg->ob_type->tp_name);
1012 length = PyMapping_Length (py_arg);
1014 Py_DECREF (py_keys);
1018 py_values = PyMapping_Values (py_arg);
1019 if (py_values == NULL) {
1020 Py_DECREF (py_keys);
1024 key_from_py_marshaller = hash_cache->key_cache->from_py_marshaller;
1025 value_from_py_marshaller = hash_cache->value_cache->from_py_marshaller;
1027 switch (hash_cache->key_cache->type_tag) {
1028 case GI_TYPE_TAG_UTF8:
1029 case GI_TYPE_TAG_FILENAME:
1030 hash_func = g_str_hash;
1031 equal_func = g_str_equal;
1038 hash_ = g_hash_table_new (hash_func, equal_func);
1039 if (hash_ == NULL) {
1041 Py_DECREF (py_keys);
1042 Py_DECREF (py_values);
1046 for (i = 0; i < length; i++) {
1047 GIArgument key, value;
1048 PyObject *py_key = PyList_GET_ITEM (py_keys, i);
1049 PyObject *py_value = PyList_GET_ITEM (py_values, i);
1050 if (py_key == NULL || py_value == NULL)
1053 if (!key_from_py_marshaller ( state,
1055 hash_cache->key_cache,
1060 if (!value_from_py_marshaller ( state,
1062 hash_cache->value_cache,
1067 g_hash_table_insert (hash_, key.v_pointer, value.v_pointer);
1070 /* FIXME: cleanup hash keys and values */
1071 Py_XDECREF (py_key);
1072 Py_XDECREF (py_value);
1073 Py_DECREF (py_keys);
1074 Py_DECREF (py_values);
1075 g_hash_table_unref (hash_);
1076 _PyGI_ERROR_PREFIX ("Item %i: ", i);
1080 arg->v_pointer = hash_;
1085 _pygi_marshal_from_py_gerror (PyGIInvokeState *state,
1086 PyGICallableCache *callable_cache,
1087 PyGIArgCache *arg_cache,
1091 PyErr_Format (PyExc_NotImplementedError,
1092 "Marshalling for GErrors is not implemented");
1097 _pygi_marshal_from_py_interface_callback (PyGIInvokeState *state,
1098 PyGICallableCache *callable_cache,
1099 PyGIArgCache *arg_cache,
1103 GICallableInfo *callable_info;
1104 PyGICClosure *closure;
1105 PyGIArgCache *user_data_cache = NULL;
1106 PyGIArgCache *destroy_cache = NULL;
1107 PyGICallbackCache *callback_cache;
1108 PyObject *py_user_data = NULL;
1110 callback_cache = (PyGICallbackCache *)arg_cache;
1112 if (callback_cache->user_data_index > 0) {
1113 user_data_cache = callable_cache->args_cache[callback_cache->user_data_index];
1114 if (user_data_cache->py_arg_index < state->n_py_in_args) {
1115 py_user_data = PyTuple_GetItem (state->py_in_args, user_data_cache->py_arg_index);
1119 py_user_data = Py_None;
1120 Py_INCREF (Py_None);
1124 if (py_arg == Py_None && !(py_user_data == Py_None || py_user_data == NULL)) {
1125 Py_DECREF (py_user_data);
1126 PyErr_Format (PyExc_TypeError,
1127 "When passing None for a callback userdata must also be None");
1132 if (py_arg == Py_None) {
1133 Py_XDECREF (py_user_data);
1137 if (!PyCallable_Check (py_arg)) {
1138 Py_XDECREF (py_user_data);
1139 PyErr_Format (PyExc_TypeError,
1140 "Callback needs to be a function or method not %s",
1141 py_arg->ob_type->tp_name);
1146 if (callback_cache->destroy_notify_index > 0)
1147 destroy_cache = callable_cache->args_cache[callback_cache->destroy_notify_index];
1149 callable_info = (GICallableInfo *)callback_cache->interface_info;
1151 closure = _pygi_make_native_closure (callable_info, callback_cache->scope, py_arg, py_user_data);
1152 arg->v_pointer = closure->closure;
1153 if (user_data_cache != NULL) {
1154 state->in_args[user_data_cache->c_arg_index].v_pointer = closure;
1157 if (destroy_cache) {
1158 PyGICClosure *destroy_notify = _pygi_destroy_notify_create ();
1159 state->in_args[destroy_cache->c_arg_index].v_pointer = destroy_notify->closure;
1166 _pygi_marshal_from_py_interface_enum (PyGIInvokeState *state,
1167 PyGICallableCache *callable_cache,
1168 PyGIArgCache *arg_cache,
1174 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
1176 is_instance = PyObject_IsInstance (py_arg, iface_cache->py_type);
1178 int_ = PYGLIB_PyNumber_Long (py_arg);
1184 arg->v_long = PYGLIB_PyLong_AsLong (int_);
1187 /* If this is not an instance of the Enum type that we want
1188 * we need to check if the value is equivilant to one of the
1189 * Enum's memebers */
1192 gboolean is_found = FALSE;
1194 for (i = 0; i < g_enum_info_get_n_values (iface_cache->interface_info); i++) {
1195 GIValueInfo *value_info =
1196 g_enum_info_get_value (iface_cache->interface_info, i);
1197 glong enum_value = g_value_info_get_value (value_info);
1198 g_base_info_unref ( (GIBaseInfo *)value_info);
1199 if (arg->v_long == enum_value) {
1212 PyErr_Format (PyExc_TypeError, "Expected a %s, but got %s",
1213 iface_cache->type_name, py_arg->ob_type->tp_name);
1218 _pygi_marshal_from_py_interface_flags (PyGIInvokeState *state,
1219 PyGICallableCache *callable_cache,
1220 PyGIArgCache *arg_cache,
1226 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
1228 is_instance = PyObject_IsInstance (py_arg, iface_cache->py_type);
1230 int_ = PYGLIB_PyNumber_Long (py_arg);
1236 arg->v_long = PYGLIB_PyLong_AsLong (int_);
1239 /* only 0 or argument of type Flag is allowed */
1240 if (!is_instance && arg->v_long != 0)
1246 PyErr_Format (PyExc_TypeError, "Expected a %s, but got %s",
1247 iface_cache->type_name, py_arg->ob_type->tp_name);
1253 _pygi_marshal_from_py_interface_struct (PyGIInvokeState *state,
1254 PyGICallableCache *callable_cache,
1255 PyGIArgCache *arg_cache,
1259 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
1261 if (py_arg == Py_None) {
1262 arg->v_pointer = NULL;
1266 /* FIXME: handle this large if statement in the cache
1267 * and set the correct marshaller
1270 if (iface_cache->g_type == G_TYPE_CLOSURE) {
1272 GType object_gtype = pyg_type_from_object_strict (py_arg, FALSE);
1274 if ( !(PyCallable_Check(py_arg) ||
1275 g_type_is_a (object_gtype, G_TYPE_CLOSURE))) {
1276 PyErr_Format (PyExc_TypeError, "Must be callable, not %s",
1277 py_arg->ob_type->tp_name);
1281 if (g_type_is_a (object_gtype, G_TYPE_CLOSURE))
1282 closure = (GClosure *)pyg_boxed_get (py_arg, void);
1284 closure = pyg_closure_new (py_arg, NULL, NULL);
1286 if (closure == NULL) {
1287 PyErr_SetString (PyExc_RuntimeError, "PyObject conversion to GClosure failed");
1291 arg->v_pointer = closure;
1293 } else if (iface_cache->g_type == G_TYPE_VALUE) {
1297 object_type = pyg_type_from_object_strict ( (PyObject *) py_arg->ob_type, FALSE);
1298 if (object_type == G_TYPE_INVALID) {
1299 PyErr_SetString (PyExc_RuntimeError, "unable to retrieve object's GType");
1303 /* if already a gvalue, use that, else marshal into gvalue */
1304 if (object_type == G_TYPE_VALUE) {
1305 value = (GValue *)( (PyGObject *)py_arg)->obj;
1307 value = g_slice_new0 (GValue);
1308 g_value_init (value, object_type);
1309 if (pyg_value_from_pyobject (value, py_arg) < 0) {
1310 g_slice_free (GValue, value);
1311 PyErr_SetString (PyExc_RuntimeError, "PyObject conversion to GValue failed");
1316 arg->v_pointer = value;
1318 } else if (iface_cache->is_foreign) {
1320 success = pygi_struct_foreign_convert_to_g_argument (py_arg,
1321 iface_cache->interface_info,
1322 arg_cache->transfer,
1326 } else if (!PyObject_IsInstance (py_arg, iface_cache->py_type)) {
1327 PyErr_Format (PyExc_TypeError, "Expected %s, but got %s",
1328 iface_cache->type_name,
1329 iface_cache->py_type->ob_type->tp_name);
1333 if (g_type_is_a (iface_cache->g_type, G_TYPE_BOXED)) {
1334 arg->v_pointer = pyg_boxed_get (py_arg, void);
1335 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING) {
1336 arg->v_pointer = g_boxed_copy (iface_cache->g_type, arg->v_pointer);
1338 } else if (g_type_is_a (iface_cache->g_type, G_TYPE_POINTER) ||
1339 g_type_is_a (iface_cache->g_type, G_TYPE_VARIANT) ||
1340 iface_cache->g_type == G_TYPE_NONE) {
1341 arg->v_pointer = pyg_pointer_get (py_arg, void);
1343 PyErr_Format (PyExc_NotImplementedError,
1344 "structure type '%s' is not supported yet",
1345 g_type_name(iface_cache->g_type));
1352 _pygi_marshal_from_py_interface_boxed (PyGIInvokeState *state,
1353 PyGICallableCache *callable_cache,
1354 PyGIArgCache *arg_cache,
1358 PyErr_Format (PyExc_NotImplementedError,
1359 "Marshalling for this type is not implemented yet");
1364 _pygi_marshal_from_py_interface_object (PyGIInvokeState *state,
1365 PyGICallableCache *callable_cache,
1366 PyGIArgCache *arg_cache,
1370 if (py_arg == Py_None) {
1371 arg->v_pointer = NULL;
1375 if (!PyObject_IsInstance (py_arg, ( (PyGIInterfaceCache *)arg_cache)->py_type)) {
1376 PyErr_Format (PyExc_TypeError, "Expected %s, but got %s",
1377 ( (PyGIInterfaceCache *)arg_cache)->type_name,
1378 ( (PyGIInterfaceCache *)arg_cache)->py_type->ob_type->tp_name);
1382 arg->v_pointer = pygobject_get(py_arg);
1383 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
1384 g_object_ref (arg->v_pointer);
1390 _pygi_marshal_from_py_interface_union (PyGIInvokeState *state,
1391 PyGICallableCache *callable_cache,
1392 PyGIArgCache *arg_cache,
1396 PyErr_Format(PyExc_NotImplementedError,
1397 "Marshalling for this type is not implemented yet");
1401 gboolean _pygi_marshal_from_py_interface_instance (PyGIInvokeState *state,
1402 PyGICallableCache *callable_cache,
1403 PyGIArgCache *arg_cache,
1407 GIInfoType info_type;
1408 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
1410 info_type = g_base_info_get_type (iface_cache->interface_info);
1411 switch (info_type) {
1412 case GI_INFO_TYPE_UNION:
1413 case GI_INFO_TYPE_STRUCT:
1415 GType type = iface_cache->g_type;
1417 if (!PyObject_IsInstance (py_arg, iface_cache->py_type)) {
1418 PyErr_Format (PyExc_TypeError, "Expected a %s, but got %s",
1419 iface_cache->type_name,
1420 py_arg->ob_type->tp_name);
1424 if (g_type_is_a (type, G_TYPE_BOXED)) {
1425 arg->v_pointer = pyg_boxed_get (py_arg, void);
1426 } else if (g_type_is_a (type, G_TYPE_POINTER) ||
1427 g_type_is_a (type, G_TYPE_VARIANT) ||
1428 type == G_TYPE_NONE) {
1429 arg->v_pointer = pyg_pointer_get (py_arg, void);
1431 PyErr_Format (PyExc_TypeError, "unable to convert an instance of '%s'", g_type_name (type));
1437 case GI_INFO_TYPE_OBJECT:
1438 case GI_INFO_TYPE_INTERFACE:
1439 arg->v_pointer = pygobject_get (py_arg);
1440 if (arg->v_pointer != NULL) {
1441 GType obj_type = G_OBJECT_TYPE (( GObject *)arg->v_pointer);
1442 GType expected_type = iface_cache->g_type;
1444 if (!g_type_is_a (obj_type, expected_type)) {
1445 PyErr_Format (PyExc_TypeError, "Expected a %s, but got %s",
1446 iface_cache->type_name,
1447 py_arg->ob_type->tp_name);
1453 /* Other types don't have methods. */
1454 g_assert_not_reached ();