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"
26 #include "pygi-type.h"
27 #include "pygpointer.h"
28 #include "pygi-python-compat.h"
30 #include <girepository.h>
34 struct_get_info (PyTypeObject
*type
)
37 GIBaseInfo
*info
= NULL
;
39 py_info
= PyObject_GetAttrString ((PyObject
*)type
, "__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
)
65 PyObject
*error_type
, *error_value
, *error_traceback
;
66 gboolean have_error
= !!PyErr_Occurred ();
69 PyErr_Fetch (&error_type
, &error_value
, &error_traceback
);
71 info
= struct_get_info (Py_TYPE (self
));
73 if (info
!= NULL
&& g_struct_info_is_foreign ( (GIStructInfo
*) info
)) {
74 pygi_struct_foreign_release (info
, pyg_pointer_get_ptr (self
));
75 } else if (self
->free_on_dealloc
) {
76 g_free (pyg_pointer_get_ptr (self
));
80 g_base_info_unref (info
);
84 PyErr_Restore (error_type
, error_value
, error_traceback
);
86 Py_TYPE (self
)->tp_free ((PyObject
*)self
);
90 struct_new (PyTypeObject
*type
,
94 static char *kwlist
[] = { NULL
};
99 PyObject
*self
= NULL
;
101 if (!PyArg_ParseTupleAndKeywords (args
, kwargs
, "", kwlist
)) {
105 info
= struct_get_info ( type
);
107 if (PyErr_ExceptionMatches (PyExc_AttributeError
)) {
108 PyErr_Format (PyExc_TypeError
, "missing introspection information");
113 size
= g_struct_info_get_size ( (GIStructInfo
*) info
);
115 PyErr_Format (PyExc_TypeError
,
116 "struct cannot be created directly; try using a constructor, see: help(%s.%s)",
117 g_base_info_get_namespace (info
),
118 g_base_info_get_name (info
));
121 pointer
= g_try_malloc0 (size
);
122 if (pointer
== NULL
) {
127 self
= pygi_struct_new (type
, pointer
, TRUE
);
133 g_base_info_unref (info
);
135 return (PyObject
*) self
;
139 struct_init (PyObject
*self
,
143 /* Don't call PyGPointer's init, which raises an exception. */
147 PYGLIB_DEFINE_TYPE("gi.Struct", PyGIStruct_Type
, PyGIStruct
);
151 pygi_struct_new_from_g_type (GType g_type
,
153 gboolean free_on_dealloc
)
158 type
= (PyTypeObject
*)pygi_type_import_by_g_type (g_type
);
161 type
= (PyTypeObject
*)&PyGIStruct_Type
; /* fallback */
163 if (!PyType_IsSubtype (type
, &PyGIStruct_Type
)) {
164 PyErr_SetString (PyExc_TypeError
, "must be a subtype of gi.Struct");
168 self
= (PyGIStruct
*) type
->tp_alloc (type
, 0);
173 pyg_pointer_set_ptr (self
, pointer
);
174 ( (PyGPointer
*) self
)->gtype
= g_type
;
175 self
->free_on_dealloc
= free_on_dealloc
;
177 return (PyObject
*) self
;
182 pygi_struct_new (PyTypeObject
*type
,
184 gboolean free_on_dealloc
)
189 if (!PyType_IsSubtype (type
, &PyGIStruct_Type
)) {
190 PyErr_SetString (PyExc_TypeError
, "must be a subtype of gi.Struct");
194 self
= (PyGIStruct
*) type
->tp_alloc (type
, 0);
199 g_type
= pyg_type_from_object ( (PyObject
*) type
);
201 pyg_pointer_set_ptr (self
, pointer
);
202 ( (PyGPointer
*) self
)->gtype
= g_type
;
203 self
->free_on_dealloc
= free_on_dealloc
;
205 return (PyObject
*) self
;
209 struct_repr(PyGIStruct
*self
)
213 PyGPointer
*pointer
= (PyGPointer
*)self
;
215 info
= struct_get_info (Py_TYPE (self
));
219 repr
= PYGLIB_PyUnicode_FromFormat ("<%s.%s object at %p (%s at %p)>",
220 g_base_info_get_namespace (info
),
221 g_base_info_get_name (info
),
222 self
, g_type_name (pointer
->gtype
),
225 g_base_info_unref (info
);
231 * Returns 0 on success, or -1 and sets an exception.
234 pygi_struct_register_types (PyObject
*m
)
236 Py_TYPE(&PyGIStruct_Type
) = &PyType_Type
;
237 g_assert (Py_TYPE (&PyGPointer_Type
) != NULL
);
238 PyGIStruct_Type
.tp_base
= &PyGPointer_Type
;
239 PyGIStruct_Type
.tp_new
= (newfunc
) struct_new
;
240 PyGIStruct_Type
.tp_init
= (initproc
) struct_init
;
241 PyGIStruct_Type
.tp_dealloc
= (destructor
) struct_dealloc
;
242 PyGIStruct_Type
.tp_flags
= (Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
);
243 PyGIStruct_Type
.tp_repr
= (reprfunc
)struct_repr
;
245 if (PyType_Ready (&PyGIStruct_Type
) < 0)
247 Py_INCREF ((PyObject
*) &PyGIStruct_Type
);
248 if (PyModule_AddObject (m
, "Struct", (PyObject
*) &PyGIStruct_Type
) < 0) {
249 Py_DECREF ((PyObject
*) &PyGIStruct_Type
);