pygi-argument.c 62.9 KB
Newer Older
Simon van der Linden's avatar
Simon van der Linden committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/* -*- Mode: C; c-basic-offset: 4 -*-
 * vim: tabstop=4 shiftwidth=4 expandtab
 *
 * Copyright (C) 2005-2009 Johan Dahlin <johan@gnome.org>
 *
 *   pygi-argument.c: GArgument - PyObject conversion functions.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
 * USA
 */

#include "pygi-private.h"

#include <string.h>
#include <time.h>

#include <datetime.h>
#include <pygobject.h>

32

Simon van der Linden's avatar
Simon van der Linden committed
33 34 35 36 37
static void
_pygi_g_type_tag_py_bounds (GITypeTag   type_tag,
                            PyObject  **lower,
                            PyObject  **upper)
{
38
    switch (type_tag) {
Simon van der Linden's avatar
Simon van der Linden committed
39
        case GI_TYPE_TAG_INT8:
40 41
            *lower = PyInt_FromLong (-128);
            *upper = PyInt_FromLong (127);
Simon van der Linden's avatar
Simon van der Linden committed
42 43
            break;
        case GI_TYPE_TAG_UINT8:
44 45
            *upper = PyInt_FromLong (255);
            *lower = PyInt_FromLong (0);
Simon van der Linden's avatar
Simon van der Linden committed
46 47
            break;
        case GI_TYPE_TAG_INT16:
48 49
            *lower = PyInt_FromLong (-32768);
            *upper = PyInt_FromLong (32767);
Simon van der Linden's avatar
Simon van der Linden committed
50 51
            break;
        case GI_TYPE_TAG_UINT16:
52 53
            *upper = PyInt_FromLong (65535);
            *lower = PyInt_FromLong (0);
Simon van der Linden's avatar
Simon van der Linden committed
54 55
            break;
        case GI_TYPE_TAG_INT32:
56 57
            *lower = PyInt_FromLong (G_MININT32);
            *upper = PyInt_FromLong (G_MAXINT32);
Simon van der Linden's avatar
Simon van der Linden committed
58 59 60
            break;
        case GI_TYPE_TAG_UINT32:
            /* Note: On 32-bit archs, this number doesn't fit in a long. */
61 62
            *upper = PyLong_FromLongLong (G_MAXUINT32);
            *lower = PyInt_FromLong (0);
Simon van der Linden's avatar
Simon van der Linden committed
63 64 65
            break;
        case GI_TYPE_TAG_INT64:
            /* Note: On 32-bit archs, these numbers don't fit in a long. */
66 67
            *lower = PyLong_FromLongLong (G_MININT64);
            *upper = PyLong_FromLongLong (G_MAXINT64);
Simon van der Linden's avatar
Simon van der Linden committed
68 69
            break;
        case GI_TYPE_TAG_UINT64:
70 71
            *upper = PyLong_FromUnsignedLongLong (G_MAXUINT64);
            *lower = PyInt_FromLong (0);
Simon van der Linden's avatar
Simon van der Linden committed
72 73
            break;
        case GI_TYPE_TAG_SHORT:
74 75
            *lower = PyInt_FromLong (G_MINSHORT);
            *upper = PyInt_FromLong (G_MAXSHORT);
Simon van der Linden's avatar
Simon van der Linden committed
76 77
            break;
        case GI_TYPE_TAG_USHORT:
78 79
            *upper = PyInt_FromLong (G_MAXUSHORT);
            *lower = PyInt_FromLong (0);
Simon van der Linden's avatar
Simon van der Linden committed
80 81
            break;
        case GI_TYPE_TAG_INT:
82 83
            *lower = PyInt_FromLong (G_MININT);
            *upper = PyInt_FromLong (G_MAXINT);
Simon van der Linden's avatar
Simon van der Linden committed
84 85 86
            break;
        case GI_TYPE_TAG_UINT:
            /* Note: On 32-bit archs, this number doesn't fit in a long. */
87 88
            *upper = PyLong_FromLongLong (G_MAXUINT);
            *lower = PyInt_FromLong (0);
Simon van der Linden's avatar
Simon van der Linden committed
89 90 91
            break;
        case GI_TYPE_TAG_LONG:
        case GI_TYPE_TAG_SSIZE:
92 93
            *lower = PyInt_FromLong (G_MINLONG);
            *upper = PyInt_FromLong (G_MAXLONG);
Simon van der Linden's avatar
Simon van der Linden committed
94 95 96
            break;
        case GI_TYPE_TAG_ULONG:
        case GI_TYPE_TAG_SIZE:
97 98
            *upper = PyLong_FromUnsignedLongLong (G_MAXULONG);
            *lower = PyInt_FromLong (0);
Simon van der Linden's avatar
Simon van der Linden committed
99 100
            break;
        case GI_TYPE_TAG_FLOAT:
101 102
            *upper = PyFloat_FromDouble (G_MAXFLOAT);
            *lower = PyFloat_FromDouble (-G_MAXFLOAT);
Simon van der Linden's avatar
Simon van der Linden committed
103 104
            break;
        case GI_TYPE_TAG_DOUBLE:
105 106
            *upper = PyFloat_FromDouble (G_MAXDOUBLE);
            *lower = PyFloat_FromDouble (-G_MAXDOUBLE);
Simon van der Linden's avatar
Simon van der Linden committed
107 108
            break;
        default:
109
            PyErr_SetString (PyExc_TypeError, "Non-numeric type tag");
Simon van der Linden's avatar
Simon van der Linden committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
            *lower = *upper = NULL;
            return;
    }
}

gint
_pygi_g_registered_type_info_check_object (GIRegisteredTypeInfo *info,
                                           gboolean              is_instance,
                                           PyObject             *object)
{
    gint retval;

    GType g_type;
    PyObject *py_type;
    gchar *type_name_expected = NULL;
Tomeu Vizoso's avatar
Tomeu Vizoso committed
125
    GIInfoType interface_type;
Simon van der Linden's avatar
Simon van der Linden committed
126

127 128 129
    interface_type = g_base_info_get_type (info);
    if ( (interface_type == GI_INFO_TYPE_STRUCT) &&
            (g_struct_info_is_foreign ( (GIStructInfo*) info))) {
Tomeu Vizoso's avatar
Tomeu Vizoso committed
130 131 132
        /* TODO: Could we check is the correct foreign type? */
        return 1;
    }
Simon van der Linden's avatar
Simon van der Linden committed
133

134
    g_type = g_registered_type_info_get_g_type (info);
Simon van der Linden's avatar
Simon van der Linden committed
135
    if (g_type != G_TYPE_NONE) {
136
        py_type = _pygi_type_get_from_g_type (g_type);
Simon van der Linden's avatar
Simon van der Linden committed
137
    } else {
138
        py_type = _pygi_type_import_by_gi_info ( (GIBaseInfo *) info);
Simon van der Linden's avatar
Simon van der Linden committed
139 140 141
    }

    if (py_type == NULL) {
Tomeu Vizoso's avatar
Tomeu Vizoso committed
142
        return 0;
Simon van der Linden's avatar
Simon van der Linden committed
143 144
    }

145
    g_assert (PyType_Check (py_type));
Simon van der Linden's avatar
Simon van der Linden committed
146 147

    if (is_instance) {
148
        retval = PyObject_IsInstance (object, py_type);
Simon van der Linden's avatar
Simon van der Linden committed
149
        if (!retval) {
150 151
            type_name_expected = _pygi_g_base_info_get_fullname (
                                     (GIBaseInfo *) info);
Simon van der Linden's avatar
Simon van der Linden committed
152 153
        }
    } else {
154
        if (!PyObject_Type (py_type)) {
Simon van der Linden's avatar
Simon van der Linden committed
155 156
            type_name_expected = "type";
            retval = 0;
157 158 159 160
        } else if (!PyType_IsSubtype ( (PyTypeObject *) object,
                                       (PyTypeObject *) py_type)) {
            type_name_expected = _pygi_g_base_info_get_fullname (
                                     (GIBaseInfo *) info);
Simon van der Linden's avatar
Simon van der Linden committed
161 162 163 164 165 166
            retval = 0;
        } else {
            retval = 1;
        }
    }

167
    Py_DECREF (py_type);
Simon van der Linden's avatar
Simon van der Linden committed
168 169 170 171 172 173 174 175

    if (!retval) {
        PyTypeObject *object_type;

        if (type_name_expected == NULL) {
            return -1;
        }

176
        object_type = (PyTypeObject *) PyObject_Type (object);
Simon van der Linden's avatar
Simon van der Linden committed
177 178 179 180
        if (object_type == NULL) {
            return -1;
        }

181 182
        PyErr_Format (PyExc_TypeError, "Must be %s, not %s",
                      type_name_expected, object_type->tp_name);
Simon van der Linden's avatar
Simon van der Linden committed
183

184
        g_free (type_name_expected);
Simon van der Linden's avatar
Simon van der Linden committed
185 186 187 188 189 190 191
    }

    return retval;
}

