Imported Upstream version 3.21.91
[platform/upstream/python-gobject.git] / gi / pygi-struct.c
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, see <http://www.gnu.org/licenses/>.
20  */
21
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"
28
29 #include <girepository.h>
30 #include <pyglib-python-compat.h>
31
32
33 static GIBaseInfo *
34 _struct_get_info (PyObject *self)
35 {
36     PyObject *py_info;
37     GIBaseInfo *info = NULL;
38
39     py_info = PyObject_GetAttrString (self, "__info__");
40     if (py_info == NULL) {
41         return NULL;
42     }
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;
50     }
51
52     info = ( (PyGIBaseInfo *) py_info)->info;
53     g_base_info_ref (info);
54
55 out:
56     Py_DECREF (py_info);
57
58     return info;
59 }
60
61 static void
62 _struct_dealloc (PyGIStruct *self)
63 {
64     GIBaseInfo *info = _struct_get_info ( (PyObject *) self );
65
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));
70     }
71
72     if (info != NULL) {
73         g_base_info_unref (info);
74     }
75
76     Py_TYPE (self)->tp_free ((PyObject *)self);
77 }
78
79 static PyObject *
80 _struct_new (PyTypeObject *type,
81              PyObject     *args,
82              PyObject     *kwargs)
83 {
84     static char *kwlist[] = { NULL };
85
86     GIBaseInfo *info;
87     gsize size;
88     gpointer pointer;
89     PyObject *self = NULL;
90
91     if (!PyArg_ParseTupleAndKeywords (args, kwargs, "", kwlist)) {
92         return NULL;
93     }
94
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");
99         }
100         return NULL;
101     }
102
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;
110     }
111     pointer = g_try_malloc0 (size);
112     if (pointer == NULL) {
113         PyErr_NoMemory();
114         goto out;
115     }
116
117     self = _pygi_struct_new (type, pointer, TRUE);
118     if (self == NULL) {
119         g_free (pointer);
120     }
121
122 out:
123     g_base_info_unref (info);
124
125     return (PyObject *) self;
126 }
127
128 static int
129 _struct_init (PyObject *self,
130               PyObject *args,
131               PyObject *kwargs)
132 {
133     /* Don't call PyGPointer's init, which raises an exception. */
134     return 0;
135 }
136
137 PYGLIB_DEFINE_TYPE("gi.Struct", PyGIStruct_Type, PyGIStruct);
138
139
140 PyObject *
141 _pygi_struct_new_from_g_type (GType g_type,
142                               gpointer      pointer,
143                               gboolean      free_on_dealloc)
144 {
145     PyGIStruct *self;
146     PyTypeObject *type;
147
148     type = (PyTypeObject *)pygi_type_import_by_g_type (g_type);
149
150     if (!type)
151         type = (PyTypeObject *)&PyGIStruct_Type; /* fallback */
152
153     if (!PyType_IsSubtype (type, &PyGIStruct_Type)) {
154         PyErr_SetString (PyExc_TypeError, "must be a subtype of gi.Struct");
155         return NULL;
156     }
157
158     self = (PyGIStruct *) type->tp_alloc (type, 0);
159     if (self == NULL) {
160         return NULL;
161     }
162
163     pyg_pointer_set_ptr (self, pointer);
164     ( (PyGPointer *) self)->gtype = g_type;
165     self->free_on_dealloc = free_on_dealloc;
166
167     return (PyObject *) self;
168 }
169
170
171 PyObject *
172 _pygi_struct_new (PyTypeObject *type,
173                   gpointer      pointer,
174                   gboolean      free_on_dealloc)
175 {
176     PyGIStruct *self;
177     GType g_type;
178
179     if (!PyType_IsSubtype (type, &PyGIStruct_Type)) {
180         PyErr_SetString (PyExc_TypeError, "must be a subtype of gi.Struct");
181         return NULL;
182     }
183
184     self = (PyGIStruct *) type->tp_alloc (type, 0);
185     if (self == NULL) {
186         return NULL;
187     }
188
189     g_type = pyg_type_from_object ( (PyObject *) type);
190
191     pyg_pointer_set_ptr (self, pointer);
192     ( (PyGPointer *) self)->gtype = g_type;
193     self->free_on_dealloc = free_on_dealloc;
194
195     return (PyObject *) self;
196 }
197
198 static PyObject *
199 _struct_repr(PyGIStruct *self)
200 {
201     PyObject* repr;
202     GIBaseInfo *info;
203     PyGPointer *pointer = (PyGPointer *)self;
204
205     info = _struct_get_info ((PyObject *)self);
206     if (info == NULL)
207         return NULL;
208
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);
214
215     g_base_info_unref (info);
216
217     return repr;
218 }
219
220 void
221 _pygi_struct_register_types (PyObject *m)
222 {
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;
230
231     if (PyType_Ready (&PyGIStruct_Type))
232         return;
233     if (PyModule_AddObject (m, "Struct", (PyObject *) &PyGIStruct_Type))
234         return;
235 }