pygi-marshal-to-py.c 22 KB
Newer Older
1 2 3 4 5
/* -*- Mode: C; c-basic-offset: 4 -*-
 * vim: tabstop=4 shiftwidth=4 expandtab
 *
 * Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>,  Red Hat, Inc.
 *
6
 *   pygi-marshal-from-py.c: functions for converting C types to PyObject
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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"
25 26
#include "pygobject-private.h"
#include "pygparamspec.h"
27 28 29 30

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

31
#include <pyglib.h>
32 33 34 35
#include <pyglib-python-compat.h>

#include "pygi-cache.h"
#include "pygi-marshal-cleanup.h"
36
#include "pygi-marshal-to-py.h"
37
#include "pygi-argument.h"
38

39
static gboolean
David Malcolm's avatar
David Malcolm committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
gi_argument_to_c_long (GIArgument *arg_in,
                       long *c_long_out,
                       GITypeTag type_tag)
{
    switch (type_tag) {
      case GI_TYPE_TAG_INT8:
          *c_long_out = arg_in->v_int8;
          return TRUE;
      case GI_TYPE_TAG_UINT8:
          *c_long_out = arg_in->v_uint8;
          return TRUE;
      case GI_TYPE_TAG_INT16:
          *c_long_out = arg_in->v_int16;
          return TRUE;
      case GI_TYPE_TAG_UINT16:
          *c_long_out = arg_in->v_uint16;
          return TRUE;
      case GI_TYPE_TAG_INT32:
          *c_long_out = arg_in->v_int32;
          return TRUE;
      case GI_TYPE_TAG_UINT32:
          *c_long_out = arg_in->v_uint32;
          return TRUE;
      case GI_TYPE_TAG_INT64:
          *c_long_out = arg_in->v_int64;
          return TRUE;
      case GI_TYPE_TAG_UINT64:
          *c_long_out = arg_in->v_uint64;
          return TRUE;
      default:
          PyErr_Format (PyExc_TypeError,
                        "Unable to marshal %s to C long",
                        g_type_tag_to_string (type_tag));
          return FALSE;
    }
}

77
static gboolean
David Malcolm's avatar
David Malcolm committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
gi_argument_to_gsize (GIArgument *arg_in,
                      gsize      *gsize_out,
                      GITypeTag   type_tag)
{
    switch (type_tag) {
      case GI_TYPE_TAG_INT8:
          *gsize_out = arg_in->v_int8;
          return TRUE;
      case GI_TYPE_TAG_UINT8:
          *gsize_out = arg_in->v_uint8;
          return TRUE;
      case GI_TYPE_TAG_INT16:
          *gsize_out = arg_in->v_int16;
          return TRUE;
      case GI_TYPE_TAG_UINT16:
          *gsize_out = arg_in->v_uint16;
          return TRUE;
      case GI_TYPE_TAG_INT32:
          *gsize_out = arg_in->v_int32;
          return TRUE;
      case GI_TYPE_TAG_UINT32:
          *gsize_out = arg_in->v_uint32;
          return TRUE;
      case GI_TYPE_TAG_INT64:
          *gsize_out = arg_in->v_int64;
          return TRUE;
      case GI_TYPE_TAG_UINT64:
          *gsize_out = arg_in->v_uint64;
          return TRUE;
      default:
          PyErr_Format (PyExc_TypeError,
                        "Unable to marshal %s to gsize",
                        g_type_tag_to_string (type_tag));
          return FALSE;
    }
}

115
PyObject *
116 117 118 119
_pygi_marshal_to_py_array (PyGIInvokeState   *state,
                           PyGICallableCache *callable_cache,
                           PyGIArgCache      *arg_cache,
                           GIArgument        *arg)
120 121 122 123
{
    GArray *array_;
    PyObject *py_obj = NULL;
    PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;
124
    PyGIArgGArray *array_cache = (PyGIArgGArray *)arg_cache;
125 126 127 128 129 130
    gsize processed_items = 0;

     /* GArrays make it easier to iterate over arrays
      * with different element sizes but requires that
      * we allocate a GArray if the argument was a C array
      */