gint
_pygi_g_type_info_check_object (GITypeInfo *type_info,
192 193
                                PyObject   *object,
                                gboolean   allow_none)
Simon van der Linden's avatar
Simon van der Linden committed
194 195 196 197
{
    GITypeTag type_tag;
    gint retval = 1;

198 199 200 201
    if (allow_none && object == Py_None) {
        return retval;
    }

202
    type_tag = g_type_info_get_tag (type_info);
Simon van der Linden's avatar
Simon van der Linden committed
203 204 205

    switch (type_tag) {
        case GI_TYPE_TAG_VOID:
Tomeu Vizoso's avatar
Tomeu Vizoso committed
206
            /* No check; VOID means undefined type */
Simon van der Linden's avatar
Simon van der Linden committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
            break;
        case GI_TYPE_TAG_BOOLEAN:
            /* No check; every Python object has a truth value. */
            break;
        case GI_TYPE_TAG_INT8:
        case GI_TYPE_TAG_UINT8:
        case GI_TYPE_TAG_INT16:
        case GI_TYPE_TAG_UINT16:
        case GI_TYPE_TAG_INT32:
        case GI_TYPE_TAG_UINT32:
        case GI_TYPE_TAG_INT64:
        case GI_TYPE_TAG_UINT64:
        case GI_TYPE_TAG_SHORT:
        case GI_TYPE_TAG_USHORT:
        case GI_TYPE_TAG_INT:
        case GI_TYPE_TAG_UINT:
        case GI_TYPE_TAG_LONG:
        case GI_TYPE_TAG_ULONG:
        case GI_TYPE_TAG_SSIZE:
        case GI_TYPE_TAG_SIZE:
        case GI_TYPE_TAG_FLOAT:
        case GI_TYPE_TAG_DOUBLE:
        {
            PyObject *number, *lower, *upper;

232 233 234
            if (!PyNumber_Check (object)) {
                PyErr_Format (PyExc_TypeError, "Must be number, not %s",
                              object->ob_type->tp_name);
Simon van der Linden's avatar
Simon van der Linden committed
235 236 237 238 239
                retval = 0;
                break;
            }

            if (type_tag == GI_TYPE_TAG_FLOAT || type_tag == GI_TYPE_TAG_DOUBLE) {
240
                number = PyNumber_Float (object);
Simon van der Linden's avatar
Simon van der Linden committed
241
            } else {
242
                number = PyNumber_Int (object);
Simon van der Linden's avatar
Simon van der Linden committed
243 244
            }

245
            _pygi_g_type_tag_py_bounds (type_tag, &lower, &upper);
Simon van der Linden's avatar
Simon van der Linden committed
246 247 248 249 250 251 252

            if (lower == NULL || upper == NULL || number == NULL) {
                retval = -1;
                goto check_number_release;
            }

            /* Check bounds */
253 254
            if (PyObject_Compare (lower, number) > 0
                    || PyObject_Compare (upper, number) < 0) {
Simon van der Linden's avatar
Simon van der Linden committed
255 256 257 258 259 260 261 262
                PyObject *lower_str;
                PyObject *upper_str;

                if (PyErr_Occurred()) {
                    retval = -1;
                    goto check_number_release;
                }

263 264
                lower_str = PyObject_Str (lower);
                upper_str = PyObject_Str (upper);
Simon van der Linden's avatar
Simon van der Linden committed
265 266 267 268 269
                if (lower_str == NULL || upper_str == NULL) {
                    retval = -1;
                    goto check_number_error_release;
                }

270 271 272
                PyErr_Format (PyExc_ValueError, "Must range from %s to %s",
                              PyString_AS_STRING (lower_str),
                              PyString_AS_STRING (upper_str));
Simon van der Linden's avatar
Simon van der Linden committed
273 274 275 276

                retval = 0;

check_number_error_release:
277 278
                Py_XDECREF (lower_str);
                Py_XDECREF (upper_str);
Simon van der Linden's avatar
Simon van der Linden committed
279 280 281
            }

check_number_release:
282 283 284
            Py_XDECREF (number);
            Py_XDECREF (lower);
            Py_XDECREF (upper);
Simon van der Linden's avatar
Simon van der Linden committed
285 286 287
            break;
        }
        case GI_TYPE_TAG_TIME_T:
288 289 290
            if (!PyDateTime_Check (object)) {
                PyErr_Format (PyExc_TypeError, "Must be datetime.datetime, not %s",
                              object->ob_type->tp_name);
Simon van der Linden's avatar
Simon van der Linden committed
291 292 293 294 295 296 297
                retval = 0;
            }
            break;
        case GI_TYPE_TAG_GTYPE:
        {
            gint is_instance;

298
            is_instance = PyObject_IsInstance (object, (PyObject *) &PyGTypeWrapper_Type);
Simon van der Linden's avatar
Simon van der Linden committed
299 300 301 302 303
            if (is_instance < 0) {
                retval = -1;
                break;
            }

304 305 306
            if (!is_instance && (!PyType_Check (object) || pyg_type_from_object (object) == 0)) {
                PyErr_Format (PyExc_TypeError, "Must be gobject.GType, not %s",
                              object->ob_type->tp_name);
Simon van der Linden's avatar
Simon van der Linden committed
307 308 309 310 311 312
                retval = 0;
            }
            break;
        }
        case GI_TYPE_TAG_UTF8:
        case GI_TYPE_TAG_FILENAME:
313 314 315
            if (!PyString_Check (object)) {
                PyErr_Format (PyExc_TypeError, "Must be string, not %s",
                              object->ob_type->tp_name);
Simon van der Linden's avatar
Simon van der Linden committed
316 317 318 319 320 321 322 323 324 325
                retval = 0;
            }
            break;
        case GI_TYPE_TAG_ARRAY:
        {
            gssize fixed_size;
            Py_ssize_t length;
            GITypeInfo *item_type_info;
            Py_ssize_t i;

326 327 328
            if (!PySequence_Check (object)) {
                PyErr_Format (PyExc_TypeError, "Must be sequence, not %s",
                              object->ob_type->tp_name);
Simon van der Linden's avatar
Simon van der Linden committed
329 330 331 332
                retval = 0;
                break;
            }

333
            length = PySequence_Length (object);
Simon van der Linden's avatar
Simon van der Linden committed
334 335 336 337 338
            if (length < 0) {
                retval = -1;
                break;
            }

339
            fixed_size = g_type_info_get_array_fixed_size (type_info);
Simon van der Linden's avatar
Simon van der Linden committed
340
            if (fixed_size >= 0 && length != fixed_size) {
341 342
                PyErr_Format (PyExc_ValueError, "Must contain %zd items, not %zd",
                              fixed_size, length);
Simon van der Linden's avatar
Simon van der Linden committed
343 344 345 346
                retval = 0;
                break;
            }

347 348
            item_type_info = g_type_info_get_param_type (type_info, 0);
            g_assert (item_type_info != NULL);
Simon van der Linden's avatar
Simon van der Linden committed
349 350 351 352

            for (i = 0; i < length; i++) {
                PyObject *item;

353
                item = PySequence_GetItem (object, i);
Simon van der Linden's avatar
Simon van der Linden committed
354 355 356 357 358
                if (item == NULL) {
                    retval = -1;
                    break;
                }

359
                retval = _pygi_g_type_info_check_object (item_type_info, item, TRUE);
Simon van der Linden's avatar
Simon van der Linden committed
360

361
                Py_DECREF (item);
Simon van der Linden's avatar
Simon van der Linden committed
362 363 364 365 366

                if (retval < 0) {
                    break;
                }
                if (!retval) {
367
                    _PyGI_ERROR_PREFIX ("Item %zd: ", i);
Simon van der Linden's avatar
Simon van der Linden committed
368 369 370 371
                    break;
                }
            }

372
            g_base_info_unref ( (GIBaseInfo *) item_type_info);
Simon van der Linden's avatar
Simon van der Linden committed
373 374 375 376 377 378 379 380

            break;
        }
        case GI_TYPE_TAG_INTERFACE:
        {
            GIBaseInfo *info;
            GIInfoType info_type;

381 382
            info = g_type_info_get_interface (type_info);
            g_assert (info != NULL);
Simon van der Linden's avatar
Simon van der Linden committed
383

384
            info_type = g_base_info_get_type (info);
Simon van der Linden's avatar
Simon van der Linden committed
385 386 387

            switch (info_type) {
                case GI_INFO_TYPE_CALLBACK:
388 389 390
                    if (!PyCallable_Check (object)) {
                        PyErr_Format (PyExc_TypeError, "Must be callable, not %s",
                                      object->ob_type->tp_name);
391 392
                        retval = 0;
                    }
Simon van der Linden's avatar
Simon van der Linden committed
393 394
                    break;
                case GI_INFO_TYPE_ENUM:
395 396
                    retval = _pygi_g_registered_type_info_check_object (
                                 (GIRegisteredTypeInfo *) info, TRUE, object);
397
                    break;
Simon van der Linden's avatar
Simon van der Linden committed
398
                case GI_INFO_TYPE_FLAGS:
399
                    if (PyNumber_Check (object)) {
400
                        /* Accept 0 as a valid flag value */
401
                        PyObject *number = PyNumber_Int (object);
402 403 404
                        if (number == NULL)
                            PyErr_Clear();
                        else {
405
                            long value = PyInt_AsLong (number);
406 407 408 409 410 411
                            if (value == 0)
                                break;
                            else if (value == -1)
                                PyErr_Clear();
                        }
                    }
412 413
                    retval = _pygi_g_registered_type_info_check_object (
                                 (GIRegisteredTypeInfo *) info, TRUE, object);
Simon van der Linden's avatar
Simon van der Linden committed
414 415 416 417 418 419
                    break;
                case GI_INFO_TYPE_STRUCT:
                {
                    GType type;

                    /* Handle special cases. */
420 421
                    type = g_registered_type_info_get_g_type ( (GIRegisteredTypeInfo *) info);
                    if (g_type_is_a (type, G_TYPE_VALUE)) {
Simon van der Linden's avatar
Simon van der Linden committed
422
                        GType object_type;
423
                        object_type = pyg_type_from_object ( (PyObject *) object->ob_type);
Simon van der Linden's avatar
Simon van der Linden committed
424
                        if (object_type == G_TYPE_INVALID) {
425 426
                            PyErr_Format (PyExc_TypeError, "Must be of a known GType, not %s",
                                          object->ob_type->tp_name);
Simon van der Linden's avatar
Simon van der Linden committed
427 428 429
                            retval = 0;
                        }
                        break;
430 431 432 433
                    } else if (g_type_is_a (type, G_TYPE_CLOSURE)) {
                        if (!PyCallable_Check (object)) {
                            PyErr_Format (PyExc_TypeError, "Must be callable, not %s",
                                          object->ob_type->tp_name);
Simon van der Linden's avatar
Simon van der Linden committed
434 435 436 437 438 439 440 441
                            retval = 0;
                        }
                        break;
                    }

                    /* Fallback. */
                }
                case GI_INFO_TYPE_BOXED:
442
                case GI_INFO_TYPE_INTERFACE:
Simon van der Linden's avatar
Simon van der Linden committed
443 444
                case GI_INFO_TYPE_OBJECT:
                case GI_INFO_TYPE_UNION:
445
                    retval = _pygi_g_registered_type_info_check_object ( (GIRegisteredTypeInfo *) info, TRUE, object);
Simon van der Linden's avatar
Simon van der Linden committed
446 447 448 449 450
                    break;
                default:
                    g_assert_not_reached();
            }

451
            g_base_info_unref (info);
Simon van der Linden's avatar
Simon van der Linden committed
452 453 454 455 456 457 458 459 460
            break;
        }
        case GI_TYPE_TAG_GLIST:
        case GI_TYPE_TAG_GSLIST:
        {
            Py_ssize_t length;
            GITypeInfo *item_type_info;
            Py_ssize_t i;

461 462 463
            if (!PySequence_Check (object)) {
                PyErr_Format (PyExc_TypeError, "Must be sequence, not %s",
                              object->ob_type->tp_name);
Simon van der Linden's avatar
Simon van der Linden committed
464 465 466 467
                retval = 0;
                break;
            }

468
            length = PySequence_Length (object);
Simon van der Linden's avatar
Simon van der Linden committed
469 470 471 472 473
            if (length < 0) {
                retval = -1;
                break;
            }

474 475
            item_type_info = g_type_info_get_param_type (type_info, 0);
            g_assert (item_type_info != NULL);
Simon van der Linden's avatar
Simon van der Linden committed
476 477 478 479

            for (i = 0; i < length; i++) {
                PyObject *item;

480
                item = PySequence_GetItem (object, i);
Simon van der Linden's avatar
Simon van der Linden committed
481 482 483 484 485
                if (item == NULL) {
                    retval = -1;
                    break;
                }

486
                retval = _pygi_g_type_info_check_object (item_type_info, item, TRUE);
Simon van der Linden's avatar
Simon van der Linden committed
487

488
                Py_DECREF (item);
Simon van der Linden's avatar
Simon van der Linden committed
489 490 491 492 493

                if (retval < 0) {
                    break;
                }
                if (!retval) {
494
                    _PyGI_ERROR_PREFIX ("Item %zd: ", i);
Simon van der Linden's avatar
Simon van der Linden committed
495 496 497 498
                    break;
                }
            }

499
            g_base_info_unref ( (GIBaseInfo *) item_type_info);
Simon van der Linden's avatar
Simon van der Linden committed
500 501 502 503 504 505 506 507 508 509 510
            break;
        }
        case GI_TYPE_TAG_GHASH:
        {
            Py_ssize_t length;
            PyObject *keys;
            PyObject *values;
            GITypeInfo *key_type_info;
            GITypeInfo *value_type_info;
            Py_ssize_t i;

511 512 513
            if (!PyMapping_Check (object)) {
                PyErr_Format (PyExc_TypeError, "Must be mapping, not %s",
                              object->ob_type->tp_name);
Simon van der Linden's avatar
Simon van der Linden committed
514 515 516 517
                retval = 0;
                break;
            }

518
            length = PyMapping_Length (object);
Simon van der Linden's avatar
Simon van der Linden committed
519 520 521 522 523
            if (length < 0) {
                retval = -1;
                break;
            }

524
            keys = PyMapping_Keys (object);
Simon van der Linden's avatar
Simon van der Linden committed
525 526 527 528 529
            if (keys == NULL) {
                retval = -1;
                break;
            }

530
            values = PyMapping_Values (object);
Simon van der Linden's avatar
Simon van der Linden committed
531 532
            if (values == NULL) {
                retval = -1;
533
                Py_DECREF (keys);
Simon van der Linden's avatar
Simon van der Linden committed
534 535 536
                break;
            }

537 538
            key_type_info = g_type_info_get_param_type (type_info, 0);
            g_assert (key_type_info != NULL);
Simon van der Linden's avatar
Simon van der Linden committed
539

540 541
            value_type_info = g_type_info_get_param_type (type_info, 1);
            g_assert (value_type_info != NULL);
Simon van der Linden's avatar
Simon van der Linden committed
542 543 544 545 546

            for (i = 0; i < length; i++) {
                PyObject *key;
                PyObject *value;

547 548
                key = PyList_GET_ITEM (keys, i);
                value = PyList_GET_ITEM (values, i);
Simon van der Linden's avatar
Simon van der Linden committed
549

550
                retval = _pygi_g_type_info_check_object (key_type_info, key, TRUE);
Simon van der Linden's avatar
Simon van der Linden committed
551 552 553 554
                if (retval < 0) {
                    break;
                }
                if (!retval) {
555
                    _PyGI_ERROR_PREFIX ("Key %zd :", i);
Simon van der Linden's avatar
Simon van der Linden committed
556 557 558
                    break;
                }

559
                retval = _pygi_g_type_info_check_object (value_type_info, value, TRUE);
Simon van der Linden's avatar
Simon van der Linden committed
560 561 562 563
                if (retval < 0) {
                    break;
                }
                if (!retval) {
564
                    _PyGI_ERROR_PREFIX ("Value %zd :", i);
Simon van der Linden's avatar
Simon van der Linden committed
565 566 567 568
                    break;
                }
            }

569 570 571 572
            g_base_info_unref ( (GIBaseInfo *) key_type_info);
            g_base_info_unref ( (GIBaseInfo *) value_type_info);
            Py_DECREF (values);
            Py_DECREF (keys);
Simon van der Linden's avatar
Simon van der Linden committed
573 574 575
            break;
        }
        case GI_TYPE_TAG_ERROR:
576
            PyErr_SetString (PyExc_NotImplementedError, "Error marshalling is not supported yet");
Simon van der Linden's avatar
Simon van der Linden committed
577 578 579 580 581 582 583 584 585 586
            /* TODO */
            break;
    }

    return retval;
}

