pygi-marshal-to-py.c 4.81 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
PyObject *
78 79 80 81
_pygi_marshal_to_py_interface_enum (PyGIInvokeState   *state,
                                    PyGICallableCache *callable_cache,
                                    PyGIArgCache      *arg_cache,
                                    GIArgument        *arg)
82 83 84
{
    PyObject *py_obj = NULL;
    PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
David Malcolm's avatar
David Malcolm committed
85 86 87 88 89 90 91 92 93 94
    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;
    }
95 96

    if (iface_cache->g_type == G_TYPE_NONE) {
David Malcolm's avatar
David Malcolm committed
97
        py_obj = PyObject_CallFunction (iface_cache->py_type, "l", c_long);
98
    } else {
David Malcolm's avatar
David Malcolm committed
99
        py_obj = pyg_enum_from_gtype (iface_cache->g_type, c_long);
100
    }
101
    g_base_info_unref (interface);
102 103 104 105
    return py_obj;
}

PyObject *
106 107 108 109
_pygi_marshal_to_py_interface_flags (PyGIInvokeState   *state,
                                     PyGICallableCache *callable_cache,
                                     PyGIArgCache      *arg_cache,
                                     GIArgument        *arg)
110 111 112
{
    PyObject *py_obj = NULL;
    PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
David Malcolm's avatar
David Malcolm committed
113 114 115 116 117 118 119 120
    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))) {
121
        g_base_info_unref (interface);
David Malcolm's avatar
David Malcolm committed
122 123
        return NULL;
    }
124

125
    g_base_info_unref (interface);
126 127 128 129 130 131 132 133 134 135
    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
136
        if (PyTuple_SetItem (py_args, 0, PyLong_FromLong (c_long)) != 0) {
137 138 139 140 141
            Py_DECREF (py_args);
            Py_DECREF (py_type);
            return NULL;
        }

David Malcolm's avatar
David Malcolm committed
142
        py_obj = PyObject_CallFunction (py_type, "l", c_long);
143 144 145 146

        Py_DECREF (py_args);
        Py_DECREF (py_type);
    } else {
David Malcolm's avatar
David Malcolm committed
147
        py_obj = pyg_flags_from_gtype (iface_cache->g_type, c_long);
148 149 150 151 152
    }

    return py_obj;
}