Debian Codegraph @

gi/pygi-cache-error.c

raw
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
3 *
4 * Copyright (C) 1998-2003 James Henstridge
5 * 2004-2008 Johan Dahlin
6 * Copyright (C) 2011 John (J5) Palmieri <[email protected]>
7 * Copyright (C) 2014 Simon Feltman <[email protected]>
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-error.h"
24 #include "pygi-cache-private.h"
25
26 static gboolean
27 _pygi_marshal_from_py_gerror (PyGIInvokeState *state,
28 PyGICallableCache *callable_cache,
29 PyGIArgCache *arg_cache, PyObject *py_arg,
30 GIArgument *arg, gpointer *cleanup_data)
31 {
32 GError *error = NULL;
33 if (Py_IsNone (py_arg)) {
34 arg->v_pointer = NULL;
35 *cleanup_data = NULL;
36 return TRUE;
37 } else if (pygi_error_marshal_from_py (py_arg, &error)) {
38 arg->v_pointer = error;
39 *cleanup_data = error;
40 return TRUE;
41 } else {
42 return FALSE;
43 }
44 }
45
46
47 static void
48 _pygi_marshal_from_py_gerror_cleanup (PyGIInvokeState *state,
49 PyGIArgCache *arg_cache,
50 PyObject *py_arg, gpointer data,
51 gboolean was_processed)
52 {
53 if (was_processed) {
54 g_error_free ((GError *)data);
55 }
56 }
57
58 static PyObject *
59 _pygi_marshal_to_py_gerror (PyGIInvokeState *state,
60 PyGICallableCache *callable_cache,
61 PyGIArgCache *arg_cache, GIArgument *arg,
62 gpointer *cleanup_data)
63 {
64 GError *error = arg->v_pointer;
65 PyObject *py_obj = NULL;
66
67 py_obj = pygi_error_marshal_to_py (&error);
68
69 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING && error != NULL) {
70 g_error_free (error);
71 }
72
73 return py_obj;
74 }
75
76 PyGIArgCache *
77 pygi_arg_gerror_new_from_info (GITypeInfo *type_info, GIArgInfo *arg_info,
78 GITransfer transfer, PyGIDirection direction)
79 {
80 PyGIArgCache *arg_cache;
81
82 arg_cache = pygi_arg_cache_alloc ();
83
84 pygi_arg_base_setup (arg_cache, type_info, arg_info, transfer, direction);
85
86 if (direction & PYGI_DIRECTION_FROM_PYTHON) {
87 arg_cache->from_py_marshaller = _pygi_marshal_from_py_gerror;
88
89 /* Assign cleanup function if we manage memory after call completion. */
90 if (arg_cache->transfer == GI_TRANSFER_NOTHING) {
91 arg_cache->from_py_cleanup = _pygi_marshal_from_py_gerror_cleanup;
92 }
93 }
94
95 if (direction & PYGI_DIRECTION_TO_PYTHON) {
96 arg_cache->to_py_marshaller = _pygi_marshal_to_py_gerror;
97 arg_cache->meta_type = PYGI_META_ARG_TYPE_PARENT;
98 }
99
100 return arg_cache;
101 }