| 1 |
/* -*- Mode: C; c-basic-offset: 4 -*- |
| 2 |
* vim: tabstop=4 shiftwidth=4 expandtab |
| 3 |
* |
| 4 |
* Copyright (C) 2011 John (J5) Palmieri <[email protected]> |
| 5 |
* Copyright (C) 2014 Simon Feltman <[email protected]> |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Lesser General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2.1 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public |
| 18 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "pygi-argument.h" |
| 22 |
#include "pygi-basictype.h" |
| 23 |
#include "pygi-info.h" |
| 24 |
#include "pygi-util.h" |
| 25 |
#include "pygi-cache-private.h" |
| 26 |
|
| 27 |
/* |
| 28 |
* GArray to Python |
| 29 |
*/ |
| 30 |
|
| 31 |
static gboolean |
| 32 |
gi_argument_from_py_ssize_t (GIArgument *arg_out, Py_ssize_t size_in, |
| 33 |
GITypeTag type_tag) |
| 34 |
{ |
| 35 |
switch (type_tag) { |
| 36 |
case GI_TYPE_TAG_VOID: |
| 37 |
case GI_TYPE_TAG_BOOLEAN: |
| 38 |
goto unhandled_type; |
| 39 |
|
| 40 |
case GI_TYPE_TAG_INT8: |
| 41 |
if (size_in >= G_MININT8 && size_in <= G_MAXINT8) { |
| 42 |
arg_out->v_int8 = (gint8)size_in; |
| 43 |
return TRUE; |
| 44 |
} else { |
| 45 |
goto overflow; |
| 46 |
} |
| 47 |
|
| 48 |
case GI_TYPE_TAG_UINT8: |
| 49 |
if (size_in >= 0 && size_in <= G_MAXUINT8) { |
| 50 |
arg_out->v_uint8 = (guint8)size_in; |
| 51 |
return TRUE; |
| 52 |
} else { |
| 53 |
goto overflow; |
| 54 |
} |
| 55 |
|
| 56 |
case GI_TYPE_TAG_INT16: |
| 57 |
if (size_in >= G_MININT16 && size_in <= G_MAXINT16) { |
| 58 |
arg_out->v_int16 = (gint16)size_in; |
| 59 |
return TRUE; |
| 60 |
} else { |
| 61 |
goto overflow; |
| 62 |
} |
| 63 |
|
| 64 |
case GI_TYPE_TAG_UINT16: |
| 65 |
if (size_in >= 0 && size_in <= G_MAXUINT16) { |
| 66 |
arg_out->v_uint16 = (guint16)size_in; |
| 67 |
return TRUE; |
| 68 |
} else { |
| 69 |
goto overflow; |
| 70 |
} |
| 71 |
|
| 72 |
/* Ranges assume two's complement */ |
| 73 |
case GI_TYPE_TAG_INT32: |
| 74 |
if (size_in >= G_MININT32 && size_in <= G_MAXINT32) { |
| 75 |
arg_out->v_int32 = (gint32)size_in; |
| 76 |
return TRUE; |
| 77 |
} else { |
| 78 |
goto overflow; |
| 79 |
} |
| 80 |
|
| 81 |
case GI_TYPE_TAG_UINT32: |
| 82 |
if (size_in >= 0 && (gsize)size_in <= G_MAXUINT32) { |
| 83 |
arg_out->v_uint32 = (guint32)size_in; |
| 84 |
return TRUE; |
| 85 |
} else { |
| 86 |
goto overflow; |
| 87 |
} |
| 88 |
|
| 89 |
case GI_TYPE_TAG_INT64: |
| 90 |
arg_out->v_int64 = size_in; |
| 91 |
return TRUE; |
| 92 |
|
| 93 |
case GI_TYPE_TAG_UINT64: |
| 94 |
if (size_in >= 0) { |
| 95 |
arg_out->v_uint64 = size_in; |
| 96 |
return TRUE; |
| 97 |
} else { |
| 98 |
goto overflow; |
| 99 |
} |
| 100 |
|
| 101 |
case GI_TYPE_TAG_FLOAT: |
| 102 |
case GI_TYPE_TAG_DOUBLE: |
| 103 |
case GI_TYPE_TAG_GTYPE: |
| 104 |
case GI_TYPE_TAG_UTF8: |
| 105 |
case GI_TYPE_TAG_FILENAME: |
| 106 |
case GI_TYPE_TAG_ARRAY: |
| 107 |
case GI_TYPE_TAG_INTERFACE: |
| 108 |
case GI_TYPE_TAG_GLIST: |
| 109 |
case GI_TYPE_TAG_GSLIST: |
| 110 |
case GI_TYPE_TAG_GHASH: |
| 111 |
case GI_TYPE_TAG_ERROR: |
| 112 |
case GI_TYPE_TAG_UNICHAR: |
| 113 |
goto unhandled_type; |
| 114 |
default: |
| 115 |
g_assert_not_reached (); |
| 116 |
} |
| 117 |
|
| 118 |
overflow: |
| 119 |
PyErr_Format (PyExc_OverflowError, |
| 120 |
"Unable to marshal C Py_ssize_t %zd to %s", size_in, |
| 121 |
gi_type_tag_to_string (type_tag)); |
| 122 |
return FALSE; |
| 123 |
|
| 124 |
unhandled_type: |
| 125 |
PyErr_Format (PyExc_TypeError, "Unable to marshal C Py_ssize_t %zd to %s", |
| 126 |
size_in, gi_type_tag_to_string (type_tag)); |
| 127 |
return FALSE; |
| 128 |
} |
| 129 |
|
| 130 |
static gboolean |
| 131 |
_pygi_marshal_from_py_array (PyGIInvokeState *state, |
| 132 |
PyGICallableCache *callable_cache, |
| 133 |
PyGIArgCache *arg_cache, PyObject *py_arg, |
| 134 |
GIArgument *arg, gpointer *cleanup_data) |
| 135 |
{ |
| 136 |
PyGIMarshalFromPyFunc from_py_marshaller; |
| 137 |
guint i = 0; |
| 138 |
gsize success_count = 0; |
| 139 |
Py_ssize_t py_length; |
| 140 |
size_t fixed_size; |
| 141 |
guint length; |
| 142 |
guint item_size; |
| 143 |
gboolean is_ptr_array; |
| 144 |
GArray *array_ = NULL; |
| 145 |
PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache; |
| 146 |
PyGIArgGArray *array_cache = (PyGIArgGArray *)arg_cache; |
| 147 |
GITransfer cleanup_transfer = arg_cache->transfer; |
| 148 |
gboolean is_zero_terminated = |
| 149 |
gi_type_info_is_zero_terminated (arg_cache->type_info); |
| 150 |
GIArrayType array_type; |
| 151 |
|
| 152 |
if (Py_IsNone (py_arg)) { |
| 153 |
arg->v_pointer = NULL; |
| 154 |
return TRUE; |
| 155 |
} |
| 156 |
|
| 157 |
if (!PySequence_Check (py_arg)) { |
| 158 |
PyErr_Format (PyExc_TypeError, "Must be sequence, not %s", |
| 159 |
Py_TYPE (py_arg)->tp_name); |
| 160 |
return FALSE; |
| 161 |
} else if (PyUnicode_Check (py_arg) |
| 162 |
&& sequence_cache->item_cache->type_tag |
| 163 |
!= GI_TYPE_TAG_UNICHAR) { |
| 164 |
PyErr_SetString (PyExc_TypeError, |
| 165 |
"Unable to marshal str as an array, use .encode() to " |
| 166 |
"convert to bytes"); |
| 167 |
return FALSE; |
| 168 |
} |
| 169 |
|
| 170 |
py_length = PySequence_Length (py_arg); |
| 171 |
if (py_length < 0) return FALSE; |
| 172 |
|
| 173 |
if (!pygi_guint_from_pyssize (py_length, &length)) return FALSE; |
| 174 |
|
| 175 |
if (gi_type_info_get_array_fixed_size (arg_cache->type_info, &fixed_size) |
| 176 |
&& (guint)fixed_size != length) { |
| 177 |
PyErr_Format (PyExc_ValueError, "Must contain %zd items, not %u", |
| 178 |
fixed_size, length); |
| 179 |
|
| 180 |
return FALSE; |
| 181 |
} |
| 182 |
|
| 183 |
item_size = (guint)array_cache->item_size; |
| 184 |
is_ptr_array = (gi_type_info_get_array_type (arg_cache->type_info) |
| 185 |
== GI_ARRAY_TYPE_PTR_ARRAY); |
| 186 |
if (is_ptr_array) { |
| 187 |
array_ = (GArray *)g_ptr_array_sized_new (length); |
| 188 |
} else { |
| 189 |
array_ = |
| 190 |
g_array_sized_new (is_zero_terminated, TRUE, item_size, length); |
| 191 |
} |
| 192 |
|
| 193 |
if (array_ == NULL) { |
| 194 |
PyErr_NoMemory (); |
| 195 |
return FALSE; |
| 196 |
} |
| 197 |
|
| 198 |
array_type = gi_type_info_get_array_type (arg_cache->type_info); |
| 199 |
|
| 200 |
if (sequence_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8 |
| 201 |
&& PyBytes_Check (py_arg)) { |
| 202 |
gchar *data = PyBytes_AsString (py_arg); |
| 203 |
|
| 204 |
/* Avoid making a copy if the data |
| 205 |
* is not transferred to the C function |
| 206 |
* and cannot not be modified by it. |
| 207 |
*/ |
| 208 |
if (array_type == GI_ARRAY_TYPE_C |
| 209 |
&& arg_cache->transfer == GI_TRANSFER_NOTHING |
| 210 |
&& !is_zero_terminated) { |
| 211 |
g_free (array_->data); |
| 212 |
array_->data = data; |
| 213 |
cleanup_transfer = GI_TRANSFER_EVERYTHING; |
| 214 |
} else { |
| 215 |
memcpy (array_->data, data, length); |
| 216 |
} |
| 217 |
array_->len = length; |
| 218 |
if (is_zero_terminated) { |
| 219 |
/* If array_ has been created with zero_termination, space for the |
| 220 |
* terminator is properly allocated, so we're not off-by-one here. */ |
| 221 |
array_->data[length] = '\0'; |
| 222 |
} |
| 223 |
goto array_success; |
| 224 |
} |
| 225 |
|
| 226 |
from_py_marshaller = sequence_cache->item_cache->from_py_marshaller; |
| 227 |
for (i = 0, success_count = 0; i < length; i++) { |
| 228 |
GIArgument item = PYGI_ARG_INIT; |
| 229 |
gpointer item_cleanup_data = NULL; |
| 230 |
PyObject *py_item = PySequence_GetItem (py_arg, i); |
| 231 |
if (py_item == NULL) goto err; |
| 232 |
|
| 233 |
if (!from_py_marshaller (state, callable_cache, |
| 234 |
sequence_cache->item_cache, py_item, &item, |
| 235 |
&item_cleanup_data)) { |
| 236 |
Py_DECREF (py_item); |
| 237 |
goto err; |
| 238 |
} |
| 239 |
Py_DECREF (py_item); |
| 240 |
|
| 241 |
if (item_cleanup_data != NULL && item_cleanup_data != item.v_pointer) { |
| 242 |
/* We only support one level of data discrepancy between an items |
| 243 |
* data and its cleanup data. This is because we only track a single |
| 244 |
* extra cleanup data pointer per-argument and cannot track the entire |
| 245 |
* array of items differing data and cleanup_data. |
| 246 |
* For example, this would fail if trying to marshal an array of |
| 247 |
* callback closures marked with SCOPE call type where the cleanup data |
| 248 |
* is different from the items v_pointer, likewise an array of arrays. |
| 249 |
*/ |
| 250 |
PyErr_SetString ( |
| 251 |
PyExc_RuntimeError, |
| 252 |
"Cannot cleanup item data for array due to " |
| 253 |
"the items data its cleanup data being different."); |
| 254 |
goto err; |
| 255 |
} |
| 256 |
|
| 257 |
/* FIXME: it is much more efficent to have seperate marshaller |
| 258 |
* for ptr arrays than doing the evaluation |
| 259 |
* and casting each loop iteration |
| 260 |
*/ |
| 261 |
if (is_ptr_array) { |
| 262 |
g_ptr_array_add ((GPtrArray *)array_, item.v_pointer); |
| 263 |
} else if (sequence_cache->item_cache->is_pointer) { |
| 264 |
/* if the item is a pointer, simply copy the pointer */ |
| 265 |
g_assert (item_size == sizeof (item.v_pointer)); |
| 266 |
g_array_insert_val (array_, i, item); |
| 267 |
} else if (sequence_cache->item_cache->type_tag |
| 268 |
== GI_TYPE_TAG_INTERFACE) { |
| 269 |
/* Special case handling of flat arrays of gvalue/boxed/struct */ |
| 270 |
PyGIInterfaceCache *item_iface_cache = |
| 271 |
(PyGIInterfaceCache *)sequence_cache->item_cache; |
| 272 |
GIBaseInfo *base_info = |
| 273 |
(GIBaseInfo *)item_iface_cache->interface_info; |
| 274 |
|
| 275 |
if (GI_IS_STRUCT_INFO (base_info) |
| 276 |
|| GI_IS_UNION_INFO (base_info)) { |
| 277 |
PyGIArgCache *item_arg_cache = |
| 278 |
(PyGIArgCache *)item_iface_cache; |
| 279 |
PyGIMarshalFromPyCleanupFunc from_py_cleanup = |
| 280 |
item_arg_cache->from_py_cleanup; |
| 281 |
|
| 282 |
if (g_type_is_a (item_iface_cache->g_type, G_TYPE_VALUE)) { |
| 283 |
/* Special case GValue flat arrays to properly init and copy the contents. */ |
| 284 |
GValue *dest = |
| 285 |
(GValue *)(void *)(array_->data + (i * item_size)); |
| 286 |
if (item.v_pointer != NULL) { |
| 287 |
memset (dest, 0, item_size); |
| 288 |
g_value_init (dest, |
| 289 |
G_VALUE_TYPE ((GValue *)item.v_pointer)); |
| 290 |
g_value_copy ((GValue *)item.v_pointer, dest); |
| 291 |
} |
| 292 |
/* Manually increment the length because we are manually setting the memory. */ |
| 293 |
array_->len++; |
| 294 |
|
| 295 |
} else { |
| 296 |
/* Handles flat arrays of boxed or struct types. */ |
| 297 |
g_array_insert_vals (array_, i, item.v_pointer, 1); |
| 298 |
} |
| 299 |
|
| 300 |
/* Cleanup any memory left by the per-item marshaler because |
| 301 |
* _pygi_marshal_cleanup_from_py_array will not know about this |
| 302 |
* due to "item" being a temporarily marshaled value done on the stack. |
| 303 |
*/ |
| 304 |
if (from_py_cleanup) |
| 305 |
from_py_cleanup (state, item_arg_cache, py_item, |
| 306 |
item_cleanup_data, TRUE); |
| 307 |
} else { |
| 308 |
g_array_insert_val (array_, i, item); |
| 309 |
} |
| 310 |
} else { |
| 311 |
/* default value copy of a simple type */ |
| 312 |
g_array_insert_val (array_, i, item); |
| 313 |
} |
| 314 |
|
| 315 |
success_count++; |
| 316 |
} |
| 317 |
goto array_success; |
| 318 |
|
| 319 |
err: |
| 320 |
if (sequence_cache->item_cache->from_py_cleanup != NULL) { |
| 321 |
gsize j; |
| 322 |
PyGIMarshalFromPyCleanupFunc cleanup_func = |
| 323 |
sequence_cache->item_cache->from_py_cleanup; |
| 324 |
PyObject *type, *value, *traceback; |
| 325 |
|
| 326 |
PyErr_Fetch (&type, &value, &traceback); |
| 327 |
|
| 328 |
/* Only attempt per item cleanup on pointer items */ |
| 329 |
if (sequence_cache->item_cache->is_pointer) { |
| 330 |
for (j = 0; j < success_count; j++) { |
| 331 |
PyObject *py_seq_item = PySequence_GetItem (py_arg, j); |
| 332 |
cleanup_func (state, sequence_cache->item_cache, py_seq_item, |
| 333 |
is_ptr_array |
| 334 |
? g_ptr_array_index ((GPtrArray *)array_, j) |
| 335 |
: g_array_index (array_, gpointer, j), |
| 336 |
TRUE); |
| 337 |
Py_DECREF (py_seq_item); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
PyErr_Restore (type, value, traceback); |
| 342 |
} |
| 343 |
|
| 344 |
if (is_ptr_array) |
| 345 |
g_ptr_array_free ((GPtrArray *)array_, TRUE); |
| 346 |
else |
| 347 |
g_array_free (array_, TRUE); |
| 348 |
_PyGI_ERROR_PREFIX ("Item %u: ", i); |
| 349 |
return FALSE; |
| 350 |
|
| 351 |
array_success: |
| 352 |
if (array_cache->has_len_arg) { |
| 353 |
/* we have an child arg to handle */ |
| 354 |
PyGIArgCache *child_cache = _pygi_callable_cache_get_arg ( |
| 355 |
callable_cache, array_cache->len_arg_index); |
| 356 |
|
| 357 |
if (!gi_argument_from_py_ssize_t ( |
| 358 |
&state->args[child_cache->c_arg_index].arg_value, length, |
| 359 |
child_cache->type_tag)) { |
| 360 |
goto err; |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
if (array_type == GI_ARRAY_TYPE_C) { |
| 365 |
/* In the case of GI_ARRAY_C, we give the data directly as the argument |
| 366 |
* but keep the array_ wrapper as cleanup data so we don't have to find |
| 367 |
* it's length again. |
| 368 |
*/ |
| 369 |
arg->v_pointer = array_->data; |
| 370 |
|
| 371 |
if (cleanup_transfer == GI_TRANSFER_EVERYTHING) { |
| 372 |
g_array_free (array_, FALSE); |
| 373 |
*cleanup_data = NULL; |
| 374 |
} else { |
| 375 |
*cleanup_data = array_; |
| 376 |
} |
| 377 |
} else { |
| 378 |
arg->v_pointer = array_; |
| 379 |
|
| 380 |
if (cleanup_transfer == GI_TRANSFER_NOTHING) { |
| 381 |
/* Free everything in cleanup. */ |
| 382 |
*cleanup_data = array_; |
| 383 |
} else if (cleanup_transfer == GI_TRANSFER_CONTAINER) { |
| 384 |
/* Make a shallow copy so we can free the elements later in cleanup |
| 385 |
* because it is possible invoke will free the list before our cleanup. */ |
| 386 |
*cleanup_data = |
| 387 |
is_ptr_array ? (gpointer)g_ptr_array_ref ((GPtrArray *)array_) |
| 388 |
: (gpointer)g_array_ref (array_); |
| 389 |
} else { /* GI_TRANSFER_EVERYTHING */ |
| 390 |
/* No cleanup, everything is given to the callee. */ |
| 391 |
*cleanup_data = NULL; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
return TRUE; |
| 396 |
} |
| 397 |
|
| 398 |
static void |
| 399 |
_pygi_marshal_cleanup_from_py_array (PyGIInvokeState *state, |
| 400 |
PyGIArgCache *arg_cache, PyObject *py_arg, |
| 401 |
gpointer data, gboolean was_processed) |
| 402 |
{ |
| 403 |
if (was_processed) { |
| 404 |
GArray *array_ = NULL; |
| 405 |
GPtrArray *ptr_array_ = NULL; |
| 406 |
PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache; |
| 407 |
PyGIArgGArray *array_cache = (PyGIArgGArray *)arg_cache; |
| 408 |
GIArrayType array_type = |
| 409 |
gi_type_info_get_array_type (arg_cache->type_info); |
| 410 |
|
| 411 |
if (array_type == GI_ARRAY_TYPE_PTR_ARRAY) { |
| 412 |
ptr_array_ = (GPtrArray *)data; |
| 413 |
} else { |
| 414 |
array_ = (GArray *)data; |
| 415 |
} |
| 416 |
|
| 417 |
/* clean up items first */ |
| 418 |
if (sequence_cache->item_cache->from_py_cleanup != NULL) { |
| 419 |
gsize i; |
| 420 |
guint len; |
| 421 |
PyGIMarshalFromPyCleanupFunc cleanup_func = |
| 422 |
sequence_cache->item_cache->from_py_cleanup; |
| 423 |
|
| 424 |
g_assert (array_ || ptr_array_); |
| 425 |
len = (array_ != NULL) ? array_->len : ptr_array_->len; |
| 426 |
|
| 427 |
for (i = 0; i < len; i++) { |
| 428 |
gpointer item; |
| 429 |
PyObject *py_item = NULL; |
| 430 |
|
| 431 |
/* case 1: GPtrArray */ |
| 432 |
if (ptr_array_ != NULL) |
| 433 |
item = g_ptr_array_index (ptr_array_, i); |
| 434 |
/* case 2: C array or GArray with object pointers */ |
| 435 |
else if (sequence_cache->item_cache->is_pointer) |
| 436 |
item = g_array_index (array_, gpointer, i); |
| 437 |
/* case 3: C array or GArray with simple types or structs */ |
| 438 |
else { |
| 439 |
item = array_->data + i * array_cache->item_size; |
| 440 |
/* special-case hack: GValue array items do not get slice |
| 441 |
* allocated in _pygi_marshal_from_py_array(), so we must |
| 442 |
* not try to deallocate it as a slice and thus |
| 443 |
* short-circuit cleanup_func. */ |
| 444 |
if (cleanup_func == pygi_arg_gvalue_from_py_cleanup) { |
| 445 |
g_value_unset ((GValue *)item); |
| 446 |
continue; |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
py_item = PySequence_GetItem (py_arg, i); |
| 451 |
cleanup_func (state, sequence_cache->item_cache, py_item, item, |
| 452 |
TRUE); |
| 453 |
Py_XDECREF (py_item); |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
/* Only free the array when we didn't transfer ownership */ |
| 458 |
if (array_type == GI_ARRAY_TYPE_C) { |
| 459 |
/* always free the GArray wrapper created in from_py marshaling and |
| 460 |
* passed back as cleanup_data |
| 461 |
*/ |
| 462 |
g_array_free (array_, arg_cache->transfer == GI_TRANSFER_NOTHING); |
| 463 |
} else { |
| 464 |
if (array_ != NULL) |
| 465 |
g_array_unref (array_); |
| 466 |
else |
| 467 |
g_ptr_array_unref (ptr_array_); |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
/* |
| 473 |
* GArray from Python |
| 474 |
*/ |
| 475 |
|
| 476 |
static GArray * |
| 477 |
_wrap_c_array (PyGIInvokeState *state, PyGIArgGArray *array_cache, |
| 478 |
gpointer data) |
| 479 |
{ |
| 480 |
size_t len = 0; |
| 481 |
|
| 482 |
if (gi_type_info_get_array_fixed_size ( |
| 483 |
((PyGIArgCache *)array_cache)->type_info, &len)) { |
| 484 |
/* len is set. */ |
| 485 |
} else if (gi_type_info_is_zero_terminated ( |
| 486 |
((PyGIArgCache *)array_cache)->type_info)) { |
| 487 |
if (data == NULL) |
| 488 |
len = 0; |
| 489 |
else if (array_cache->item_size == 1) |
| 490 |
len = strlen ((gchar *)data); |
| 491 |
else if (array_cache->item_size == sizeof (gpointer)) |
| 492 |
len = g_strv_length ((gchar **)data); |
| 493 |
else if (array_cache->item_size == sizeof (int)) |
| 494 |
for (len = 0; *(((int *)data) + len); len++); |
| 495 |
else if (array_cache->item_size == sizeof (short)) |
| 496 |
for (len = 0; *(((short *)data) + len); len++); |
| 497 |
else |
| 498 |
g_assert_not_reached (); |
| 499 |
} else if (array_cache->has_len_arg) { |
| 500 |
GIArgument len_arg = state->args[array_cache->len_arg_index].arg_value; |
| 501 |
|
| 502 |
if (array_cache->len_arg_cache) |
| 503 |
pygi_argument_to_gsize ( |
| 504 |
len_arg, array_cache->len_arg_cache->type_tag, &len); |
| 505 |
else |
| 506 |
/* short circuit from pygi_argument_to_py_with_array_length */ |
| 507 |
len = len_arg.v_size; |
| 508 |
} |
| 509 |
|
| 510 |
return g_array_new_take (data, (guint)len, FALSE, |
| 511 |
(guint)array_cache->item_size); |
| 512 |
} |
| 513 |
|
| 514 |
static PyObject * |
| 515 |
_pygi_marshal_to_py_array (PyGIInvokeState *state, |
| 516 |
PyGICallableCache *callable_cache, |
| 517 |
PyGIArgCache *arg_cache, GIArgument *arg, |
| 518 |
gpointer *cleanup_data) |
| 519 |
{ |
| 520 |
GArray *array_; |
| 521 |
PyObject *py_obj = NULL; |
| 522 |
PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache; |
| 523 |
PyGIArgGArray *array_cache = (PyGIArgGArray *)arg_cache; |
| 524 |
guint processed_items = 0; |
| 525 |
GIArrayType array_type = |
| 526 |
gi_type_info_get_array_type (arg_cache->type_info); |
| 527 |
|
| 528 |
/* GArrays make it easier to iterate over arrays |
| 529 |
* with different element sizes but requires that |
| 530 |
* we allocate a GArray if the argument was a C array |
| 531 |
*/ |
| 532 |
if (array_type == GI_ARRAY_TYPE_C) { |
| 533 |
array_ = _wrap_c_array (state, array_cache, arg->v_pointer); |
| 534 |
} else { |
| 535 |
array_ = arg->v_pointer; |
| 536 |
} |
| 537 |
|
| 538 |
if (seq_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8) { |
| 539 |
if (arg->v_pointer == NULL) { |
| 540 |
py_obj = PyBytes_FromString (""); |
| 541 |
} else { |
| 542 |
py_obj = PyBytes_FromStringAndSize (array_->data, array_->len); |
| 543 |
} |
| 544 |
} else { |
| 545 |
if (arg->v_pointer == NULL) { |
| 546 |
py_obj = PyList_New (0); |
| 547 |
} else { |
| 548 |
guint i; |
| 549 |
|
| 550 |
gsize item_size; |
| 551 |
PyGIMarshalToPyFunc item_to_py_marshaller; |
| 552 |
PyGIArgCache *item_arg_cache; |
| 553 |
GPtrArray *item_cleanups; |
| 554 |
gboolean item_size_warning = FALSE; |
| 555 |
|
| 556 |
py_obj = PyList_New (array_->len); |
| 557 |
if (py_obj == NULL) goto err; |
| 558 |
|
| 559 |
item_cleanups = g_ptr_array_sized_new (array_->len); |
| 560 |
*cleanup_data = item_cleanups; |
| 561 |
|
| 562 |
item_arg_cache = seq_cache->item_cache; |
| 563 |
item_to_py_marshaller = item_arg_cache->to_py_marshaller; |
| 564 |
|
| 565 |
item_size = g_array_get_element_size (array_); |
| 566 |
|
| 567 |
for (i = 0; i < array_->len; i++) { |
| 568 |
GIArgument item_arg = PYGI_ARG_INIT; |
| 569 |
PyObject *py_item; |
| 570 |
gpointer item_cleanup_data = NULL; |
| 571 |
|
| 572 |
/* If we are receiving an array of pointers, simply assign the pointer |
| 573 |
* and move on, letting the per-item marshaler deal with the |
| 574 |
* various transfer modes and ref counts (e.g. g_variant_ref_sink). |
| 575 |
*/ |
| 576 |
if (array_type == GI_ARRAY_TYPE_PTR_ARRAY) { |
| 577 |
item_arg.v_pointer = |
| 578 |
g_ptr_array_index ((GPtrArray *)array_, i); |
| 579 |
|
| 580 |
} else if (item_arg_cache->is_pointer) { |
| 581 |
if (item_size == sizeof (gpointer)) { |
| 582 |
item_arg.v_pointer = |
| 583 |
g_array_index (array_, gpointer, i); |
| 584 |
} else { |
| 585 |
// TODO: this warning needs to be actionable |
| 586 |
// I do not have enough context to show which field/argument is in error, maybe |
| 587 |
// allow to raise errors for those cases (e.g. an option that can be set in unit tests) |
| 588 |
if (!item_size_warning) { |
| 589 |
g_warning ( |
| 590 |
"Array type is assumed to be a pointer, but " |
| 591 |
"item size is %" G_GSIZE_FORMAT " bytes", |
| 592 |
item_size); |
| 593 |
item_size_warning = TRUE; |
| 594 |
} |
| 595 |
|
| 596 |
// For now, copy the right number of bytes |
| 597 |
memcpy (&item_arg, array_->data + i * item_size, |
| 598 |
item_size); |
| 599 |
} |
| 600 |
} else if (item_arg_cache->type_tag == GI_TYPE_TAG_INTERFACE) { |
| 601 |
PyGIInterfaceCache *iface_cache = |
| 602 |
(PyGIInterfaceCache *)item_arg_cache; |
| 603 |
|
| 604 |
/* FIXME: This probably doesn't work with boxed types or gvalues. |
| 605 |
* See fx. _pygi_marshal_from_py_array() */ |
| 606 |
if (GI_IS_STRUCT_INFO (iface_cache->interface_info)) { |
| 607 |
if (arg_cache->transfer == GI_TRANSFER_EVERYTHING |
| 608 |
&& !g_type_is_a (iface_cache->g_type, |
| 609 |
G_TYPE_BOXED)) { |
| 610 |
/* array elements are structs */ |
| 611 |
gpointer *_struct = g_malloc (item_size); |
| 612 |
memcpy (_struct, array_->data + i * item_size, |
| 613 |
item_size); |
| 614 |
item_arg.v_pointer = _struct; |
| 615 |
} else { |
| 616 |
item_arg.v_pointer = array_->data + i * item_size; |
| 617 |
} |
| 618 |
} else if (GI_IS_ENUM_INFO (iface_cache->interface_info)) { |
| 619 |
memcpy (&item_arg, array_->data + i * item_size, |
| 620 |
item_size); |
| 621 |
} else { |
| 622 |
item_arg.v_pointer = |
| 623 |
g_array_index (array_, gpointer, i); |
| 624 |
} |
| 625 |
} else { |
| 626 |
memcpy (&item_arg, array_->data + i * item_size, |
| 627 |
item_size); |
| 628 |
} |
| 629 |
|
| 630 |
py_item = item_to_py_marshaller (state, callable_cache, |
| 631 |
item_arg_cache, &item_arg, |
| 632 |
&item_cleanup_data); |
| 633 |
|
| 634 |
g_ptr_array_index (item_cleanups, i) = item_cleanup_data; |
| 635 |
|
| 636 |
if (py_item == NULL) { |
| 637 |
Py_CLEAR (py_obj); |
| 638 |
|
| 639 |
g_ptr_array_unref (item_cleanups); |
| 640 |
|
| 641 |
goto err; |
| 642 |
} |
| 643 |
PyList_SET_ITEM (py_obj, i, py_item); |
| 644 |
processed_items++; |
| 645 |
} |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
if (array_type == GI_ARRAY_TYPE_C) g_array_free (array_, FALSE); |
| 650 |
|
| 651 |
return py_obj; |
| 652 |
|
| 653 |
err: |
| 654 |
if (array_type == GI_ARRAY_TYPE_C) { |
| 655 |
g_array_free (array_, arg_cache->transfer == GI_TRANSFER_EVERYTHING); |
| 656 |
} else { |
| 657 |
/* clean up unprocessed items */ |
| 658 |
if (seq_cache->item_cache->to_py_cleanup != NULL) { |
| 659 |
guint j; |
| 660 |
PyGIMarshalToPyCleanupFunc cleanup_func = |
| 661 |
seq_cache->item_cache->to_py_cleanup; |
| 662 |
for (j = processed_items; j < array_->len; j++) { |
| 663 |
cleanup_func (state, seq_cache->item_cache, NULL, |
| 664 |
g_array_index (array_, gpointer, j), FALSE); |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
if (arg_cache->transfer == GI_TRANSFER_EVERYTHING) |
| 669 |
g_array_free (array_, TRUE); |
| 670 |
} |
| 671 |
|
| 672 |
return NULL; |
| 673 |
} |
| 674 |
|
| 675 |
static void |
| 676 |
_pygi_marshal_cleanup_to_py_array (PyGIInvokeState *state, |
| 677 |
PyGIArgCache *arg_cache, |
| 678 |
gpointer cleanup_data, gpointer data, |
| 679 |
gboolean was_processed) |
| 680 |
{ |
| 681 |
GArray *array_ = NULL; |
| 682 |
GPtrArray *ptr_array_ = NULL; |
| 683 |
PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache; |
| 684 |
PyGIArgGArray *array_cache = (PyGIArgGArray *)arg_cache; |
| 685 |
gboolean free_array = FALSE; |
| 686 |
gboolean free_array_full = TRUE; |
| 687 |
GIArrayType array_type = |
| 688 |
gi_type_info_get_array_type (arg_cache->type_info); |
| 689 |
|
| 690 |
if (data == NULL) return; |
| 691 |
|
| 692 |
if (arg_cache->transfer == GI_TRANSFER_EVERYTHING |
| 693 |
|| arg_cache->transfer == GI_TRANSFER_CONTAINER) { |
| 694 |
free_array = TRUE; |
| 695 |
} |
| 696 |
|
| 697 |
/* If this isn't a garray create one to help process variable sized |
| 698 |
array elements */ |
| 699 |
if (array_type == GI_ARRAY_TYPE_C) { |
| 700 |
array_ = _wrap_c_array (state, array_cache, data); |
| 701 |
|
| 702 |
if (array_ == NULL) return; |
| 703 |
|
| 704 |
free_array = TRUE; |
| 705 |
free_array_full = arg_cache->transfer != GI_TRANSFER_NOTHING; |
| 706 |
} else if (array_type == GI_ARRAY_TYPE_PTR_ARRAY) { |
| 707 |
ptr_array_ = (GPtrArray *)data; |
| 708 |
} else { |
| 709 |
array_ = (GArray *)data; |
| 710 |
} |
| 711 |
|
| 712 |
if (sequence_cache->item_cache->to_py_cleanup != NULL) { |
| 713 |
GPtrArray *item_cleanups = (GPtrArray *)cleanup_data; |
| 714 |
gsize i; |
| 715 |
guint len; |
| 716 |
PyGIMarshalToPyCleanupFunc cleanup_func = |
| 717 |
sequence_cache->item_cache->to_py_cleanup; |
| 718 |
|
| 719 |
g_assert (array_ || ptr_array_); |
| 720 |
len = (array_ != NULL) ? array_->len : ptr_array_->len; |
| 721 |
|
| 722 |
for (i = 0; i < len; i++) { |
| 723 |
cleanup_func (state, sequence_cache->item_cache, |
| 724 |
g_ptr_array_index (item_cleanups, i), |
| 725 |
(array_ != NULL) |
| 726 |
? g_array_index (array_, gpointer, i) |
| 727 |
: g_ptr_array_index (ptr_array_, i), |
| 728 |
was_processed); |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
if (cleanup_data) g_ptr_array_unref ((GPtrArray *)cleanup_data); |
| 733 |
|
| 734 |
if (free_array) { |
| 735 |
if (array_ != NULL) |
| 736 |
g_array_free (array_, free_array_full); |
| 737 |
else |
| 738 |
g_ptr_array_free (ptr_array_, free_array_full); |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
static void |
| 743 |
_array_cache_free_func (PyGIArgGArray *cache) |
| 744 |
{ |
| 745 |
if (cache != NULL) { |
| 746 |
pygi_arg_cache_free (((PyGISequenceCache *)cache)->item_cache); |
| 747 |
g_slice_free (PyGIArgGArray, cache); |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
static void |
| 752 |
pygi_arg_garray_len_arg_setup (PyGIArgGArray *seq_cache, GITypeInfo *type_info, |
| 753 |
PyGICallableCache *callable_cache, |
| 754 |
PyGIDirection direction, gssize arg_index, |
| 755 |
gssize *py_arg_index) |
| 756 |
{ |
| 757 |
/* attempt len_arg_index setup for the first time */ |
| 758 |
if (!seq_cache->has_len_arg) { |
| 759 |
seq_cache->has_len_arg = gi_type_info_get_array_length_index ( |
| 760 |
type_info, &seq_cache->len_arg_index); |
| 761 |
|
| 762 |
/* offset by self arg for methods and vfuncs */ |
| 763 |
if (seq_cache->has_len_arg && callable_cache != NULL) { |
| 764 |
seq_cache->len_arg_index += callable_cache->args_offset; |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
if (seq_cache->has_len_arg) { |
| 769 |
PyGIArgCache *child_cache = NULL; |
| 770 |
|
| 771 |
child_cache = seq_cache->len_arg_cache = _pygi_callable_cache_get_arg ( |
| 772 |
callable_cache, seq_cache->len_arg_index); |
| 773 |
if (child_cache == NULL) { |
| 774 |
child_cache = seq_cache->len_arg_cache = pygi_arg_cache_alloc (); |
| 775 |
_pygi_callable_cache_set_arg ( |
| 776 |
callable_cache, seq_cache->len_arg_index, child_cache); |
| 777 |
} else { |
| 778 |
/* If the "length" arg cache already exists (the length comes before |
| 779 |
* the array in the argument list), remove it from the to_py_args list |
| 780 |
* because it does not belong in "to python" return tuple. The length |
| 781 |
* will implicitly be a part of the returned Python list. |
| 782 |
*/ |
| 783 |
if (direction & PYGI_DIRECTION_TO_PYTHON) { |
| 784 |
callable_cache->to_py_args = |
| 785 |
g_slist_remove (callable_cache->to_py_args, child_cache); |
| 786 |
} |
| 787 |
|
| 788 |
/* This is a case where the arg cache already exists and has been |
| 789 |
* setup by another array argument sharing the same length argument. |
| 790 |
* See: gi_marshalling_tests_multi_array_key_value_in |
| 791 |
*/ |
| 792 |
if (child_cache->meta_type == PYGI_META_ARG_TYPE_CHILD) return; |
| 793 |
} |
| 794 |
|
| 795 |
/* There is a length argument for this array, so increment the number |
| 796 |
* of "to python" child arguments when applicable. |
| 797 |
*/ |
| 798 |
if (direction & PYGI_DIRECTION_TO_PYTHON) |
| 799 |
callable_cache->n_to_py_child_args++; |
| 800 |
|
| 801 |
child_cache->meta_type = PYGI_META_ARG_TYPE_CHILD; |
| 802 |
child_cache->direction = direction; |
| 803 |
child_cache->to_py_marshaller = |
| 804 |
pygi_marshal_to_py_basic_type_cache_adapter; |
| 805 |
child_cache->from_py_marshaller = |
| 806 |
pygi_marshal_from_py_basic_type_cache_adapter; |
| 807 |
child_cache->py_arg_index = -1; |
| 808 |
|
| 809 |
/* ugly edge case code: |
| 810 |
* |
| 811 |
* When the length comes before the array parameter we need to update |
| 812 |
* indexes of arguments after the index argument. |
| 813 |
*/ |
| 814 |
if (((gssize)seq_cache->len_arg_index) < arg_index |
| 815 |
&& direction & PYGI_DIRECTION_FROM_PYTHON) { |
| 816 |
(*py_arg_index) -= 1; |
| 817 |
callable_cache->n_py_args -= 1; |
| 818 |
|
| 819 |
for (guint i = seq_cache->len_arg_index + 1; |
| 820 |
i < _pygi_callable_cache_args_len (callable_cache); i++) { |
| 821 |
PyGIArgCache *update_cache = |
| 822 |
_pygi_callable_cache_get_arg (callable_cache, i); |
| 823 |
if (update_cache == NULL) break; |
| 824 |
|
| 825 |
update_cache->py_arg_index -= 1; |
| 826 |
} |
| 827 |
} |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
static gboolean |
| 832 |
pygi_arg_garray_setup ( |
| 833 |
PyGIArgGArray *sc, GITypeInfo *type_info, |
| 834 |
GIArgInfo *arg_info, /* may be NULL for return arguments */ |
| 835 |
GITransfer transfer, PyGIDirection direction, |
| 836 |
PyGICallableCache *callable_cache) |
| 837 |
{ |
| 838 |
GITypeInfo *item_type_info; |
| 839 |
PyGIArgCache *arg_cache = (PyGIArgCache *)sc; |
| 840 |
|
| 841 |
if (!pygi_arg_sequence_setup ((PyGISequenceCache *)sc, type_info, arg_info, |
| 842 |
transfer, direction, callable_cache)) { |
| 843 |
return FALSE; |
| 844 |
} |
| 845 |
|
| 846 |
((PyGIArgCache *)sc)->destroy_notify = |
| 847 |
(GDestroyNotify)_array_cache_free_func; |
| 848 |
sc->has_len_arg = FALSE; /* setup by pygi_arg_garray_len_arg_setup */ |
| 849 |
|
| 850 |
item_type_info = gi_type_info_get_param_type (type_info, 0); |
| 851 |
sc->item_size = _pygi_gi_type_info_size (item_type_info); |
| 852 |
gi_base_info_unref ((GIBaseInfo *)item_type_info); |
| 853 |
|
| 854 |
if (direction & PYGI_DIRECTION_FROM_PYTHON) { |
| 855 |
arg_cache->from_py_marshaller = _pygi_marshal_from_py_array; |
| 856 |
arg_cache->from_py_cleanup = _pygi_marshal_cleanup_from_py_array; |
| 857 |
} |
| 858 |
|
| 859 |
if (direction & PYGI_DIRECTION_TO_PYTHON) { |
| 860 |
arg_cache->to_py_marshaller = _pygi_marshal_to_py_array; |
| 861 |
arg_cache->to_py_cleanup = _pygi_marshal_cleanup_to_py_array; |
| 862 |
} |
| 863 |
|
| 864 |
return TRUE; |
| 865 |
} |
| 866 |
|
| 867 |
PyGIArgCache * |
| 868 |
pygi_arg_garray_new_from_info ( |
| 869 |
GITypeInfo *type_info, GIArgInfo *arg_info, GITransfer transfer, |
| 870 |
PyGIDirection direction, PyGICallableCache *callable_cache /* nullable */, |
| 871 |
gssize arg_index, gssize *py_arg_index) |
| 872 |
{ |
| 873 |
PyGIArgGArray *array_cache = g_slice_new0 (PyGIArgGArray); |
| 874 |
if (array_cache == NULL) return NULL; |
| 875 |
|
| 876 |
if (!pygi_arg_garray_setup (array_cache, type_info, arg_info, transfer, |
| 877 |
direction, callable_cache)) { |
| 878 |
pygi_arg_cache_free ((PyGIArgCache *)array_cache); |
| 879 |
return NULL; |
| 880 |
} |
| 881 |
|
| 882 |
if (callable_cache) |
| 883 |
pygi_arg_garray_len_arg_setup (array_cache, type_info, callable_cache, |
| 884 |
direction, arg_index, py_arg_index); |
| 885 |
|
| 886 |
return (PyGIArgCache *)array_cache; |
| 887 |
} |