2 #include <gobject/gmarshal.h>
4 #include "test-thread.h"
5 #include "test-unknown.h"
6 #include "test-floating.h"
8 #include <pyglib-python-compat.h>
10 static PyTypeObject *_PyGObject_Type;
11 #define PyGObject_Type (*_PyGObject_Type)
13 static PyObject * _wrap_TestInterface__do_iface_method(PyObject *cls,
18 test_type_get_type(void)
20 static GType gtype = 0;
28 parent_type = g_type_from_name("PyGObject");
30 g_error("could not get PyGObject from testmodule");
32 type_info = (GTypeInfo *)g_new0(GTypeInfo, 1);
34 g_type_query(parent_type, &query);
35 type_info->class_size = query.class_size;
36 type_info->instance_size = query.instance_size;
38 gtype = g_type_register_static(parent_type,
39 "TestType", type_info, 0);
41 g_error("Could not register TestType");
47 #define TYPE_TEST (test_type_get_type())
50 _wrap_get_test_thread (PyObject * self)
54 test_thread_get_type();
55 g_assert (g_type_is_a (TEST_TYPE_THREAD, G_TYPE_OBJECT));
56 obj = g_object_new (TEST_TYPE_THREAD, NULL);
57 g_assert (obj != NULL);
59 return pygobject_new(obj);
63 _wrap_get_unknown (PyObject * self)
66 obj = g_object_new (TEST_TYPE_UNKNOWN, NULL);
67 return pygobject_new(obj);
71 _wrap_create_test_type (PyObject * self)
75 obj = g_object_new(TYPE_TEST, NULL);
76 rv = pygobject_new(obj);
82 _wrap_test_g_object_new (PyObject * self)
87 obj = g_object_new(g_type_from_name("PyGObject"), NULL);
88 rv = PYGLIB_PyLong_FromLong(obj->ref_count); /* should be == 2 at this point */
95 _wrap_test_interface_iface_method(PyGObject *self, PyObject *args, PyObject *kwargs)
97 static char *kwlist[] = { NULL };
99 if (!PyArg_ParseTupleAndKeywords(args, kwargs,":", kwlist))
102 test_interface_iface_method(TEST_INTERFACE(self->obj));
108 static const PyMethodDef _PyTestInterface_methods[] = {
109 { "iface_method", (PyCFunction)_wrap_test_interface_iface_method, METH_VARARGS|METH_KEYWORDS,
111 { "do_iface_method", (PyCFunction)_wrap_TestInterface__do_iface_method, METH_VARARGS|METH_KEYWORDS|METH_CLASS,
113 { NULL, NULL, 0, NULL }
117 PYGLIB_DEFINE_TYPE("test.Interface", PyTestInterface_Type, PyObject);
120 _wrap_TestInterface__do_iface_method(PyObject *cls, PyObject *args, PyObject *kwargs)
122 TestInterfaceIface *iface;
123 static char *kwlist[] = { "self", NULL };
126 if (!PyArg_ParseTupleAndKeywords(args, kwargs,"O!:TestInterface.iface_method", kwlist, &PyTestInterface_Type, &self))
129 iface = g_type_interface_peek(g_type_class_peek(pyg_type_from_object(cls)),
130 TEST_TYPE_INTERFACE);
131 if (iface->iface_method)
132 iface->iface_method(TEST_INTERFACE(self->obj));
134 PyErr_SetString(PyExc_NotImplementedError,
135 "interface method TestInterface.iface_method not implemented");
142 PYGLIB_DEFINE_TYPE("testhelper.Unknown", PyTestUnknown_Type, PyGObject);
145 _wrap_TestInterface__proxy_do_iface_method(TestInterface *self)
147 PyGILState_STATE __py_state;
153 __py_state = pyg_gil_state_ensure();
154 py_self = pygobject_new((GObject *) self);
156 if (PyErr_Occurred())
158 pyg_gil_state_release(__py_state);
161 py_args = PyTuple_New(0);
162 py_method = PyObject_GetAttrString(py_self, "do_iface_method");
164 if (PyErr_Occurred())
168 pyg_gil_state_release(__py_state);
171 py_retval = PyObject_CallObject(py_method, py_args);
173 if (PyErr_Occurred())
175 Py_DECREF(py_method);
178 pyg_gil_state_release(__py_state);
181 if (py_retval != Py_None) {
182 if (PyErr_Occurred())
184 PyErr_SetString(PyExc_TypeError, "retval should be None");
185 Py_DECREF(py_retval);
186 Py_DECREF(py_method);
189 pyg_gil_state_release(__py_state);
193 Py_DECREF(py_retval);
194 Py_DECREF(py_method);
197 pyg_gil_state_release(__py_state);
201 __TestInterface__interface_init(TestInterfaceIface *iface,
202 PyTypeObject *pytype)
204 TestInterfaceIface *parent_iface = g_type_interface_peek_parent(iface);
207 py_method = pytype ? PyObject_GetAttrString((PyObject *) pytype,
208 "do_iface_method") : NULL;
210 if (py_method && !PyObject_TypeCheck(py_method, &PyCFunction_Type)) {
211 iface->iface_method = _wrap_TestInterface__proxy_do_iface_method;
215 iface->iface_method = parent_iface->iface_method;
217 Py_XDECREF(py_method);
221 static const GInterfaceInfo __TestInterface__iinfo = {
222 (GInterfaceInitFunc) __TestInterface__interface_init,
227 /* TestFloatingWithSinkFunc */
228 PYGLIB_DEFINE_TYPE("testhelper.FloatingWithSinkFunc", PyTestFloatingWithSinkFunc_Type, PyGObject);
230 /* TestFloatingWithoutSinkFunc */
231 PYGLIB_DEFINE_TYPE("testhelper.FloatingWithoutSinkFunc", PyTestFloatingWithoutSinkFunc_Type, PyGObject);
233 /* TestOwnedByLibrary */
234 PYGLIB_DEFINE_TYPE("testhelper.OwnedByLibrary", PyTestOwnedByLibrary_Type, PyGObject);
237 _wrap_test_owned_by_library_release (PyGObject *self)
239 test_owned_by_library_release (TEST_OWNED_BY_LIBRARY (self->obj));
243 static const PyMethodDef _PyTestOwnedByLibrary_methods[] = {
244 { "release", (PyCFunction)_wrap_test_owned_by_library_release, METH_NOARGS, NULL },
245 { NULL, NULL, 0, NULL }
248 /* TestFloatingAndSunk */
249 PYGLIB_DEFINE_TYPE("testhelper.FloatingAndSunk", PyTestFloatingAndSunk_Type, PyGObject);
252 _wrap_test_floating_and_sunk_release (PyGObject *self)
254 test_floating_and_sunk_release (TEST_FLOATING_AND_SUNK (self->obj));
258 static const PyMethodDef _PyTestFloatingAndSunk_methods[] = {
259 { "release", (PyCFunction)_wrap_test_floating_and_sunk_release, METH_NOARGS, NULL },
260 { NULL, NULL, 0, NULL }
265 #include <glib-object.h>
268 test1_callback (GObject *object, char *data)
270 g_return_if_fail (G_IS_OBJECT (object));
271 g_return_if_fail (!strcmp (data, "user-data"));
275 test1_callback_swapped (char *data, GObject *object)
277 g_return_if_fail (G_IS_OBJECT (object));
278 g_return_if_fail (!strcmp (data, "user-data"));
282 test2_callback (GObject *object, char *string)
284 g_return_if_fail (G_IS_OBJECT (object));
285 g_return_if_fail (!strcmp (string, "string"));
289 test3_callback (GObject *object, double d)
291 g_return_val_if_fail (G_IS_OBJECT (object), -1);
292 g_return_val_if_fail (d == 42.0, -1);
298 test4_callback (GObject *object,
299 gboolean b, long l, float f, double d, guint uint, gulong ulong,
302 g_return_if_fail (b == TRUE);
303 g_return_if_fail (l == 10L);
304 g_return_if_fail (f <= 3.14001 && f >= 3.13999);
305 g_return_if_fail (d <= 1.78001 && d >= 1.77999);
306 g_return_if_fail (uint == 20);
307 g_return_if_fail (ulong == 30L);
311 test_float_callback (GObject *object, float f)
313 g_return_val_if_fail (G_IS_OBJECT (object), -1);
314 g_return_val_if_fail (f <= 1.234001 && f >= 1.123999, -1);
320 test_double_callback (GObject *object, double d)
322 g_return_val_if_fail (G_IS_OBJECT (object), -1);
323 g_return_val_if_fail (d <= 1.234001 && d >= 1.123999, -1);
329 test_string_callback (GObject *object, char *s)
331 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
332 g_return_val_if_fail (!strcmp(s, "str"), NULL);
338 test_object_callback (GObject *object, GObject *o)
340 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
346 connectcallbacks (GObject *object)
349 gchar *data = "user-data";
351 g_signal_connect (G_OBJECT (object),
353 G_CALLBACK (test1_callback),
355 g_signal_connect_swapped (G_OBJECT (object),
357 G_CALLBACK (test1_callback_swapped),
359 g_signal_connect (G_OBJECT (object),
361 G_CALLBACK (test2_callback),
363 g_signal_connect (G_OBJECT (object),
365 G_CALLBACK (test3_callback),
367 g_signal_connect (G_OBJECT (object),
369 G_CALLBACK (test4_callback),
371 g_signal_connect (G_OBJECT (object),
373 G_CALLBACK (test_float_callback),
375 g_signal_connect (G_OBJECT (object),
377 G_CALLBACK (test_double_callback),
379 g_signal_connect (G_OBJECT (object),
381 G_CALLBACK (test_string_callback),
383 g_signal_connect (G_OBJECT (object),
385 G_CALLBACK (test_object_callback),
390 _wrap_connectcallbacks(PyObject * self, PyObject *args)
394 if (!PyArg_ParseTuple(args, "O", &obj))
397 connectcallbacks (G_OBJECT (obj->obj));
404 _wrap_test_value(PyObject *self, PyObject *args)
406 GValue tvalue = {0,}, *value = &tvalue;
409 if (!PyArg_ParseTuple(args, "O", &obj))
412 g_value_init(value, G_TYPE_VALUE);
413 if (pyg_value_from_pyobject(value, obj)) {
414 PyErr_SetString(PyExc_TypeError, "Could not convert to GValue");
418 return pyg_value_as_pyobject(value, FALSE);
422 _wrap_test_value_array(PyObject *self, PyObject *args)
424 GValue tvalue = {0,}, *value = &tvalue;
427 if (!PyArg_ParseTuple(args, "O", &obj))
430 g_value_init(value, G_TYPE_VALUE_ARRAY);
431 if (pyg_value_from_pyobject(value, obj)) {
432 PyErr_SetString(PyExc_TypeError, "Could not convert to GValueArray");
436 return pyg_value_as_pyobject(value, FALSE);
440 _wrap_test_gerror_exception(PyObject *self, PyObject *args)
447 if (!PyArg_ParseTuple(args, "O", &py_method))
450 py_args = PyTuple_New(0);
451 py_ret = PyObject_CallObject(py_method, py_args);
452 if (pyg_gerror_exception_check(&err)) {
453 pyg_error_check(&err);
457 Py_DECREF(py_method);
466 _wrap_test_owned_by_library_get_instance_list (PyObject *self)
468 PyObject *py_list, *py_obj;
471 list = test_owned_by_library_get_instance_list ();
473 if ((py_list = PyList_New (0)) == NULL) {
476 for (tmp = list; tmp != NULL; tmp = tmp->next) {
477 py_obj = pygobject_new (G_OBJECT (tmp->data));
478 if (py_obj == NULL) {
482 PyList_Append (py_list, py_obj);
489 _wrap_test_floating_and_sunk_get_instance_list (PyObject *self)
491 PyObject *py_list, *py_obj;
494 list = test_floating_and_sunk_get_instance_list ();
496 if ((py_list = PyList_New (0)) == NULL) {
499 for (tmp = list; tmp != NULL; tmp = tmp->next) {
500 py_obj = pygobject_new (G_OBJECT (tmp->data));
501 if (py_obj == NULL) {
505 PyList_Append (py_list, py_obj);
511 static PyMethodDef testhelper_functions[] = {
512 { "get_test_thread", (PyCFunction)_wrap_get_test_thread, METH_NOARGS },
513 { "get_unknown", (PyCFunction)_wrap_get_unknown, METH_NOARGS },
514 { "create_test_type", (PyCFunction)_wrap_create_test_type, METH_NOARGS },
515 { "test_g_object_new", (PyCFunction)_wrap_test_g_object_new, METH_NOARGS },
516 { "connectcallbacks", (PyCFunction)_wrap_connectcallbacks, METH_VARARGS },
517 { "test_value", (PyCFunction)_wrap_test_value, METH_VARARGS },
518 { "test_value_array", (PyCFunction)_wrap_test_value_array, METH_VARARGS },
519 { "test_gerror_exception", (PyCFunction)_wrap_test_gerror_exception, METH_VARARGS },
520 { "owned_by_library_get_instance_list", (PyCFunction)_wrap_test_owned_by_library_get_instance_list, METH_NOARGS },
521 { "floating_and_sunk_get_instance_list", (PyCFunction)_wrap_test_floating_and_sunk_get_instance_list, METH_NOARGS },
525 PYGLIB_MODULE_START(testhelper, "testhelper")
530 pygobject_init(-1, -1, -1);
532 d = PyModule_GetDict(module);
534 if ((m = PyImport_ImportModule("gi._gobject")) != NULL) {
535 PyObject *moddict = PyModule_GetDict(m);
537 _PyGObject_Type = (PyTypeObject *)PyDict_GetItemString(moddict, "GObject");
538 if (_PyGObject_Type == NULL) {
539 PyErr_SetString(PyExc_ImportError,
540 "cannot import name GObject from gobject");
541 return PYGLIB_MODULE_ERROR_RETURN;
544 PyErr_SetString(PyExc_ImportError,
545 "could not import gobject");
546 return PYGLIB_MODULE_ERROR_RETURN;
550 PyTestInterface_Type.tp_methods = (struct PyMethodDef*)_PyTestInterface_methods;
551 PyTestInterface_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
552 pyg_register_interface(d, "Interface", TEST_TYPE_INTERFACE,
553 &PyTestInterface_Type);
554 pyg_register_interface_info(TEST_TYPE_INTERFACE, &__TestInterface__iinfo);
558 PyTestUnknown_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
559 PyTestUnknown_Type.tp_weaklistoffset = offsetof(PyGObject, weakreflist);
560 PyTestUnknown_Type.tp_dictoffset = offsetof(PyGObject, inst_dict);
561 pygobject_register_class(d, "Unknown", TEST_TYPE_UNKNOWN,
565 &PyTestInterface_Type));
566 pyg_set_object_has_new_constructor(TEST_TYPE_UNKNOWN);
567 //pyg_register_class_init(TEST_TYPE_UNKNOWN, __GtkUIManager_class_init);
569 /* TestFloatingWithSinkFunc */
570 PyTestFloatingWithSinkFunc_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
571 PyTestFloatingWithSinkFunc_Type.tp_weaklistoffset = offsetof(PyGObject, weakreflist);
572 PyTestFloatingWithSinkFunc_Type.tp_dictoffset = offsetof(PyGObject, inst_dict);
573 pygobject_register_class(d, "FloatingWithSinkFunc", TEST_TYPE_FLOATING_WITH_SINK_FUNC,
574 &PyTestFloatingWithSinkFunc_Type,
577 pyg_set_object_has_new_constructor(TEST_TYPE_FLOATING_WITH_SINK_FUNC);
578 pygobject_register_sinkfunc(TEST_TYPE_FLOATING_WITH_SINK_FUNC, sink_test_floating_with_sink_func);
580 /* TestFloatingWithoutSinkFunc */
581 PyTestFloatingWithoutSinkFunc_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
582 PyTestFloatingWithoutSinkFunc_Type.tp_weaklistoffset = offsetof(PyGObject, weakreflist);
583 PyTestFloatingWithoutSinkFunc_Type.tp_dictoffset = offsetof(PyGObject, inst_dict);
584 pygobject_register_class(d, "FloatingWithoutSinkFunc", TEST_TYPE_FLOATING_WITHOUT_SINK_FUNC,
585 &PyTestFloatingWithoutSinkFunc_Type,
588 pyg_set_object_has_new_constructor(TEST_TYPE_FLOATING_WITHOUT_SINK_FUNC);
590 /* TestOwnedByLibrary */
591 PyTestOwnedByLibrary_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
592 PyTestOwnedByLibrary_Type.tp_methods = (struct PyMethodDef*)_PyTestOwnedByLibrary_methods;
593 PyTestOwnedByLibrary_Type.tp_weaklistoffset = offsetof(PyGObject, weakreflist);
594 PyTestOwnedByLibrary_Type.tp_dictoffset = offsetof(PyGObject, inst_dict);
595 pygobject_register_class(d, "OwnedByLibrary", TEST_TYPE_OWNED_BY_LIBRARY,
596 &PyTestOwnedByLibrary_Type,
599 pyg_set_object_has_new_constructor(TEST_TYPE_OWNED_BY_LIBRARY);
601 /* TestFloatingAndSunk */
602 PyTestFloatingAndSunk_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
603 PyTestFloatingAndSunk_Type.tp_methods = (struct PyMethodDef*)_PyTestFloatingAndSunk_methods;
604 PyTestFloatingAndSunk_Type.tp_weaklistoffset = offsetof(PyGObject, weakreflist);
605 PyTestFloatingAndSunk_Type.tp_dictoffset = offsetof(PyGObject, inst_dict);
606 pygobject_register_class(d, "FloatingAndSunk", TEST_TYPE_FLOATING_AND_SUNK,
607 &PyTestFloatingAndSunk_Type,
610 pyg_set_object_has_new_constructor(TEST_TYPE_FLOATING_AND_SUNK);