GArray *
_pygi_argument_to_array (GArgument  *arg,
                         GArgument  *args[],
587 588
                         GITypeInfo *type_info,
                         gboolean is_method)
Simon van der Linden's avatar
Simon van der Linden committed
589 590 591 592 593 594 595
{
    GITypeInfo *item_type_info;
    gboolean is_zero_terminated;
    gsize item_size;
    gssize length;
    GArray *g_array;

596 597 598
    if (arg->v_pointer == NULL) {
        return NULL;
    }
Simon van der Linden's avatar
Simon van der Linden committed
599

600 601 602 603
    is_zero_terminated = g_type_info_is_zero_terminated (type_info);
    item_type_info = g_type_info_get_param_type (type_info, 0);

    item_size = _pygi_g_type_info_size (item_type_info);
Simon van der Linden's avatar
Simon van der Linden committed
604

605
    g_base_info_unref ( (GIBaseInfo *) item_type_info);
Simon van der Linden's avatar
Simon van der Linden committed
606 607

    if (is_zero_terminated) {
608
        length = g_strv_length (arg->v_pointer);
Simon van der Linden's avatar
Simon van der Linden committed
609
    } else {
610
        length = g_type_info_get_array_fixed_size (type_info);
Simon van der Linden's avatar
Simon van der Linden committed
611 612 613
        if (length < 0) {
            gint length_arg_pos;

614 615
            length_arg_pos = g_type_info_get_array_length (type_info);
            g_assert (length_arg_pos >= 0);
Simon van der Linden's avatar
Simon van der Linden committed
616

617 618 619 620 621 622
            if (is_method) {
                length_arg_pos--;
            }

            g_assert (length_arg_pos >= 0);

Simon van der Linden's avatar
Simon van der Linden committed
623 624 625 626 627
            /* FIXME: Take into account the type of the argument. */
            length = args[length_arg_pos]->v_int;
        }
    }

628 629
    g_assert (length >= 0);

630
    g_array = g_array_new (is_zero_terminated, FALSE, item_size);
Simon van der Linden's avatar
Simon van der Linden committed
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

    g_array->data = arg->v_pointer;
    g_array->len = length;

    return g_array;
}

