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"
27 #include "pygpointer.h"
29 #include <girepository.h>
30 #include <pyglib-python-compat.h>
34 _struct_get_info (PyObject *self)
37 GIBaseInfo *info = NULL;
39 py_info = PyObject_GetAttrString (self, "__info__");
40 if (py_info == 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);
52 info = ( (PyGIBaseInfo *) py_info)->info;
53 g_base_info_ref (info);
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));
73 g_base_info_unref (info);
76 Py_TYPE (self)->tp_free ((PyObject *)self);
80 _struct_new (PyTypeObject *type,
84 static char *kwlist[] = { NULL };
89 PyObject *self = NULL;
91 if (!PyArg_ParseTupleAndKeywords (args, kwargs, "", kwlist)) {
95 info = _struct_get_info ( (PyObject *) type );
97 if (PyErr_ExceptionMatches (PyExc_AttributeError)) {
98 PyErr_Format (PyExc_TypeError, "missing introspection information");
103 size = g_struct_info_get_size ( (GIStructInfo *) info);
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));
111 pointer = g_try_malloc0 (size);
112 if (pointer == NULL) {
117 self = _pygi_struct_new (type, pointer, TRUE);
123 g_base_info_unref (info);
125 return (PyObject *) self;
129 _struct_init (PyObject *self,
133 /* Don't call PyGPointer's init, which raises an exception. */
137 PYGLIB_DEFINE_TYPE("gi.Struct", PyGIStruct_Type, PyGIStruct);
141 _pygi_struct_new_from_g_type (GType g_type,
143 gboolean free_on_dealloc)
148 type = (PyTypeObject *)pygi_type_import_by_g_type (g_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");
158 self = (PyGIStruct *) type->tp_alloc (type, 0);
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;
172 _pygi_struct_new (PyTypeObject *type,
174 gboolean free_on_dealloc)
179 if (!PyType_IsSubtype (type, &PyGIStruct_Type)) {
180 PyErr_SetString (PyExc_TypeError, "must be a subtype of gi.Struct");
184 self = (PyGIStruct *) type->tp_alloc (type, 0);
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;
199 _struct_repr(PyGIStruct *self)
203 PyGPointer *pointer = (PyGPointer *)self;
205 info = _struct_get_info ((PyObject *)self);
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),
215 g_base_info_unref (info);
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))
233 if (PyModule_AddObject (m, "Struct", (PyObject *) &PyGIStruct_Type))