1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
3 *
4 * Copyright (C) 2009 Simon van der Linden <svdlinden@src.gnome.org>
5 *
6 * pygi-struct.c: wrapper to handle non-registered structures.
7 *
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.
12 *
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.
17 *
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
21 * USA
22 */
23
24 #include "pygi-private.h"
25
26 #include <pygobject.h>
27 #include <girepository.h>
28 #include <pyglib-python-compat.h>
29
30 static void
_struct_dealloc(PyGIStruct * self)31 _struct_dealloc (PyGIStruct *self)
32 {
33 GIBaseInfo *info = _pygi_object_get_gi_info (
34 (PyObject *) self,
35 &PyGIStructInfo_Type);
36
37 PyObject_GC_UnTrack ( (PyObject *) self);
38
39 PyObject_ClearWeakRefs ( (PyObject *) self);
40
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);
45 }
46
47 g_base_info_unref (info);
48
49 Py_TYPE( (PyGPointer *) self )->tp_free ( (PyObject *) self);
50 }
51
52 static PyObject *
_struct_new(PyTypeObject * type,PyObject * args,PyObject * kwargs)53 _struct_new (PyTypeObject *type,
54 PyObject *args,
55 PyObject *kwargs)
56 {
57 static char *kwlist[] = { NULL };
58
59 GIBaseInfo *info;
60 gsize size;
61 gpointer pointer;
62 PyObject *self = NULL;
63
64 if (!PyArg_ParseTupleAndKeywords (args, kwargs, "", kwlist)) {
65 return NULL;
66 }
67
68 info = _pygi_object_get_gi_info ( (PyObject *) type, &PyGIStructInfo_Type);
69 if (info == NULL) {
70 if (PyErr_ExceptionMatches (PyExc_AttributeError)) {
71 PyErr_Format (PyExc_TypeError, "missing introspection information");
72 }
73 return NULL;
74 }
75
76 size = g_struct_info_get_size ( (GIStructInfo *) info);
77 pointer = g_try_malloc0 (size);
78 if (pointer == NULL) {
79 PyErr_NoMemory();
80 goto out;
81 }
82
83 self = _pygi_struct_new (type, pointer, TRUE);
84 if (self == NULL) {
85 g_free (pointer);
86 }
87
88 out:
89 g_base_info_unref (info);
90
91 return (PyObject *) self;
92 }
93
94 static int
_struct_init(PyObject * self,PyObject * args,PyObject * kwargs)95 _struct_init (PyObject *self,
96 PyObject *args,
97 PyObject *kwargs)
98 {
99 /* Don't call PyGPointer's init, which raises an exception. */
100 return 0;
101 }
102
103 PYGLIB_DEFINE_TYPE("gi.Struct", PyGIStruct_Type, PyGIStruct);
104
105 PyObject *
_pygi_struct_new(PyTypeObject * type,gpointer pointer,gboolean free_on_dealloc)106 _pygi_struct_new (PyTypeObject *type,
107 gpointer pointer,
108 gboolean free_on_dealloc)
109 {
110 PyGIStruct *self;
111 GType g_type;
112
113 if (!PyType_IsSubtype (type, &PyGIStruct_Type)) {
114 PyErr_SetString (PyExc_TypeError, "must be a subtype of gi.Struct");
115 return NULL;
116 }
117
118 self = (PyGIStruct *) type->tp_alloc (type, 0);
119 if (self == NULL) {
120 return NULL;
121 }
122
123 g_type = pyg_type_from_object ( (PyObject *) type);
124
125 ( (PyGPointer *) self)->gtype = g_type;
126 ( (PyGPointer *) self)->pointer = pointer;
127 self->free_on_dealloc = free_on_dealloc;
128
129 return (PyObject *) self;
130 }
131
132 void
_pygi_struct_register_types(PyObject * m)133 _pygi_struct_register_types (PyObject *m)
134 {
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);
141
142 if (PyType_Ready (&PyGIStruct_Type))
143 return;
144 if (PyModule_AddObject (m, "Struct", (PyObject *) &PyGIStruct_Type))
145 return;
146 }
147