131
    if (array_cache->array_type == GI_ARRAY_TYPE_C) {
132
        gsize len;
133
        if (array_cache->fixed_size >= 0) {
134
            g_assert(arg->v_pointer != NULL);
135 136
            len = array_cache->fixed_size;
        } else if (array_cache->is_zero_terminated) {
137 138 139
            if (arg->v_pointer == NULL) {
                len = 0;
            } else if (seq_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8) {
140 141 142 143
                len = strlen (arg->v_pointer);
            } else {
                len = g_strv_length ((gchar **)arg->v_pointer);
            }
144
        } else {
145
            GIArgument *len_arg = state->args[array_cache->len_arg_index];
146
            PyGIArgCache *arg_cache = _pygi_callable_cache_get_arg (callable_cache,
147
                                                                    array_cache->len_arg_index);
David Malcolm's avatar
David Malcolm committed
148

149
            if (!gi_argument_to_gsize (len_arg, &len, arg_cache->type_tag)) {
David Malcolm's avatar
David Malcolm committed
150 151
                return NULL;
            }
152 153 154 155
        }

        array_ = g_array_new (FALSE,
                              FALSE,
156
                              array_cache->item_size);
157 158 159
        if (array_ == NULL) {
            PyErr_NoMemory ();

160
            if (arg_cache->transfer == GI_TRANSFER_EVERYTHING && arg->v_pointer != NULL)
161 162 163 164
                g_free (arg->v_pointer);

            return NULL;
        }
165 166 167

        if (array_->data != NULL) 
            g_free (array_->data);
168 169
        array_->data = arg->v_pointer;
        array_->len = len;
170 171
    } else {
        array_ = arg->v_pointer;
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    }

    if (seq_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8) {
        if (arg->v_pointer == NULL) {
            py_obj = PYGLIB_PyBytes_FromString ("");
        } else {
            py_obj = PYGLIB_PyBytes_FromStringAndSize (array_->data, array_->len);
        }
    } else {
        if (arg->v_pointer == NULL) {
            py_obj = PyList_New (0);
        } else {
            int i;

            gsize item_size;
187
            PyGIMarshalToPyFunc item_to_py_marshaller;
188 189 190 191 192 193 194 195
            PyGIArgCache *item_arg_cache;

            py_obj = PyList_New (array_->len);
            if (py_obj == NULL)
                goto err;


            item_arg_cache = seq_cache->item_cache;
196
            item_to_py_marshaller = item_arg_cache->to_py_marshaller;
197 198 199 200

            item_size = g_array_get_element_size (array_);

            for (i = 0; i < array_->len; i++) {
201
                GIArgument item_arg = {0};
202 203
                PyObject *py_item;

204 205 206 207
                /* If we are receiving an array of pointers, simply assign the pointer
                 * and move on, letting the per-item marshaler deal with the
                 * various transfer modes and ref counts (e.g. g_variant_ref_sink).
                 */
208
                if (array_cache->array_type == GI_ARRAY_TYPE_PTR_ARRAY) {
209
                    item_arg.v_pointer = g_ptr_array_index ( ( GPtrArray *)array_, i);
210 211 212 213

                } else if (item_arg_cache->is_pointer) {
                    item_arg.v_pointer = g_array_index (array_, gpointer, i);

214
                } else if (item_arg_cache->type_tag == GI_TYPE_TAG_INTERFACE) {
215 216
                    PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *) item_arg_cache;

217
                    // FIXME: This probably doesn't work with boxed types or gvalues. See fx. _pygi_marshal_from_py_array()
218 219
                    switch (g_base_info_get_type (iface_cache->interface_info)) {
                        case GI_INFO_TYPE_STRUCT:
220
                            if (arg_cache->transfer == GI_TRANSFER_EVERYTHING &&
221
                                       !g_type_is_a (iface_cache->g_type, G_TYPE_BOXED)) {
222
                                /* array elements are structs */
223 224 225 226
                                gpointer *_struct = g_malloc (item_size);
                                memcpy (_struct, array_->data + i * item_size,
                                        item_size);
                                item_arg.v_pointer = _struct;
227
                            } else {
228
                                item_arg.v_pointer = array_->data + i * item_size;
229
                            }
230 231 232 233
                            break;
                        default:
                            item_arg.v_pointer = g_array_index (array_, gpointer, i);
                            break;
234 235 236 237 238
                    }
                } else {
                    memcpy (&item_arg, array_->data + i * item_size, item_size);
                }

239
                py_item = item_to_py_marshaller ( state,
240 241 242 243 244 245 246
                                                callable_cache,
                                                item_arg_cache,
                                                &item_arg);

                if (py_item == NULL) {
                    Py_CLEAR (py_obj);

247
                    if (array_cache->array_type == GI_ARRAY_TYPE_C)
248 249 250 251 252 253 254 255 256 257
                        g_array_unref (array_);

                    goto err;
                }
                PyList_SET_ITEM (py_obj, i, py_item);
                processed_items++;
            }
        }
    }

