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 32 33 34 35 36 37 38 39 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 77 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
/* -*- Mode: C; c-basic-offset: 4 -*-
* vim: tabstop=4 shiftwidth=4 expandtab
*
* Copyright (C) 2005-2009 Johan Dahlin <johan@gnome.org>
*
* pygi-argument.c: GIArgument - 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, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <time.h>
#include "pygenum.h"
#include "pygflags.h"
#include "pygi-type.h"
#include "pygi-argument.h"
#include "pygi-basictype.h"
#include "pygi-boxed.h"
#include "pygi-error.h"
#include "pygi-foreign.h"
#include "pygi-info.h"
#include "pygi-util.h"
#include "pygi-value.h"
#include "pygi-cache-private.h"
GIArgument
pygi_argument_from_py (GITypeInfo *type_info, PyObject *object,
GITransfer transfer,
PyGIArgumentFromPyCleanupData *arg_cleanup)
{
GIArgument arg = PYGI_ARG_INIT;
PyGIArgCache *cache = pygi_arg_cache_new (
type_info, /*arg_info=*/NULL, transfer, PYGI_DIRECTION_FROM_PYTHON,
/*callable_cache=*/NULL, 0, 0);
if (!cache->from_py_marshaller (&arg_cleanup->state,
/*callable_cache=*/NULL, cache, object,
&arg, &arg_cleanup->cleanup_data)) {
if (!PyErr_Occurred ())
PyErr_Format (PyExc_TypeError,
"Unable to convert argument %R to its C equivalent",
object);
}
arg_cleanup->cache = cache;
arg_cleanup->object = object;
return arg;
}
void
pygi_argument_from_py_cleanup (PyGIArgumentFromPyCleanupData *arg_cleanup)
{
PyGIArgCache *cache = (PyGIArgCache *)arg_cleanup->cache;
if (cache) {
if (cache->from_py_cleanup != NULL
&& arg_cleanup->cleanup_data != NULL)
cache->from_py_cleanup (&arg_cleanup->state, cache,
arg_cleanup->object,
arg_cleanup->cleanup_data, TRUE);
pygi_arg_cache_free (cache);
memset (arg_cleanup, 0, sizeof (PyGIArgumentFromPyCleanupData));
}
}
PyObject *
pygi_argument_to_py (GITypeInfo *type_info, GIArgument arg,
GITransfer transfer)
{
PyGIInvokeState state = { 0 };
gpointer cleanup_data = NULL;
PyObject *object;
PyGIArgCache *cache = pygi_arg_cache_new (
type_info, /*arg_info=*/NULL, transfer, PYGI_DIRECTION_TO_PYTHON,
/*callable_cache=*/NULL, 0, 0);
object = cache->to_py_marshaller (&state, /*callable_cache=*/NULL, cache,
&arg, &cleanup_data);
if (cache->to_py_cleanup && arg.v_pointer)
cache->to_py_cleanup (&state, cache, cleanup_data, arg.v_pointer,
TRUE);
pygi_arg_cache_free (cache);
return object;
}
PyObject *
pygi_argument_to_py_with_array_length (GITypeInfo *type_info, GIArgument arg,
GITransfer transfer, gsize array_length)
{
PyGIInvokeState state = { 0 };
PyGIInvokeArgState arg_state = { 0 };
gpointer cleanup_data = NULL;
PyObject *object;
PyGIArgCache *cache = pygi_arg_garray_new_from_info (
type_info, /*arg_info=*/NULL, transfer, PYGI_DIRECTION_TO_PYTHON,
/*callable_cache=*/NULL, 0, NULL);
arg_state.arg_value = (GIArgument){ .v_size = array_length };
state.args = &arg_state;
((PyGIArgGArray *)cache)->has_len_arg = TRUE;
object = cache->to_py_marshaller (&state, /*callable_cache=*/NULL, cache,
&arg, &cleanup_data);
if (cache->to_py_cleanup && arg.v_pointer)
cache->to_py_cleanup (&state, cache, cleanup_data, arg.v_pointer,
TRUE);
pygi_arg_cache_free (cache);
return object;
}
gboolean
pygi_argument_to_gsize (GIArgument arg, GITypeTag type_tag, gsize *gsize_out)
{
switch (type_tag) {
case GI_TYPE_TAG_INT8:
*gsize_out = arg.v_int8;
return TRUE;
case GI_TYPE_TAG_UINT8:
*gsize_out = arg.v_uint8;
return TRUE;
case GI_TYPE_TAG_INT16:
*gsize_out = arg.v_int16;
return TRUE;
case GI_TYPE_TAG_UINT16:
*gsize_out = arg.v_uint16;
return TRUE;
case GI_TYPE_TAG_INT32:
*gsize_out = arg.v_int32;
return TRUE;
case GI_TYPE_TAG_UINT32:
case GI_TYPE_TAG_UNICHAR:
*gsize_out = arg.v_uint32;
return TRUE;
case GI_TYPE_TAG_INT64:
if (arg.v_uint64 > G_MAXSIZE) {
PyErr_Format (PyExc_TypeError, "Unable to marshal %s to gsize",
gi_type_tag_to_string (type_tag));
return FALSE;
}
*gsize_out = (gsize)arg.v_int64;
return TRUE;
case GI_TYPE_TAG_UINT64:
if (arg.v_uint64 > G_MAXSIZE) {
PyErr_Format (PyExc_TypeError, "Unable to marshal %s to gsize",
gi_type_tag_to_string (type_tag));
return FALSE;
}
*gsize_out = (gsize)arg.v_uint64;
return TRUE;
case GI_TYPE_TAG_VOID:
case GI_TYPE_TAG_BOOLEAN:
case GI_TYPE_TAG_FLOAT:
case GI_TYPE_TAG_DOUBLE:
case GI_TYPE_TAG_GTYPE:
case GI_TYPE_TAG_UTF8:
case GI_TYPE_TAG_FILENAME:
case GI_TYPE_TAG_ARRAY:
case GI_TYPE_TAG_INTERFACE:
case GI_TYPE_TAG_GLIST:
case GI_TYPE_TAG_GSLIST:
case GI_TYPE_TAG_GHASH:
case GI_TYPE_TAG_ERROR:
PyErr_Format (PyExc_TypeError, "Unable to marshal %s to gsize",
gi_type_tag_to_string (type_tag));
return FALSE;
default:
g_assert_not_reached ();
}
}
static GITypeTag
_pygi_get_storage_type (GITypeInfo *type_info)
{
GITypeTag type_tag = gi_type_info_get_tag (type_info);
if (type_tag == GI_TYPE_TAG_INTERFACE) {
GIBaseInfo *interface = gi_type_info_get_interface (type_info);
if (GI_IS_ENUM_INFO (interface))
type_tag = gi_enum_info_get_storage_type ((GIEnumInfo *)interface);
/* FIXME: we might have something to do for other types */
gi_base_info_unref (interface);
}
return type_tag;
}
void
_pygi_hash_pointer_to_arg_in_place (GIArgument *arg, GITypeInfo *type_info)
{
GITypeTag type_tag = _pygi_get_storage_type (type_info);
switch (type_tag) {
case GI_TYPE_TAG_INT8:
arg->v_int8 = (gint8)GPOINTER_TO_INT (arg->v_pointer);
break;
case GI_TYPE_TAG_INT16:
arg->v_int16 = (gint16)GPOINTER_TO_INT (arg->v_pointer);
break;
case GI_TYPE_TAG_INT32:
arg->v_int32 = (gint32)GPOINTER_TO_INT (arg->v_pointer);
break;
case GI_TYPE_TAG_INT64:
arg->v_int64 = (gint64)GPOINTER_TO_INT (arg->v_pointer);
break;
case GI_TYPE_TAG_UINT8:
arg->v_uint8 = (guint8)GPOINTER_TO_UINT (arg->v_pointer);
break;
case GI_TYPE_TAG_UINT16:
arg->v_uint16 = (guint16)GPOINTER_TO_UINT (arg->v_pointer);
break;
case GI_TYPE_TAG_UINT32:
case GI_TYPE_TAG_UNICHAR:
arg->v_uint32 = (guint32)GPOINTER_TO_UINT (arg->v_pointer);
break;
case GI_TYPE_TAG_UINT64:
arg->v_uint64 = (guint64)GPOINTER_TO_UINT (arg->v_pointer);
break;
case GI_TYPE_TAG_GTYPE:
arg->v_size = GPOINTER_TO_SIZE (arg->v_pointer);
break;
case GI_TYPE_TAG_VOID:
case GI_TYPE_TAG_BOOLEAN:
case GI_TYPE_TAG_FLOAT:
case GI_TYPE_TAG_DOUBLE:
case GI_TYPE_TAG_UTF8:
case GI_TYPE_TAG_FILENAME:
case GI_TYPE_TAG_INTERFACE:
case GI_TYPE_TAG_ARRAY:
case GI_TYPE_TAG_GLIST:
case GI_TYPE_TAG_GSLIST:
case GI_TYPE_TAG_GHASH:
case GI_TYPE_TAG_ERROR:
break;
default:
g_assert_not_reached ();
}
}
gpointer
_pygi_arg_to_hash_pointer (const GIArgument arg, GITypeInfo *type_info)
{
GITypeTag type_tag = _pygi_get_storage_type (type_info);
switch (type_tag) {
case GI_TYPE_TAG_INT8:
return GINT_TO_POINTER (arg.v_int8);
case GI_TYPE_TAG_UINT8:
return GUINT_TO_POINTER (arg.v_uint8);
case GI_TYPE_TAG_INT16:
return GINT_TO_POINTER (arg.v_int16);
case GI_TYPE_TAG_UINT16:
return GUINT_TO_POINTER (arg.v_uint16);
case GI_TYPE_TAG_INT32:
return GINT_TO_POINTER (arg.v_int32);
case GI_TYPE_TAG_UINT32:
case GI_TYPE_TAG_UNICHAR:
return GUINT_TO_POINTER (arg.v_uint32);
case GI_TYPE_TAG_INT64:
return GINT_TO_POINTER (arg.v_int64);
case GI_TYPE_TAG_UINT64:
return GUINT_TO_POINTER (arg.v_uint64);
case GI_TYPE_TAG_GTYPE:
return GSIZE_TO_POINTER (arg.v_size);
case GI_TYPE_TAG_UTF8:
case GI_TYPE_TAG_FILENAME:
case GI_TYPE_TAG_INTERFACE:
case GI_TYPE_TAG_ARRAY:
return arg.v_pointer;
case GI_TYPE_TAG_VOID:
case GI_TYPE_TAG_BOOLEAN:
case GI_TYPE_TAG_FLOAT:
case GI_TYPE_TAG_DOUBLE:
case GI_TYPE_TAG_GLIST:
case GI_TYPE_TAG_GSLIST:
case GI_TYPE_TAG_GHASH:
case GI_TYPE_TAG_ERROR:
g_critical ("Unsupported type %s", gi_type_tag_to_string (type_tag));
return arg.v_pointer;
default:
g_assert_not_reached ();
}
}
|