tests: don't test for specific device labels
[pygobject.git] / gi / pygi-struct.c
blob4c05b7c4b9b018dbda756c04df9eabcb88bb4a44
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
4 * Copyright (C) 2009 Simon van der Linden <svdlinden@src.gnome.org>
6 * pygi-struct.c: wrapper to handle non-registered structures.
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.
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.
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/>.
22 #include "pygi-struct.h"
23 #include "pygi-foreign.h"
24 #include "pygi-info.h"
25 #include "pygi-type.h"
26 #include "pygi-type.h"
27 #include "pygpointer.h"
28 #include "pygi-python-compat.h"
30 #include <girepository.h>
33 static GIBaseInfo *
34 struct_get_info (PyTypeObject *type)
36 PyObject *py_info;
37 GIBaseInfo *info = NULL;
39 py_info = PyObject_GetAttrString ((PyObject *)type, "__info__");
40 if (py_info == NULL) {
41 return NULL;
43 if (!PyObject_TypeCheck (py_info, &PyGIStructInfo_Type) &&
44 !PyObject_TypeCheck (py_info, &PyGIUnionInfo_Type)) {
45 PyErr_Format (PyExc_TypeError, "attribute '__info__' must be %s or %s, not %s",
46 PyGIStructInfo_Type.tp_name,
47 PyGIUnionInfo_Type.tp_name,
48 Py_TYPE(py_info)->tp_name);
49 goto out;
52 info = ( (PyGIBaseInfo *) py_info)->info;
53 g_base_info_ref (info);
55 out:
56 Py_DECREF (py_info);
58 return info;
61 static void
62 struct_dealloc (PyGIStruct *self)
64 GIBaseInfo *info;
65 PyObject *error_type, *error_value, *error_traceback;
66 gboolean have_error = !!PyErr_Occurred ();
68 if (have_error)
69 PyErr_Fetch (&error_type, &error_value, &error_traceback);
71 info = struct_get_info (Py_TYPE (self));
73 if (info != NULL && g_struct_info_is_foreign ( (GIStructInfo *) info)) {
74 pygi_struct_foreign_release (info, pyg_pointer_get_ptr (self));
75 } else if (self->free_on_dealloc) {
76 g_free (pyg_pointer_get_ptr (self));
79 if (info != NULL) {
80 g_base_info_unref (info);
83 if (have_error)
84 PyErr_Restore (error_type, error_value, error_traceback);
86 Py_TYPE (self)->tp_free ((PyObject *)self);
89 static PyObject *
90 struct_new (PyTypeObject *type,
91 PyObject *args,
92 PyObject *kwargs)
94 static char *kwlist[] = { NULL };
96 GIBaseInfo *info;
97 gsize size;
98 gpointer pointer;
99 PyObject *self = NULL;
101 if (!PyArg_ParseTupleAndKeywords (args, kwargs, "", kwlist)) {
102 return NULL;
105 info = struct_get_info ( type );
106 if (info == NULL) {
107 if (PyErr_ExceptionMatches (PyExc_AttributeError)) {
108 PyErr_Format (PyExc_TypeError, "missing introspection information");
110 return NULL;
113 size = g_struct_info_get_size ( (GIStructInfo *) info);
114 if (size == 0) {
115 PyErr_Format (PyExc_TypeError,
116 "struct cannot be created directly; try using a constructor, see: help(%s.%s)",
117 g_base_info_get_namespace (info),
118 g_base_info_get_name (info));
119 goto out;
121 pointer = g_try_malloc0 (size);
122 if (pointer == NULL) {
123 PyErr_NoMemory();
124 goto out;
127 self = pygi_struct_new (type, pointer, TRUE);
128 if (self == NULL) {
129 g_free (pointer);
132 out:
133 g_base_info_unref (info);
135 return (PyObject *) self;
138 static int
139 struct_init (PyObject *self,
140 PyObject *args,
141 PyObject *kwargs)
143 /* Don't call PyGPointer's init, which raises an exception. */
144 return 0;
147 PYGLIB_DEFINE_TYPE("gi.Struct", PyGIStruct_Type, PyGIStruct);
150 PyObject *
151 pygi_struct_new_from_g_type (GType g_type,
152 gpointer pointer,
153 gboolean free_on_dealloc)
155 PyGIStruct *self;
156 PyTypeObject *type;
158 type = (PyTypeObject *)pygi_type_import_by_g_type (g_type);
160 if (!type)
161 type = (PyTypeObject *)&PyGIStruct_Type; /* fallback */
163 if (!PyType_IsSubtype (type, &PyGIStruct_Type)) {
164 PyErr_SetString (PyExc_TypeError, "must be a subtype of gi.Struct");
165 return NULL;
168 self = (PyGIStruct *) type->tp_alloc (type, 0);
169 if (self == NULL) {
170 return NULL;
173 pyg_pointer_set_ptr (self, pointer);
174 ( (PyGPointer *) self)->gtype = g_type;
175 self->free_on_dealloc = free_on_dealloc;
177 return (PyObject *) self;
181 PyObject *
182 pygi_struct_new (PyTypeObject *type,
183 gpointer pointer,
184 gboolean free_on_dealloc)
186 PyGIStruct *self;
187 GType g_type;
189 if (!PyType_IsSubtype (type, &PyGIStruct_Type)) {
190 PyErr_SetString (PyExc_TypeError, "must be a subtype of gi.Struct");
191 return NULL;
194 self = (PyGIStruct *) type->tp_alloc (type, 0);
195 if (self == NULL) {
196 return NULL;
199 g_type = pyg_type_from_object ( (PyObject *) type);
201 pyg_pointer_set_ptr (self, pointer);
202 ( (PyGPointer *) self)->gtype = g_type;
203 self->free_on_dealloc = free_on_dealloc;
205 return (PyObject *) self;
208 static PyObject *
209 struct_repr(PyGIStruct *self)
211 PyObject* repr;
212 GIBaseInfo *info;
213 PyGPointer *pointer = (PyGPointer *)self;
215 info = struct_get_info (Py_TYPE (self));
216 if (info == NULL)
217 return NULL;
219 repr = PYGLIB_PyUnicode_FromFormat ("<%s.%s object at %p (%s at %p)>",
220 g_base_info_get_namespace (info),
221 g_base_info_get_name (info),
222 self, g_type_name (pointer->gtype),
223 pointer->pointer);
225 g_base_info_unref (info);
227 return repr;
231 * Returns 0 on success, or -1 and sets an exception.
234 pygi_struct_register_types (PyObject *m)
236 Py_TYPE(&PyGIStruct_Type) = &PyType_Type;
237 g_assert (Py_TYPE (&PyGPointer_Type) != NULL);
238 PyGIStruct_Type.tp_base = &PyGPointer_Type;
239 PyGIStruct_Type.tp_new = (newfunc) struct_new;
240 PyGIStruct_Type.tp_init = (initproc) struct_init;
241 PyGIStruct_Type.tp_dealloc = (destructor) struct_dealloc;
242 PyGIStruct_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
243 PyGIStruct_Type.tp_repr = (reprfunc)struct_repr;
245 if (PyType_Ready (&PyGIStruct_Type) < 0)
246 return -1;
247 Py_INCREF ((PyObject *) &PyGIStruct_Type);
248 if (PyModule_AddObject (m, "Struct", (PyObject *) &PyGIStruct_Type) < 0) {
249 Py_DECREF ((PyObject *) &PyGIStruct_Type);
250 return -1;
253 return 0;