GArgument
_pygi_argument_from_object (PyObject   *object,
                            GITypeInfo *type_info,
                            GITransfer  transfer)
{
    GArgument arg;
    GITypeTag type_tag;

Tomeu Vizoso's avatar
Tomeu Vizoso committed
646
    memset(&arg, 0, sizeof(GArgument));
647
    type_tag = g_type_info_get_tag (type_info);
Simon van der Linden's avatar
Simon van der Linden committed
648 649 650

    switch (type_tag) {
        case GI_TYPE_TAG_VOID:
651
            g_warn_if_fail (transfer == GI_TRANSFER_NOTHING);
Tomeu Vizoso's avatar
Tomeu Vizoso committed
652
            arg.v_pointer = object;
Simon van der Linden's avatar
Simon van der Linden committed
653 654 655
            break;
        case GI_TYPE_TAG_BOOLEAN:
        {
656
            arg.v_boolean = PyObject_IsTrue (object);
Simon van der Linden's avatar
Simon van der Linden committed
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
            break;
        }
        case GI_TYPE_TAG_INT8:
        case GI_TYPE_TAG_UINT8:
        case GI_TYPE_TAG_INT16:
        case GI_TYPE_TAG_UINT16:
        case GI_TYPE_TAG_INT32:
        case GI_TYPE_TAG_SHORT:
        case GI_TYPE_TAG_USHORT:
        case GI_TYPE_TAG_INT:
        case GI_TYPE_TAG_LONG:
        case GI_TYPE_TAG_SSIZE:
        {
            PyObject *int_;

672
            int_ = PyNumber_Int (object);
Simon van der Linden's avatar
Simon van der Linden committed
673 674 675 676
            if (int_ == NULL) {
                break;
            }

677
            arg.v_long = PyInt_AsLong (int_);
Simon van der Linden's avatar
Simon van der Linden committed
678

679
            Py_DECREF (int_);
Simon van der Linden's avatar
Simon van der Linden committed
680 681 682 683 684 685 686 687 688 689 690 691

            break;
        }
        case GI_TYPE_TAG_UINT32:
        case GI_TYPE_TAG_UINT64:
        case GI_TYPE_TAG_UINT:
        case GI_TYPE_TAG_ULONG:
        case GI_TYPE_TAG_SIZE:
        {
            PyObject *number;
            guint64 value;

692
            number = PyNumber_Int (object);
Simon van der Linden's avatar
Simon van der Linden committed
693 694 695 696
            if (number == NULL) {
                break;
            }

697 698
            if (PyInt_Check (number)) {
                value = PyInt_AS_LONG (number);
Simon van der Linden's avatar
Simon van der Linden committed
699
            } else {
700
                value = PyLong_AsUnsignedLongLong (number);
Simon van der Linden's avatar
Simon van der Linden committed
701 702
            }

703
            arg.v_uint64 = value;
Simon van der Linden's avatar
Simon van der Linden committed
704

705
            Py_DECREF (number);
Simon van der Linden's avatar
Simon van der Linden committed
706 707 708 709 710 711 712 713

            break;
        }
        case GI_TYPE_TAG_INT64:
        {
            PyObject *number;
            gint64 value;

714
            number = PyNumber_Int (object);
Simon van der Linden's avatar
Simon van der Linden committed
715 716 717 718
            if (number == NULL) {
                break;
            }

719 720
            if (PyInt_Check (number)) {
                value = PyInt_AS_LONG (number);
Simon van der Linden's avatar
Simon van der Linden committed
721
            } else {
722
                value = PyLong_AsLongLong (number);
Simon van der Linden's avatar
Simon van der Linden committed
723 724
            }

725
            arg.v_int64 = value;
Simon van der Linden's avatar
Simon van der Linden committed
726

727
            Py_DECREF (number);
Simon van der Linden's avatar
Simon van der Linden committed
728 729 730 731 732 733 734

            break;
        }
        case GI_TYPE_TAG_FLOAT:
        {
            PyObject *float_;

735
            float_ = PyNumber_Float (object);
Simon van der Linden's avatar
Simon van der Linden committed
736 737 738 739
            if (float_ == NULL) {
                break;
            }

740 741
            arg.v_float = (float) PyFloat_AsDouble (float_);
            Py_DECREF (float_);
Simon van der Linden's avatar
Simon van der Linden committed
742 743 744 745 746 747 748

            break;
        }
        case GI_TYPE_TAG_DOUBLE:
        {
            PyObject *float_;

749
            float_ = PyNumber_Float (object);
Simon van der Linden's avatar
Simon van der Linden committed
750 751 752 753
            if (float_ == NULL) {
                break;
            }

754 755
            arg.v_double = PyFloat_AsDouble (float_);
            Py_DECREF (float_);
Simon van der Linden's avatar
Simon van der Linden committed
756 757 758 759 760 761 762 763

            break;
        }
        case GI_TYPE_TAG_TIME_T:
        {
            PyDateTime_DateTime *py_datetime;
            struct tm datetime;

764
            py_datetime = (PyDateTime_DateTime *) object;
Simon van der Linden's avatar
Simon van der Linden committed
765 766

            if (py_datetime->hastzinfo) {
767
                if (PyErr_WarnEx (NULL, "tzinfo ignored; only local time is supported", 1) < 0) {
Simon van der Linden's avatar
Simon van der Linden committed
768 769 770 771
                    break;
                }
            }

772 773 774 775 776 777
            datetime.tm_sec = PyDateTime_DATE_GET_SECOND (py_datetime);
            datetime.tm_min = PyDateTime_DATE_GET_MINUTE (py_datetime);
            datetime.tm_hour = PyDateTime_DATE_GET_HOUR (py_datetime);
            datetime.tm_mday = PyDateTime_GET_DAY (py_datetime);
            datetime.tm_mon = PyDateTime_GET_MONTH (py_datetime) - 1;
            datetime.tm_year = PyDateTime_GET_YEAR (py_datetime) - 1900;
Simon van der Linden's avatar
Simon van der Linden committed
778 779
            datetime.tm_isdst = -1;

780
            arg.v_long = mktime (&datetime);
781
            if (arg.v_long == -1) {
782
                PyErr_SetString (PyExc_RuntimeError, "datetime conversion failed");
Simon van der Linden's avatar
Simon van der Linden committed
783 784 785 786 787 788 789
                break;
            }

            break;
        }
        case GI_TYPE_TAG_GTYPE:
        {
790
            arg.v_long = pyg_type_from_object (object);
Simon van der Linden's avatar
Simon van der Linden committed
791 792 793 794 795 796 797

            break;
        }
        case GI_TYPE_TAG_UTF8:
        {
            const gchar *string;

798
            if (object == Py_None) {
799 800 801 802
                arg.v_string = NULL;
                break;
            }

803
            string = PyString_AsString (object);
Simon van der Linden's avatar
Simon van der Linden committed
804 805

            /* Don't need to check for errors, since g_strdup is NULL-proof. */
806
            arg.v_string = g_strdup (string);
Simon van der Linden's avatar
Simon van der Linden committed
807 808 809 810 811 812 813
            break;
        }
        case GI_TYPE_TAG_FILENAME:
        {
            GError *error = NULL;
            const gchar *string;

814
            string = PyString_AsString (object);
Simon van der Linden's avatar
Simon van der Linden committed
815 816 817 818
            if (string == NULL) {
                break;
            }

819
            arg.v_string = g_filename_from_utf8 (string, -1, NULL, NULL, &error);
Simon van der Linden's avatar
Simon van der Linden committed
820
            if (arg.v_string == NULL) {
821
                PyErr_SetString (PyExc_Exception, error->message);
Simon van der Linden's avatar
Simon van der Linden committed
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
                /* TODO: Convert the error to an exception. */
            }

            break;
        }
        case GI_TYPE_TAG_ARRAY:
        {
            Py_ssize_t length;
            gboolean is_zero_terminated;
            GITypeInfo *item_type_info;
            gsize item_size;
            GArray *array;
            GITransfer item_transfer;
            Py_ssize_t i;

837
            if (object == Py_None) {
838 839 840 841
                arg.v_pointer = NULL;
                break;
            }

842
            length = PySequence_Length (object);
Simon van der Linden's avatar
Simon van der Linden committed
843 844 845 846
            if (length < 0) {
                break;
            }

847 848
            is_zero_terminated = g_type_info_is_zero_terminated (type_info);
            item_type_info = g_type_info_get_param_type (type_info, 0);
Simon van der Linden's avatar
Simon van der Linden committed
849

850
            item_size = _pygi_g_type_info_size (item_type_info);
Simon van der Linden's avatar
Simon van der Linden committed
851

852
            array = g_array_sized_new (is_zero_terminated, FALSE, item_size, length);
Simon van der Linden's avatar
Simon van der Linden committed
853
            if (array == NULL) {
854
                g_base_info_unref ( (GIBaseInfo *) item_type_info);
Simon van der Linden's avatar
Simon van der Linden committed
855 856 857 858 859 860 861 862 863 864
                PyErr_NoMemory();
                break;
            }

            item_transfer = transfer == GI_TRANSFER_CONTAINER ? GI_TRANSFER_NOTHING : transfer;

            for (i = 0; i < length; i++) {
                PyObject *py_item;
                GArgument item;

865
                py_item = PySequence_GetItem (object, i);
Simon van der Linden's avatar
Simon van der Linden committed
866 867 868 869
                if (py_item == NULL) {
                    goto array_item_error;
                }

870
                item = _pygi_argument_from_object (py_item, item_type_info, item_transfer);
Simon van der Linden's avatar
Simon van der Linden committed
871

872
                Py_DECREF (py_item);
Simon van der Linden's avatar
Simon van der Linden committed
873 874 875 876 877

                if (PyErr_Occurred()) {
                    goto array_item_error;
                }

878
                g_array_insert_val (array, i, item);
Simon van der Linden's avatar
Simon van der Linden committed
879 880 881 882
                continue;

array_item_error:
                /* Free everything we have converted so far. */
883 884
                _pygi_argument_release ( (GArgument *) &array, type_info,
                                         GI_TRANSFER_NOTHING, GI_DIRECTION_IN);
Simon van der Linden's avatar
Simon van der Linden committed
885 886
                array = NULL;

887
                _PyGI_ERROR_PREFIX ("Item %zd: ", i);
Simon van der Linden's avatar
Simon van der Linden committed
888 889 890 891 892
                break;
            }

            arg.v_pointer = array;

893
            g_base_info_unref ( (GIBaseInfo *) item_type_info);
Simon van der Linden's avatar
Simon van der Linden committed
894 895 896 897 898 899 900
            break;
        }
        case GI_TYPE_TAG_INTERFACE:
        {
            GIBaseInfo *info;
            GIInfoType info_type;

901 902
            info = g_type_info_get_interface (type_info);
            info_type = g_base_info_get_type (info);
Simon van der Linden's avatar
Simon van der Linden committed
903 904 905

            switch (info_type) {
                case GI_INFO_TYPE_CALLBACK:
906 907
                    /* This should be handled in invoke() */
                    g_assert_not_reached();
Simon van der Linden's avatar
Simon van der Linden committed
908 909 910
                    break;
                case GI_INFO_TYPE_BOXED:
                case GI_INFO_TYPE_STRUCT:
Tomeu Vizoso's avatar
Tomeu Vizoso committed
911
                case GI_INFO_TYPE_UNION:
Simon van der Linden's avatar
Simon van der Linden committed
912 913 914
                {
                    GType type;

915 916 917 918 919
                    if (object == Py_None) {
                        arg.v_pointer = NULL;
                        break;
                    }

920
                    type = g_registered_type_info_get_g_type ( (GIRegisteredTypeInfo *) info);
Simon van der Linden's avatar
Simon van der Linden committed
921 922

                    /* Handle special cases first. */
923
                    if (g_type_is_a (type, G_TYPE_VALUE)) {
Simon van der Linden's avatar
Simon van der Linden committed
924 925 926 927
                        GValue *value;
                        GType object_type;
                        gint retval;

928
                        object_type = pyg_type_from_object ( (PyObject *) object->ob_type);
Simon van der Linden's avatar
Simon van der Linden committed
929
                        if (object_type == G_TYPE_INVALID) {
930
                            PyErr_SetString (PyExc_RuntimeError, "unable to retrieve object's GType");
Simon van der Linden's avatar
Simon van der Linden committed
931 932 933
                            break;
                        }

934
                        g_warn_if_fail (transfer == GI_TRANSFER_NOTHING);
Simon van der Linden's avatar
Simon van der Linden committed
935

936 937
                        value = g_slice_new0 (GValue);
                        g_value_init (value, object_type);
Simon van der Linden's avatar
Simon van der Linden committed
938

939
                        retval = pyg_value_from_pyobject (value, object);
Simon van der Linden's avatar
Simon van der Linden committed
940
                        if (retval < 0) {
941 942
                            g_slice_free (GValue, value);
                            PyErr_SetString (PyExc_RuntimeError, "PyObject conversion to GValue failed");
Simon van der Linden's avatar
Simon van der Linden committed
943 944 945 946
                            break;
                        }

                        arg.v_pointer = value;
947
                    } else if (g_type_is_a (type, G_TYPE_CLOSURE)) {
Simon van der Linden's avatar
Simon van der Linden committed
948 949
                        GClosure *closure;

950
                        g_warn_if_fail (transfer == GI_TRANSFER_NOTHING);
Simon van der Linden's avatar
Simon van der Linden committed
951

952
                        closure = pyg_closure_new (object, NULL, NULL);
Simon van der Linden's avatar
Simon van der Linden committed
953
                        if (closure == NULL) {
954
                            PyErr_SetString (PyExc_RuntimeError, "PyObject conversion to GClosure failed");
Simon van der Linden's avatar
Simon van der Linden committed
955 956 957 958
                            break;
                        }

                        arg.v_pointer = closure;
959 960
                    } else if (g_type_is_a (type, G_TYPE_BOXED)) {
                        arg.v_pointer = pyg_boxed_get (object, void);
Simon van der Linden's avatar
Simon van der Linden committed
961
                        if (transfer == GI_TRANSFER_EVERYTHING) {
962
                            arg.v_pointer = g_boxed_copy (type, arg.v_pointer);
Simon van der Linden's avatar
Simon van der Linden committed
963
                        }
964
                    } else if ( (type == G_TYPE_NONE) && (g_struct_info_is_foreign (info))) {
965 966
                        PyObject *result;
                        result = pygi_struct_foreign_convert_to_g_argument (
967 968 969 970
                                     object, type_info, transfer, &arg);
                    } else if (g_type_is_a (type, G_TYPE_POINTER) || type == G_TYPE_NONE) {
                        g_warn_if_fail (!g_type_info_is_pointer (type_info) || transfer == GI_TRANSFER_NOTHING);
                        arg.v_pointer = pyg_pointer_get (object, void);
Simon van der Linden's avatar
Simon van der Linden committed
971
                    } else {
972
                        PyErr_Format (PyExc_NotImplementedError, "structure type '%s' is not supported yet", g_type_name (type));
Simon van der Linden's avatar
Simon van der Linden committed
973 974 975 976 977 978 979 980 981
                    }

                    break;
                }
                case GI_INFO_TYPE_ENUM:
                case GI_INFO_TYPE_FLAGS:
                {
                    PyObject *int_;

982
                    int_ = PyNumber_Int (object);
Simon van der Linden's avatar
Simon van der Linden committed
983 984 985 986
                    if (int_ == NULL) {
                        break;
                    }

987
                    arg.v_long = PyInt_AsLong (int_);
Simon van der Linden's avatar
Simon van der Linden committed
988

989
                    Py_DECREF (int_);
Simon van der Linden's avatar
Simon van der Linden committed
990 991 992

                    break;
                }
993
                case GI_INFO_TYPE_INTERFACE:
Simon van der Linden's avatar
Simon van der Linden committed
994
                case GI_INFO_TYPE_OBJECT:
995 996 997 998 999
                    if (object == Py_None) {
                        arg.v_pointer = NULL;
                        break;
                    }

1000
                    arg.v_pointer = pygobject_get (object);
Simon van der Linden's avatar
Simon van der Linden committed
1001
                    if (transfer == GI_TRANSFER_EVERYTHING) {
1002
                        g_object_ref (arg.v_pointer);
Simon van der Linden's avatar
Simon van der Linden committed
1003 1004 1005 1006 1007 1008
                    }

                    break;
                default:
                    g_assert_not_reached();
            }
1009
            g_base_info_unref (info);
Simon van der Linden's avatar
Simon van der Linden committed
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
            break;
        }
        case GI_TYPE_TAG_GLIST:
        case GI_TYPE_TAG_GSLIST:
        {
            Py_ssize_t length;
            GITypeInfo *item_type_info;
            GSList *list = NULL;
            GITransfer item_transfer;
            Py_ssize_t i;

1021 1022 1023 1024 1025
            if (object == Py_None) {
                arg.v_pointer = NULL;
                break;
            }

1026
            length = PySequence_Length (object);
Simon van der Linden's avatar
Simon van der Linden committed
1027 1028 1029 1030
            if (length < 0) {
                break;
            }

1031 1032
            item_type_info = g_type_info_get_param_type (type_info, 0);
            g_assert (item_type_info != NULL);
Simon van der Linden's avatar
Simon van der Linden committed
1033 1034 1035 1036 1037 1038 1039

            item_transfer = transfer == GI_TRANSFER_CONTAINER ? GI_TRANSFER_NOTHING : transfer;

            for (i = length - 1; i >= 0; i--) {
                PyObject *py_item;
                GArgument item;

1040
                py_item = PySequence_GetItem (object, i);
Simon van der Linden's avatar
Simon van der Linden committed
1041 1042 1043 1044
                if (py_item == NULL) {
                    goto list_item_error;
                }

1045
                item = _pygi_argument_from_object (py_item, item_type_info, item_transfer);
Simon van der Linden's avatar
Simon van der Linden committed
1046

1047
                Py_DECREF (py_item);
Simon van der Linden's avatar
Simon van der Linden committed
1048 1049 1050 1051 1052 1053

                if (PyErr_Occurred()) {
                    goto list_item_error;
                }

                if (type_tag == GI_TYPE_TAG_GLIST) {
1054
                    list = (GSList *) g_list_prepend ( (GList *) list, item.v_pointer);
Simon van der Linden's avatar
Simon van der Linden committed
1055
                } else {
1056
                    list = g_slist_prepend (list, item.v_pointer);
Simon van der Linden's avatar
Simon van der Linden committed
1057 1058 1059 1060 1061 1062
                }

                continue;

list_item_error:
                /* Free everything we have converted so far. */
1063 1064
                _pygi_argument_release ( (GArgument *) &list, type_info,
                                         GI_TRANSFER_NOTHING, GI_DIRECTION_IN);
Simon van der Linden's avatar
Simon van der Linden committed
1065 1066
                list = NULL;

1067
                _PyGI_ERROR_PREFIX ("Item %zd: ", i);
Simon van der Linden's avatar
Simon van der Linden committed
1068 1069 1070 1071 1072
                break;
            }

            arg.v_pointer = list;

1073
            g_base_info_unref ( (GIBaseInfo *) item_type_info);
Simon van der Linden's avatar
Simon van der Linden committed
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091

            break;
        }
        case GI_TYPE_TAG_GHASH:
        {
            Py_ssize_t length;
            PyObject *keys;
            PyObject *values;
            GITypeInfo *key_type_info;
            GITypeInfo *value_type_info;
            GITypeTag key_type_tag;
            GHashFunc hash_func;
            GEqualFunc equal_func;
            GHashTable *hash_table;
            GITransfer item_transfer;
            Py_ssize_t i;


1092 1093
            if (object == Py_None) {
                arg.v_pointer = NULL;
1094 1095 1096
                break;
            }

1097
            length = PyMapping_Length (object);
Simon van der Linden's avatar
Simon van der Linden committed
1098 1099 1100 1101
            if (length < 0) {
                break;
            }

1102
            keys = PyMapping_Keys (object);
Simon van der Linden's avatar
Simon van der Linden committed
1103 1104 1105 1106
            if (keys == NULL) {
                break;
            }

1107
            values = PyMapping_Values (object);
Simon van der Linden's avatar
Simon van der Linden committed
1108
            if (values == NULL) {
1109
                Py_DECREF (keys);
Simon van der Linden's avatar
Simon van der Linden committed
1110 1111 1112
                break;
            }

1113 1114
            key_type_info = g_type_info_get_param_type (type_info, 0);
            g_assert (key_type_info != NULL);
Simon van der Linden's avatar
Simon van der Linden committed
1115

1116 1117
            value_type_info = g_type_info_get_param_type (type_info, 1);
            g_assert (value_type_info != NULL);
Simon van der Linden's avatar
Simon van der Linden committed
1118

1119
            key_type_tag = g_type_info_get_tag (key_type_info);
Simon van der Linden's avatar
Simon van der Linden committed
1120

1121
            switch (key_type_tag) {
Simon van der Linden's avatar
Simon van der Linden committed
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
                case GI_TYPE_TAG_UTF8:
                case GI_TYPE_TAG_FILENAME:
                    hash_func = g_str_hash;
                    equal_func = g_str_equal;
                    break;
                default:
                    hash_func = NULL;
                    equal_func = NULL;
            }

1132
            hash_table = g_hash_table_new (hash_func, equal_func);
Simon van der Linden's avatar
Simon van der Linden committed
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
            if (hash_table == NULL) {
                PyErr_NoMemory();
                goto hash_table_release;
            }

            item_transfer = transfer == GI_TRANSFER_CONTAINER ? GI_TRANSFER_NOTHING : transfer;

            for (i = 0; i < length; i++) {
                PyObject *py_key;
                PyObject *py_value;
                GArgument key;
                GArgument value;

1146 1147
                py_key = PyList_GET_ITEM (keys, i);
                py_value = PyList_GET_ITEM (values, i);
Simon van der Linden's avatar
Simon van der Linden committed
1148

1149
                key = _pygi_argument_from_object (py_key, key_type_info, item_transfer);
Simon van der Linden's avatar
Simon van der Linden committed
1150 1151 1152 1153
                if (PyErr_Occurred()) {
                    goto hash_table_item_error;
                }

1154
                value = _pygi_argument_from_object (py_value, value_type_info, item_transfer);
Simon van der Linden's avatar
Simon van der Linden committed
1155
                if (PyErr_Occurred()) {
1156
                    _pygi_argument_release (&key, type_info, GI_TRANSFER_NOTHING, GI_DIRECTION_IN);
Simon van der Linden's avatar
Simon van der Linden committed
1157 1158 1159
                    goto hash_table_item_error;
                }

1160
                g_hash_table_insert (hash_table, key.v_pointer, value.v_pointer);
Simon van der Linden's avatar
Simon van der Linden committed
1161 1162 1163 1164
                continue;

hash_table_item_error:
                /* Free everything we have converted so far. */
1165 1166
                _pygi_argument_release ( (GArgument *) &hash_table, type_info,
                                         GI_TRANSFER_NOTHING, GI_DIRECTION_IN);
Simon van der Linden's avatar
Simon van der Linden committed
1167 1168
                hash_table = NULL;

1169
                _PyGI_ERROR_PREFIX ("Item %zd: ", i);
Simon van der Linden's avatar
Simon van der Linden committed
1170 1171 1172 1173 1174 1175
                break;
            }

            arg.v_pointer = hash_table;

hash_table_release:
1176 1177 1178 1179
            g_base_info_unref ( (GIBaseInfo *) key_type_info);
            g_base_info_unref ( (GIBaseInfo *) value_type_info);
            Py_DECREF (keys);
            Py_DECREF (values);
Simon van der Linden's avatar
Simon van der Linden committed
1180 1181 1182
            break;
        }
        case GI_TYPE_TAG_ERROR:
1183
            PyErr_SetString (PyExc_NotImplementedError, "error marshalling is not supported yet");
Simon van der Linden's avatar
Simon van der Linden committed
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
            /* TODO */
            break;
    }

    return arg;
}