258
    if (array_cache->array_type == GI_ARRAY_TYPE_C)
259 260 261 262 263
        g_array_free (array_, FALSE);

    return py_obj;

err:
264
    if (array_cache->array_type == GI_ARRAY_TYPE_C) {
265 266 267
        g_array_free (array_, arg_cache->transfer == GI_TRANSFER_EVERYTHING);
    } else {
        /* clean up unprocessed items */
268
        if (seq_cache->item_cache->to_py_cleanup != NULL) {
269
            int j;
270
            PyGIMarshalCleanupFunc cleanup_func = seq_cache->item_cache->to_py_cleanup;
271 272 273
            for (j = processed_items; j < array_->len; j++) {
                cleanup_func (state,
                              seq_cache->item_cache,
274
                              NULL,
275 276 277 278 279 280 281 282 283 284 285 286 287
                              g_array_index (array_, gpointer, j),
                              FALSE);
            }
        }

        if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
            g_array_free (array_, TRUE);
    }

    return NULL;
}

PyObject *
288 289 290 291
_pygi_marshal_to_py_glist (PyGIInvokeState   *state,
                           PyGICallableCache *callable_cache,
                           PyGIArgCache      *arg_cache,
                           GIArgument        *arg)
292 293 294 295 296
{
    GList *list_;
    gsize length;
    gsize i;

297
    PyGIMarshalToPyFunc item_to_py_marshaller;
298 299 300 301 302 303 304 305 306 307 308 309 310
    PyGIArgCache *item_arg_cache;
    PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;

    PyObject *py_obj = NULL;

    list_ = arg->v_pointer;
    length = g_list_length (list_);

    py_obj = PyList_New (length);
    if (py_obj == NULL)
        return NULL;

    item_arg_cache = seq_cache->item_cache;
311
    item_to_py_marshaller = item_arg_cache->to_py_marshaller;
312 313 314 315 316 317

    for (i = 0; list_ != NULL; list_ = g_list_next (list_), i++) {
        GIArgument item_arg;
        PyObject *py_item;

        item_arg.v_pointer = list_->data;
318 319 320 321 322
        _pygi_hash_pointer_to_arg (&item_arg, item_arg_cache->type_tag);
        py_item = item_to_py_marshaller (state,
                                         callable_cache,
                                         item_arg_cache,
                                         &item_arg);
323 324 325 326 327 328 329 330 331 332 333 334 335 336

        if (py_item == NULL) {
            Py_CLEAR (py_obj);
            _PyGI_ERROR_PREFIX ("Item %zu: ", i);
            return NULL;
        }

        PyList_SET_ITEM (py_obj, i, py_item);
    }

    return py_obj;
}

PyObject *
337 338 339 340
_pygi_marshal_to_py_gslist (PyGIInvokeState   *state,
                            PyGICallableCache *callable_cache,
                            PyGIArgCache      *arg_cache,
                            GIArgument        *arg)
