| 1 |
/* -*- Mode: C; c-basic-offset: 4 -*- |
| 2 |
* vim: tabstop=4 shiftwidth=4 expandtab |
| 3 |
* |
| 4 |
* Copyright (C) 2005-2009 Johan Dahlin <[email protected]> |
| 5 |
* Copyright (C) 2011 John (J5) Palimier <[email protected]> |
| 6 |
* |
| 7 |
* pygi-invoke.c: main invocation function |
| 8 |
* |
| 9 |
* This library is free software; you can redistribute it and/or |
| 10 |
* modify it under the terms of the GNU Lesser General Public |
| 11 |
* License as published by the Free Software Foundation; either |
| 12 |
* version 2.1 of the License, or (at your option) any later version. |
| 13 |
* |
| 14 |
* This library is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
* Lesser General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU Lesser General Public |
| 20 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "pygi-argument.h" |
| 24 |
#include "pygi-async.h" |
| 25 |
#include "pygi-boxed.h" |
| 26 |
#include "pygi-error.h" |
| 27 |
#include "pygi-foreign.h" |
| 28 |
#include "pygi-invoke.h" |
| 29 |
#include "pygi-marshal-cleanup.h" |
| 30 |
#include "pygi-resulttuple.h" |
| 31 |
|
| 32 |
extern PyObject *_PyGIDefaultArgPlaceholder; |
| 33 |
|
| 34 |
static PyGIArgCache * |
| 35 |
next_python_argument (PyGICallableCache *cache, Py_ssize_t i, |
| 36 |
Py_ssize_t *skipped_args) |
| 37 |
{ |
| 38 |
PyGIArgCache *arg_cache = |
| 39 |
g_ptr_array_index (cache->args_cache, i + *skipped_args); |
| 40 |
|
| 41 |
/* Skip over automatically filled in arguments (e.g. GDestroyNotify) */ |
| 42 |
while (arg_cache->py_arg_index < 0 || i != arg_cache->py_arg_index) { |
| 43 |
*skipped_args += 1; |
| 44 |
g_assert (i + *skipped_args < (Py_ssize_t)cache->args_cache->len); |
| 45 |
arg_cache = g_ptr_array_index (cache->args_cache, i + *skipped_args); |
| 46 |
} |
| 47 |
return arg_cache; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* _py_args_combine_and_check_length: |
| 52 |
* @cache: PyGICallableCache |
| 53 |
* @py_args: an array of arguments as in the vectorcall protocol |
| 54 |
* @py_nargsf: the argument count plus flags |
| 55 |
* @py_kwnames: a tuple of keyword names |
| 56 |
* |
| 57 |
* Returns: New value reference to the combined py_args and py_kwargs. |
| 58 |
*/ |
| 59 |
static PyObject * |
| 60 |
_py_args_combine_and_check_length (PyGICallableCache *cache, |
| 61 |
PyObject *const *py_args, size_t py_nargsf, |
| 62 |
PyObject *py_kwnames) |
| 63 |
{ |
| 64 |
PyObject *combined_py_args = NULL; |
| 65 |
Py_ssize_t n_py_args, n_py_kwargs, i, skipped_args = 0; |
| 66 |
gssize n_expected_args = cache->n_py_args; |
| 67 |
|
| 68 |
n_py_args = PyVectorcall_NARGS (py_nargsf); |
| 69 |
if (py_kwnames == NULL) |
| 70 |
n_py_kwargs = 0; |
| 71 |
else |
| 72 |
n_py_kwargs = PyTuple_GET_SIZE (py_kwnames); |
| 73 |
|
| 74 |
if (cache->user_data_varargs_arg == NULL && n_expected_args < n_py_args) { |
| 75 |
char *full_name = pygi_callable_cache_get_full_name (cache); |
| 76 |
PyErr_Format (PyExc_TypeError, |
| 77 |
"%.200s() takes exactly %zd %sargument%s (%zd given)", |
| 78 |
full_name, n_expected_args, |
| 79 |
n_py_kwargs > 0 ? "non-keyword " : "", |
| 80 |
n_expected_args == 1 ? "" : "s", n_py_args); |
| 81 |
g_free (full_name); |
| 82 |
return NULL; |
| 83 |
} |
| 84 |
|
| 85 |
if (cache->user_data_varargs_arg != NULL && n_py_kwargs > 0 |
| 86 |
&& n_expected_args < n_py_args) { |
| 87 |
char *full_name = pygi_callable_cache_get_full_name (cache); |
| 88 |
PyErr_Format (PyExc_TypeError, |
| 89 |
"%.200s() cannot use variable user data arguments with " |
| 90 |
"keyword arguments", |
| 91 |
full_name); |
| 92 |
g_free (full_name); |
| 93 |
return NULL; |
| 94 |
} |
| 95 |
|
| 96 |
/* will hold arguments from both py_args and py_kwargs |
| 97 |
* when they are combined into a single tuple */ |
| 98 |
combined_py_args = PyTuple_New (n_expected_args); |
| 99 |
|
| 100 |
/* Add the positional arguments */ |
| 101 |
for (i = 0; i < n_py_args && i < n_expected_args; i++) { |
| 102 |
PyGIArgCache *arg_cache = |
| 103 |
next_python_argument (cache, i, &skipped_args); |
| 104 |
|
| 105 |
if (arg_cache == cache->user_data_varargs_arg) { |
| 106 |
PyObject *user_data = PyTuple_New (n_py_args - i); |
| 107 |
Py_ssize_t j; |
| 108 |
|
| 109 |
for (j = i; j < n_py_args; j++) { |
| 110 |
Py_INCREF (py_args[j]); |
| 111 |
PyTuple_SET_ITEM (user_data, j - i, py_args[j]); |
| 112 |
} |
| 113 |
PyTuple_SET_ITEM (combined_py_args, i, user_data); |
| 114 |
} else { |
| 115 |
Py_INCREF (py_args[i]); |
| 116 |
PyTuple_SET_ITEM (combined_py_args, i, py_args[i]); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/* Process keyword arguments */ |
| 121 |
for (i = 0; i < n_py_kwargs; i++) { |
| 122 |
PyObject *py_kwname, *arg_item; |
| 123 |
const char *kwname; |
| 124 |
PyGIArgCache *arg_cache; |
| 125 |
gboolean is_varargs_user_data; |
| 126 |
|
| 127 |
py_kwname = PyTuple_GET_ITEM (py_kwnames, i); |
| 128 |
kwname = PyUnicode_AsUTF8AndSize (py_kwname, NULL); |
| 129 |
if (kwname == NULL) { |
| 130 |
Py_DECREF (combined_py_args); |
| 131 |
return NULL; |
| 132 |
} |
| 133 |
arg_cache = g_hash_table_lookup (cache->arg_name_hash, kwname); |
| 134 |
if (!arg_cache) { |
| 135 |
char *full_name = pygi_callable_cache_get_full_name (cache); |
| 136 |
PyErr_Format ( |
| 137 |
PyExc_TypeError, |
| 138 |
"%.200s() got an unexpected keyword argument '%.400s'", |
| 139 |
full_name, kwname); |
| 140 |
g_free (full_name); |
| 141 |
Py_DECREF (combined_py_args); |
| 142 |
return NULL; |
| 143 |
} |
| 144 |
is_varargs_user_data = arg_cache == cache->user_data_varargs_arg; |
| 145 |
|
| 146 |
/* Have we already seen this argument? */ |
| 147 |
arg_item = |
| 148 |
PyTuple_GET_ITEM (combined_py_args, arg_cache->py_arg_index); |
| 149 |
if (arg_item != NULL) { |
| 150 |
char *full_name = pygi_callable_cache_get_full_name (cache); |
| 151 |
PyErr_Format ( |
| 152 |
PyExc_TypeError, |
| 153 |
"%.200s() got multiple values for keyword argument '%.200s'", |
| 154 |
full_name, kwname); |
| 155 |
g_free (full_name); |
| 156 |
Py_DECREF (combined_py_args); |
| 157 |
return NULL; |
| 158 |
} |
| 159 |
arg_item = py_args[n_py_args + i]; |
| 160 |
if (is_varargs_user_data) { |
| 161 |
/* Special case where user_data is passed as a keyword |
| 162 |
* argument (user_data=foo) Wrap the value in a tuple to |
| 163 |
* represent variable args for marshaling later on. |
| 164 |
*/ |
| 165 |
PyObject *user_data = Py_BuildValue ("(O)", arg_item, NULL); |
| 166 |
PyTuple_SET_ITEM (combined_py_args, arg_cache->py_arg_index, |
| 167 |
user_data); |
| 168 |
} else { |
| 169 |
Py_INCREF (arg_item); |
| 170 |
PyTuple_SET_ITEM (combined_py_args, arg_cache->py_arg_index, |
| 171 |
arg_item); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/* Fill in defaults and check for missing arguments */ |
| 176 |
for (i = n_py_args; i < n_expected_args; i++) { |
| 177 |
PyObject *arg_item = PyTuple_GET_ITEM (combined_py_args, i); |
| 178 |
PyGIArgCache *arg_cache; |
| 179 |
|
| 180 |
if (arg_item != NULL) continue; |
| 181 |
|
| 182 |
arg_cache = next_python_argument (cache, i, &skipped_args); |
| 183 |
|
| 184 |
if (arg_cache == cache->user_data_varargs_arg) { |
| 185 |
/* For varargs user_data, pass an empty tuple when nothing |
| 186 |
* is given. */ |
| 187 |
PyTuple_SET_ITEM (combined_py_args, i, PyTuple_New (0)); |
| 188 |
} else if (pygi_arg_cache_allow_none (arg_cache)) { |
| 189 |
/* If the argument supports a default, use a place holder in the |
| 190 |
* argument tuple, this will be checked later during marshaling. |
| 191 |
*/ |
| 192 |
Py_INCREF (_PyGIDefaultArgPlaceholder); |
| 193 |
PyTuple_SET_ITEM (combined_py_args, i, _PyGIDefaultArgPlaceholder); |
| 194 |
} else { |
| 195 |
char *full_name = pygi_callable_cache_get_full_name (cache); |
| 196 |
PyErr_Format ( |
| 197 |
PyExc_TypeError, |
| 198 |
"%.200s() takes exactly %zd %sargument%s (%zd given)", |
| 199 |
full_name, n_expected_args, |
| 200 |
n_py_kwargs > 0 ? "non-keyword " : "", |
| 201 |
n_expected_args == 1 ? "" : "s", n_py_args); |
| 202 |
g_free (full_name); |
| 203 |
Py_DECREF (combined_py_args); |
| 204 |
return NULL; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
return combined_py_args; |
| 209 |
} |
| 210 |
|
| 211 |
/* To reduce calls to g_slice_*() we (1) allocate all the memory depended on |
| 212 |
* the argument count in one go and (2) keep one version per argument count |
| 213 |
* around for faster reuse. |
| 214 |
*/ |
| 215 |
|
| 216 |
#define PyGI_INVOKE_ARG_STATE_SIZE(n) \ |
| 217 |
(n * (sizeof (PyGIInvokeArgState) + sizeof (GIArgument *))) |
| 218 |
#define PyGI_INVOKE_ARG_STATE_N_MAX 10 |
| 219 |
static gpointer free_arg_state[PyGI_INVOKE_ARG_STATE_N_MAX]; |
| 220 |
|
| 221 |
/** |
| 222 |
* _pygi_invoke_arg_state_init: |
| 223 |
* Sets PyGIInvokeState.args and PyGIInvokeState.ffi_args. |
| 224 |
* On error returns FALSE and sets an exception. |
| 225 |
*/ |
| 226 |
gboolean |
| 227 |
_pygi_invoke_arg_state_init (PyGIInvokeState *state) |
| 228 |
{ |
| 229 |
gpointer mem; |
| 230 |
|
| 231 |
if (state->n_args < PyGI_INVOKE_ARG_STATE_N_MAX |
| 232 |
&& (mem = free_arg_state[state->n_args]) != NULL) { |
| 233 |
free_arg_state[state->n_args] = NULL; |
| 234 |
memset (mem, 0, PyGI_INVOKE_ARG_STATE_SIZE (state->n_args)); |
| 235 |
} else { |
| 236 |
mem = g_slice_alloc0 (PyGI_INVOKE_ARG_STATE_SIZE (state->n_args)); |
| 237 |
} |
| 238 |
|
| 239 |
if (mem == NULL && state->n_args != 0) { |
| 240 |
PyErr_NoMemory (); |
| 241 |
return FALSE; |
| 242 |
} |
| 243 |
|
| 244 |
if (mem != NULL) { |
| 245 |
state->args = mem; |
| 246 |
state->ffi_args = |
| 247 |
(gpointer)((gchar *)mem |
| 248 |
+ state->n_args * sizeof (PyGIInvokeArgState)); |
| 249 |
} |
| 250 |
|
| 251 |
return TRUE; |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* _pygi_invoke_arg_state_free: |
| 256 |
* Frees PyGIInvokeState.args and PyGIInvokeState.ffi_args |
| 257 |
*/ |
| 258 |
void |
| 259 |
_pygi_invoke_arg_state_free (PyGIInvokeState *state) |
| 260 |
{ |
| 261 |
if (state->n_args < PyGI_INVOKE_ARG_STATE_N_MAX |
| 262 |
&& free_arg_state[state->n_args] == NULL) { |
| 263 |
free_arg_state[state->n_args] = state->args; |
| 264 |
return; |
| 265 |
} |
| 266 |
|
| 267 |
g_slice_free1 (PyGI_INVOKE_ARG_STATE_SIZE (state->n_args), state->args); |
| 268 |
} |
| 269 |
|
| 270 |
static gboolean |
| 271 |
_invoke_state_init_from_cache (PyGIInvokeState *state, |
| 272 |
PyGIFunctionCache *function_cache, |
| 273 |
PyObject *const *py_args, size_t py_nargsf, |
| 274 |
PyObject *kwnames) |
| 275 |
{ |
| 276 |
PyGICallableCache *cache = (PyGICallableCache *)function_cache; |
| 277 |
|
| 278 |
state->n_args = _pygi_callable_cache_args_len (cache); |
| 279 |
|
| 280 |
if (pygi_callable_cache_can_throw_gerror (cache)) { |
| 281 |
state->n_args++; |
| 282 |
} |
| 283 |
|
| 284 |
/* Copy the function pointer to the state for the normal case. For vfuncs, |
| 285 |
* this has already been filled out based on the implementor's GType. |
| 286 |
*/ |
| 287 |
if (state->function_ptr == NULL) |
| 288 |
state->function_ptr = function_cache->invoker.native_address; |
| 289 |
|
| 290 |
state->py_in_args = |
| 291 |
_py_args_combine_and_check_length (cache, py_args, py_nargsf, kwnames); |
| 292 |
|
| 293 |
if (state->py_in_args == NULL) { |
| 294 |
return FALSE; |
| 295 |
} |
| 296 |
state->n_py_in_args = PyTuple_Size (state->py_in_args); |
| 297 |
|
| 298 |
if (!_pygi_invoke_arg_state_init (state)) { |
| 299 |
return FALSE; |
| 300 |
} |
| 301 |
|
| 302 |
state->error = NULL; |
| 303 |
|
| 304 |
if (pygi_callable_cache_can_throw_gerror (cache)) { |
| 305 |
gssize error_index = state->n_args - 1; |
| 306 |
/* The ffi argument for GError needs to be a triple pointer. */ |
| 307 |
state->args[error_index].arg_pointer.v_pointer = &state->error; |
| 308 |
state->ffi_args[error_index] = &(state->args[error_index].arg_pointer); |
| 309 |
} |
| 310 |
|
| 311 |
return TRUE; |
| 312 |
} |
| 313 |
|
| 314 |
static void |
| 315 |
_invoke_state_clear (PyGIInvokeState *state, PyGIFunctionCache *function_cache) |
| 316 |
{ |
| 317 |
_pygi_invoke_arg_state_free (state); |
| 318 |
Py_XDECREF (state->py_in_args); |
| 319 |
Py_XDECREF (state->py_async); |
| 320 |
} |
| 321 |
|
| 322 |
static gboolean |
| 323 |
_caller_alloc (PyGIArgCache *arg_cache, GIArgument *arg) |
| 324 |
{ |
| 325 |
if (arg_cache->type_tag == GI_TYPE_TAG_INTERFACE) { |
| 326 |
PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache; |
| 327 |
|
| 328 |
arg->v_pointer = NULL; |
| 329 |
if (g_type_is_a (iface_cache->g_type, G_TYPE_BOXED)) { |
| 330 |
arg->v_pointer = pygi_boxed_alloc ( |
| 331 |
GI_BASE_INFO (iface_cache->interface_info), NULL); |
| 332 |
} else if (iface_cache->g_type == G_TYPE_VALUE) { |
| 333 |
arg->v_pointer = g_slice_new0 (GValue); |
| 334 |
} else if (iface_cache->is_foreign) { |
| 335 |
PyObject *foreign_struct = |
| 336 |
pygi_struct_foreign_convert_from_g_argument ( |
| 337 |
iface_cache->interface_info, GI_TRANSFER_NOTHING, NULL); |
| 338 |
|
| 339 |
pygi_struct_foreign_convert_to_g_argument ( |
| 340 |
foreign_struct, iface_cache->interface_info, |
| 341 |
GI_TRANSFER_EVERYTHING, arg); |
| 342 |
} else { |
| 343 |
gssize size = gi_struct_info_get_size ( |
| 344 |
(GIStructInfo *)iface_cache->interface_info); |
| 345 |
arg->v_pointer = g_malloc0 (size); |
| 346 |
} |
| 347 |
} else if (arg_cache->type_tag == GI_TYPE_TAG_ARRAY) { |
| 348 |
PyGIArgGArray *array_cache = (PyGIArgGArray *)arg_cache; |
| 349 |
size_t fixed_size; |
| 350 |
|
| 351 |
switch (gi_type_info_get_array_type (arg_cache->type_info)) { |
| 352 |
case GI_ARRAY_TYPE_C: |
| 353 |
if (gi_type_info_get_array_fixed_size (arg_cache->type_info, |
| 354 |
&fixed_size)) { |
| 355 |
arg->v_pointer = |
| 356 |
g_malloc0 (array_cache->item_size * fixed_size); |
| 357 |
break; |
| 358 |
} |
| 359 |
return FALSE; |
| 360 |
case GI_ARRAY_TYPE_ARRAY: |
| 361 |
arg->v_pointer = |
| 362 |
g_array_new (TRUE, TRUE, (guint)array_cache->item_size); |
| 363 |
break; |
| 364 |
case GI_ARRAY_TYPE_PTR_ARRAY: |
| 365 |
arg->v_pointer = g_ptr_array_new (); |
| 366 |
break; |
| 367 |
case GI_ARRAY_TYPE_BYTE_ARRAY: |
| 368 |
arg->v_pointer = g_byte_array_new (); |
| 369 |
break; |
| 370 |
default: |
| 371 |
g_assert_not_reached (); |
| 372 |
} |
| 373 |
} else { |
| 374 |
return FALSE; |
| 375 |
} |
| 376 |
|
| 377 |
if (arg->v_pointer == NULL) return FALSE; |
| 378 |
|
| 379 |
|
| 380 |
return TRUE; |
| 381 |
} |
| 382 |
|
| 383 |
/* pygi_invoke_marshal_in_args: |
| 384 |
* |
| 385 |
* Fills out the state struct argument lists. arg_values will always hold |
| 386 |
* actual values marshaled either to or from Python and C. arg_pointers will |
| 387 |
* hold pointers (via v_pointer) to auxilary value storage. This will normally |
| 388 |
* point to values stored in arg_values. In the case of caller allocated |
| 389 |
* out args, arg_pointers[x].v_pointer will point to newly allocated memory. |
| 390 |
* arg_pointers inserts a level of pointer indirection between arg_values |
| 391 |
* and the argument list ffi receives when dealing with non-caller allocated |
| 392 |
* out arguments. |
| 393 |
* |
| 394 |
* For example: |
| 395 |
* [[ |
| 396 |
* void callee (int *i, int j) { *i = 50 - j; } |
| 397 |
* void caller () { |
| 398 |
* int i = 0; |
| 399 |
* callee (&i, 8); |
| 400 |
* } |
| 401 |
* |
| 402 |
* args[0] == &arg_pointers[0]; |
| 403 |
* arg_pointers[0].v_pointer == &arg_values[0]; |
| 404 |
* arg_values[0].v_int == 42; |
| 405 |
* |
| 406 |
* args[1] == &arg_values[1]; |
| 407 |
* arg_values[1].v_int == 8; |
| 408 |
* ]] |
| 409 |
* |
| 410 |
*/ |
| 411 |
static gboolean |
| 412 |
_invoke_marshal_in_args (PyGIInvokeState *state, |
| 413 |
PyGIFunctionCache *function_cache) |
| 414 |
{ |
| 415 |
PyGICallableCache *cache = (PyGICallableCache *)function_cache; |
| 416 |
|
| 417 |
if (state->n_py_in_args > cache->n_py_args) { |
| 418 |
char *full_name = pygi_callable_cache_get_full_name (cache); |
| 419 |
PyErr_Format (PyExc_TypeError, |
| 420 |
"%s() takes exactly %zd argument(s) (%zd given)", |
| 421 |
full_name, cache->n_py_args, state->n_py_in_args); |
| 422 |
g_free (full_name); |
| 423 |
return FALSE; |
| 424 |
} |
| 425 |
|
| 426 |
if (function_cache->async_finish && function_cache->async_callback |
| 427 |
&& function_cache->async_callback->py_arg_index < state->n_py_in_args |
| 428 |
&& PyTuple_GET_ITEM (state->py_in_args, |
| 429 |
function_cache->async_callback->py_arg_index) |
| 430 |
== _PyGIDefaultArgPlaceholder) { |
| 431 |
/* We are dealing with an async call that returns an awaitable */ |
| 432 |
PyObject *cancellable = NULL; |
| 433 |
|
| 434 |
/* Try to resolve any passed GCancellable. */ |
| 435 |
if (function_cache->async_cancellable |
| 436 |
&& function_cache->async_cancellable->py_arg_index |
| 437 |
< state->n_py_in_args) |
| 438 |
cancellable = PyTuple_GET_ITEM ( |
| 439 |
state->py_in_args, |
| 440 |
function_cache->async_cancellable->py_arg_index); |
| 441 |
|
| 442 |
if (cancellable == _PyGIDefaultArgPlaceholder) cancellable = NULL; |
| 443 |
|
| 444 |
state->py_async = |
| 445 |
pygi_async_new (function_cache->async_finish, cancellable); |
| 446 |
} |
| 447 |
|
| 448 |
for (guint i = 0; i < _pygi_callable_cache_args_len (cache); i++) { |
| 449 |
GIArgument *c_arg = &state->args[i].arg_value; |
| 450 |
PyGIArgCache *arg_cache = g_ptr_array_index (cache->args_cache, i); |
| 451 |
PyObject *py_arg = NULL; |
| 452 |
gboolean marshal = TRUE; |
| 453 |
|
| 454 |
switch (arg_cache->direction) { |
| 455 |
case PYGI_DIRECTION_FROM_PYTHON: |
| 456 |
/* The ffi argument points directly at memory in arg_values. */ |
| 457 |
state->ffi_args[i] = c_arg; |
| 458 |
|
| 459 |
if (arg_cache->meta_type == PYGI_META_ARG_TYPE_CLOSURE) { |
| 460 |
state->ffi_args[i]->v_pointer = state->user_data; |
| 461 |
continue; |
| 462 |
} else if (arg_cache->meta_type != PYGI_META_ARG_TYPE_PARENT) |
| 463 |
continue; |
| 464 |
|
| 465 |
if (arg_cache->py_arg_index >= state->n_py_in_args) { |
| 466 |
char *full_name = pygi_callable_cache_get_full_name (cache); |
| 467 |
PyErr_Format (PyExc_TypeError, |
| 468 |
"%s() takes exactly %zd argument(s) (%zd given)", |
| 469 |
full_name, cache->n_py_args, |
| 470 |
state->n_py_in_args); |
| 471 |
g_free (full_name); |
| 472 |
|
| 473 |
/* clean up all of the args we have already marshalled, |
| 474 |
* since invoke will not be called. |
| 475 |
*/ |
| 476 |
pygi_marshal_cleanup_args_from_py_parameter_fail (state, cache, |
| 477 |
i); |
| 478 |
return FALSE; |
| 479 |
} |
| 480 |
|
| 481 |
py_arg = |
| 482 |
PyTuple_GET_ITEM (state->py_in_args, arg_cache->py_arg_index); |
| 483 |
|
| 484 |
break; |
| 485 |
case PYGI_DIRECTION_BIDIRECTIONAL: |
| 486 |
if (arg_cache->meta_type != PYGI_META_ARG_TYPE_CHILD) { |
| 487 |
if (arg_cache->py_arg_index >= state->n_py_in_args) { |
| 488 |
char *full_name = |
| 489 |
pygi_callable_cache_get_full_name (cache); |
| 490 |
PyErr_Format ( |
| 491 |
PyExc_TypeError, |
| 492 |
"%s() takes exactly %zd argument(s) (%zd given)", |
| 493 |
full_name, cache->n_py_args, state->n_py_in_args); |
| 494 |
g_free (full_name); |
| 495 |
pygi_marshal_cleanup_args_from_py_parameter_fail ( |
| 496 |
state, cache, i); |
| 497 |
return FALSE; |
| 498 |
} |
| 499 |
|
| 500 |
py_arg = PyTuple_GET_ITEM (state->py_in_args, |
| 501 |
arg_cache->py_arg_index); |
| 502 |
} |
| 503 |
/* Fall through */ |
| 504 |
|
| 505 |
case PYGI_DIRECTION_TO_PYTHON: |
| 506 |
/* arg_pointers always stores a pointer to the data to be marshaled "to python" |
| 507 |
* even in cases where arg_pointers is not being used as indirection between |
| 508 |
* ffi and arg_values. This gives a guarantee that out argument marshaling |
| 509 |
* (_invoke_marshal_out_args) can always rely on arg_pointers pointing to |
| 510 |
* the correct chunk of memory to marshal. |
| 511 |
*/ |
| 512 |
state->args[i].arg_pointer.v_pointer = c_arg; |
| 513 |
|
| 514 |
if (pygi_arg_cache_is_caller_allocates (arg_cache)) { |
| 515 |
/* In the case of caller allocated out args, we don't use |
| 516 |
* an extra level of indirection and state->args will point |
| 517 |
* directly at the data to be marshaled. However, as noted |
| 518 |
* above, arg_pointers will also point to this caller allocated |
| 519 |
* chunk of memory used by out argument marshaling. |
| 520 |
*/ |
| 521 |
state->ffi_args[i] = c_arg; |
| 522 |
|
| 523 |
if (!_caller_alloc (arg_cache, c_arg)) { |
| 524 |
char *full_name = |
| 525 |
pygi_callable_cache_get_full_name (cache); |
| 526 |
PyErr_Format (PyExc_TypeError, |
| 527 |
"Could not caller allocate argument %zd of " |
| 528 |
"callable %s", |
| 529 |
i, full_name); |
| 530 |
g_free (full_name); |
| 531 |
pygi_marshal_cleanup_args_from_py_parameter_fail ( |
| 532 |
state, cache, i); |
| 533 |
return FALSE; |
| 534 |
} |
| 535 |
} else { |
| 536 |
/* Non-caller allocated out args will use arg_pointers as an |
| 537 |
* extra level of indirection */ |
| 538 |
state->ffi_args[i] = &state->args[i].arg_pointer; |
| 539 |
} |
| 540 |
|
| 541 |
break; |
| 542 |
default: |
| 543 |
g_assert_not_reached (); |
| 544 |
} |
| 545 |
|
| 546 |
if (py_arg == _PyGIDefaultArgPlaceholder) { |
| 547 |
/* If this is the cancellable, then we may override it later if we |
| 548 |
* detect an async call. |
| 549 |
*/ |
| 550 |
marshal = FALSE; |
| 551 |
|
| 552 |
if (state->py_async |
| 553 |
&& arg_cache->async_context |
| 554 |
== PYGI_ASYNC_CONTEXT_CANCELLABLE) { |
| 555 |
marshal = TRUE; |
| 556 |
py_arg = ((PyGIAsync *)state->py_async)->cancellable; |
| 557 |
} else if (state->py_async |
| 558 |
&& arg_cache->async_context |
| 559 |
== PYGI_ASYNC_CONTEXT_CALLBACK) { |
| 560 |
marshal = TRUE; |
| 561 |
} else { |
| 562 |
c_arg->v_pointer = NULL; |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
if (marshal && arg_cache->from_py_marshaller != NULL |
| 567 |
&& arg_cache->meta_type != PYGI_META_ARG_TYPE_CHILD) { |
| 568 |
gboolean success; |
| 569 |
gpointer cleanup_data = NULL; |
| 570 |
|
| 571 |
if (!pygi_arg_cache_allow_none (arg_cache) && Py_IsNone (py_arg)) { |
| 572 |
PyErr_Format (PyExc_TypeError, |
| 573 |
"Argument %zd does not allow None as a value", |
| 574 |
i); |
| 575 |
|
| 576 |
pygi_marshal_cleanup_args_from_py_parameter_fail (state, cache, |
| 577 |
i); |
| 578 |
return FALSE; |
| 579 |
} |
| 580 |
success = arg_cache->from_py_marshaller ( |
| 581 |
state, cache, arg_cache, py_arg, c_arg, &cleanup_data); |
| 582 |
state->args[i].arg_cleanup_data = cleanup_data; |
| 583 |
|
| 584 |
if (!success) { |
| 585 |
pygi_marshal_cleanup_args_from_py_parameter_fail (state, cache, |
| 586 |
i); |
| 587 |
return FALSE; |
| 588 |
} |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
return TRUE; |
| 593 |
} |
| 594 |
|
| 595 |
static PyObject * |
| 596 |
_invoke_marshal_out_args (PyGIInvokeState *state, |
| 597 |
PyGIFunctionCache *function_cache) |
| 598 |
{ |
| 599 |
PyGICallableCache *cache = (PyGICallableCache *)function_cache; |
| 600 |
PyObject *py_out = NULL; |
| 601 |
PyObject *py_return = NULL; |
| 602 |
gssize n_out_args = cache->n_to_py_args - cache->n_to_py_child_args; |
| 603 |
|
| 604 |
if (cache->return_cache) { |
| 605 |
if (!pygi_callable_cache_skip_return (cache)) { |
| 606 |
gpointer cleanup_data = NULL; |
| 607 |
py_return = cache->return_cache->to_py_marshaller ( |
| 608 |
state, cache, cache->return_cache, &state->return_arg, |
| 609 |
&cleanup_data); |
| 610 |
state->to_py_return_arg_cleanup_data = cleanup_data; |
| 611 |
if (py_return == NULL) { |
| 612 |
pygi_marshal_cleanup_args_return_fail (state, cache); |
| 613 |
return NULL; |
| 614 |
} |
| 615 |
} else { |
| 616 |
if (cache->return_cache->transfer == GI_TRANSFER_EVERYTHING) { |
| 617 |
PyGIMarshalToPyCleanupFunc to_py_cleanup = |
| 618 |
cache->return_cache->to_py_cleanup; |
| 619 |
|
| 620 |
if (to_py_cleanup != NULL) |
| 621 |
to_py_cleanup (state, cache->return_cache, NULL, |
| 622 |
&state->return_arg, FALSE); |
| 623 |
} |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
/* Return the async future if we have one. */ |
| 628 |
if (state->py_async) { |
| 629 |
/* We must have no return value */ |
| 630 |
g_assert (n_out_args == 0); |
| 631 |
g_assert (pygi_callable_cache_skip_return (cache) |
| 632 |
|| cache->return_cache->type_tag == GI_TYPE_TAG_VOID); |
| 633 |
|
| 634 |
Py_DECREF (py_return); |
| 635 |
return Py_NewRef (state->py_async); |
| 636 |
} |
| 637 |
|
| 638 |
if (n_out_args == 0) { |
| 639 |
if (pygi_callable_cache_skip_return (cache) && state->error == NULL) { |
| 640 |
/* we skip the return value and have no (out) arguments to return, |
| 641 |
* so py_return should be NULL. But we must not return NULL, |
| 642 |
* otherwise Python will expect an exception. |
| 643 |
*/ |
| 644 |
g_assert (py_return == NULL); |
| 645 |
py_return = Py_NewRef (Py_None); |
| 646 |
} |
| 647 |
|
| 648 |
py_out = py_return; |
| 649 |
} else if (!cache->has_return && n_out_args == 1) { |
| 650 |
/* if we get here there is one out arg an no return */ |
| 651 |
PyGIArgCache *arg_cache = (PyGIArgCache *)cache->to_py_args->data; |
| 652 |
gpointer cleanup_data = NULL; |
| 653 |
py_out = arg_cache->to_py_marshaller ( |
| 654 |
state, cache, arg_cache, |
| 655 |
state->args[arg_cache->c_arg_index].arg_pointer.v_pointer, |
| 656 |
&cleanup_data); |
| 657 |
state->args[arg_cache->c_arg_index].to_py_arg_cleanup_data = |
| 658 |
cleanup_data; |
| 659 |
if (py_out == NULL) { |
| 660 |
pygi_marshal_cleanup_args_to_py_parameter_fail (state, cache, 0); |
| 661 |
return NULL; |
| 662 |
} |
| 663 |
|
| 664 |
} else { |
| 665 |
/* return a tuple */ |
| 666 |
gssize py_arg_index = 0; |
| 667 |
GSList *cache_item = cache->to_py_args; |
| 668 |
gssize tuple_len = cache->has_return + n_out_args; |
| 669 |
|
| 670 |
py_out = pygi_resulttuple_new (cache->resulttuple_type, tuple_len); |
| 671 |
|
| 672 |
if (py_out == NULL) { |
| 673 |
pygi_marshal_cleanup_args_to_py_parameter_fail (state, cache, |
| 674 |
py_arg_index); |
| 675 |
return NULL; |
| 676 |
} |
| 677 |
|
| 678 |
if (cache->has_return) { |
| 679 |
PyTuple_SET_ITEM (py_out, py_arg_index, py_return); |
| 680 |
py_arg_index++; |
| 681 |
} |
| 682 |
|
| 683 |
for (; py_arg_index < tuple_len; py_arg_index++) { |
| 684 |
PyGIArgCache *arg_cache = (PyGIArgCache *)cache_item->data; |
| 685 |
gpointer cleanup_data = NULL; |
| 686 |
PyObject *py_obj = arg_cache->to_py_marshaller ( |
| 687 |
state, cache, arg_cache, |
| 688 |
state->args[arg_cache->c_arg_index].arg_pointer.v_pointer, |
| 689 |
&cleanup_data); |
| 690 |
state->args[arg_cache->c_arg_index].to_py_arg_cleanup_data = |
| 691 |
cleanup_data; |
| 692 |
|
| 693 |
if (py_obj == NULL) { |
| 694 |
if (cache->has_return) py_arg_index--; |
| 695 |
|
| 696 |
pygi_marshal_cleanup_args_to_py_parameter_fail (state, cache, |
| 697 |
py_arg_index); |
| 698 |
Py_DECREF (py_out); |
| 699 |
return NULL; |
| 700 |
} |
| 701 |
|
| 702 |
PyTuple_SET_ITEM (py_out, py_arg_index, py_obj); |
| 703 |
cache_item = cache_item->next; |
| 704 |
} |
| 705 |
} |
| 706 |
return py_out; |
| 707 |
} |
| 708 |
|
| 709 |
PyObject * |
| 710 |
pygi_invoke_c_callable (PyGIFunctionCache *function_cache, |
| 711 |
PyGIInvokeState *state, PyObject *const *py_args, |
| 712 |
size_t py_nargsf, PyObject *py_kwnames) |
| 713 |
{ |
| 714 |
PyGICallableCache *cache = (PyGICallableCache *)function_cache; |
| 715 |
GIFFIReturnValue ffi_return_value = PYGI_ARG_INIT; |
| 716 |
PyObject *ret = NULL; |
| 717 |
|
| 718 |
if (Py_EnterRecursiveCall (" while calling a GICallable")) return NULL; |
| 719 |
; |
| 720 |
|
| 721 |
if (!_invoke_state_init_from_cache (state, function_cache, py_args, |
| 722 |
py_nargsf, py_kwnames)) |
| 723 |
goto err; |
| 724 |
|
| 725 |
if (!_invoke_marshal_in_args (state, function_cache)) goto err; |
| 726 |
|
| 727 |
Py_BEGIN_ALLOW_THREADS; |
| 728 |
|
| 729 |
ffi_call (&function_cache->invoker.cif, state->function_ptr, |
| 730 |
(void *)&ffi_return_value, (void **)state->ffi_args); |
| 731 |
|
| 732 |
Py_END_ALLOW_THREADS; |
| 733 |
|
| 734 |
/* If the callable throws, the address of state->error will be bound into |
| 735 |
* the state->args as the last value. When the callee sets an error using |
| 736 |
* the state->args passed, it will have the side effect of setting |
| 737 |
* state->error allowing for easy checking here. |
| 738 |
*/ |
| 739 |
if (state->error != NULL) { |
| 740 |
if (pygi_error_check (&state->error)) { |
| 741 |
/* even though we errored out, the call itself was successful, |
| 742 |
so we assume the call processed all of the parameters */ |
| 743 |
pygi_marshal_cleanup_args_from_py_marshal_success (state, cache); |
| 744 |
goto err; |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
if (cache->return_cache) { |
| 749 |
gi_type_info_extract_ffi_return_value (cache->return_cache->type_info, |
| 750 |
&ffi_return_value, |
| 751 |
&state->return_arg); |
| 752 |
} |
| 753 |
|
| 754 |
ret = _invoke_marshal_out_args (state, function_cache); |
| 755 |
pygi_marshal_cleanup_args_from_py_marshal_success (state, cache); |
| 756 |
|
| 757 |
if (ret != NULL) |
| 758 |
pygi_marshal_cleanup_args_to_py_marshal_success (state, cache); |
| 759 |
|
| 760 |
err: |
| 761 |
_invoke_state_clear (state, function_cache); |
| 762 |
Py_LeaveRecursiveCall (); |
| 763 |
return ret; |
| 764 |
} |
| 765 |
|
| 766 |
PyObject * |
| 767 |
pygi_callable_info_invoke (PyGICallableInfo *self, PyObject *const *py_args, |
| 768 |
size_t py_nargsf, PyObject *py_kwnames) |
| 769 |
{ |
| 770 |
PyGIFunctionCache *cache = pygi_callable_info_get_cache (self); |
| 771 |
|
| 772 |
if (cache == NULL) return NULL; |
| 773 |
|
| 774 |
return pygi_function_cache_invoke (cache, py_args, py_nargsf, py_kwnames); |
| 775 |
} |