PyObject *
_pygi_argument_to_object (GArgument  *arg,
                          GITypeInfo *type_info,
                          GITransfer transfer)
{
    GITypeTag type_tag;
    PyObject *object = NULL;

1199
    type_tag = g_type_info_get_tag (type_info);
Simon van der Linden's avatar
Simon van der Linden committed
1200 1201
    switch (type_tag) {
        case GI_TYPE_TAG_VOID:
1202
            if (g_type_info_is_pointer (type_info)) {
Tomeu Vizoso's avatar
Tomeu Vizoso committed
1203
                /* Raw Python objects are passed to void* args */
1204
                g_warn_if_fail (transfer == GI_TRANSFER_NOTHING);
Tomeu Vizoso's avatar
Tomeu Vizoso committed
1205 1206 1207
                object = arg->v_pointer;
            } else
                object = Py_None;
1208
            Py_XINCREF (object);
Simon van der Linden's avatar
Simon van der Linden committed
1209 1210 1211
            break;
        case GI_TYPE_TAG_BOOLEAN:
        {
1212
            object = PyBool_FromLong (arg->v_boolean);
Simon van der Linden's avatar
Simon van der Linden committed
1213 1214 1215 1216
            break;
        }
        case GI_TYPE_TAG_INT8:
        {
1217
            object = PyInt_FromLong (arg->v_int8);
Simon van der Linden's avatar
Simon van der Linden committed
1218 1219 1220 1221
            break;
        }
        case GI_TYPE_TAG_UINT8:
        {
1222
            object = PyInt_FromLong (arg->v_uint8);
Simon van der Linden's avatar
Simon van der Linden committed
1223 1224 1225 1226
            break;
        }
        case GI_TYPE_TAG_INT16:
        {
1227
            object = PyInt_FromLong (arg->v_int16);
Simon van der Linden's avatar
Simon van der Linden committed
1228 1229 1230 1231
            break;
        }
        case GI_TYPE_TAG_UINT16:
        {
1232
            object = PyInt_FromLong (arg->v_uint16);
Simon van der Linden's avatar
Simon van der Linden committed
1233 1234 1235 1236
            break;
        }
        case GI_TYPE_TAG_INT32:
        {
1237
            object = PyInt_FromLong (arg->v_int32);
Simon van der Linden's avatar
Simon van der Linden committed
1238 1239 1240 1241
            break;
        }
        case GI_TYPE_TAG_UINT32:
        {
1242
            object = PyLong_FromLongLong (arg->v_uint32);
Simon van der Linden's avatar
Simon van der Linden committed
1243 1244 1245 1246
            break;
        }
        case GI_TYPE_TAG_INT64:
        {
1247
            object = PyLong_FromLongLong (arg->v_int64);
Simon van der Linden's avatar
Simon van der Linden committed
1248 1249 1250 1251
            break;
        }
        case GI_TYPE_TAG_UINT64:
        {
1252
            object = PyLong_FromUnsignedLongLong (arg->v_uint64);
Simon van der Linden's avatar
Simon van der Linden committed
1253 1254 1255 1256
            break;
        }
        case GI_TYPE_TAG_SHORT:
        {
1257
            object = PyInt_FromLong (arg->v_short);
Simon van der Linden's avatar
Simon van der Linden committed
1258 1259 1260 1261
            break;
        }
        case GI_TYPE_TAG_USHORT:
        {
1262
            object = PyInt_FromLong (arg->v_ushort);
Simon van der Linden's avatar
Simon van der Linden committed
1263 1264 1265 1266
            break;
        }
        case GI_TYPE_TAG_INT:
        {
1267
            object = PyInt_FromLong (arg->v_int);
Simon van der Linden's avatar
Simon van der Linden committed
1268 1269 1270 1271
            break;
        }
        case GI_TYPE_TAG_UINT:
        {
1272
            object = PyLong_FromLongLong (arg->v_uint);
Simon van der Linden's avatar
Simon van der Linden committed
1273 1274 1275 1276
            break;
        }
        case GI_TYPE_TAG_LONG:
        {
1277
            object = PyInt_FromLong (arg->v_long);
Simon van der Linden's avatar
Simon van der Linden committed
1278 1279 1280 1281
            break;
        }
        case GI_TYPE_TAG_ULONG:
        {
1282
            object = PyLong_FromUnsignedLongLong (arg->v_ulong);
Simon van der Linden's avatar
Simon van der Linden committed
1283 1284 1285 1286
            break;
        }
        case GI_TYPE_TAG_SSIZE:
        {
1287
            object = PyInt_FromLong (arg->v_ssize);
Simon van der Linden's avatar
Simon van der Linden committed
1288 1289 1290 1291
            break;
        }
        case GI_TYPE_TAG_SIZE:
        {
1292
            object = PyLong_FromUnsignedLongLong (arg->v_size);
Simon van der Linden's avatar
Simon van der Linden committed
1293 1294 1295 1296
            break;
        }
        case GI_TYPE_TAG_FLOAT:
        {
1297
            object = PyFloat_FromDouble (arg->v_float);
Simon van der Linden's avatar
Simon van der Linden committed
1298 1299 1300 1301
            break;
        }
        case GI_TYPE_TAG_DOUBLE:
        {
1302
            object = PyFloat_FromDouble (arg->v_double);
Simon van der Linden's avatar
Simon van der Linden committed
1303 1304 1305 1306 1307 1308
            break;
        }
        case GI_TYPE_TAG_TIME_T:
        {
            time_t *time_;
            struct tm *datetime;
1309

1310
            time_ = (time_t *) &arg->v_long;
1311

1312 1313 1314 1315 1316 1317 1318 1319 1320
            datetime = localtime (time_);
            object = PyDateTime_FromDateAndTime (
                         datetime->tm_year + 1900,
                         datetime->tm_mon + 1,
                         datetime->tm_mday,
                         datetime->tm_hour,
                         datetime->tm_min,
                         datetime->tm_sec,
                         0);
Simon van der Linden's avatar
Simon van der Linden committed
1321 1322 1323 1324
            break;
        }
        case GI_TYPE_TAG_GTYPE:
        {
1325
            object = pyg_type_wrapper_new ( (GType) arg->v_long);
Simon van der Linden's avatar
Simon van der Linden committed
1326 1327 1328
            break;
        }
        case GI_TYPE_TAG_UTF8:
1329 1330
            if (arg->v_string == NULL) {
                object = Py_None;
1331
                Py_INCREF (object);
1332 1333 1334
                break;
            }

1335
            object = PyString_FromString (arg->v_string);
Simon van der Linden's avatar
Simon van der Linden committed
1336 1337 1338 1339 1340 1341
            break;
        case GI_TYPE_TAG_FILENAME:
        {
            GError *error = NULL;
            gchar *string;