Imported Upstream version 3.13.1
[platform/upstream/pygobject2.git] / tests / testhelpermodule.c
1 #include "pygobject.h"
2 #include <gobject/gmarshal.h>
3
4 #include "test-thread.h"
5 #include "test-unknown.h"
6 #include "test-floating.h"
7
8 #include <pyglib-python-compat.h>
9
10 static PyObject * _wrap_TestInterface__do_iface_method(PyObject *cls,
11                                                        PyObject *args,
12                                                        PyObject *kwargs);
13
14 static GType
15 test_type_get_type(void)
16 {
17     static GType gtype = 0;
18     GType parent_type;
19     
20     if (gtype == 0)
21     {
22         GTypeInfo *type_info;
23         GTypeQuery query;
24         
25         parent_type = g_type_from_name("PyGObject");
26         if (parent_type == 0)
27              g_error("could not get PyGObject from testmodule");
28
29         type_info = (GTypeInfo *)g_new0(GTypeInfo, 1);
30         
31         g_type_query(parent_type, &query);
32         type_info->class_size = query.class_size;
33         type_info->instance_size = query.instance_size;
34         
35         gtype = g_type_register_static(parent_type,
36                                        "TestType", type_info, 0);
37         if (!gtype)
38              g_error("Could not register TestType");
39     }
40     
41     return gtype;
42 }
43
44 #define TYPE_TEST (test_type_get_type())
45
46 static PyObject *
47 _wrap_get_test_thread (PyObject * self)
48 {
49   GObject *obj;
50
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);
55   
56   return pygobject_new(obj);
57 }
58
59 static PyObject *
60 _wrap_get_unknown (PyObject * self)
61 {
62   GObject *obj;
63   obj = g_object_new (TEST_TYPE_UNKNOWN, NULL);
64   return pygobject_new(obj);
65 }
66
67 static PyObject *
68 _wrap_create_test_type (PyObject * self)
69 {
70     GObject *obj;
71     PyObject *rv;
72     obj = g_object_new(TYPE_TEST, NULL);
73     rv = pygobject_new(obj);
74     g_object_unref(obj);
75     return rv;
76 }
77
78 static PyObject *
79 _wrap_test_g_object_new (PyObject * self)
80 {
81     GObject *obj;
82     PyObject *rv;
83
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 */
86     g_object_unref(obj);
87     return rv;
88 }
89
90 /* TestUnknown */
91 static PyObject *
92 _wrap_test_interface_iface_method(PyGObject *self, PyObject *args, PyObject *kwargs)
93 {
94     static char *kwlist[] = { NULL };
95
96     if (!PyArg_ParseTupleAndKeywords(args, kwargs,":", kwlist))
97         return NULL;
98     
99     test_interface_iface_method(TEST_INTERFACE(self->obj));
100     
101     Py_INCREF(Py_None);
102     return Py_None;
103 }
104
105 static const PyMethodDef _PyTestInterface_methods[] = {
106     { "iface_method", (PyCFunction)_wrap_test_interface_iface_method, METH_VARARGS|METH_KEYWORDS,
107       NULL },
108     { "do_iface_method", (PyCFunction)_wrap_TestInterface__do_iface_method, METH_VARARGS|METH_KEYWORDS|METH_CLASS,
109       NULL },
110     { NULL, NULL, 0, NULL }
111 };
112
113 /* TestInterface */
114 PYGLIB_DEFINE_TYPE("test.Interface", PyTestInterface_Type, PyObject);
115
116 static PyObject *
117 _wrap_TestInterface__do_iface_method(PyObject *cls, PyObject *args, PyObject *kwargs)
118 {
119   TestInterfaceIface *iface;
120   static char *kwlist[] = { "self", NULL };
121   PyGObject *self;
122
123   if (!PyArg_ParseTupleAndKeywords(args, kwargs,"O!:TestInterface.iface_method", kwlist, &PyTestInterface_Type, &self))
124     return NULL;
125   
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));
130   else {
131     PyErr_SetString(PyExc_NotImplementedError,
132                     "interface method TestInterface.iface_method not implemented");
133     return NULL;
134   }
135   Py_INCREF(Py_None);
136   return Py_None;
137 }
138
139 PYGLIB_DEFINE_TYPE("testhelper.Unknown", PyTestUnknown_Type, PyGObject);
140
141 static void
142 _wrap_TestInterface__proxy_do_iface_method(TestInterface *self)
143 {
144     PyGILState_STATE __py_state;
145     PyObject *py_self;
146     PyObject *py_retval;
147     PyObject *py_args;
148     PyObject *py_method;
149     
150     __py_state = pyg_gil_state_ensure();
151     py_self = pygobject_new((GObject *) self);
152     if (!py_self) {
153         if (PyErr_Occurred())
154             PyErr_Print();
155         pyg_gil_state_release(__py_state);
156         return;
157     }
158     py_args = PyTuple_New(0);
159     py_method = PyObject_GetAttrString(py_self, "do_iface_method");
160     if (!py_method) {
161         if (PyErr_Occurred())
162             PyErr_Print();
163         Py_DECREF(py_args);
164         Py_DECREF(py_self);
165         pyg_gil_state_release(__py_state);
166         return;
167     }
168     py_retval = PyObject_CallObject(py_method, py_args);
169     if (!py_retval) {
170         if (PyErr_Occurred())
171             PyErr_Print();
172         Py_DECREF(py_method);
173         Py_DECREF(py_args);
174         Py_DECREF(py_self);
175         pyg_gil_state_release(__py_state);
176         return;
177     }
178     if (py_retval != Py_None) {
179         if (PyErr_Occurred())
180             PyErr_Print();
181         PyErr_SetString(PyExc_TypeError, "retval should be None");
182         Py_DECREF(py_retval);
183         Py_DECREF(py_method);
184         Py_DECREF(py_args);
185         Py_DECREF(py_self);
186         pyg_gil_state_release(__py_state);
187         return;
188     }
189     
190     Py_DECREF(py_retval);
191     Py_DECREF(py_method);
192     Py_DECREF(py_args);
193     Py_DECREF(py_self);
194     pyg_gil_state_release(__py_state);
195 }
196
197 static void
198 __TestInterface__interface_init(TestInterfaceIface *iface,
199                                 PyTypeObject *pytype)
200 {
201     TestInterfaceIface *parent_iface = g_type_interface_peek_parent(iface);
202     PyObject *py_method;
203
204     py_method = pytype ? PyObject_GetAttrString((PyObject *) pytype,
205                                                 "do_iface_method") : NULL;
206
207     if (py_method && !PyObject_TypeCheck(py_method, &PyCFunction_Type)) {
208         iface->iface_method = _wrap_TestInterface__proxy_do_iface_method;
209     } else {
210         PyErr_Clear();
211         if (parent_iface) {
212             iface->iface_method = parent_iface->iface_method;
213         }
214         Py_XDECREF(py_method);
215     }
216 }
217
218 static const GInterfaceInfo __TestInterface__iinfo = {
219     (GInterfaceInitFunc) __TestInterface__interface_init,
220     NULL,
221     NULL
222 };
223
224 /* TestFloating */
225 PYGLIB_DEFINE_TYPE("testhelper.Floating", PyTestFloating_Type, PyGObject);
226
227 /* TestOwnedByLibrary */
228 PYGLIB_DEFINE_TYPE("testhelper.OwnedByLibrary", PyTestOwnedByLibrary_Type, PyGObject);
229
230 static PyObject *
231 _wrap_test_owned_by_library_release (PyGObject *self)
232 {
233     test_owned_by_library_release (TEST_OWNED_BY_LIBRARY (self->obj));
234     return Py_None;
235 }
236
237 static const PyMethodDef _PyTestOwnedByLibrary_methods[] = {
238     { "release", (PyCFunction)_wrap_test_owned_by_library_release, METH_NOARGS, NULL },
239     { NULL, NULL, 0, NULL }
240 };
241
242 /* TestFloatingAndSunk */
243 PYGLIB_DEFINE_TYPE("testhelper.FloatingAndSunk", PyTestFloatingAndSunk_Type, PyGObject);
244
245 static PyObject *
246 _wrap_test_floating_and_sunk_release (PyGObject *self)
247 {
248     test_floating_and_sunk_release (TEST_FLOATING_AND_SUNK (self->obj));
249     return Py_None;
250 }
251
252 static const PyMethodDef _PyTestFloatingAndSunk_methods[] = {
253     { "release", (PyCFunction)_wrap_test_floating_and_sunk_release, METH_NOARGS, NULL },
254     { NULL, NULL, 0, NULL }
255 };
256
257
258 #include <string.h>
259 #include <glib-object.h>
260
261 static void
262 test1_callback (GObject *object, char *data)
263 {
264   g_return_if_fail (G_IS_OBJECT (object));
265   g_return_if_fail (!strcmp (data, "user-data"));
266 }
267
268 static void
269 test1_callback_swapped (char *data, GObject *object)
270 {
271   g_return_if_fail (G_IS_OBJECT (object));
272   g_return_if_fail (!strcmp (data, "user-data"));
273 }
274
275 static void
276 test2_callback (GObject *object, char *string)
277 {
278   g_return_if_fail (G_IS_OBJECT (object));
279   g_return_if_fail (!strcmp (string, "string"));
280 }
281
282 static int
283 test3_callback (GObject *object, double d)
284 {
285   g_return_val_if_fail (G_IS_OBJECT (object), -1);
286   g_return_val_if_fail (d == 42.0, -1);
287
288   return 20;
289 }
290
291 static void
292 test4_callback (GObject *object,
293                 gboolean b, long l, float f, double d, guint uint, gulong ulong,
294                 gpointer user_data)
295 {
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);
302 }
303
304 static float
305 test_float_callback (GObject *object, float f)
306 {
307   g_return_val_if_fail (G_IS_OBJECT (object), -1);
308   g_return_val_if_fail (f <= 1.234001 && f >= 1.123999, -1);
309
310   return f;
311 }
312
313 static double
314 test_double_callback (GObject *object, double d)
315 {
316   g_return_val_if_fail (G_IS_OBJECT (object), -1);
317   g_return_val_if_fail (d <= 1.234001 && d >= 1.123999, -1);
318
319   return d;
320 }
321
322 static gint64
323 test_int64_callback (GObject *object, gint64 i)
324 {
325   g_return_val_if_fail (G_IS_OBJECT (object), -1);
326
327   if (i == G_MAXINT64)
328       return i-1;
329   return i;
330 }
331
332 static char *
333 test_string_callback (GObject *object, char *s)
334 {
335   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
336   g_return_val_if_fail (!strcmp(s, "str"), NULL);
337
338   return g_strdup (s);
339 }
340
341 static GObject *
342 test_object_callback (GObject *object, GObject *o)
343 {
344   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
345
346   return o;
347 }
348
349 static GParamSpec *
350 test_paramspec_callback (GObject *object)
351 {
352   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
353
354   return g_param_spec_boolean ("test-param", "test", "test boolean", TRUE, G_PARAM_READABLE);
355 }
356
357 static GValue *
358 test_gvalue_callback (GObject *object, const GValue *v)
359 {
360   GValue *ret = g_malloc0 (sizeof (GValue));
361
362   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
363   g_return_val_if_fail (G_IS_VALUE (v), NULL);
364
365   g_value_init (ret, G_VALUE_TYPE (v));
366   g_value_copy (v, ret);
367   return ret;
368 }
369
370 static GValue *
371 test_gvalue_ret_callback (GObject *object, GType type)
372 {
373   GValue *ret = g_malloc0 (sizeof (GValue));
374
375   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
376
377   g_value_init (ret, type);
378
379   switch (type) {
380     case G_TYPE_INT:
381       g_value_set_int(ret, G_MAXINT);
382       break;
383     case G_TYPE_INT64:
384       g_value_set_int64(ret, G_MAXINT64);
385       break;
386     case G_TYPE_UINT:
387       g_value_set_uint(ret, G_MAXUINT);
388       break;
389     case G_TYPE_UINT64:
390       g_value_set_uint64(ret, G_MAXUINT64);
391       break;
392     case G_TYPE_STRING:
393       g_value_set_string(ret, "hello");
394       break;
395     default:
396       g_critical ("test_gvalue_ret_callback() does not support type %s", g_type_name (type));
397   }
398
399   return ret;
400 }
401
402 static GParamSpec *
403 test_paramspec_in_callback (GObject *object, GParamSpec *p)
404 {
405   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
406   g_return_val_if_fail (G_IS_PARAM_SPEC (p), NULL);
407
408   return p;
409 }
410
411 static void
412 connectcallbacks (GObject *object)
413 {
414
415   gchar *data = "user-data";
416   
417   g_signal_connect (G_OBJECT (object),
418                     "test1",
419                     G_CALLBACK (test1_callback), 
420                     data);
421   g_signal_connect_swapped (G_OBJECT (object),
422                     "test1",
423                     G_CALLBACK (test1_callback_swapped), 
424                     data);
425   g_signal_connect (G_OBJECT (object),
426                     "test2",
427                     G_CALLBACK (test2_callback), 
428                     NULL);
429   g_signal_connect (G_OBJECT (object),
430                     "test3",
431                     G_CALLBACK (test3_callback), 
432                     NULL);
433   g_signal_connect (G_OBJECT (object),
434                     "test4",
435                     G_CALLBACK (test4_callback), 
436                     NULL);
437   g_signal_connect (G_OBJECT (object),
438                     "test_float",
439                     G_CALLBACK (test_float_callback), 
440                     NULL);
441   g_signal_connect (G_OBJECT (object),
442                     "test_double",
443                     G_CALLBACK (test_double_callback), 
444                     NULL);
445   g_signal_connect (G_OBJECT (object),
446                     "test_int64",
447                     G_CALLBACK (test_int64_callback), 
448                     NULL);
449   g_signal_connect (G_OBJECT (object),
450                     "test_string",
451                     G_CALLBACK (test_string_callback), 
452                     NULL);
453   g_signal_connect (G_OBJECT (object),
454                     "test_object",
455                     G_CALLBACK (test_object_callback), 
456                     NULL);
457   g_signal_connect (G_OBJECT (object),
458                     "test_paramspec",
459                     G_CALLBACK (test_paramspec_callback), 
460                     NULL);
461   g_signal_connect (G_OBJECT (object),
462                     "test_gvalue",
463                     G_CALLBACK (test_gvalue_callback), 
464                     NULL);
465   g_signal_connect (G_OBJECT (object),
466                     "test_gvalue_ret",
467                     G_CALLBACK (test_gvalue_ret_callback), 
468                     NULL);
469   g_signal_connect (G_OBJECT (object),
470                     "test_paramspec_in",
471                     G_CALLBACK (test_paramspec_in_callback), 
472                     NULL);
473 }
474
475 static PyObject *
476 _wrap_connectcallbacks(PyObject * self, PyObject *args)
477 {
478     PyGObject *obj;
479
480     if (!PyArg_ParseTuple(args, "O", &obj))
481       return NULL;
482
483     connectcallbacks (G_OBJECT (obj->obj));
484
485     Py_INCREF(Py_None);
486     return Py_None;
487 }
488
489 static PyObject *
490 _wrap_test_value(PyObject *self, PyObject *args)
491 {
492   GValue tvalue = {0,}, *value = &tvalue;
493   PyObject *obj;
494
495   if (!PyArg_ParseTuple(args, "O", &obj))
496     return NULL;
497
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");
501     return NULL;
502   }
503   
504   return pyg_value_as_pyobject(value, FALSE);
505 }
506
507 static PyObject *
508 _wrap_test_value_array(PyObject *self, PyObject *args)
509 {
510   GValue tvalue = {0,}, *value = &tvalue;
511   PyObject *obj;
512
513   if (!PyArg_ParseTuple(args, "O", &obj))
514     return NULL;
515
516   G_GNUC_BEGIN_IGNORE_DEPRECATIONS
517   g_value_init(value, G_TYPE_VALUE_ARRAY);
518   G_GNUC_END_IGNORE_DEPRECATIONS
519
520   if (pyg_value_from_pyobject(value, obj)) {
521     PyErr_SetString(PyExc_TypeError, "Could not convert to GValueArray");
522     return NULL;
523   }
524   
525   return pyg_value_as_pyobject(value, FALSE);
526 }
527
528 static PyObject *
529 _wrap_test_gerror_exception(PyObject *self, PyObject *args)
530 {
531     PyObject *py_method;
532     PyObject *py_args;
533     PyObject *py_ret;
534     GError *err = NULL;
535
536     if (!PyArg_ParseTuple(args, "O", &py_method))
537         return NULL;
538
539     py_args = PyTuple_New(0);
540     py_ret = PyObject_CallObject(py_method, py_args);
541     if (pyg_gerror_exception_check(&err)) {
542         pyg_error_check(&err);
543         return NULL;
544     }       
545
546     Py_DECREF(py_args);
547     Py_DECREF(py_ret);
548
549     Py_INCREF(Py_None);
550     return Py_None;
551 }
552
553 static PyObject *
554 _wrap_test_owned_by_library_get_instance_list (PyObject *self)
555 {
556     PyObject *py_list, *py_obj;
557     GSList *list, *tmp;
558
559     list = test_owned_by_library_get_instance_list ();
560
561     if ((py_list = PyList_New (0)) == NULL) {
562         return NULL;
563     }
564     for (tmp = list; tmp != NULL; tmp = tmp->next) {
565         py_obj = pygobject_new (G_OBJECT (tmp->data));
566         if (py_obj == NULL) {
567             Py_DECREF (py_list);
568             return NULL;
569         }
570         PyList_Append (py_list, py_obj);
571         Py_DECREF (py_obj);
572     }
573     return py_list;
574 }
575
576 static PyObject *
577 _wrap_test_floating_and_sunk_get_instance_list (PyObject *self)
578 {
579     PyObject *py_list, *py_obj;
580     GSList *list, *tmp;
581
582     list = test_floating_and_sunk_get_instance_list ();
583
584     if ((py_list = PyList_New (0)) == NULL) {
585        return NULL;
586     }
587     for (tmp = list; tmp != NULL; tmp = tmp->next) {
588        py_obj = pygobject_new (G_OBJECT (tmp->data));
589        if (py_obj == NULL) {
590            Py_DECREF (py_list);
591            return NULL;
592        }
593        PyList_Append (py_list, py_obj);
594        Py_DECREF (py_obj);
595     }
596     return py_list;
597 }
598
599 static PyMethodDef testhelper_functions[] = {
600     { "get_test_thread", (PyCFunction)_wrap_get_test_thread, METH_NOARGS },
601     { "get_unknown", (PyCFunction)_wrap_get_unknown, METH_NOARGS },
602     { "create_test_type", (PyCFunction)_wrap_create_test_type, METH_NOARGS },
603     { "test_g_object_new", (PyCFunction)_wrap_test_g_object_new, METH_NOARGS },
604     { "connectcallbacks", (PyCFunction)_wrap_connectcallbacks, METH_VARARGS },
605     { "test_value", (PyCFunction)_wrap_test_value, METH_VARARGS },      
606     { "test_value_array", (PyCFunction)_wrap_test_value_array, METH_VARARGS },
607     { "test_gerror_exception", (PyCFunction)_wrap_test_gerror_exception, METH_VARARGS },
608     { "owned_by_library_get_instance_list", (PyCFunction)_wrap_test_owned_by_library_get_instance_list, METH_NOARGS },
609     { "floating_and_sunk_get_instance_list", (PyCFunction)_wrap_test_floating_and_sunk_get_instance_list, METH_NOARGS },
610     { NULL, NULL }
611 };
612
613 PYGLIB_MODULE_START(testhelper, "testhelper")
614 {
615   PyObject *m, *d;
616   
617   pygobject_init(-1, -1, -1);
618
619   d = PyModule_GetDict(module);
620
621   if ((m = PyImport_ImportModule("gi.repository.GObject")) == NULL) {
622     PyErr_SetString(PyExc_ImportError,
623                     "could not import gobject");
624     return PYGLIB_MODULE_ERROR_RETURN;
625   }
626
627   /* TestInterface */
628   PyTestInterface_Type.tp_methods = (struct PyMethodDef*)_PyTestInterface_methods;
629   PyTestInterface_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);  
630   pyg_register_interface(d, "Interface", TEST_TYPE_INTERFACE,
631                          &PyTestInterface_Type);
632   pyg_register_interface_info(TEST_TYPE_INTERFACE, &__TestInterface__iinfo);
633
634
635   /* TestUnknown */
636   PyTestUnknown_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
637   PyTestUnknown_Type.tp_weaklistoffset = offsetof(PyGObject, weakreflist);
638   PyTestUnknown_Type.tp_dictoffset = offsetof(PyGObject, inst_dict);
639   pygobject_register_class(d, "Unknown", TEST_TYPE_UNKNOWN,
640                            &PyTestUnknown_Type,
641                            Py_BuildValue("(O)",
642                            &PyGObject_Type,
643                            &PyTestInterface_Type));
644
645   /* TestFloating */
646   PyTestFloating_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
647   PyTestFloating_Type.tp_weaklistoffset = offsetof(PyGObject, weakreflist);
648   PyTestFloating_Type.tp_dictoffset = offsetof(PyGObject, inst_dict);
649   pygobject_register_class(d, "Floating", TEST_TYPE_FLOATING,
650                            &PyTestFloating_Type,
651                            Py_BuildValue("(O)",
652                            &PyGObject_Type));
653
654   /* TestOwnedByLibrary */
655   PyTestOwnedByLibrary_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
656   PyTestOwnedByLibrary_Type.tp_methods = (struct PyMethodDef*)_PyTestOwnedByLibrary_methods;
657   PyTestOwnedByLibrary_Type.tp_weaklistoffset = offsetof(PyGObject, weakreflist);
658   PyTestOwnedByLibrary_Type.tp_dictoffset = offsetof(PyGObject, inst_dict);
659   pygobject_register_class(d, "OwnedByLibrary", TEST_TYPE_OWNED_BY_LIBRARY,
660                            &PyTestOwnedByLibrary_Type,
661                            Py_BuildValue("(O)",
662                            &PyGObject_Type));
663
664   /* TestFloatingAndSunk */
665   PyTestFloatingAndSunk_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
666   PyTestFloatingAndSunk_Type.tp_methods = (struct PyMethodDef*)_PyTestFloatingAndSunk_methods;
667   PyTestFloatingAndSunk_Type.tp_weaklistoffset = offsetof(PyGObject, weakreflist);
668   PyTestFloatingAndSunk_Type.tp_dictoffset = offsetof(PyGObject, inst_dict);
669   pygobject_register_class(d, "FloatingAndSunk", TEST_TYPE_FLOATING_AND_SUNK,
670                            &PyTestFloatingAndSunk_Type,
671                            Py_BuildValue("(O)",
672                            &PyGObject_Type));
673 }
674 PYGLIB_MODULE_END
675