1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
4 * Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>
5 * Copyright (C) 2014 Simon Feltman <sfeltman@gnome.org>
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.
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.
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/>.
23 #include <pyglib-python-compat.h>
25 #include "pygi-struct-marshal.h"
26 #include "pygi-struct.h"
27 #include "pygi-foreign.h"
28 #include "pygi-value.h"
29 #include "pygi-type.h"
30 #include "pygi-boxed.h"
31 #include "pygi-info.h"
32 #include "pygpointer.h"
37 * _is_union_member - check to see if the py_arg is actually a member of the
41 _is_union_member (GIInterfaceInfo *interface_info, PyObject *py_arg) {
44 GIUnionInfo *union_info;
46 gboolean is_member = FALSE;
48 info_type = g_base_info_get_type (interface_info);
50 if (info_type != GI_INFO_TYPE_UNION)
53 union_info = (GIUnionInfo *) interface_info;
54 n_fields = g_union_info_get_n_fields (union_info);
56 for (i = 0; i < n_fields; i++) {
57 GIFieldInfo *field_info;
58 GITypeInfo *field_type_info;
60 field_info = g_union_info_get_field (union_info, i);
61 field_type_info = g_field_info_get_type (field_info);
63 /* we can only check if the members are interfaces */
64 if (g_type_info_get_tag (field_type_info) == GI_TYPE_TAG_INTERFACE) {
65 GIInterfaceInfo *field_iface_info;
68 field_iface_info = g_type_info_get_interface (field_type_info);
69 py_type = _pygi_type_import_by_gi_info ((GIBaseInfo *) field_iface_info);
71 if (py_type != NULL && PyObject_IsInstance (py_arg, py_type)) {
76 g_base_info_unref ( ( GIBaseInfo *) field_iface_info);
79 g_base_info_unref ( ( GIBaseInfo *) field_type_info);
80 g_base_info_unref ( ( GIBaseInfo *) field_info);
94 /* pygi_arg_gvalue_from_py_marshal:
98 * copy_reference: TRUE if arg should use the pointer reference held by py_arg
99 * when it is already holding a GValue vs. copying the value.
102 pygi_arg_gvalue_from_py_marshal (PyObject *py_arg,
105 gboolean copy_reference) {
109 object_type = pyg_type_from_object_strict ( (PyObject *) py_arg->ob_type, FALSE);
110 if (object_type == G_TYPE_INVALID) {
111 PyErr_SetString (PyExc_RuntimeError, "unable to retrieve object's GType");
115 /* if already a gvalue, use that, else marshal into gvalue */
116 if (object_type == G_TYPE_VALUE) {
117 GValue *source_value = pyg_boxed_get (py_arg, GValue);
118 if (copy_reference) {
119 value = source_value;
121 value = g_slice_new0 (GValue);
122 g_value_init (value, G_VALUE_TYPE (source_value));
123 g_value_copy (source_value, value);
126 value = g_slice_new0 (GValue);
127 g_value_init (value, object_type);
128 if (pyg_value_from_pyobject (value, py_arg) < 0) {
129 g_slice_free (GValue, value);
130 PyErr_SetString (PyExc_RuntimeError, "PyObject conversion to GValue failed");
135 arg->v_pointer = value;
140 pygi_arg_gvalue_from_py_cleanup (PyGIInvokeState *state,
141 PyGIArgCache *arg_cache,
144 gboolean was_processed)
146 /* Note py_arg can be NULL for hash table which is a bug. */
147 if (was_processed && py_arg != NULL) {
148 GType py_object_type =
149 pyg_type_from_object_strict ( (PyObject *) py_arg->ob_type, FALSE);
151 /* When a GValue was not passed, it means the marshalers created a new
152 * one to pass in, clean this up.
154 if (py_object_type != G_TYPE_VALUE) {
155 g_value_unset ((GValue *) data);
156 g_slice_free (GValue, data);
161 /* pygi_arg_gclosure_from_py_marshal:
166 pygi_arg_gclosure_from_py_marshal (PyObject *py_arg,
171 GType object_gtype = pyg_type_from_object_strict (py_arg, FALSE);
173 if ( !(PyCallable_Check(py_arg) ||
174 g_type_is_a (object_gtype, G_TYPE_CLOSURE))) {
175 PyErr_Format (PyExc_TypeError, "Must be callable, not %s",
176 py_arg->ob_type->tp_name);
180 if (g_type_is_a (object_gtype, G_TYPE_CLOSURE)) {
181 closure = (GClosure *)pyg_boxed_get (py_arg, void);
182 /* Make sure we own a ref which is held until cleanup. */
183 if (closure != NULL) {
184 g_closure_ref (closure);
187 closure = pyg_closure_new (py_arg, NULL, NULL);
188 g_closure_ref (closure);
189 g_closure_sink (closure);
192 if (closure == NULL) {
193 PyErr_SetString (PyExc_RuntimeError, "PyObject conversion to GClosure failed");
197 /* Add an additional ref when transfering everything to the callee. */
198 if (transfer == GI_TRANSFER_EVERYTHING) {
199 g_closure_ref (closure);
202 arg->v_pointer = closure;
207 arg_gclosure_from_py_cleanup (PyGIInvokeState *state,
208 PyGIArgCache *arg_cache,
210 gpointer cleanup_data,
211 gboolean was_processed)
213 if (cleanup_data != NULL) {
214 g_closure_unref (cleanup_data);
218 /* pygi_arg_struct_from_py_marshal:
220 * Dispatcher to various sub marshalers
223 pygi_arg_struct_from_py_marshal (PyObject *py_arg,
225 const gchar *arg_name,
226 GIBaseInfo *interface_info,
230 gboolean copy_reference,
234 gboolean is_union = FALSE;
236 if (py_arg == Py_None) {
237 arg->v_pointer = NULL;
241 /* FIXME: handle this large if statement in the cache
242 * and set the correct marshaller
245 if (g_type_is_a (g_type, G_TYPE_CLOSURE)) {
246 return pygi_arg_gclosure_from_py_marshal (py_arg, arg, transfer);
247 } else if (g_type_is_a (g_type, G_TYPE_VALUE)) {
248 return pygi_arg_gvalue_from_py_marshal(py_arg,
252 } else if (is_foreign) {
254 success = pygi_struct_foreign_convert_to_g_argument (py_arg,
259 return (success == Py_None);
260 } else if (!PyObject_IsInstance (py_arg, py_type)) {
261 /* first check to see if this is a member of the expected union */
262 is_union = _is_union_member (interface_info, py_arg);
268 if (g_type_is_a (g_type, G_TYPE_BOXED)) {
269 /* Additionally use pyg_type_from_object to pull the stashed __gtype__
270 * attribute off of the input argument for type checking. This is needed
271 * to work around type discrepancies in cases with aliased (typedef) types.
272 * e.g. GtkAllocation, GdkRectangle.
273 * See: https://bugzilla.gnomethere are .org/show_bug.cgi?id=707140
275 if (is_union || pyg_boxed_check (py_arg, g_type) ||
276 g_type_is_a (pyg_type_from_object (py_arg), g_type)) {
277 arg->v_pointer = pyg_boxed_get (py_arg, void);
278 if (transfer == GI_TRANSFER_EVERYTHING) {
279 arg->v_pointer = g_boxed_copy (g_type, arg->v_pointer);
285 } else if (g_type_is_a (g_type, G_TYPE_POINTER) ||
286 g_type_is_a (g_type, G_TYPE_VARIANT) ||
287 g_type == G_TYPE_NONE) {
288 g_warn_if_fail (g_type_is_a (g_type, G_TYPE_VARIANT) || !is_pointer || transfer == GI_TRANSFER_NOTHING);
290 if (g_type_is_a (g_type, G_TYPE_VARIANT) &&
291 pyg_type_from_object (py_arg) != G_TYPE_VARIANT) {
292 PyErr_SetString (PyExc_TypeError, "expected GLib.Variant");
295 arg->v_pointer = pyg_pointer_get (py_arg, void);
296 if (transfer == GI_TRANSFER_EVERYTHING) {
297 g_variant_ref ((GVariant *)arg->v_pointer);
301 PyErr_Format (PyExc_NotImplementedError,
302 "structure type '%s' is not supported yet",
303 g_type_name(g_type));
310 gchar *type_name = _pygi_g_base_info_get_fullname (interface_info);
311 PyObject *module = PyObject_GetAttrString(py_arg, "__module__");
313 PyErr_Format (PyExc_TypeError, "argument %s: Expected %s, but got %s%s%s",
314 arg_name ? arg_name : "self",
316 module ? PYGLIB_PyUnicode_AsString(module) : "",
318 py_arg->ob_type->tp_name);
327 arg_struct_from_py_marshal_adapter (PyGIInvokeState *state,
328 PyGICallableCache *callable_cache,
329 PyGIArgCache *arg_cache,
332 gpointer *cleanup_data)
334 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
336 gboolean res = pygi_arg_struct_from_py_marshal (py_arg,
339 iface_cache->interface_info,
341 iface_cache->py_type,
343 TRUE, /*copy_reference*/
344 iface_cache->is_foreign,
345 arg_cache->is_pointer);
347 /* Assume struct marshaling is always a pointer and assign cleanup_data
348 * here rather than passing it further down the chain.
350 *cleanup_data = arg->v_pointer;
355 arg_foreign_from_py_cleanup (PyGIInvokeState *state,
356 PyGIArgCache *arg_cache,
359 gboolean was_processed)
361 if (state->failed && was_processed) {
362 pygi_struct_foreign_release (
363 ( (PyGIInterfaceCache *)arg_cache)->interface_info,
370 pygi_arg_struct_to_py_marshal (GIArgument *arg,
371 GIInterfaceInfo *interface_info,
375 gboolean is_allocated,
378 PyObject *py_obj = NULL;
380 if (arg->v_pointer == NULL) {
384 if (g_type_is_a (g_type, G_TYPE_VALUE)) {
385 py_obj = pyg_value_as_pyobject (arg->v_pointer, FALSE);
386 } else if (is_foreign) {
387 py_obj = pygi_struct_foreign_convert_from_g_argument (interface_info,
390 } else if (g_type_is_a (g_type, G_TYPE_BOXED)) {
392 /* Force a boxed copy if we are not transfered ownership and the
393 * memory is not caller allocated. */
394 py_obj = _pygi_boxed_new ((PyTypeObject *) py_type,
396 transfer == GI_TRANSFER_NOTHING && !is_allocated,
398 g_struct_info_get_size(interface_info) : 0);
400 } else if (g_type_is_a (g_type, G_TYPE_POINTER)) {
401 if (py_type == NULL ||
402 !PyType_IsSubtype ((PyTypeObject *) py_type, &PyGIStruct_Type)) {
403 g_warn_if_fail (transfer == GI_TRANSFER_NOTHING);
404 py_obj = pyg_pointer_new (g_type, arg->v_pointer);
406 py_obj = _pygi_struct_new ( (PyTypeObject *) py_type,
408 transfer == GI_TRANSFER_EVERYTHING);
410 } else if (g_type_is_a (g_type, G_TYPE_VARIANT)) {
411 /* Note: sink the variant (add a ref) only if we are not transfered ownership.
412 * GLib.Variant overrides __del__ which will then call "g_variant_unref" for
413 * cleanup in either case. */
415 if (transfer == GI_TRANSFER_NOTHING) {
416 g_variant_ref_sink (arg->v_pointer);
418 py_obj = _pygi_struct_new ((PyTypeObject *) py_type,
422 } else if (g_type == G_TYPE_NONE) {
424 py_obj = _pygi_struct_new ((PyTypeObject *) py_type,
426 transfer == GI_TRANSFER_EVERYTHING || is_allocated);
429 PyErr_Format (PyExc_NotImplementedError,
430 "structure type '%s' is not supported yet",
431 g_type_name (g_type));
438 arg_struct_to_py_marshal_adapter (PyGIInvokeState *state,
439 PyGICallableCache *callable_cache,
440 PyGIArgCache *arg_cache,
443 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
445 return pygi_arg_struct_to_py_marshal (arg,
446 iface_cache->interface_info,
448 iface_cache->py_type,
450 arg_cache->is_caller_allocates,
451 iface_cache->is_foreign);
455 arg_boxed_to_py_marshal_pass_by_ref (PyGIInvokeState *state,
456 PyGICallableCache *callable_cache,
457 PyGIArgCache *arg_cache,
460 PyObject *py_obj = NULL;
461 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
463 if (arg->v_pointer == NULL) {
467 if (g_type_is_a (iface_cache->g_type, G_TYPE_BOXED)) {
468 if (iface_cache->py_type) {
469 py_obj = _pygi_boxed_new ((PyTypeObject *) iface_cache->py_type,
471 FALSE, /* copy_boxed */
472 0); /* slice_alloc */
473 ((PyGBoxed *)py_obj)->free_on_dealloc = FALSE;
476 PyErr_Format (PyExc_NotImplementedError,
477 "expected boxed type but got %s",
478 g_type_name (iface_cache->g_type));
485 arg_foreign_to_py_cleanup (PyGIInvokeState *state,
486 PyGIArgCache *arg_cache,
489 gboolean was_processed)
491 if (!was_processed && arg_cache->transfer == GI_TRANSFER_EVERYTHING) {
492 pygi_struct_foreign_release (
493 ( (PyGIInterfaceCache *)arg_cache)->interface_info,
499 arg_type_class_from_py_marshal (PyGIInvokeState *state,
500 PyGICallableCache *callable_cache,
501 PyGIArgCache *arg_cache,
504 gpointer *cleanup_data)
506 GType gtype = pyg_type_from_object (py_arg);
508 if (G_TYPE_IS_CLASSED (gtype)) {
509 arg->v_pointer = g_type_class_ref (gtype);
510 *cleanup_data = arg->v_pointer;
513 PyErr_Format (PyExc_TypeError,
514 "Unable to retrieve a GObject type class from \"%s\".",
515 Py_TYPE(py_arg)->tp_name);
521 arg_type_class_from_py_cleanup (PyGIInvokeState *state,
522 PyGIArgCache *arg_cache,
525 gboolean was_processed)
528 g_type_class_unref (data);
533 arg_struct_from_py_setup (PyGIArgCache *arg_cache,
534 GIInterfaceInfo *iface_info,
537 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
539 if (g_struct_info_is_gtype_struct ((GIStructInfo*)iface_info)) {
540 arg_cache->from_py_marshaller = arg_type_class_from_py_marshal;
541 /* Since we always add a ref in the marshalling, only unref the
542 * GTypeClass when we don't transfer ownership. */
543 if (transfer == GI_TRANSFER_NOTHING) {
544 arg_cache->from_py_cleanup = arg_type_class_from_py_cleanup;
548 arg_cache->from_py_marshaller = arg_struct_from_py_marshal_adapter;
550 if (g_type_is_a (iface_cache->g_type, G_TYPE_CLOSURE)) {
551 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_cleanup = pygi_arg_gvalue_from_py_cleanup;
556 } else if (iface_cache->is_foreign) {
557 arg_cache->from_py_cleanup = arg_foreign_from_py_cleanup;
563 arg_struct_to_py_setup (PyGIArgCache *arg_cache,
564 GIInterfaceInfo *iface_info,
568 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
570 /* HACK to force GtkTreeModel:iter_next() and iter_previous() vfunc implementations
571 * to receive their Gtk.TreeIter argument as pass-by-reference. We create a new
572 * PyGIBoxed wrapper which does not copy the memory and also does not free it.
573 * This is needed to hack the noted vfunc implementations so they can continue
574 * working with bug https://bugzilla.gnome.org/show_bug.cgi?id=722899
575 * being fixed. This hack should be removed once GTK+ has fixed bug
576 * https://bugzilla.gnome.org/show_bug.cgi?id=734465
577 * and we've moved to a new major version.
579 if (arg_info && g_strcmp0 (iface_cache->type_name, "Gtk.TreeIter") == 0) {
582 GIBaseInfo *info = g_base_info_get_container (arg_info);
583 if (info && g_base_info_get_type (info) == GI_INFO_TYPE_CALLBACK &&
584 (g_strcmp0 (g_base_info_get_name (info), "iter_next") == 0 ||
585 g_strcmp0 (g_base_info_get_name (info), "iter_previous") == 0)) {
588 info = g_base_info_get_container (info);
589 if (info && g_base_info_get_type (info) == GI_INFO_TYPE_TYPE &&
590 g_type_info_get_tag ((GITypeInfo *)info) == GI_TYPE_TAG_INTERFACE) {
593 info = g_base_info_get_container (info);
594 if (info && g_base_info_get_type (info) == GI_INFO_TYPE_FIELD) {
597 info = g_base_info_get_container (info);
598 if (info && g_base_info_get_type (info) == GI_INFO_TYPE_STRUCT &&
599 g_strcmp0 (g_base_info_get_name (info), "TreeModelIface") == 0) {
600 arg_cache->to_py_marshaller = arg_boxed_to_py_marshal_pass_by_ref;
607 if (arg_cache->to_py_marshaller == NULL) {
608 arg_cache->to_py_marshaller = arg_struct_to_py_marshal_adapter;
611 if (iface_cache->is_foreign)
612 arg_cache->to_py_cleanup = arg_foreign_to_py_cleanup;
616 pygi_arg_struct_new_from_info (GITypeInfo *type_info,
619 PyGIDirection direction,
620 GIInterfaceInfo *iface_info)
622 PyGIArgCache *cache = NULL;
623 PyGIInterfaceCache *iface_cache;
625 cache = pygi_arg_interface_new_from_info (type_info,
633 iface_cache = (PyGIInterfaceCache *)cache;
634 iface_cache->is_foreign = (g_base_info_get_type ((GIBaseInfo *) iface_info) == GI_INFO_TYPE_STRUCT) &&
635 (g_struct_info_is_foreign ((GIStructInfo*) iface_info));
637 if (direction & PYGI_DIRECTION_FROM_PYTHON) {
638 arg_struct_from_py_setup (cache, iface_info, transfer);
641 if (direction & PYGI_DIRECTION_TO_PYTHON) {
642 arg_struct_to_py_setup (cache, iface_info, transfer, arg_info);