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-private.h"
23 #include "pygobject-private.h"
25 #include <girepository.h>
26 #include <pyglib-python-compat.h>
30 _struct_get_info (PyObject *self)
33 GIBaseInfo *info = NULL;
35 py_info = PyObject_GetAttrString (self, "__info__");
36 if (py_info == NULL) {
39 if (!PyObject_TypeCheck (py_info, &PyGIStructInfo_Type) &&
40 !PyObject_TypeCheck (py_info, &PyGIUnionInfo_Type)) {
41 PyErr_Format (PyExc_TypeError, "attribute '__info__' must be %s or %s, not %s",
42 PyGIStructInfo_Type.tp_name,
43 PyGIUnionInfo_Type.tp_name,
44 Py_TYPE(py_info)->tp_name);
48 info = ( (PyGIBaseInfo *) py_info)->info;
49 g_base_info_ref (info);
58 _struct_dealloc (PyGIStruct *self)
60 GIBaseInfo *info = _struct_get_info ( (PyObject *) self );
62 if (info != NULL && g_struct_info_is_foreign ( (GIStructInfo *) info)) {
63 pygi_struct_foreign_release (info, pyg_pointer_get_ptr (self));
64 } else if (self->free_on_dealloc) {
65 g_free (pyg_pointer_get_ptr (self));
69 g_base_info_unref (info);
72 Py_TYPE (self)->tp_free ((PyObject *)self);
76 _struct_new (PyTypeObject *type,
80 static char *kwlist[] = { NULL };
85 PyObject *self = NULL;
87 if (!PyArg_ParseTupleAndKeywords (args, kwargs, "", kwlist)) {
91 info = _struct_get_info ( (PyObject *) type );
93 if (PyErr_ExceptionMatches (PyExc_AttributeError)) {
94 PyErr_Format (PyExc_TypeError, "missing introspection information");
99 size = g_struct_info_get_size ( (GIStructInfo *) info);
101 PyErr_Format (PyExc_TypeError,
102 "struct cannot be created directly; try using a constructor, see: help(%s.%s)",
103 g_base_info_get_namespace (info),
104 g_base_info_get_name (info));
107 pointer = g_try_malloc0 (size);
108 if (pointer == NULL) {
113 self = _pygi_struct_new (type, pointer, TRUE);
119 g_base_info_unref (info);
121 return (PyObject *) self;
125 _struct_init (PyObject *self,
129 /* Don't call PyGPointer's init, which raises an exception. */
133 PYGLIB_DEFINE_TYPE("gi.Struct", PyGIStruct_Type, PyGIStruct);
137 _pygi_struct_new_from_g_type (GType g_type,
139 gboolean free_on_dealloc)
144 type = (PyTypeObject *)pygi_type_import_by_g_type (g_type);
147 type = (PyTypeObject *)&PyGIStruct_Type; /* fallback */
149 if (!PyType_IsSubtype (type, &PyGIStruct_Type)) {
150 PyErr_SetString (PyExc_TypeError, "must be a subtype of gi.Struct");
154 self = (PyGIStruct *) type->tp_alloc (type, 0);
159 pyg_pointer_set_ptr (self, pointer);
160 ( (PyGPointer *) self)->gtype = g_type;
161 self->free_on_dealloc = free_on_dealloc;
163 return (PyObject *) self;
168 _pygi_struct_new (PyTypeObject *type,
170 gboolean free_on_dealloc)
175 if (!PyType_IsSubtype (type, &PyGIStruct_Type)) {
176 PyErr_SetString (PyExc_TypeError, "must be a subtype of gi.Struct");
180 self = (PyGIStruct *) type->tp_alloc (type, 0);
185 g_type = pyg_type_from_object ( (PyObject *) type);
187 pyg_pointer_set_ptr (self, pointer);
188 ( (PyGPointer *) self)->gtype = g_type;
189 self->free_on_dealloc = free_on_dealloc;
191 return (PyObject *) self;
195 _struct_repr(PyGIStruct *self)
199 PyGPointer *pointer = (PyGPointer *)self;
201 info = _struct_get_info ((PyObject *)self);
205 repr = PYGLIB_PyUnicode_FromFormat ("<%s.%s object at %p (%s at %p)>",
206 g_base_info_get_namespace (info),
207 g_base_info_get_name (info),
208 self, g_type_name (pointer->gtype),
211 g_base_info_unref (info);
217 _pygi_struct_register_types (PyObject *m)
219 Py_TYPE(&PyGIStruct_Type) = &PyType_Type;
220 PyGIStruct_Type.tp_base = &PyGPointer_Type;
221 PyGIStruct_Type.tp_new = (newfunc) _struct_new;
222 PyGIStruct_Type.tp_init = (initproc) _struct_init;
223 PyGIStruct_Type.tp_dealloc = (destructor) _struct_dealloc;
224 PyGIStruct_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
225 PyGIStruct_Type.tp_repr = (reprfunc)_struct_repr;
227 if (PyType_Ready (&PyGIStruct_Type))
229 if (PyModule_AddObject (m, "Struct", (PyObject *) &PyGIStruct_Type))