Debian Codegraph @

gi/pygi-boxed.c

raw
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
3 *
4 * Copyright (C) 2009 Simon van der Linden <[email protected]>
5 *
6 * pygi-boxed.c: wrapper to handle registered structures.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "pygi-type.h"
23
24 #include "pygboxed.h"
25 #include "pygi-basictype.h"
26 #include "pygi-boxed.h"
27 #include "pygi-info.h"
28 #include "pygi-util.h"
29
30 struct _PyGIBoxed {
31 PyGBoxed base;
32 gboolean slice_allocated;
33 gsize size;
34 };
35
36 static void
37 boxed_clear (PyGIBoxed *self)
38 {
39 gpointer boxed = pyg_boxed_get_ptr (self);
40 GType g_type = ((PyGBoxed *)self)->gtype;
41
42 if (((PyGBoxed *)self)->free_on_dealloc && boxed != NULL) {
43 if (self->slice_allocated) {
44 if (g_type && g_type_is_a (g_type, G_TYPE_VALUE))
45 g_value_unset (boxed);
46 g_slice_free1 (self->size, boxed);
47 self->slice_allocated = FALSE;
48 self->size = 0;
49 } else {
50 g_boxed_free (g_type, boxed);
51 }
52 }
53 pyg_boxed_set_ptr (self, NULL);
54 }
55
56 static PyObject *
57 boxed_clear_wrapper (PyGIBoxed *self)
58 {
59 boxed_clear (self);
60
61 Py_RETURN_NONE;
62 }
63
64
65 static void
66 boxed_dealloc (PyGIBoxed *self)
67 {
68 boxed_clear (self);
69
70 Py_TYPE (self)->tp_free ((PyObject *)self);
71 }
72
73 void *
74 pygi_boxed_alloc (GIBaseInfo *info, gsize *size_out)
75 {
76 gpointer boxed = NULL;
77 gsize size = 0;
78
79 if (GI_IS_UNION_INFO (info)) {
80 size = gi_union_info_get_size ((GIUnionInfo *)info);
81 } else if (GI_IS_STRUCT_INFO (info)) {
82 size = gi_struct_info_get_size ((GIStructInfo *)info);
83 } else {
84 PyErr_Format (PyExc_TypeError,
85 "info should be Boxed or Union, not '%d'",
86 g_type_name (G_TYPE_FROM_INSTANCE (info)));
87 return NULL;
88 }
89
90 if (size == 0) {
91 PyErr_Format (PyExc_TypeError,
92 "boxed cannot be created directly; try using a "
93 "constructor, see: help(%s.%s)",
94 gi_base_info_get_namespace (info),
95 gi_base_info_get_name (info));
96 return NULL;
97 }
98
99 if (size_out != NULL) *size_out = size;
100
101 boxed = g_slice_alloc0 (size);
102 if (boxed == NULL) PyErr_NoMemory ();
103 return boxed;
104 }
105
106 static PyObject *
107 boxed_new (PyTypeObject *type, PyObject *args, PyObject *kwargs)
108 {
109 GIBaseInfo *info;
110 gsize size = 0;
111 gpointer boxed;
112 PyGIBoxed *self = NULL;
113
114 info = _pygi_object_get_gi_info ((PyObject *)type, &PyGIBaseInfo_Type);
115 if (info == NULL) {
116 if (PyErr_ExceptionMatches (PyExc_AttributeError)) {
117 PyErr_Format (PyExc_TypeError,
118 "missing introspection information");
119 }
120 return NULL;
121 }
122
123 boxed = pygi_boxed_alloc (info, &size);
124 if (boxed == NULL) {
125 goto out;
126 }
127
128 self = (PyGIBoxed *)pygi_boxed_new (type, boxed, TRUE, size);
129 if (self == NULL) {
130 g_slice_free1 (size, boxed);
131 goto out;
132 }
133
134 self->size = size;
135 self->slice_allocated = TRUE;
136
137 out:
138 gi_base_info_unref (info);
139
140 return (PyObject *)self;
141 }
142
143 static int
144 boxed_init (PyObject *self, PyObject *args, PyObject *kwargs)
145 {
146 static char *kwlist[] = { NULL };
147
148 if (!PyArg_ParseTupleAndKeywords (args, kwargs, "", kwlist)) {
149 PyErr_Clear ();
150 PyErr_Warn (
151 PyExc_DeprecationWarning,
152 "Passing arguments to gi.types.Boxed.__init__() is deprecated. "
153 "All arguments passed will be ignored.");
154 }
155
156 /* Don't call PyGBoxed's init, which raises an exception. */
157 return 0;
158 }
159
160 PYGI_DEFINE_TYPE ("gi.Boxed", PyGIBoxed_Type, PyGIBoxed);
161
162 PyObject *
163 pygi_boxed_new (PyTypeObject *type, gpointer boxed, gboolean free_on_dealloc,
164 gsize allocated_slice)
165 {
166 PyGIBoxed *self;
167
168 if (!boxed) {
169 Py_RETURN_NONE;
170 }
171
172 if (!PyType_IsSubtype (type, &PyGIBoxed_Type)) {
173 PyErr_SetString (PyExc_TypeError, "must be a subtype of gi.Boxed");
174 return NULL;
175 }
176
177 self = (PyGIBoxed *)type->tp_alloc (type, 0);
178 if (self == NULL) {
179 return NULL;
180 }
181
182 ((PyGBoxed *)self)->gtype = pyg_type_from_object ((PyObject *)type);
183 ((PyGBoxed *)self)->free_on_dealloc = free_on_dealloc;
184 pyg_boxed_set_ptr (self, boxed);
185 if (allocated_slice > 0) {
186 self->size = allocated_slice;
187 self->slice_allocated = TRUE;
188 } else {
189 self->size = 0;
190 self->slice_allocated = FALSE;
191 }
192
193 return (PyObject *)self;
194 }
195
196 /**
197 * pygi_boxed_copy_in_place:
198 *
199 * Replace the boxed pointer held by this wrapper with a boxed copy
200 * freeing the previously held pointer (when free_on_dealloc is TRUE).
201 * This can be used in cases where Python is passed a reference which
202 * it does not own and the wrapper is held by the Python program
203 * longer than the duration of a callback it was passed to.
204 */
205 void
206 pygi_boxed_copy_in_place (PyGIBoxed *self)
207 {
208 PyGBoxed *pygboxed = (PyGBoxed *)self;
209 gpointer ptr = pyg_boxed_get_ptr (self);
210 gpointer copy = NULL;
211
212 if (ptr) copy = g_boxed_copy (pygboxed->gtype, ptr);
213
214 boxed_clear (self);
215 pyg_boxed_set_ptr (pygboxed, copy);
216 pygboxed->free_on_dealloc = TRUE;
217 }
218
219 static PyMethodDef boxed_methods[] = {
220 { "_clear_boxed", (PyCFunction)boxed_clear_wrapper, METH_NOARGS },
221 { NULL, NULL, 0 },
222 };
223
224 /**
225 * Returns 0 on success, or -1 and sets an exception.
226 */
227 int
228 pygi_boxed_register_types (PyObject *m)
229 {
230 Py_SET_TYPE (&PyGIBoxed_Type, &PyType_Type);
231 g_assert (Py_TYPE (&PyGBoxed_Type) != NULL);
232 PyGIBoxed_Type.tp_base = &PyGBoxed_Type;
233 PyGIBoxed_Type.tp_new = (newfunc)boxed_new;
234 PyGIBoxed_Type.tp_init = (initproc)boxed_init;
235 PyGIBoxed_Type.tp_dealloc = (destructor)boxed_dealloc;
236 PyGIBoxed_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
237 PyGIBoxed_Type.tp_methods = boxed_methods;
238
239 if (PyType_Ready (&PyGIBoxed_Type) < 0) return -1;
240 Py_INCREF ((PyObject *)&PyGIBoxed_Type);
241 if (PyModule_AddObject (m, "Boxed", (PyObject *)&PyGIBoxed_Type) < 0) {
242 Py_DECREF ((PyObject *)&PyGIBoxed_Type);
243 return -1;
244 }
245
246 return 0;
247 }