2 #include <gobject/gmarshal.h>
4 #include "test-thread.h"
5 #include "test-unknown.h"
6 #include "test-floating.h"
8 #include "pygi-python-compat.h"
10 static PyObject
* _wrap_TestInterface__do_iface_method(PyObject
*cls
,
15 test_type_get_type(void)
17 static GType gtype
= 0;
25 parent_type
= g_type_from_name("PyGObject");
27 g_error("could not get PyGObject from testmodule");
29 type_info
= (GTypeInfo
*)g_new0(GTypeInfo
, 1);
31 g_type_query(parent_type
, &query
);
32 type_info
->class_size
= (guint16
)query
.class_size
;
33 type_info
->instance_size
= (guint16
)query
.instance_size
;
35 gtype
= g_type_register_static(parent_type
,
36 "TestType", type_info
, 0);
38 g_error("Could not register TestType");
44 #define TYPE_TEST (test_type_get_type())
47 _wrap_get_test_thread (PyObject
* self
)
51 test_thread_get_type();
52 g_assert (g_type_is_a (TEST_TYPE_THREAD
, G_TYPE_OBJECT
));
53 obj
= g_object_new (TEST_TYPE_THREAD
, NULL
);
54 g_assert (obj
!= NULL
);
56 return pygobject_new(obj
);
60 _wrap_get_unknown (PyObject
* self
)
63 obj
= g_object_new (TEST_TYPE_UNKNOWN
, NULL
);
64 return pygobject_new(obj
);
68 _wrap_create_test_type (PyObject
* self
)
72 obj
= g_object_new(TYPE_TEST
, NULL
);
73 rv
= pygobject_new(obj
);
79 _wrap_test_g_object_new (PyObject
* self
)
84 obj
= g_object_new(g_type_from_name("PyGObject"), NULL
);
85 rv
= PYGLIB_PyLong_FromLong(obj
->ref_count
); /* should be == 2 at this point */
92 _wrap_test_interface_iface_method(PyGObject
*self
, PyObject
*args
, PyObject
*kwargs
)
94 static char *kwlist
[] = { NULL
};
96 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
,":", kwlist
))
99 test_interface_iface_method(TEST_INTERFACE(self
->obj
));
105 static const PyMethodDef _PyTestInterface_methods
[] = {
106 { "iface_method", (PyCFunction
)_wrap_test_interface_iface_method
, METH_VARARGS
|METH_KEYWORDS
,
108 { "do_iface_method", (PyCFunction
)_wrap_TestInterface__do_iface_method
, METH_VARARGS
|METH_KEYWORDS
|METH_CLASS
,
110 { NULL
, NULL
, 0, NULL
}
114 PYGLIB_DEFINE_TYPE("test.Interface", PyTestInterface_Type
, PyObject
);
117 _wrap_TestInterface__do_iface_method(PyObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
119 TestInterfaceIface
*iface
;
120 static char *kwlist
[] = { "self", NULL
};
123 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
,"O!:TestInterface.iface_method", kwlist
, &PyTestInterface_Type
, &self
))
126 iface
= g_type_interface_peek(g_type_class_peek(pyg_type_from_object(cls
)),
127 TEST_TYPE_INTERFACE
);
128 if (iface
->iface_method
)
129 iface
->iface_method(TEST_INTERFACE(self
->obj
));
131 PyErr_SetString(PyExc_NotImplementedError
,
132 "interface method TestInterface.iface_method not implemented");
139 PYGLIB_DEFINE_TYPE("testhelper.Unknown", PyTestUnknown_Type
, PyGObject
);
142 _wrap_TestInterface__proxy_do_iface_method(TestInterface
*self
)
144 PyGILState_STATE __py_state
;
150 __py_state
= PyGILState_Ensure();
151 py_self
= pygobject_new((GObject
*) self
);
153 if (PyErr_Occurred())
155 PyGILState_Release(__py_state
);
158 py_args
= PyTuple_New(0);
159 py_method
= PyObject_GetAttrString(py_self
, "do_iface_method");
161 if (PyErr_Occurred())
165 PyGILState_Release(__py_state
);
168 py_retval
= PyObject_CallObject(py_method
, py_args
);
170 if (PyErr_Occurred())
172 Py_DECREF(py_method
);
175 PyGILState_Release(__py_state
);
178 if (py_retval
!= Py_None
) {
179 if (PyErr_Occurred())
181 PyErr_SetString(PyExc_TypeError
, "retval should be None");
182 Py_DECREF(py_retval
);
183 Py_DECREF(py_method
);
186 PyGILState_Release(__py_state
);
190 Py_DECREF(py_retval
);
191 Py_DECREF(py_method
);
194 PyGILState_Release(__py_state
);
198 __TestInterface__interface_init(TestInterfaceIface
*iface
,
199 PyTypeObject
*pytype
)
201 TestInterfaceIface
*parent_iface
= g_type_interface_peek_parent(iface
);
204 py_method
= pytype
? PyObject_GetAttrString((PyObject
*) pytype
,
205 "do_iface_method") : NULL
;
207 if (py_method
&& !PyObject_TypeCheck(py_method
, &PyCFunction_Type
)) {
208 iface
->iface_method
= _wrap_TestInterface__proxy_do_iface_method
;
212 iface
->iface_method
= parent_iface
->iface_method
;
214 Py_XDECREF(py_method
);
218 static const GInterfaceInfo __TestInterface__iinfo
= {
219 (GInterfaceInitFunc
) __TestInterface__interface_init
,
225 PYGLIB_DEFINE_TYPE("testhelper.Floating", PyTestFloating_Type
, PyGObject
);
227 /* TestOwnedByLibrary */
228 PYGLIB_DEFINE_TYPE("testhelper.OwnedByLibrary", PyTestOwnedByLibrary_Type
, PyGObject
);
231 _wrap_test_owned_by_library_release (PyGObject
*self
)
233 test_owned_by_library_release (TEST_OWNED_BY_LIBRARY (self
->obj
));
237 static const PyMethodDef _PyTestOwnedByLibrary_methods
[] = {
238 { "release", (PyCFunction
)_wrap_test_owned_by_library_release
, METH_NOARGS
, NULL
},
239 { NULL
, NULL
, 0, NULL
}
242 /* TestFloatingAndSunk */
243 PYGLIB_DEFINE_TYPE("testhelper.FloatingAndSunk", PyTestFloatingAndSunk_Type
, PyGObject
);
246 _wrap_test_floating_and_sunk_release (PyGObject
*self
)
248 test_floating_and_sunk_release (TEST_FLOATING_AND_SUNK (self
->obj
));
252 static const PyMethodDef _PyTestFloatingAndSunk_methods
[] = {
253 { "release", (PyCFunction
)_wrap_test_floating_and_sunk_release
, METH_NOARGS
, NULL
},
254 { NULL
, NULL
, 0, NULL
}
259 #include <glib-object.h>
262 test1_callback (GObject
*object
, char *data
)
264 g_return_if_fail (G_IS_OBJECT (object
));
265 g_return_if_fail (!strcmp (data
, "user-data"));
269 test1_callback_swapped (char *data
, GObject
*object
)
271 g_return_if_fail (G_IS_OBJECT (object
));
272 g_return_if_fail (!strcmp (data
, "user-data"));
276 test2_callback (GObject
*object
, char *string
)
278 g_return_if_fail (G_IS_OBJECT (object
));
279 g_return_if_fail (!strcmp (string
, "string"));
283 test3_callback (GObject
*object
, double d
)
285 g_return_val_if_fail (G_IS_OBJECT (object
), -1);
286 g_return_val_if_fail (d
== 42.0, -1);
292 test4_callback (GObject
*object
,
293 gboolean b
, long l
, float f
, double d
, guint uint
, gulong ulong
,
296 g_return_if_fail (b
== TRUE
);
297 g_return_if_fail (l
== 10L);
298 g_return_if_fail (f
<= 3.14001 && f
>= 3.13999);
299 g_return_if_fail (d
<= 1.78001 && d
>= 1.77999);
300 g_return_if_fail (uint
== 20);
301 g_return_if_fail (ulong
== 30L);
305 test_float_callback (GObject
*object
, float f
)
307 g_return_val_if_fail (G_IS_OBJECT (object
), -1);
308 g_return_val_if_fail (f
<= 1.234001 && f
>= 1.123999, -1);
314 test_double_callback (GObject
*object
, double d
)
316 g_return_val_if_fail (G_IS_OBJECT (object
), -1);
317 g_return_val_if_fail (d
<= 1.234001 && d
>= 1.123999, -1);
323 test_int64_callback (GObject
*object
, gint64 i
)
325 g_return_val_if_fail (G_IS_OBJECT (object
), -1);
333 test_string_callback (GObject
*object
, char *s
)
335 g_return_val_if_fail (G_IS_OBJECT (object
), NULL
);
336 g_return_val_if_fail (!strcmp(s
, "str"), NULL
);
342 test_object_callback (GObject
*object
, GObject
*o
)
344 g_return_val_if_fail (G_IS_OBJECT (object
), NULL
);
350 test_paramspec_callback (GObject
*object
)
352 g_return_val_if_fail (G_IS_OBJECT (object
), NULL
);
354 return g_param_spec_boolean ("test-param", "test", "test boolean", TRUE
, G_PARAM_READABLE
);
358 test_gvalue_callback (GObject
*object
, const GValue
*v
)
360 GValue
*ret
= g_malloc0 (sizeof (GValue
));
362 g_return_val_if_fail (G_IS_OBJECT (object
), NULL
);
363 g_return_val_if_fail (G_IS_VALUE (v
), NULL
);
365 g_value_init (ret
, G_VALUE_TYPE (v
));
366 g_value_copy (v
, ret
);
371 test_gvalue_ret_callback (GObject
*object
, GType type
)
373 GValue
*ret
= g_malloc0 (sizeof (GValue
));
375 g_return_val_if_fail (G_IS_OBJECT (object
), NULL
);
377 g_value_init (ret
, type
);
381 g_value_set_int(ret
, G_MAXINT
);
384 g_value_set_int64(ret
, G_MAXINT64
);
387 g_value_set_uint(ret
, G_MAXUINT
);
390 g_value_set_uint64(ret
, G_MAXUINT64
);
393 g_value_set_string(ret
, "hello");
396 g_critical ("test_gvalue_ret_callback() does not support type %s", g_type_name (type
));
403 test_paramspec_in_callback (GObject
*object
, GParamSpec
*p
)
405 g_return_val_if_fail (G_IS_OBJECT (object
), NULL
);
406 g_return_val_if_fail (G_IS_PARAM_SPEC (p
), NULL
);
412 connectcallbacks (GObject
*object
)
415 gchar
*data
= "user-data";
417 g_signal_connect (G_OBJECT (object
),
419 G_CALLBACK (test1_callback
),
421 g_signal_connect_swapped (G_OBJECT (object
),
423 G_CALLBACK (test1_callback_swapped
),
425 g_signal_connect (G_OBJECT (object
),
427 G_CALLBACK (test2_callback
),
429 g_signal_connect (G_OBJECT (object
),
431 G_CALLBACK (test3_callback
),
433 g_signal_connect (G_OBJECT (object
),
435 G_CALLBACK (test4_callback
),
437 g_signal_connect (G_OBJECT (object
),
439 G_CALLBACK (test_float_callback
),
441 g_signal_connect (G_OBJECT (object
),
443 G_CALLBACK (test_double_callback
),
445 g_signal_connect (G_OBJECT (object
),
447 G_CALLBACK (test_int64_callback
),
449 g_signal_connect (G_OBJECT (object
),
451 G_CALLBACK (test_string_callback
),
453 g_signal_connect (G_OBJECT (object
),
455 G_CALLBACK (test_object_callback
),
457 g_signal_connect (G_OBJECT (object
),
459 G_CALLBACK (test_paramspec_callback
),
461 g_signal_connect (G_OBJECT (object
),
463 G_CALLBACK (test_gvalue_callback
),
465 g_signal_connect (G_OBJECT (object
),
467 G_CALLBACK (test_gvalue_ret_callback
),
469 g_signal_connect (G_OBJECT (object
),
471 G_CALLBACK (test_paramspec_in_callback
),
476 _wrap_connectcallbacks(PyObject
* self
, PyObject
*args
)
480 if (!PyArg_ParseTuple(args
, "O", &obj
))
483 connectcallbacks (G_OBJECT (obj
->obj
));
490 _wrap_test_value(PyObject
*self
, PyObject
*args
)
492 GValue tvalue
= {0,}, *value
= &tvalue
;
495 if (!PyArg_ParseTuple(args
, "O", &obj
))
498 g_value_init(value
, G_TYPE_VALUE
);
499 if (pyg_value_from_pyobject(value
, obj
)) {
500 PyErr_SetString(PyExc_TypeError
, "Could not convert to GValue");
504 return pyg_value_as_pyobject(value
, FALSE
);
508 _wrap_test_state_ensure_release(PyObject
*self
, PyObject
*args
)
510 int state
= pyg_gil_state_ensure ();
511 pyg_gil_state_release (state
);
517 _wrap_test_value_array(PyObject
*self
, PyObject
*args
)
519 GValue tvalue
= {0,}, *value
= &tvalue
;
522 if (!PyArg_ParseTuple(args
, "O", &obj
))
525 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
526 g_value_init(value
, G_TYPE_VALUE_ARRAY
);
527 G_GNUC_END_IGNORE_DEPRECATIONS
529 if (pyg_value_from_pyobject(value
, obj
)) {
530 PyErr_SetString(PyExc_TypeError
, "Could not convert to GValueArray");
534 return pyg_value_as_pyobject(value
, FALSE
);
539 _wrap_value_array_get_nth_type(PyObject
*self
, PyObject
*args
)
547 if (!PyArg_ParseTuple(args
, "OI", &obj
, &n
))
550 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
552 if (pyg_boxed_check(obj
, G_TYPE_VALUE
) &&
553 G_VALUE_HOLDS(pyg_boxed_get(obj
, GValue
), G_TYPE_VALUE_ARRAY
)) {
554 arr
= g_value_get_boxed(pyg_boxed_get(obj
, GValue
));
555 } else if (pyg_boxed_check(obj
, G_TYPE_VALUE_ARRAY
)) {
556 arr
= pyg_boxed_get(obj
, GValueArray
);
558 PyErr_SetString(PyExc_TypeError
, "First argument is not GValueArray");
562 if (n
>= arr
->n_values
) {
563 PyErr_SetString(PyExc_TypeError
, "Index is out of bounds");
566 nth
= g_value_array_get_nth(arr
, n
);
567 type
= G_VALUE_TYPE(nth
);
569 G_GNUC_END_IGNORE_DEPRECATIONS
571 return pyg_type_wrapper_new(type
);
575 _wrap_constant_strip_prefix(PyObject
*self
, PyObject
*args
)
577 const char *name
, *strip_prefix
;
580 if (!PyArg_ParseTuple (args
, "ss", &name
, &strip_prefix
))
583 result
= pyg_constant_strip_prefix (name
, strip_prefix
);
584 return PYGLIB_PyUnicode_FromString (result
);
588 _wrap_test_gerror_exception(PyObject
*self
, PyObject
*args
)
595 if (!PyArg_ParseTuple(args
, "O", &py_method
))
598 py_args
= PyTuple_New(0);
599 py_ret
= PyObject_CallObject(py_method
, py_args
);
600 if (pyg_gerror_exception_check(&err
)) {
601 pyg_error_check(&err
);
613 _wrap_test_owned_by_library_get_instance_list (PyObject
*self
)
615 PyObject
*py_list
, *py_obj
;
618 list
= test_owned_by_library_get_instance_list ();
620 if ((py_list
= PyList_New (0)) == NULL
) {
623 for (tmp
= list
; tmp
!= NULL
; tmp
= tmp
->next
) {
624 py_obj
= pygobject_new (G_OBJECT (tmp
->data
));
625 if (py_obj
== NULL
) {
629 PyList_Append (py_list
, py_obj
);
636 _wrap_test_floating_and_sunk_get_instance_list (PyObject
*self
)
638 PyObject
*py_list
, *py_obj
;
641 list
= test_floating_and_sunk_get_instance_list ();
643 if ((py_list
= PyList_New (0)) == NULL
) {
646 for (tmp
= list
; tmp
!= NULL
; tmp
= tmp
->next
) {
647 py_obj
= pygobject_new (G_OBJECT (tmp
->data
));
648 if (py_obj
== NULL
) {
652 PyList_Append (py_list
, py_obj
);
660 _wrap_test_parse_constructor_args (PyObject
*self
, PyObject
*args
)
662 char *arg_names
[] = {"label", NULL
};
663 char *prop_names
[] = {"label", NULL
};
664 GParameter params
[1] = {{0}};
665 PyObject
*parsed_args
[1];
668 if (!PyArg_ParseTuple(args
, "O", &(parsed_args
[0])))
671 if (!pyg_parse_constructor_args (
672 TYPE_TEST
, arg_names
, prop_names
, params
, &nparams
, parsed_args
)) {
676 return PYGLIB_PyLong_FromLong (nparams
);
680 _wrap_test_to_unichar_conv (PyObject
* self
, PyObject
*args
)
685 if (!PyArg_ParseTuple(args
, "O", &obj
))
688 if (!pyg_pyobj_to_unichar_conv (obj
, &result
))
691 return PYGLIB_PyLong_FromLong (result
);
694 static PyMethodDef testhelper_functions
[] = {
695 { "test_parse_constructor_args", (PyCFunction
)_wrap_test_parse_constructor_args
, METH_VARARGS
},
696 { "get_test_thread", (PyCFunction
)_wrap_get_test_thread
, METH_NOARGS
},
697 { "test_to_unichar_conv", (PyCFunction
)_wrap_test_to_unichar_conv
, METH_VARARGS
},
698 { "get_unknown", (PyCFunction
)_wrap_get_unknown
, METH_NOARGS
},
699 { "create_test_type", (PyCFunction
)_wrap_create_test_type
, METH_NOARGS
},
700 { "test_state_ensure_release", (PyCFunction
)_wrap_test_state_ensure_release
, METH_NOARGS
},
701 { "test_g_object_new", (PyCFunction
)_wrap_test_g_object_new
, METH_NOARGS
},
702 { "connectcallbacks", (PyCFunction
)_wrap_connectcallbacks
, METH_VARARGS
},
703 { "test_value", (PyCFunction
)_wrap_test_value
, METH_VARARGS
},
704 { "test_value_array", (PyCFunction
)_wrap_test_value_array
, METH_VARARGS
},
705 { "value_array_get_nth_type", (PyCFunction
)_wrap_value_array_get_nth_type
, METH_VARARGS
},
706 { "constant_strip_prefix", (PyCFunction
)_wrap_constant_strip_prefix
, METH_VARARGS
},
707 { "test_gerror_exception", (PyCFunction
)_wrap_test_gerror_exception
, METH_VARARGS
},
708 { "owned_by_library_get_instance_list", (PyCFunction
)_wrap_test_owned_by_library_get_instance_list
, METH_NOARGS
},
709 { "floating_and_sunk_get_instance_list", (PyCFunction
)_wrap_test_floating_and_sunk_get_instance_list
, METH_NOARGS
},
713 PYGLIB_MODULE_START(testhelper
, "testhelper")
715 PyObject
*gobject_module
;
719 if ((gobject_module
= pygobject_init(-1, -1, -1)) == NULL
)
721 Py_DECREF (gobject_module
);
723 d
= PyModule_GetDict(module
);
725 if ((m
= PyImport_ImportModule("gi.repository.GObject")) == NULL
) {
726 PyErr_SetString(PyExc_ImportError
,
727 "could not import gobject");
728 return PYGLIB_MODULE_ERROR_RETURN
;
732 PyTestInterface_Type
.tp_methods
= (struct PyMethodDef
*)_PyTestInterface_methods
;
733 PyTestInterface_Type
.tp_flags
= (Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
);
734 pyg_register_interface(d
, "Interface", TEST_TYPE_INTERFACE
,
735 &PyTestInterface_Type
);
736 pyg_register_interface_info(TEST_TYPE_INTERFACE
, &__TestInterface__iinfo
);
740 PyTestUnknown_Type
.tp_flags
= (Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
);
741 PyTestUnknown_Type
.tp_weaklistoffset
= offsetof(PyGObject
, weakreflist
);
742 PyTestUnknown_Type
.tp_dictoffset
= offsetof(PyGObject
, inst_dict
);
743 pygobject_register_class(d
, "Unknown", TEST_TYPE_UNKNOWN
,
747 &PyTestInterface_Type
));
750 PyTestFloating_Type
.tp_flags
= (Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
);
751 PyTestFloating_Type
.tp_weaklistoffset
= offsetof(PyGObject
, weakreflist
);
752 PyTestFloating_Type
.tp_dictoffset
= offsetof(PyGObject
, inst_dict
);
753 pygobject_register_class(d
, "Floating", TEST_TYPE_FLOATING
,
754 &PyTestFloating_Type
,
758 /* TestOwnedByLibrary */
759 PyTestOwnedByLibrary_Type
.tp_flags
= (Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
);
760 PyTestOwnedByLibrary_Type
.tp_methods
= (struct PyMethodDef
*)_PyTestOwnedByLibrary_methods
;
761 PyTestOwnedByLibrary_Type
.tp_weaklistoffset
= offsetof(PyGObject
, weakreflist
);
762 PyTestOwnedByLibrary_Type
.tp_dictoffset
= offsetof(PyGObject
, inst_dict
);
763 pygobject_register_class(d
, "OwnedByLibrary", TEST_TYPE_OWNED_BY_LIBRARY
,
764 &PyTestOwnedByLibrary_Type
,
768 /* TestFloatingAndSunk */
769 PyTestFloatingAndSunk_Type
.tp_flags
= (Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
);
770 PyTestFloatingAndSunk_Type
.tp_methods
= (struct PyMethodDef
*)_PyTestFloatingAndSunk_methods
;
771 PyTestFloatingAndSunk_Type
.tp_weaklistoffset
= offsetof(PyGObject
, weakreflist
);
772 PyTestFloatingAndSunk_Type
.tp_dictoffset
= offsetof(PyGObject
, inst_dict
);
773 pygobject_register_class(d
, "FloatingAndSunk", TEST_TYPE_FLOATING_AND_SUNK
,
774 &PyTestFloatingAndSunk_Type
,