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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
/* -*- Mode: C; c-basic-offset: 4 -*-
* vim: tabstop=4 shiftwidth=4 expandtab
*
* Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>
* Copyright (C) 2014 Simon Feltman <sfeltman@gnome.org>
*
* 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 "pygi-type.h"
#include "pygenum.h"
#include "pygflags.h"
#include "pygi-cache-private.h"
static gboolean
gi_argument_from_c_long (GIArgument *arg_out, long c_long_in,
GITypeTag type_tag)
{
switch (type_tag) {
case GI_TYPE_TAG_INT8:
arg_out->v_int8 = (gint8)c_long_in;
return TRUE;
case GI_TYPE_TAG_UINT8:
arg_out->v_uint8 = (guint8)c_long_in;
return TRUE;
case GI_TYPE_TAG_INT16:
arg_out->v_int16 = (gint16)c_long_in;
return TRUE;
case GI_TYPE_TAG_UINT16:
arg_out->v_uint16 = (guint16)c_long_in;
return TRUE;
case GI_TYPE_TAG_INT32:
arg_out->v_int32 = (gint32)c_long_in;
return TRUE;
case GI_TYPE_TAG_UINT32:
case GI_TYPE_TAG_UNICHAR:
arg_out->v_uint32 = (guint32)c_long_in;
return TRUE;
case GI_TYPE_TAG_INT64:
arg_out->v_int64 = (gint64)c_long_in;
return TRUE;
case GI_TYPE_TAG_UINT64:
arg_out->v_uint64 = (guint64)c_long_in;
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 C long %ld to %s",
c_long_in, gi_type_tag_to_string (type_tag));
return FALSE;
default:
g_assert_not_reached ();
}
}
static gboolean
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:
case GI_TYPE_TAG_UNICHAR:
*c_long_out = arg_in->v_uint32;
return TRUE;
case GI_TYPE_TAG_INT64:
if (arg_in->v_int64 > G_MAXLONG || arg_in->v_int64 < G_MINLONG) {
PyErr_Format (PyExc_TypeError, "Unable to marshal %s to C long",
gi_type_tag_to_string (type_tag));
return FALSE;
}
*c_long_out = (glong)arg_in->v_int64;
return TRUE;
case GI_TYPE_TAG_UINT64:
if (arg_in->v_uint64 > G_MAXLONG) {
PyErr_Format (PyExc_TypeError, "Unable to marshal %s to C long",
gi_type_tag_to_string (type_tag));
return FALSE;
}
*c_long_out = (glong)arg_in->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 C long",
gi_type_tag_to_string (type_tag));
return FALSE;
default:
g_assert_not_reached ();
}
}
static gboolean
_pygi_marshal_from_py_interface_enum (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg, GIArgument *arg,
gpointer *cleanup_data)
{
gint c_int;
PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
GIBaseInfo *interface = NULL;
GType enum_type =
gi_registered_type_info_get_g_type (iface_cache->interface_info);
if (pyg_enum_get_value (enum_type, py_arg, &c_int) != 0) {
return FALSE;
}
/* Write c_long into arg */
interface = gi_type_info_get_interface (arg_cache->type_info);
g_assert (GI_IS_ENUM_INFO (interface));
if (!gi_argument_from_c_long (
arg, c_int,
gi_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
gi_base_info_unref (interface);
return FALSE;
}
/* If this is not an instance of the Enum type that we want
* we need to check if the value is equivilant to one of the
* Enum's memebers */
if (!PyObject_IsInstance (py_arg, iface_cache->py_type)) {
unsigned int i;
gboolean is_found = FALSE;
for (i = 0; i < gi_enum_info_get_n_values (
GI_ENUM_INFO (iface_cache->interface_info));
i++) {
GIValueInfo *value_info = gi_enum_info_get_value (
GI_ENUM_INFO (iface_cache->interface_info), i);
gint64 enum_value = gi_value_info_get_value (value_info);
gi_base_info_unref ((GIBaseInfo *)value_info);
if (c_int == enum_value) {
is_found = TRUE;
break;
}
}
if (!is_found) {
PyErr_Format (PyExc_TypeError, "Expected a %s, but got %s",
iface_cache->type_name, Py_TYPE (py_arg)->tp_name);
gi_base_info_unref (interface);
return FALSE;
}
}
gi_base_info_unref (interface);
return TRUE;
}
static gboolean
_pygi_marshal_from_py_interface_flags (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg, GIArgument *arg,
gpointer *cleanup_data)
{
guint c_uint;
PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
GIBaseInfo *interface;
GType flags_type =
gi_registered_type_info_get_g_type (iface_cache->interface_info);
if (pyg_flags_get_value (flags_type, py_arg, &c_uint) != 0) {
return FALSE;
}
/* Write c_long into arg */
interface = gi_type_info_get_interface (arg_cache->type_info);
g_assert (GI_IS_FLAGS_INFO (interface));
if (!gi_argument_from_c_long (
arg, c_uint,
gi_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
gi_base_info_unref (interface);
return FALSE;
}
gi_base_info_unref (interface);
return TRUE;
}
static PyObject *
_pygi_marshal_to_py_interface_enum (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache, GIArgument *arg,
gpointer *cleanup_data)
{
PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
GIBaseInfo *interface;
long c_long;
interface = gi_type_info_get_interface (arg_cache->type_info);
g_assert (GI_IS_ENUM_INFO (interface) && !GI_IS_FLAGS_INFO (interface));
if (!gi_argument_to_c_long (
arg, &c_long,
gi_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
return NULL;
}
gi_base_info_unref (interface);
if (c_long < INT_MIN || c_long > INT_MAX) {
PyErr_Format (PyExc_OverflowError, "Value %ld is not in integer range",
c_long);
return NULL;
}
return pyg_enum_val_new (iface_cache->py_type, (int)c_long);
}
static PyObject *
_pygi_marshal_to_py_interface_flags (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache, GIArgument *arg,
gpointer *cleanup_data)
{
PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
GIBaseInfo *interface;
long c_long;
interface = gi_type_info_get_interface (arg_cache->type_info);
g_assert (GI_IS_FLAGS_INFO (interface));
if (!gi_argument_to_c_long (
arg, &c_long,
gi_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
gi_base_info_unref (interface);
return NULL;
}
gi_base_info_unref (interface);
return pyg_flags_val_new (iface_cache->py_type, (guint)c_long);
}
PyGIArgCache *
pygi_arg_enum_new_from_info (GITypeInfo *type_info, GIArgInfo *arg_info,
GITransfer transfer, PyGIDirection direction,
GIEnumInfo *iface_info)
{
PyGIArgCache *arg_cache = NULL;
arg_cache = pygi_arg_interface_new_from_info (
type_info, arg_info, transfer, direction,
GI_REGISTERED_TYPE_INFO (iface_info));
if (arg_cache == NULL) return NULL;
if (direction & PYGI_DIRECTION_FROM_PYTHON)
arg_cache->from_py_marshaller = _pygi_marshal_from_py_interface_enum;
if (direction & PYGI_DIRECTION_TO_PYTHON)
arg_cache->to_py_marshaller = _pygi_marshal_to_py_interface_enum;
return arg_cache;
}
PyGIArgCache *
pygi_arg_flags_new_from_info (GITypeInfo *type_info, GIArgInfo *arg_info,
GITransfer transfer, PyGIDirection direction,
GIFlagsInfo *iface_info)
{
PyGIArgCache *arg_cache = NULL;
arg_cache = pygi_arg_interface_new_from_info (
type_info, arg_info, transfer, direction,
GI_REGISTERED_TYPE_INFO (iface_info));
if (arg_cache == NULL) return NULL;
if (direction & PYGI_DIRECTION_FROM_PYTHON)
arg_cache->from_py_marshaller = _pygi_marshal_from_py_interface_flags;
if (direction & PYGI_DIRECTION_TO_PYTHON)
arg_cache->to_py_marshaller = _pygi_marshal_to_py_interface_flags;
return arg_cache;
}
|