summaryrefslogtreecommitdiff
path: root/gi/pygi-boxed.c
diff options
context:
space:
mode:
Diffstat (limited to 'gi/pygi-boxed.c')
-rw-r--r--gi/pygi-boxed.c94
1 files changed, 40 insertions, 54 deletions
diff --git a/gi/pygi-boxed.c b/gi/pygi-boxed.c
index 4903834..2fd446c 100644
--- a/gi/pygi-boxed.c
+++ b/gi/pygi-boxed.c
@@ -25,6 +25,7 @@
#include <pygobject.h>
#include <girepository.h>
+#include <pyglib-python-compat.h>
static void
_boxed_dealloc (PyGIBoxed *self)
@@ -44,7 +45,38 @@ _boxed_dealloc (PyGIBoxed *self)
}
}
- ( (PyGObject *) self)->ob_type->tp_free ( (PyObject *) self);
+ Py_TYPE( (PyGObject *) self)->tp_free ( (PyObject *) self);
+}
+
+void *
+_pygi_boxed_alloc (GIBaseInfo *info, gsize *size_out)
+{
+ gsize size;
+
+ /* FIXME: Remove when bgo#622711 is fixed */
+ if (g_registered_type_info_get_g_type (info) == G_TYPE_VALUE) {
+ size = sizeof (GValue);
+ } else {
+ switch (g_base_info_get_type (info)) {
+ case GI_INFO_TYPE_UNION:
+ size = g_union_info_get_size ( (GIUnionInfo *) info);
+ break;
+ case GI_INFO_TYPE_BOXED:
+ case GI_INFO_TYPE_STRUCT:
+ size = g_struct_info_get_size ( (GIStructInfo *) info);
+ break;
+ default:
+ PyErr_Format (PyExc_TypeError,
+ "info should be Boxed or Union, not '%d'",
+ g_base_info_get_type (info));
+ return NULL;
+ }
+ }
+
+ if( size_out != NULL)
+ *size_out = size;
+
+ return g_slice_alloc0 (size);
}
static PyObject *
@@ -55,7 +87,7 @@ _boxed_new (PyTypeObject *type,
static char *kwlist[] = { NULL };
GIBaseInfo *info;
- gsize size;
+ gsize size = 0;
gpointer boxed;
PyGIBoxed *self = NULL;
@@ -71,22 +103,7 @@ _boxed_new (PyTypeObject *type,
return NULL;
}
- switch (g_base_info_get_type (info)) {
- case GI_INFO_TYPE_UNION:
- size = g_union_info_get_size ( (GIUnionInfo *) info);
- break;
- case GI_INFO_TYPE_BOXED:
- case GI_INFO_TYPE_STRUCT:
- size = g_struct_info_get_size ( (GIStructInfo *) info);
- break;
- default:
- PyErr_Format (PyExc_TypeError,
- "info should be Boxed or Union, not '%d'",
- g_base_info_get_type (info));
- return NULL;
- }
-
- boxed = g_slice_alloc0 (size);
+ boxed = _pygi_boxed_alloc (info, &size);
if (boxed == NULL) {
PyErr_NoMemory();
goto out;
@@ -116,41 +133,7 @@ _boxed_init (PyObject *self,
return 0;
}
-
-PyTypeObject PyGIBoxed_Type = {
- PyObject_HEAD_INIT (NULL)
- 0,
- "gi.Boxed", /* tp_name */
- sizeof (PyGIBoxed), /* tp_basicsize */
- 0, /* tp_itemsize */
- (destructor) _boxed_dealloc, /* tp_dealloc */
- (printfunc) NULL, /* tp_print */
- (getattrfunc) NULL, /* tp_getattr */
- (setattrfunc) NULL, /* tp_setattr */
- (cmpfunc) NULL, /* tp_compare */
- (reprfunc) NULL, /* tp_repr */
- NULL, /* tp_as_number */
- NULL, /* tp_as_sequence */
- NULL, /* tp_as_mapping */
- (hashfunc) NULL, /* tp_hash */
- (ternaryfunc) NULL, /* tp_call */
- (reprfunc) NULL, /* tp_str */
- (getattrofunc) NULL, /* tp_getattro */
- (setattrofunc) NULL, /* tp_setattro */
- NULL, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- NULL, /* tp_doc */
- (traverseproc) NULL, /* tp_traverse */
- (inquiry) NULL, /* tp_clear */
- (richcmpfunc) NULL, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- (getiterfunc) NULL, /* tp_iter */
- (iternextfunc) NULL, /* tp_iternext */
- NULL, /* tp_methods */
- NULL, /* tp_members */
- NULL, /* tp_getset */
- (PyTypeObject *) NULL, /* tp_base */
-};
+PYGLIB_DEFINE_TYPE("gi.Boxed", PyGIBoxed_Type, PyGIBoxed);
PyObject *
_pygi_boxed_new (PyTypeObject *type,
@@ -185,10 +168,13 @@ _pygi_boxed_new (PyTypeObject *type,
void
_pygi_boxed_register_types (PyObject *m)
{
- PyGIBoxed_Type.ob_type = &PyType_Type;
+ Py_TYPE(&PyGIBoxed_Type) = &PyType_Type;
PyGIBoxed_Type.tp_base = &PyGBoxed_Type;
PyGIBoxed_Type.tp_new = (newfunc) _boxed_new;
PyGIBoxed_Type.tp_init = (initproc) _boxed_init;
+ PyGIBoxed_Type.tp_dealloc = (destructor) _boxed_dealloc;
+ PyGIBoxed_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
+
if (PyType_Ready (&PyGIBoxed_Type))
return;
if (PyModule_AddObject (m, "Boxed", (PyObject *) &PyGIBoxed_Type))