| 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 "pygboxed.h" |
| 22 |
#include "pygpointer.h" |
| 23 |
#include "pygi-boxed.h" |
| 24 |
#include "pygi-foreign.h" |
| 25 |
#include "pygi-info.h" |
| 26 |
#include "pygi-struct.h" |
| 27 |
#include "pygi-type.h" |
| 28 |
#include "pygi-value.h" |
| 29 |
#include "pygi-cache-private.h" |
| 30 |
|
| 31 |
/* |
| 32 |
* _is_union_member - check to see if the py_arg is actually a member of the |
| 33 |
* expected C union |
| 34 |
*/ |
| 35 |
static gboolean |
| 36 |
_is_union_member (GIRegisteredTypeInfo *interface_info, PyObject *py_arg) |
| 37 |
{ |
| 38 |
gint i; |
| 39 |
gint n_fields; |
| 40 |
GIUnionInfo *union_info; |
| 41 |
gboolean is_member = FALSE; |
| 42 |
|
| 43 |
if (!GI_IS_UNION_INFO (interface_info)) return FALSE; |
| 44 |
|
| 45 |
union_info = (GIUnionInfo *)interface_info; |
| 46 |
n_fields = gi_union_info_get_n_fields (union_info); |
| 47 |
|
| 48 |
for (i = 0; i < n_fields; i++) { |
| 49 |
GIFieldInfo *field_info; |
| 50 |
GITypeInfo *field_type_info; |
| 51 |
|
| 52 |
field_info = gi_union_info_get_field (union_info, i); |
| 53 |
field_type_info = gi_field_info_get_type_info (field_info); |
| 54 |
|
| 55 |
/* we can only check if the members are interfaces */ |
| 56 |
if (gi_type_info_get_tag (field_type_info) == GI_TYPE_TAG_INTERFACE) { |
| 57 |
GIBaseInfo *field_iface_info; |
| 58 |
PyObject *py_type; |
| 59 |
|
| 60 |
field_iface_info = gi_type_info_get_interface (field_type_info); |
| 61 |
py_type = pygi_type_import_by_gi_info (field_iface_info); |
| 62 |
|
| 63 |
if (py_type != NULL && PyObject_IsInstance (py_arg, py_type)) { |
| 64 |
is_member = TRUE; |
| 65 |
} |
| 66 |
|
| 67 |
Py_XDECREF (py_type); |
| 68 |
gi_base_info_unref (field_iface_info); |
| 69 |
} |
| 70 |
|
| 71 |
gi_base_info_unref ((GIBaseInfo *)field_type_info); |
| 72 |
gi_base_info_unref ((GIBaseInfo *)field_info); |
| 73 |
|
| 74 |
if (is_member) break; |
| 75 |
} |
| 76 |
|
| 77 |
return is_member; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
/* |
| 82 |
* GValue from Python |
| 83 |
*/ |
| 84 |
|
| 85 |
static gboolean |
| 86 |
pygi_arg_gvalue_from_py_marshal (PyGIInvokeState *state, |
| 87 |
PyGICallableCache *callable_cache, |
| 88 |
PyGIArgCache *arg_cache, PyObject *py_arg, |
| 89 |
GIArgument *arg, gpointer *cleanup_data) |
| 90 |
{ |
| 91 |
GValue *value; |
| 92 |
GType object_type; |
| 93 |
|
| 94 |
if (Py_IsNone (py_arg)) { |
| 95 |
arg->v_pointer = NULL; |
| 96 |
return TRUE; |
| 97 |
} |
| 98 |
|
| 99 |
object_type = |
| 100 |
pyg_type_from_object_strict ((PyObject *)Py_TYPE (py_arg), FALSE); |
| 101 |
|
| 102 |
if (object_type == G_TYPE_INVALID) { |
| 103 |
PyErr_SetString (PyExc_RuntimeError, |
| 104 |
"unable to retrieve object's GType"); |
| 105 |
return FALSE; |
| 106 |
} |
| 107 |
|
| 108 |
/* if already a gvalue, use that, else marshal into gvalue */ |
| 109 |
if (object_type == G_TYPE_VALUE) { |
| 110 |
/* We borrow a reference*/ |
| 111 |
value = pyg_boxed_get (py_arg, GValue); |
| 112 |
} else { |
| 113 |
value = g_slice_new0 (GValue); |
| 114 |
g_value_init (value, object_type); |
| 115 |
if (pyg_value_from_pyobject_with_error (value, py_arg) < 0) { |
| 116 |
g_slice_free (GValue, value); |
| 117 |
return FALSE; |
| 118 |
} |
| 119 |
|
| 120 |
if (arg_cache->transfer == GI_TRANSFER_NOTHING) { |
| 121 |
/* Free everything in cleanup. */ |
| 122 |
*cleanup_data = value; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
arg->v_pointer = value; |
| 127 |
|
| 128 |
return TRUE; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
void |
| 133 |
pygi_arg_gvalue_from_py_cleanup (PyGIInvokeState *state, |
| 134 |
PyGIArgCache *arg_cache, PyObject *py_arg, |
| 135 |
gpointer data, gboolean was_processed) |
| 136 |
{ |
| 137 |
/* Note py_arg can be NULL for hash table which is a bug. */ |
| 138 |
if (was_processed && py_arg != NULL) { |
| 139 |
GType py_object_type = |
| 140 |
pyg_type_from_object_strict ((PyObject *)Py_TYPE (py_arg), FALSE); |
| 141 |
|
| 142 |
/* When a GValue was not passed, it means the marshalers created a new |
| 143 |
* one to pass in, clean this up. |
| 144 |
*/ |
| 145 |
if (py_object_type != G_TYPE_VALUE) { |
| 146 |
g_value_unset ((GValue *)data); |
| 147 |
g_slice_free (GValue, data); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
static gboolean |
| 153 |
pygi_arg_gclosure_from_py_marshal (PyGIInvokeState *state, |
| 154 |
PyGICallableCache *callable_cache, |
| 155 |
PyGIArgCache *arg_cache, PyObject *py_arg, |
| 156 |
GIArgument *arg, gpointer *cleanup_data) |
| 157 |
{ |
| 158 |
GClosure *closure; |
| 159 |
GType object_gtype; |
| 160 |
|
| 161 |
if (Py_IsNone (py_arg)) { |
| 162 |
arg->v_pointer = NULL; |
| 163 |
return TRUE; |
| 164 |
} |
| 165 |
|
| 166 |
object_gtype = pyg_type_from_object_strict (py_arg, FALSE); |
| 167 |
|
| 168 |
if (!(PyCallable_Check (py_arg) |
| 169 |
|| g_type_is_a (object_gtype, G_TYPE_CLOSURE))) { |
| 170 |
PyErr_Format (PyExc_TypeError, "Must be callable, not %s", |
| 171 |
Py_TYPE (py_arg)->tp_name); |
| 172 |
return FALSE; |
| 173 |
} |
| 174 |
|
| 175 |
if (g_type_is_a (object_gtype, G_TYPE_CLOSURE)) { |
| 176 |
closure = (GClosure *)pyg_boxed_get (py_arg, void); |
| 177 |
/* Make sure we own a ref which is held until cleanup. */ |
| 178 |
if (closure != NULL) { |
| 179 |
g_closure_ref (closure); |
| 180 |
} |
| 181 |
} else { |
| 182 |
PyObject *functools; |
| 183 |
PyObject *partial = NULL; |
| 184 |
|
| 185 |
functools = PyImport_ImportModule ("functools"); |
| 186 |
if (functools) { |
| 187 |
partial = PyObject_GetAttrString (functools, "partial"); |
| 188 |
Py_DECREF (functools); |
| 189 |
} |
| 190 |
|
| 191 |
if (partial && PyObject_IsInstance (py_arg, partial) > 0 |
| 192 |
&& PyObject_HasAttrString (py_arg, "__gtk_template__")) { |
| 193 |
PyObject *partial_func; |
| 194 |
PyObject *partial_args; |
| 195 |
PyObject *partial_keywords; |
| 196 |
PyObject *swap_data; |
| 197 |
|
| 198 |
partial_func = PyObject_GetAttrString (py_arg, "func"); |
| 199 |
partial_args = PyObject_GetAttrString (py_arg, "args"); |
| 200 |
partial_keywords = PyObject_GetAttrString (py_arg, "keywords"); |
| 201 |
swap_data = PyDict_GetItemString (partial_keywords, "swap_data"); |
| 202 |
|
| 203 |
closure = pyg_closure_new (partial_func, partial_args, swap_data); |
| 204 |
|
| 205 |
Py_DECREF (partial_func); |
| 206 |
Py_DECREF (partial_args); |
| 207 |
Py_DECREF (partial_keywords); |
| 208 |
g_closure_ref (closure); |
| 209 |
g_closure_sink (closure); |
| 210 |
} else { |
| 211 |
closure = pyg_closure_new (py_arg, NULL, NULL); |
| 212 |
g_closure_ref (closure); |
| 213 |
g_closure_sink (closure); |
| 214 |
} |
| 215 |
|
| 216 |
if (partial) { |
| 217 |
Py_DECREF (partial); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
if (closure == NULL) { |
| 222 |
PyErr_SetString (PyExc_RuntimeError, |
| 223 |
"PyObject conversion to GClosure failed"); |
| 224 |
return FALSE; |
| 225 |
} |
| 226 |
|
| 227 |
arg->v_pointer = closure; |
| 228 |
|
| 229 |
if (arg_cache->transfer == GI_TRANSFER_NOTHING) { |
| 230 |
/* Free everything in cleanup. */ |
| 231 |
*cleanup_data = closure; |
| 232 |
} else { /* GI_TRANSFER_EVERYTHING */ |
| 233 |
/* No cleanup, everything is given to the callee. */ |
| 234 |
*cleanup_data = NULL; |
| 235 |
} |
| 236 |
|
| 237 |
return TRUE; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
static void |
| 242 |
arg_gclosure_from_py_cleanup (PyGIInvokeState *state, PyGIArgCache *arg_cache, |
| 243 |
PyObject *py_arg, gpointer cleanup_data, |
| 244 |
gboolean was_processed) |
| 245 |
{ |
| 246 |
if (cleanup_data != NULL) { |
| 247 |
g_closure_unref (cleanup_data); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
static gboolean |
| 252 |
pygi_arg_foreign_from_py_marshal (PyGIInvokeState *state, |
| 253 |
PyGICallableCache *callable_cache, |
| 254 |
PyGIArgCache *arg_cache, PyObject *py_arg, |
| 255 |
GIArgument *arg, gpointer *cleanup_data) |
| 256 |
{ |
| 257 |
PyObject *success; |
| 258 |
PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache; |
| 259 |
|
| 260 |
if (Py_IsNone (py_arg)) { |
| 261 |
arg->v_pointer = NULL; |
| 262 |
return TRUE; |
| 263 |
} |
| 264 |
|
| 265 |
success = pygi_struct_foreign_convert_to_g_argument ( |
| 266 |
py_arg, GI_REGISTERED_TYPE_INFO (iface_cache->interface_info), |
| 267 |
arg_cache->transfer, arg); |
| 268 |
|
| 269 |
*cleanup_data = arg->v_pointer; |
| 270 |
|
| 271 |
return Py_IsNone (success); |
| 272 |
} |
| 273 |
|
| 274 |
static void |
| 275 |
arg_foreign_from_py_cleanup (PyGIInvokeState *state, PyGIArgCache *arg_cache, |
| 276 |
PyObject *py_arg, gpointer data, |
| 277 |
gboolean was_processed) |
| 278 |
{ |
| 279 |
if (state->failed && was_processed) { |
| 280 |
pygi_struct_foreign_release ( |
| 281 |
GI_BASE_INFO (((PyGIInterfaceCache *)arg_cache)->interface_info), |
| 282 |
data); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
static void |
| 287 |
raise_type_error (PyObject *py_arg, const gchar *arg_name, |
| 288 |
GIRegisteredTypeInfo *interface_info) |
| 289 |
{ |
| 290 |
gchar *type_name = |
| 291 |
_pygi_gi_base_info_get_fullname (GI_BASE_INFO (interface_info)); |
| 292 |
PyObject *module = PyObject_GetAttrString (py_arg, "__module__"); |
| 293 |
|
| 294 |
PyErr_Format (PyExc_TypeError, "argument %s: Expected %s, but got %s%s%s", |
| 295 |
arg_name ? arg_name : "self", type_name, |
| 296 |
module ? PyUnicode_AsUTF8 (module) : "", module ? "." : "", |
| 297 |
Py_TYPE (py_arg)->tp_name); |
| 298 |
if (module) Py_DECREF (module); |
| 299 |
g_free (type_name); |
| 300 |
} |
| 301 |
|
| 302 |
static gboolean |
| 303 |
pygi_arg_struct_from_py_marshal (PyGIInvokeState *state, |
| 304 |
PyGICallableCache *callable_cache, |
| 305 |
PyGIArgCache *arg_cache, PyObject *py_arg, |
| 306 |
GIArgument *arg, gpointer *cleanup_data) |
| 307 |
{ |
| 308 |
PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache; |
| 309 |
const gchar *arg_name = pygi_arg_cache_get_name (arg_cache); |
| 310 |
GIRegisteredTypeInfo *interface_info = |
| 311 |
GI_REGISTERED_TYPE_INFO (iface_cache->interface_info); |
| 312 |
GType g_type = iface_cache->g_type; |
| 313 |
gboolean is_union = FALSE; |
| 314 |
|
| 315 |
if (Py_IsNone (py_arg)) { |
| 316 |
arg->v_pointer = NULL; |
| 317 |
return TRUE; |
| 318 |
} |
| 319 |
|
| 320 |
if (!PyObject_IsInstance (py_arg, iface_cache->py_type)) { |
| 321 |
/* first check to see if this is a member of the expected union */ |
| 322 |
is_union = _is_union_member (GI_REGISTERED_TYPE_INFO (interface_info), |
| 323 |
py_arg); |
| 324 |
if (!is_union) { |
| 325 |
raise_type_error (py_arg, arg_name, interface_info); |
| 326 |
return FALSE; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
if (g_type_is_a (g_type, G_TYPE_BOXED)) { |
| 331 |
/* Additionally use pyg_type_from_object to pull the stashed __gtype__ |
| 332 |
* attribute off of the input argument for type checking. This is needed |
| 333 |
* to work around type discrepancies in cases with aliased (typedef) types. |
| 334 |
* e.g. GtkAllocation, GdkRectangle. |
| 335 |
* See: https://bugzilla.gnomethere are .org/show_bug.cgi?id=707140 |
| 336 |
*/ |
| 337 |
if (is_union || pyg_boxed_check (py_arg, g_type) |
| 338 |
|| g_type_is_a (pyg_type_from_object (py_arg), g_type)) { |
| 339 |
arg->v_pointer = pyg_boxed_get (py_arg, void); |
| 340 |
if (arg_cache->transfer == GI_TRANSFER_EVERYTHING) { |
| 341 |
arg->v_pointer = g_boxed_copy (g_type, arg->v_pointer); |
| 342 |
} |
| 343 |
} else { |
| 344 |
raise_type_error (py_arg, arg_name, interface_info); |
| 345 |
return FALSE; |
| 346 |
} |
| 347 |
|
| 348 |
} else if (g_type_is_a (g_type, G_TYPE_POINTER) |
| 349 |
|| g_type_is_a (g_type, G_TYPE_VARIANT) |
| 350 |
|| g_type == G_TYPE_NONE) { |
| 351 |
g_warn_if_fail (g_type_is_a (g_type, G_TYPE_VARIANT) |
| 352 |
|| !arg_cache->is_pointer |
| 353 |
|| arg_cache->transfer == GI_TRANSFER_NOTHING); |
| 354 |
|
| 355 |
if (g_type_is_a (g_type, G_TYPE_VARIANT) |
| 356 |
&& pyg_type_from_object (py_arg) != G_TYPE_VARIANT) { |
| 357 |
PyErr_SetString (PyExc_TypeError, "expected GLib.Variant"); |
| 358 |
return FALSE; |
| 359 |
} |
| 360 |
arg->v_pointer = pyg_pointer_get (py_arg, void); |
| 361 |
if (arg_cache->transfer == GI_TRANSFER_EVERYTHING) { |
| 362 |
g_variant_ref ((GVariant *)arg->v_pointer); |
| 363 |
} |
| 364 |
|
| 365 |
} else { |
| 366 |
PyErr_Format (PyExc_NotImplementedError, |
| 367 |
"structure type '%s' is not supported yet", |
| 368 |
g_type_name (g_type)); |
| 369 |
return FALSE; |
| 370 |
} |
| 371 |
|
| 372 |
/* Assume struct marshaling is always a pointer and assign cleanup_data |
| 373 |
* here rather than passing it further down the chain. |
| 374 |
*/ |
| 375 |
*cleanup_data = arg->v_pointer; |
| 376 |
|
| 377 |
return TRUE; |
| 378 |
} |
| 379 |
|
| 380 |
static PyObject * |
| 381 |
pygi_arg_gvalue_to_py_marshal (PyGIInvokeState *state, |
| 382 |
PyGICallableCache *callable_cache, |
| 383 |
PyGIArgCache *arg_cache, GIArgument *arg, |
| 384 |
gpointer *cleanup_data) |
| 385 |
{ |
| 386 |
PyObject *py_obj; |
| 387 |
|
| 388 |
if (arg->v_pointer == NULL) { |
| 389 |
Py_RETURN_NONE; |
| 390 |
} |
| 391 |
|
| 392 |
py_obj = pyg_value_to_pyobject ( |
| 393 |
arg->v_pointer, pygi_arg_cache_is_caller_allocates (arg_cache)); |
| 394 |
|
| 395 |
// TODO: clean up GValue if we own it |
| 396 |
|
| 397 |
return py_obj; |
| 398 |
} |
| 399 |
|
| 400 |
static PyObject * |
| 401 |
pygi_arg_foreign_to_py_marshal (PyGIInvokeState *state, |
| 402 |
PyGICallableCache *callable_cache, |
| 403 |
PyGIArgCache *arg_cache, GIArgument *arg, |
| 404 |
gpointer *cleanup_data) |
| 405 |
{ |
| 406 |
PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache; |
| 407 |
|
| 408 |
PyObject *py_obj; |
| 409 |
|
| 410 |
if (arg->v_pointer == NULL) { |
| 411 |
Py_RETURN_NONE; |
| 412 |
} |
| 413 |
|
| 414 |
py_obj = pygi_struct_foreign_convert_from_g_argument ( |
| 415 |
iface_cache->interface_info, arg_cache->transfer, arg->v_pointer); |
| 416 |
|
| 417 |
*cleanup_data = py_obj; |
| 418 |
|
| 419 |
return py_obj; |
| 420 |
} |
| 421 |
|
| 422 |
static void |
| 423 |
arg_foreign_to_py_cleanup (PyGIInvokeState *state, PyGIArgCache *arg_cache, |
| 424 |
gpointer cleanup_data, gpointer data, |
| 425 |
gboolean was_processed) |
| 426 |
{ |
| 427 |
if (!was_processed && arg_cache->transfer == GI_TRANSFER_EVERYTHING) { |
| 428 |
pygi_struct_foreign_release ( |
| 429 |
GI_BASE_INFO (((PyGIInterfaceCache *)arg_cache)->interface_info), |
| 430 |
data); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
static PyObject * |
| 435 |
pygi_arg_struct_to_py_marshal (PyGIInvokeState *state, |
| 436 |
PyGICallableCache *callable_cache, |
| 437 |
PyGIArgCache *arg_cache, GIArgument *arg, |
| 438 |
gpointer *cleanup_data) |
| 439 |
{ |
| 440 |
PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache; |
| 441 |
GIRegisteredTypeInfo *interface_info = iface_cache->interface_info; |
| 442 |
GType g_type = iface_cache->g_type; |
| 443 |
PyObject *py_type = iface_cache->py_type; |
| 444 |
GITransfer transfer = arg_cache->transfer; |
| 445 |
gboolean is_allocated = pygi_arg_cache_is_caller_allocates (arg_cache); |
| 446 |
PyObject *py_obj = NULL; |
| 447 |
|
| 448 |
if (arg->v_pointer == NULL) { |
| 449 |
Py_RETURN_NONE; |
| 450 |
} |
| 451 |
|
| 452 |
if (g_type_is_a (g_type, G_TYPE_BOXED)) { |
| 453 |
if (py_type) { |
| 454 |
py_obj = pygi_boxed_new ( |
| 455 |
(PyTypeObject *)py_type, arg->v_pointer, |
| 456 |
transfer == GI_TRANSFER_EVERYTHING || is_allocated, |
| 457 |
is_allocated |
| 458 |
? gi_struct_info_get_size (GI_STRUCT_INFO (interface_info)) |
| 459 |
: 0); |
| 460 |
} |
| 461 |
} else if (g_type_is_a (g_type, G_TYPE_POINTER)) { |
| 462 |
if (py_type == NULL |
| 463 |
|| !PyType_IsSubtype ((PyTypeObject *)py_type, &PyGIStruct_Type)) { |
| 464 |
g_warn_if_fail (transfer == GI_TRANSFER_NOTHING); |
| 465 |
py_obj = pyg_pointer_new (g_type, arg->v_pointer); |
| 466 |
} else { |
| 467 |
py_obj = pygi_struct_new ((PyTypeObject *)py_type, arg->v_pointer, |
| 468 |
transfer == GI_TRANSFER_EVERYTHING); |
| 469 |
} |
| 470 |
} else if (g_type_is_a (g_type, G_TYPE_VARIANT)) { |
| 471 |
/* Note: sink the variant (add a ref) only if we are not transfered ownership. |
| 472 |
* GLib.Variant overrides __del__ which will then call "g_variant_unref" for |
| 473 |
* cleanup in either case. */ |
| 474 |
if (py_type) { |
| 475 |
if (transfer == GI_TRANSFER_NOTHING) { |
| 476 |
g_variant_ref_sink (arg->v_pointer); |
| 477 |
} |
| 478 |
py_obj = pygi_struct_new ((PyTypeObject *)py_type, arg->v_pointer, |
| 479 |
FALSE); |
| 480 |
} |
| 481 |
} else if (g_type == G_TYPE_NONE) { |
| 482 |
if (py_type) { |
| 483 |
py_obj = pygi_struct_new ( |
| 484 |
(PyTypeObject *)py_type, arg->v_pointer, |
| 485 |
transfer == GI_TRANSFER_EVERYTHING || is_allocated); |
| 486 |
} |
| 487 |
} else { |
| 488 |
PyErr_Format (PyExc_NotImplementedError, |
| 489 |
"structure type '%s' is not supported yet", |
| 490 |
g_type_name (g_type)); |
| 491 |
} |
| 492 |
|
| 493 |
*cleanup_data = py_obj; |
| 494 |
|
| 495 |
return py_obj; |
| 496 |
} |
| 497 |
|
| 498 |
static gboolean |
| 499 |
arg_type_class_from_py_marshal (PyGIInvokeState *state, |
| 500 |
PyGICallableCache *callable_cache, |
| 501 |
PyGIArgCache *arg_cache, PyObject *py_arg, |
| 502 |
GIArgument *arg, gpointer *cleanup_data) |
| 503 |
{ |
| 504 |
GType gtype = pyg_type_from_object (py_arg); |
| 505 |
|
| 506 |
if (G_TYPE_IS_CLASSED (gtype)) { |
| 507 |
arg->v_pointer = g_type_class_ref (gtype); |
| 508 |
|
| 509 |
if (arg_cache->transfer == GI_TRANSFER_NOTHING) { |
| 510 |
*cleanup_data = arg->v_pointer; |
| 511 |
} |
| 512 |
return TRUE; |
| 513 |
} else { |
| 514 |
PyErr_Format (PyExc_TypeError, |
| 515 |
"Unable to retrieve a GObject type class from \"%s\".", |
| 516 |
Py_TYPE (py_arg)->tp_name); |
| 517 |
return FALSE; |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
static void |
| 522 |
arg_type_class_from_py_cleanup (PyGIInvokeState *state, |
| 523 |
PyGIArgCache *arg_cache, PyObject *py_arg, |
| 524 |
gpointer data, gboolean was_processed) |
| 525 |
{ |
| 526 |
if (was_processed) { |
| 527 |
g_type_class_unref (data); |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
static void |
| 532 |
arg_boxed_to_py_cleanup (PyGIInvokeState *state, PyGIArgCache *arg_cache, |
| 533 |
gpointer cleanup_data, gpointer data, |
| 534 |
gboolean was_processed) |
| 535 |
{ |
| 536 |
if (arg_cache->transfer == GI_TRANSFER_NOTHING) |
| 537 |
pygi_boxed_copy_in_place ((PyGIBoxed *)cleanup_data); |
| 538 |
} |
| 539 |
|
| 540 |
static void |
| 541 |
arg_struct_from_py_setup (PyGIArgCache *arg_cache, |
| 542 |
GIRegisteredTypeInfo *iface_info, |
| 543 |
GITransfer transfer) |
| 544 |
{ |
| 545 |
PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache; |
| 546 |
|
| 547 |
if (gi_struct_info_is_gtype_struct ((GIStructInfo *)iface_info)) { |
| 548 |
arg_cache->from_py_marshaller = arg_type_class_from_py_marshal; |
| 549 |
arg_cache->from_py_cleanup = arg_type_class_from_py_cleanup; |
| 550 |
} else if (g_type_is_a (iface_cache->g_type, G_TYPE_CLOSURE)) { |
| 551 |
arg_cache->from_py_marshaller = pygi_arg_gclosure_from_py_marshal; |
| 552 |
arg_cache->from_py_cleanup = arg_gclosure_from_py_cleanup; |
| 553 |
} else if (iface_cache->g_type == G_TYPE_VALUE) { |
| 554 |
arg_cache->from_py_marshaller = pygi_arg_gvalue_from_py_marshal; |
| 555 |
arg_cache->from_py_cleanup = pygi_arg_gvalue_from_py_cleanup; |
| 556 |
} else if (iface_cache->is_foreign) { |
| 557 |
arg_cache->from_py_marshaller = pygi_arg_foreign_from_py_marshal; |
| 558 |
arg_cache->from_py_cleanup = arg_foreign_from_py_cleanup; |
| 559 |
} else { |
| 560 |
arg_cache->from_py_marshaller = pygi_arg_struct_from_py_marshal; |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
static void |
| 565 |
arg_struct_to_py_setup (PyGIArgCache *arg_cache, |
| 566 |
GIRegisteredTypeInfo *iface_info, GITransfer transfer) |
| 567 |
{ |
| 568 |
PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache; |
| 569 |
|
| 570 |
if (iface_cache->g_type == G_TYPE_VALUE) { |
| 571 |
arg_cache->to_py_marshaller = pygi_arg_gvalue_to_py_marshal; |
| 572 |
} else if (iface_cache->is_foreign) { |
| 573 |
arg_cache->to_py_marshaller = pygi_arg_foreign_to_py_marshal; |
| 574 |
arg_cache->to_py_cleanup = arg_foreign_to_py_cleanup; |
| 575 |
} else { |
| 576 |
arg_cache->to_py_marshaller = pygi_arg_struct_to_py_marshal; |
| 577 |
if (iface_cache->py_type |
| 578 |
&& g_type_is_a (iface_cache->g_type, G_TYPE_BOXED)) |
| 579 |
arg_cache->to_py_cleanup = arg_boxed_to_py_cleanup; |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
PyGIArgCache * |
| 584 |
pygi_arg_struct_new_from_info (GITypeInfo *type_info, GIArgInfo *arg_info, |
| 585 |
GITransfer transfer, PyGIDirection direction, |
| 586 |
GIRegisteredTypeInfo *iface_info) |
| 587 |
{ |
| 588 |
PyGIArgCache *cache = NULL; |
| 589 |
PyGIInterfaceCache *iface_cache; |
| 590 |
|
| 591 |
cache = pygi_arg_interface_new_from_info (type_info, arg_info, transfer, |
| 592 |
direction, iface_info); |
| 593 |
if (cache == NULL) return NULL; |
| 594 |
|
| 595 |
iface_cache = (PyGIInterfaceCache *)cache; |
| 596 |
iface_cache->is_foreign = |
| 597 |
(GI_IS_STRUCT_INFO ((GIBaseInfo *)iface_info)) |
| 598 |
&& (gi_struct_info_is_foreign ((GIStructInfo *)iface_info)); |
| 599 |
|
| 600 |
if (direction & PYGI_DIRECTION_FROM_PYTHON) { |
| 601 |
arg_struct_from_py_setup (cache, iface_info, transfer); |
| 602 |
} |
| 603 |
|
| 604 |
if (direction & PYGI_DIRECTION_TO_PYTHON) { |
| 605 |
arg_struct_to_py_setup (cache, iface_info, transfer); |
| 606 |
} |
| 607 |
|
| 608 |
return cache; |
| 609 |
} |