Add gitlab CI tests
[pygobject.git] / gi / pygi-struct.c
blob4d5b5411f94ecbaab0b200947b7356214099e97d
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 "pygtype.h"
27 #include "pygpointer.h"
29 #include <girepository.h>
30 #include <pyglib-python-compat.h>
33 static GIBaseInfo *
34 _struct_get_info (PyObject *self)
36 PyObject *py_info;
37 GIBaseInfo *info = NULL;
39 py_info = PyObject_GetAttrString (self, "__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 = _struct_get_info ( (PyObject *) self );
66 if (info != NULL && g_struct_info_is_foreign ( (GIStructInfo *) info)) {
67 pygi_struct_foreign_release (info, pyg_pointer_get_ptr (self));
68 } else if (self->free_on_dealloc) {
69 g_free (pyg_pointer_get_ptr (self));
72 if (info != NULL) {
73 g_base_info_unref (info);
76 Py_TYPE (self)->tp_free ((PyObject *)self);
79 static PyObject *
80 _struct_new (PyTypeObject *type,
81 PyObject *args,
82 PyObject *kwargs)
84 static char *kwlist[] = { NULL };
86 GIBaseInfo *info;
87 gsize size;
88 gpointer pointer;
89 PyObject *self = NULL;
91 if (!PyArg_ParseTupleAndKeywords (args, kwargs, "", kwlist)) {
92 return NULL;
95 info = _struct_get_info ( (PyObject *) type );
96 if (info == NULL) {
97 if (PyErr_ExceptionMatches (PyExc_AttributeError)) {
98 PyErr_Format (PyExc_TypeError, "missing introspection information");
100 return NULL;
103 size = g_struct_info_get_size ( (GIStructInfo *) info);
104 if (size == 0) {
105 PyErr_Format (PyExc_TypeError,
106 "struct cannot be created directly; try using a constructor, see: help(%s.%s)",
107 g_base_info_get_namespace (info),
108 g_base_info_get_name (info));
109 goto out;
111 pointer = g_try_malloc0 (size);
112 if (pointer == NULL) {
113 PyErr_NoMemory();
114 goto out;
117 self = _pygi_struct_new (type, pointer, TRUE);
118 if (self == NULL) {
119 g_free (pointer);
122 out:
123 g_base_info_unref (info);
125 return (PyObject *) self;
128 static int
129 _struct_init (PyObject *self,
130 PyObject *args,
131 PyObject *kwargs)
133 /* Don't call PyGPointer's init, which raises an exception. */
134 return 0;
137 PYGLIB_DEFINE_TYPE("gi.Struct", PyGIStruct_Type, PyGIStruct);
140 PyObject *
141 _pygi_struct_new_from_g_type (GType g_type,
142 gpointer pointer,
143 gboolean free_on_dealloc)
145 PyGIStruct *self;
146 PyTypeObject *type;
148 type = (PyTypeObject *)pygi_type_import_by_g_type (g_type);
150 if (!type)
151 type = (PyTypeObject *)&PyGIStruct_Type; /* fallback */
153 if (!PyType_IsSubtype (type, &PyGIStruct_Type)) {
154 PyErr_SetString (PyExc_TypeError, "must be a subtype of gi.Struct");
155 return NULL;
158 self = (PyGIStruct *) type->tp_alloc (type, 0);
159 if (self == NULL) {
160 return NULL;
163 pyg_pointer_set_ptr (self, pointer);
164 ( (PyGPointer *) self)->gtype = g_type;
165 self->free_on_dealloc = free_on_dealloc;
167 return (PyObject *) self;
171 PyObject *
172 _pygi_struct_new (PyTypeObject *type,
173 gpointer pointer,
174 gboolean free_on_dealloc)
176 PyGIStruct *self;
177 GType g_type;
179 if (!PyType_IsSubtype (type, &PyGIStruct_Type)) {
180 PyErr_SetString (PyExc_TypeError, "must be a subtype of gi.Struct");
181 return NULL;
184 self = (PyGIStruct *) type->tp_alloc (type, 0);
185 if (self == NULL) {
186 return NULL;
189 g_type = pyg_type_from_object ( (PyObject *) type);
191 pyg_pointer_set_ptr (self, pointer);
192 ( (PyGPointer *) self)->gtype = g_type;
193 self->free_on_dealloc = free_on_dealloc;
195 return (PyObject *) self;
198 static PyObject *
199 _struct_repr(PyGIStruct *self)
201 PyObject* repr;
202 GIBaseInfo *info;
203 PyGPointer *pointer = (PyGPointer *)self;
205 info = _struct_get_info ((PyObject *)self);
206 if (info == NULL)
207 return NULL;
209 repr = PYGLIB_PyUnicode_FromFormat ("<%s.%s object at %p (%s at %p)>",
210 g_base_info_get_namespace (info),
211 g_base_info_get_name (info),
212 self, g_type_name (pointer->gtype),
213 pointer->pointer);
215 g_base_info_unref (info);
217 return repr;
220 void
221 _pygi_struct_register_types (PyObject *m)
223 Py_TYPE(&PyGIStruct_Type) = &PyType_Type;
224 PyGIStruct_Type.tp_base = &PyGPointer_Type;
225 PyGIStruct_Type.tp_new = (newfunc) _struct_new;
226 PyGIStruct_Type.tp_init = (initproc) _struct_init;
227 PyGIStruct_Type.tp_dealloc = (destructor) _struct_dealloc;
228 PyGIStruct_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
229 PyGIStruct_Type.tp_repr = (reprfunc)_struct_repr;
231 if (PyType_Ready (&PyGIStruct_Type))
232 return;
233 if (PyModule_AddObject (m, "Struct", (PyObject *) &PyGIStruct_Type))
234 return;