341 342 343 344 345
{
    GSList *list_;
    gsize length;
    gsize i;

346
    PyGIMarshalToPyFunc item_to_py_marshaller;
347 348 349 350 351 352 353 354 355 356 357 358 359
    PyGIArgCache *item_arg_cache;
    PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;

    PyObject *py_obj = NULL;

    list_ = arg->v_pointer;
    length = g_slist_length (list_);

    py_obj = PyList_New (length);
    if (py_obj == NULL)
        return NULL;

    item_arg_cache = seq_cache->item_cache;
360
    item_to_py_marshaller = item_arg_cache->to_py_marshaller;
361 362 363 364 365 366

    for (i = 0; list_ != NULL; list_ = g_slist_next (list_), i++) {
        GIArgument item_arg;
        PyObject *py_item;

        item_arg.v_pointer = list_->data;
367 368
        _pygi_hash_pointer_to_arg (&item_arg, item_arg_cache->type_tag);
        py_item = item_to_py_marshaller (state,
369 370
                                        callable_cache,
                                        item_arg_cache,
371
                                        &item_arg);
372 373 374 375 376 377 378 379 380 381 382 383 384 385

        if (py_item == NULL) {
            Py_CLEAR (py_obj);
            _PyGI_ERROR_PREFIX ("Item %zu: ", i);
            return NULL;
        }

        PyList_SET_ITEM (py_obj, i, py_item);
    }

    return py_obj;
}

PyObject *
386 387 388 389
_pygi_marshal_to_py_gerror (PyGIInvokeState   *state,
                            PyGICallableCache *callable_cache,
                            PyGIArgCache      *arg_cache,
                            GIArgument        *arg)
390
{
391
    GError *error = arg->v_pointer;
392 393
    PyObject *py_obj = NULL;

394 395 396 397 398 399 400 401 402 403 404
    py_obj = pyglib_error_marshal(&error);

    if (arg_cache->transfer == GI_TRANSFER_EVERYTHING && error != NULL) {
        g_error_free (error);
    }

    if (py_obj != NULL) {
        return py_obj;
    } else {
        Py_RETURN_NONE;
    }
405 406 407
}

PyObject *
408 409 410 411
_pygi_marshal_to_py_interface_callback (PyGIInvokeState   *state,
                                        PyGICallableCache *callable_cache,
                                        PyGIArgCache      *arg_cache,
                                        GIArgument        *arg)
412 413 414 415
{
    PyObject *py_obj = NULL;

    PyErr_Format (PyExc_NotImplementedError,
416
                  "Marshalling a callback to PyObject is not supported");
417 418 419 420
    return py_obj;
}

PyObject *
421 422 423 424
_pygi_marshal_to_py_interface_enum (PyGIInvokeState   *state,
                                    PyGICallableCache *callable_cache,
                                    PyGIArgCache      *arg_cache,
                                    GIArgument        *arg)
