Debian Codegraph @

gi/pygi-struct.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-struct.c: wrapper to handle non-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-struct.h"
23
24 #include "pygi-foreign.h"
25 #include "pygi-info.h"
26 #include "pygi-type.h"
27 #include "pygi-util.h"
28 #include "pygpointer.h"
29
30
31 static GIBaseInfo *
32 struct_get_info (PyTypeObject *type)
33 {
34 PyObject *py_info;
35 GIBaseInfo *info = NULL;
36
37 py_info = PyObject_GetAttrString ((PyObject *)type, "__info__");
38 if (py_info == NULL) {
39 return NULL;
40 }
41 if (!PyObject_TypeCheck (py_info, &PyGIStructInfo_Type)
42 && !PyObject_TypeCheck (py_info, &PyGIUnionInfo_Type)) {
43 PyErr_Format (PyExc_TypeError,
44 "attribute '__info__' must be %s or %s, not %s",
45 PyGIStructInfo_Type.tp_name, PyGIUnionInfo_Type.tp_name,
46 Py_TYPE (py_info)->tp_name);
47 goto out;
48 }
49
50 info = ((PyGIBaseInfo *)py_info)->info;
51 gi_base_info_ref (info);
52
53 out:
54 Py_DECREF (py_info);
55
56 return info;
57 }
58
59 static void
60 struct_dealloc (PyGIStruct *self)
61 {
62 GIBaseInfo *info;
63 PyObject *error_type, *error_value, *error_traceback;
64 gboolean have_error = !!PyErr_Occurred ();
65
66 if (have_error) PyErr_Fetch (&error_type, &error_value, &error_traceback);
67
68 info = struct_get_info (Py_TYPE (self));
69
70 if (info != NULL && gi_struct_info_is_foreign ((GIStructInfo *)info)) {
71 pygi_struct_foreign_release (info, pyg_pointer_get_ptr (self));
72 } else if (self->free_on_dealloc) {
73 g_free (pyg_pointer_get_ptr (self));
74 }
75
76 if (info != NULL) {
77 gi_base_info_unref (info);
78 }
79
80 if (have_error) PyErr_Restore (error_type, error_value, error_traceback);
81
82 Py_TYPE (self)->tp_free ((PyObject *)self);
83 }
84
85 static PyObject *
86 struct_new (PyTypeObject *type, PyObject *args, PyObject *kwargs)
87 {
88 static char *kwlist[] = { NULL };
89
90 GIBaseInfo *info;
91 gsize size;
92 gpointer pointer;
93 PyObject *self = NULL;
94
95 if (!PyArg_ParseTupleAndKeywords (args, kwargs, "", kwlist)) {
96 return NULL;
97 }
98
99 info = struct_get_info (type);
100 if (info == NULL) {
101 if (PyErr_ExceptionMatches (PyExc_AttributeError)) {
102 PyErr_Format (PyExc_TypeError,
103 "missing introspection information");
104 }
105 return NULL;
106 }
107
108 size = gi_struct_info_get_size ((GIStructInfo *)info);
109 if (size == 0) {
110 PyErr_Format (PyExc_TypeError,
111 "struct cannot be created directly; try using a "
112 "constructor, see: help(%s.%s)",
113 gi_base_info_get_namespace (info),
114 gi_base_info_get_name (info));
115 goto out;
116 }
117 pointer = g_try_malloc0 (size);
118 if (pointer == NULL) {
119 PyErr_NoMemory ();
120 goto out;
121 }
122
123 self = pygi_struct_new (type, pointer, TRUE);
124 if (self == NULL) {
125 g_free (pointer);
126 }
127
128 out:
129 gi_base_info_unref (info);
130
131 return (PyObject *)self;
132 }
133
134 static int
135 struct_init (PyObject *self, PyObject *args, PyObject *kwargs)
136 {
137 /* Don't call PyGPointer's init, which raises an exception. */
138 return 0;
139 }
140
141 PYGI_DEFINE_TYPE ("gi.Struct", PyGIStruct_Type, PyGIStruct);
142
143
144 PyObject *
145 pygi_struct_new_from_g_type (GType g_type, gpointer pointer,
146 gboolean free_on_dealloc)
147 {
148 PyGIStruct *self;
149 PyTypeObject *type;
150
151 type = (PyTypeObject *)pygi_type_import_by_g_type (g_type);
152
153 if (!type) type = (PyTypeObject *)&PyGIStruct_Type; /* fallback */
154
155 if (!PyType_IsSubtype (type, &PyGIStruct_Type)) {
156 PyErr_SetString (PyExc_TypeError, "must be a subtype of gi.Struct");
157 return NULL;
158 }
159
160 self = (PyGIStruct *)type->tp_alloc (type, 0);
161 if (self == NULL) {
162 return NULL;
163 }
164
165 pyg_pointer_set_ptr (self, pointer);
166 ((PyGPointer *)self)->gtype = g_type;
167 self->free_on_dealloc = free_on_dealloc;
168
169 return (PyObject *)self;
170 }
171
172
173 PyObject *
174 pygi_struct_new (PyTypeObject *type, gpointer pointer,
175 gboolean free_on_dealloc)
176 {
177 PyGIStruct *self;
178 GType g_type;
179
180 if (!PyType_IsSubtype (type, &PyGIStruct_Type)) {
181 PyErr_SetString (PyExc_TypeError, "must be a subtype of gi.Struct");
182 return NULL;
183 }
184
185 self = (PyGIStruct *)type->tp_alloc (type, 0);
186 if (self == NULL) {
187 return NULL;
188 }
189
190 g_type = pyg_type_from_object ((PyObject *)type);
191
192 pyg_pointer_set_ptr (self, pointer);
193 ((PyGPointer *)self)->gtype = g_type;
194 self->free_on_dealloc = free_on_dealloc;
195
196 return (PyObject *)self;
197 }
198
199 static PyObject *
200 struct_repr (PyGIStruct *self)
201 {
202 PyObject *repr;
203 GIBaseInfo *info;
204 PyGPointer *pointer = (PyGPointer *)self;
205
206 info = struct_get_info (Py_TYPE (self));
207 if (info == NULL) return NULL;
208
209 repr = PyUnicode_FromFormat (
210 "<%s.%s object at %p (%s at %p)>", gi_base_info_get_namespace (info),
211 gi_base_info_get_name (info), self, g_type_name (pointer->gtype),
212 pointer->pointer);
213
214 gi_base_info_unref (info);
215
216 return repr;
217 }
218
219 /**
220 * Returns 0 on success, or -1 and sets an exception.
221 */
222 int
223 pygi_struct_register_types (PyObject *m)
224 {
225 Py_SET_TYPE (&PyGIStruct_Type, &PyType_Type);
226 g_assert (Py_TYPE (&PyGPointer_Type) != NULL);
227 PyGIStruct_Type.tp_base = &PyGPointer_Type;
228 PyGIStruct_Type.tp_new = (newfunc)struct_new;
229 PyGIStruct_Type.tp_init = (initproc)struct_init;
230 PyGIStruct_Type.tp_dealloc = (destructor)struct_dealloc;
231 PyGIStruct_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
232 PyGIStruct_Type.tp_repr = (reprfunc)struct_repr;
233
234 if (PyType_Ready (&PyGIStruct_Type) < 0) return -1;
235 Py_INCREF ((PyObject *)&PyGIStruct_Type);
236 if (PyModule_AddObject (m, "Struct", (PyObject *)&PyGIStruct_Type) < 0) {
237 Py_DECREF ((PyObject *)&PyGIStruct_Type);
238 return -1;
239 }
240
241 return 0;
242 }