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, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
24 #include "pygi-private.h"
26 #include <pygobject.h>
27 #include <girepository.h>
28 #include <pyglib-python-compat.h>
31 _struct_dealloc (PyGIStruct *self)
33 GIBaseInfo *info = _pygi_object_get_gi_info (
35 &PyGIStructInfo_Type);
37 PyObject_GC_UnTrack ( (PyObject *) self);
39 PyObject_ClearWeakRefs ( (PyObject *) self);
41 if (info != NULL && g_struct_info_is_foreign ( (GIStructInfo *) info)) {
42 pygi_struct_foreign_release (info, ( (PyGPointer *) self)->pointer);
43 } else if (self->free_on_dealloc) {
44 g_free ( ( (PyGPointer *) self)->pointer);
47 g_base_info_unref (info);
49 Py_TYPE( (PyGPointer *) self )->tp_free ( (PyObject *) self);
53 _struct_new (PyTypeObject *type,
57 static char *kwlist[] = { NULL };
62 PyObject *self = NULL;
64 if (!PyArg_ParseTupleAndKeywords (args, kwargs, "", kwlist)) {
68 info = _pygi_object_get_gi_info ( (PyObject *) type, &PyGIStructInfo_Type);
70 if (PyErr_ExceptionMatches (PyExc_AttributeError)) {
71 PyErr_Format (PyExc_TypeError, "missing introspection information");
76 size = g_struct_info_get_size ( (GIStructInfo *) info);
77 pointer = g_try_malloc0 (size);
78 if (pointer == NULL) {
83 self = _pygi_struct_new (type, pointer, TRUE);
89 g_base_info_unref (info);
91 return (PyObject *) self;
95 _struct_init (PyObject *self,
99 /* Don't call PyGPointer's init, which raises an exception. */
103 PYGLIB_DEFINE_TYPE("gi.Struct", PyGIStruct_Type, PyGIStruct);
106 _pygi_struct_new (PyTypeObject *type,
108 gboolean free_on_dealloc)
113 if (!PyType_IsSubtype (type, &PyGIStruct_Type)) {
114 PyErr_SetString (PyExc_TypeError, "must be a subtype of gi.Struct");
118 self = (PyGIStruct *) type->tp_alloc (type, 0);
123 g_type = pyg_type_from_object ( (PyObject *) type);
125 ( (PyGPointer *) self)->gtype = g_type;
126 ( (PyGPointer *) self)->pointer = pointer;
127 self->free_on_dealloc = free_on_dealloc;
129 return (PyObject *) self;
133 _pygi_struct_register_types (PyObject *m)
135 Py_TYPE(&PyGIStruct_Type) = &PyType_Type;
136 PyGIStruct_Type.tp_base = &PyGPointer_Type;
137 PyGIStruct_Type.tp_new = (newfunc) _struct_new;
138 PyGIStruct_Type.tp_init = (initproc) _struct_init;
139 PyGIStruct_Type.tp_dealloc = (destructor) _struct_dealloc;
140 PyGIStruct_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
142 if (PyType_Ready (&PyGIStruct_Type))
144 if (PyModule_AddObject (m, "Struct", (PyObject *) &PyGIStruct_Type))