425 426 427
{
    PyObject *py_obj = NULL;
    PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
David Malcolm's avatar
David Malcolm committed
428 429 430 431 432 433 434 435 436 437
    GIBaseInfo *interface;
    long c_long;

    interface = g_type_info_get_interface (arg_cache->type_info);
    g_assert (g_base_info_get_type (interface) == GI_INFO_TYPE_ENUM);

    if (!gi_argument_to_c_long(arg, &c_long,
                               g_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
        return NULL;
    }
438 439

    if (iface_cache->g_type == G_TYPE_NONE) {
David Malcolm's avatar
David Malcolm committed
440
        py_obj = PyObject_CallFunction (iface_cache->py_type, "l", c_long);
441
    } else {
David Malcolm's avatar
David Malcolm committed
442
        py_obj = pyg_enum_from_gtype (iface_cache->g_type, c_long);
443
    }
444
    g_base_info_unref (interface);
445 446 447 448
    return py_obj;
}

PyObject *
449 450 451 452
_pygi_marshal_to_py_interface_flags (PyGIInvokeState   *state,
                                     PyGICallableCache *callable_cache,
                                     PyGIArgCache      *arg_cache,
                                     GIArgument        *arg)
453 454 455
{
    PyObject *py_obj = NULL;
    PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
David Malcolm's avatar
David Malcolm committed
456 457 458 459 460 461 462 463
    GIBaseInfo *interface;
    long c_long;

    interface = g_type_info_get_interface (arg_cache->type_info);
    g_assert (g_base_info_get_type (interface) == GI_INFO_TYPE_FLAGS);

    if (!gi_argument_to_c_long(arg, &c_long,
                               g_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
464
        g_base_info_unref (interface);
David Malcolm's avatar
David Malcolm committed
465 466
        return NULL;
    }
467

468
    g_base_info_unref (interface);
469 470 471 472 473 474 475 476 477 478
    if (iface_cache->g_type == G_TYPE_NONE) {
        /* An enum with a GType of None is an enum without GType */

        PyObject *py_type = _pygi_type_import_by_gi_info (iface_cache->interface_info);
        PyObject *py_args = NULL;

        if (!py_type)
            return NULL;

        py_args = PyTuple_New (1);
David Malcolm's avatar
David Malcolm committed
479
        if (PyTuple_SetItem (py_args, 0, PyLong_FromLong (c_long)) != 0) {
480 481 482 483 484
            Py_DECREF (py_args);
            Py_DECREF (py_type);
            return NULL;
        }

David Malcolm's avatar
David Malcolm committed
485
        py_obj = PyObject_CallFunction (py_type, "l", c_long);
486 487 488 489

        Py_DECREF (py_args);
        Py_DECREF (py_type);
    } else {
David Malcolm's avatar
David Malcolm committed
490
        py_obj = pyg_flags_from_gtype (iface_cache->g_type, c_long);
491 492 493 494 495 496
    }

    return py_obj;
}

PyObject *
497 498 499 500
_pygi_marshal_to_py_interface_struct_cache_adapter (PyGIInvokeState   *state,
                                                    PyGICallableCache *callable_cache,
                                                    PyGIArgCache      *arg_cache,
                                                    GIArgument        *arg)
501 502 503
{
    PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;

504 505 506 507 508 509 510
    return _pygi_marshal_to_py_interface_struct (arg,
                                                 iface_cache->interface_info,
                                                 iface_cache->g_type,
                                                 iface_cache->py_type,
                                                 arg_cache->transfer,
                                                 arg_cache->is_caller_allocates,
                                                 iface_cache->is_foreign);
511 512 513
}

PyObject *
514 515 516 517
_pygi_marshal_to_py_interface_interface (PyGIInvokeState   *state,
                                         PyGICallableCache *callable_cache,
                                         PyGIArgCache      *arg_cache,
                                         GIArgument        *arg)
518 519 520 521 522 523 524 525 526
{
    PyObject *py_obj = NULL;

    PyErr_Format (PyExc_NotImplementedError,
                  "Marshalling for this type is not implemented yet");
    return py_obj;
}

PyObject *
527 528 529 530
_pygi_marshal_to_py_interface_boxed (PyGIInvokeState   *state,
                                     PyGICallableCache *callable_cache,
                                     PyGIArgCache      *arg_cache,
                                     GIArgument        *arg)
531 532 533 534 535 536 537 538 539
{
    PyObject *py_obj = NULL;

    PyErr_Format (PyExc_NotImplementedError,
                  "Marshalling for this type is not implemented yet");
    return py_obj;
}

PyObject *
540 541 542 543
_pygi_marshal_to_py_interface_object_cache_adapter (PyGIInvokeState   *state,
                                                    PyGICallableCache *callable_cache,
                                                    PyGIArgCache      *arg_cache,
                                                    GIArgument        *arg)
544
{
545
    return _pygi_marshal_to_py_object(arg, arg_cache->transfer);
546 547 548
}

PyObject *
549 550 551 552
_pygi_marshal_to_py_interface_union  (PyGIInvokeState   *state,
                                      PyGICallableCache *callable_cache,
                                      PyGIArgCache      *arg_cache,
                                      GIArgument        *arg)
553 554 555 556 557 558 559
{
    PyObject *py_obj = NULL;

    PyErr_Format (PyExc_NotImplementedError,
                  "Marshalling for this type is not implemented yet");
    return py_obj;
}
560 561

PyObject *
562
_pygi_marshal_to_py_object (GIArgument *arg, GITransfer transfer) {
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
    PyObject *pyobj;

    if (arg->v_pointer == NULL) {
        pyobj = Py_None;
        Py_INCREF (pyobj);

    } else if (G_IS_PARAM_SPEC(arg->v_pointer)) {
        pyobj = pyg_param_spec_new (arg->v_pointer);
        if (transfer == GI_TRANSFER_EVERYTHING)
            g_param_spec_unref (arg->v_pointer);

    } else {
         pyobj = pygobject_new_full (arg->v_pointer,
                                     /*steal=*/ transfer == GI_TRANSFER_EVERYTHING,
                                     /*type=*/  NULL);
    }

    return pyobj;
}
582 583

PyObject *
584 585 586 587 588 589 590
_pygi_marshal_to_py_interface_struct (GIArgument *arg,
                                      GIInterfaceInfo *interface_info,
                                      GType g_type,
                                      PyObject *py_type,
                                      GITransfer transfer,
                                      gboolean is_allocated,
                                      gboolean is_foreign)
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
{
    PyObject *py_obj = NULL;

    if (arg->v_pointer == NULL) {
        Py_RETURN_NONE;
    }

    if (g_type_is_a (g_type, G_TYPE_VALUE)) {
        py_obj = pyg_value_as_pyobject (arg->v_pointer, FALSE);
    } else if (is_foreign) {
        py_obj = pygi_struct_foreign_convert_from_g_argument (interface_info,
                                                              arg->v_pointer);
    } else if (g_type_is_a (g_type, G_TYPE_BOXED)) {
        if (py_type) {
            py_obj = _pygi_boxed_new ((PyTypeObject *) py_type,
                                      arg->v_pointer,
                                      transfer == GI_TRANSFER_EVERYTHING || is_allocated,
                                      is_allocated ?
                                              g_struct_info_get_size(interface_info) : 0);
        }
    } else if (g_type_is_a (g_type, G_TYPE_POINTER)) {
        if (py_type == NULL ||
                !PyType_IsSubtype ((PyTypeObject *) py_type, &PyGIStruct_Type)) {
            g_warn_if_fail (transfer == GI_TRANSFER_NOTHING);
            py_obj = pyg_pointer_new (g_type, arg->v_pointer);
        } else {
            py_obj = _pygi_struct_new ( (PyTypeObject *) py_type,
                                       arg->v_pointer,
                                       transfer == GI_TRANSFER_EVERYTHING);
        }
    } else if (g_type_is_a (g_type, G_TYPE_VARIANT)) {
622 623 624
        /* Note: sink the variant (add a ref) only if we are not transfered ownership.
         * GLib.Variant overrides __del__ which will then call "g_variant_unref" for
         * cleanup in either case. */
625
        if (py_type) {
626 627 628
            if (transfer == GI_TRANSFER_NOTHING) {
                g_variant_ref_sink (arg->v_pointer);
            }
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
            py_obj = _pygi_struct_new ((PyTypeObject *) py_type,
                                       arg->v_pointer,
                                       FALSE);
        }
    } else if (g_type == G_TYPE_NONE) {
        if (py_type) {
            py_obj = _pygi_struct_new ((PyTypeObject *) py_type,
                                       arg->v_pointer,
                                       transfer == GI_TRANSFER_EVERYTHING);
        }
    } else {
        PyErr_Format (PyExc_NotImplementedError,
                      "structure type '%s' is not supported yet",
                      g_type_name (g_type));
    }

    return py_obj;
}