Imported Upstream version 2.90.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 PyTypeObject *_PyGObject_Type;
11 #define PyGObject_Type (*_PyGObject_Type)
12
13 static PyObject * _wrap_TestInterface__do_iface_method(PyObject *cls,
14                                                        PyObject *args,
15                                                        PyObject *kwargs);
16
17 GType
18 test_type_get_type(void)
19 {
20     static GType gtype = 0;
21     GType parent_type;
22     
23     if (gtype == 0)
24     {
25         GTypeInfo *type_info;
26         GTypeQuery query;
27         
28         parent_type = g_type_from_name("PyGObject");
29         if (parent_type == 0)
30              g_error("could not get PyGObject from testmodule");
31
32         type_info = (GTypeInfo *)g_new0(GTypeInfo, 1);
33         
34         g_type_query(parent_type, &query);
35         type_info->class_size = query.class_size;
36         type_info->instance_size = query.instance_size;
37         
38         gtype = g_type_register_static(parent_type,
39                                        "TestType", type_info, 0);
40         if (!gtype)
41              g_error("Could not register TestType");
42     }
43     
44     return gtype;
45 }
46
47 #define TYPE_TEST (test_type_get_type())
48
49 static PyObject *
50 _wrap_get_test_thread (PyObject * self)
51 {
52   GObject *obj;
53
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);
58   
59   return pygobject_new(obj);
60 }
61
62 static PyObject *
63 _wrap_get_unknown (PyObject * self)
64 {
65   GObject *obj;
66   obj = g_object_new (TEST_TYPE_UNKNOWN, NULL);
67   return pygobject_new(obj);
68 }
69
70 static PyObject *
71 _wrap_create_test_type (PyObject * self)
72 {
73     GObject *obj;
74     PyObject *rv;
75     obj = g_object_new(TYPE_TEST, NULL);
76     rv = pygobject_new(obj);
77     g_object_unref(obj);
78     return rv;
79 }
80
81 static PyObject *
82 _wrap_test_g_object_new (PyObject * self)
83 {
84     GObject *obj;
85     PyObject *rv;
86
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 */
89     g_object_unref(obj);
90     return rv;
91 }
92
93 /* TestUnknown */
94 static PyObject *
95 _wrap_test_interface_iface_method(PyGObject *self, PyObject *args, PyObject *kwargs)
96 {
97     static char *kwlist[] = { NULL };
98
99     if (!PyArg_ParseTupleAndKeywords(args, kwargs,":", kwlist))
100         return NULL;
101     
102     test_interface_iface_method(TEST_INTERFACE(self->obj));
103     
104     Py_INCREF(Py_None);
105     return Py_None;
106 }
107
108 static const PyMethodDef _PyTestInterface_methods[] = {
109     { "iface_method", (PyCFunction)_wrap_test_interface_iface_method, METH_VARARGS|METH_KEYWORDS,
110       NULL },
111     { "do_iface_method", (PyCFunction)_wrap_TestInterface__do_iface_method, METH_VARARGS|METH_KEYWORDS|METH_CLASS,
112       NULL },
113     { NULL, NULL, 0, NULL }
114 };
115
116 /* TestInterface */
117 PYGLIB_DEFINE_TYPE("test.Interface", PyTestInterface_Type, PyObject);
118
119 static PyObject *
120 _wrap_TestInterface__do_iface_method(PyObject *cls, PyObject *args, PyObject *kwargs)
121 {
122   TestInterfaceIface *iface;
123   static char *kwlist[] = { "self", NULL };
124   PyGObject *self;
125
126   if (!PyArg_ParseTupleAndKeywords(args, kwargs,"O!:TestInterface.iface_method", kwlist, &PyTestInterface_Type, &self))
127     return NULL;
128   
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));
133   else {
134     PyErr_SetString(PyExc_NotImplementedError,
135                     "interface method TestInterface.iface_method not implemented");
136     return NULL;
137   }
138   Py_INCREF(Py_None);
139   return Py_None;
140 }
141
142 PYGLIB_DEFINE_TYPE("testhelper.Unknown", PyTestUnknown_Type, PyGObject);
143
144 static void
145 _wrap_TestInterface__proxy_do_iface_method(TestInterface *self)
146 {
147     PyGILState_STATE __py_state;
148     PyObject *py_self;
149     PyObject *py_retval;
150     PyObject *py_args;
151     PyObject *py_method;
152     
153     __py_state = pyg_gil_state_ensure();
154     py_self = pygobject_new((GObject *) self);
155     if (!py_self) {
156         if (PyErr_Occurred())
157             PyErr_Print();
158         pyg_gil_state_release(__py_state);
159         return;
160     }
161     py_args = PyTuple_New(0);
162     py_method = PyObject_GetAttrString(py_self, "do_iface_method");
163     if (!py_method) {
164         if (PyErr_Occurred())
165             PyErr_Print();
166         Py_DECREF(py_args);
167         Py_DECREF(py_self);
168         pyg_gil_state_release(__py_state);
169         return;
170     }
171     py_retval = PyObject_CallObject(py_method, py_args);
172     if (!py_retval) {
173         if (PyErr_Occurred())
174             PyErr_Print();
175         Py_DECREF(py_method);
176         Py_DECREF(py_args);
177         Py_DECREF(py_self);
178         pyg_gil_state_release(__py_state);
179         return;
180     }
181     if (py_retval != Py_None) {
182         if (PyErr_Occurred())
183             PyErr_Print();
184         PyErr_SetString(PyExc_TypeError, "retval should be None");
185         Py_DECREF(py_retval);
186         Py_DECREF(py_method);
187         Py_DECREF(py_args);
188         Py_DECREF(py_self);
189         pyg_gil_state_release(__py_state);
190         return;
191     }
192     
193     Py_DECREF(py_retval);
194     Py_DECREF(py_method);
195     Py_DECREF(py_args);
196     Py_DECREF(py_self);
197     pyg_gil_state_release(__py_state);
198 }
199
200 static void
201 __TestInterface__interface_init(TestInterfaceIface *iface,
202                                 PyTypeObject *pytype)
203 {
204     TestInterfaceIface *parent_iface = g_type_interface_peek_parent(iface);
205     PyObject *py_method;
206
207     py_method = pytype ? PyObject_GetAttrString((PyObject *) pytype,
208                                                 "do_iface_method") : NULL;
209
210     if (py_method && !PyObject_TypeCheck(py_method, &PyCFunction_Type)) {
211         iface->iface_method = _wrap_TestInterface__proxy_do_iface_method;
212     } else {
213         PyErr_Clear();
214         if (parent_iface) {
215             iface->iface_method = parent_iface->iface_method;
216         }
217         Py_XDECREF(py_method);
218     }
219 }
220
221 static const GInterfaceInfo __TestInterface__iinfo = {
222     (GInterfaceInitFunc) __TestInterface__interface_init,
223     NULL,
224     NULL
225 };
226
227 /* TestFloatingWithSinkFunc */
228 PYGLIB_DEFINE_TYPE("testhelper.FloatingWithSinkFunc", PyTestFloatingWithSinkFunc_Type, PyGObject);
229
230 /* TestFloatingWithoutSinkFunc */
231 PYGLIB_DEFINE_TYPE("testhelper.FloatingWithoutSinkFunc", PyTestFloatingWithoutSinkFunc_Type, PyGObject);
232
233 /* TestOwnedByLibrary */
234 PYGLIB_DEFINE_TYPE("testhelper.OwnedByLibrary", PyTestOwnedByLibrary_Type, PyGObject);
235
236 static PyObject *
237 _wrap_test_owned_by_library_release (PyGObject *self)
238 {
239     test_owned_by_library_release (TEST_OWNED_BY_LIBRARY (self->obj));
240     return Py_None;
241 }
242
243 static const PyMethodDef _PyTestOwnedByLibrary_methods[] = {
244     { "release", (PyCFunction)_wrap_test_owned_by_library_release, METH_NOARGS, NULL },
245     { NULL, NULL, 0, NULL }
246 };
247
248 /* TestFloatingAndSunk */
249 PYGLIB_DEFINE_TYPE("testhelper.FloatingAndSunk", PyTestFloatingAndSunk_Type, PyGObject);
250
251 static PyObject *
252 _wrap_test_floating_and_sunk_release (PyGObject *self)
253 {
254     test_floating_and_sunk_release (TEST_FLOATING_AND_SUNK (self->obj));
255     return Py_None;
256 }
257
258 static const PyMethodDef _PyTestFloatingAndSunk_methods[] = {
259     { "release", (PyCFunction)_wrap_test_floating_and_sunk_release, METH_NOARGS, NULL },
260     { NULL, NULL, 0, NULL }
261 };
262
263
264 #include <string.h>
265 #include <glib-object.h>
266
267 static void
268 test1_callback (GObject *object, char *data)
269 {
270   g_return_if_fail (G_IS_OBJECT (object));
271   g_return_if_fail (!strcmp (data, "user-data"));
272 }
273
274 static void
275 test1_callback_swapped (char *data, GObject *object)
276 {
277   g_return_if_fail (G_IS_OBJECT (object));
278   g_return_if_fail (!strcmp (data, "user-data"));
279 }
280
281 static void
282 test2_callback (GObject *object, char *string)
283 {
284   g_return_if_fail (G_IS_OBJECT (object));
285   g_return_if_fail (!strcmp (string, "string"));
286 }
287
288 static int
289 test3_callback (GObject *object, double d)
290 {
291   g_return_val_if_fail (G_IS_OBJECT (object), -1);
292   g_return_val_if_fail (d == 42.0, -1);
293
294   return 20;
295 }
296
297 static void
298 test4_callback (GObject *object,
299                 gboolean b, long l, float f, double d, guint uint, gulong ulong,
300                 gpointer user_data)
301 {
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);
308 }
309
310 static float
311 test_float_callback (GObject *object, float f)
312 {
313   g_return_val_if_fail (G_IS_OBJECT (object), -1);
314   g_return_val_if_fail (f <= 1.234001 && f >= 1.123999, -1);
315
316   return f;
317 }
318
319 static double
320 test_double_callback (GObject *object, double d)
321 {
322   g_return_val_if_fail (G_IS_OBJECT (object), -1);
323   g_return_val_if_fail (d <= 1.234001 && d >= 1.123999, -1);
324
325   return d;
326 }
327
328 static char *
329 test_string_callback (GObject *object, char *s)
330 {
331   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
332   g_return_val_if_fail (!strcmp(s, "str"), NULL);
333
334   return g_strdup (s);
335 }
336
337 static GObject *
338 test_object_callback (GObject *object, GObject *o)
339 {
340   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
341
342   return o;
343 }
344
345 void
346 connectcallbacks (GObject *object)
347 {
348
349   gchar *data = "user-data";
350   
351   g_signal_connect (G_OBJECT (object),
352                     "test1",
353                     G_CALLBACK (test1_callback), 
354                     data);
355   g_signal_connect_swapped (G_OBJECT (object),
356                     "test1",
357                     G_CALLBACK (test1_callback_swapped), 
358                     data);
359   g_signal_connect (G_OBJECT (object),
360                     "test2",
361                     G_CALLBACK (test2_callback), 
362                     NULL);
363   g_signal_connect (G_OBJECT (object),
364                     "test3",
365                     G_CALLBACK (test3_callback), 
366                     NULL);
367   g_signal_connect (G_OBJECT (object),
368                     "test4",
369                     G_CALLBACK (test4_callback), 
370                     NULL);
371   g_signal_connect (G_OBJECT (object),
372                     "test_float",
373                     G_CALLBACK (test_float_callback), 
374                     NULL);
375   g_signal_connect (G_OBJECT (object),
376                     "test_double",
377                     G_CALLBACK (test_double_callback), 
378                     NULL);
379   g_signal_connect (G_OBJECT (object),
380                     "test_string",
381                     G_CALLBACK (test_string_callback), 
382                     NULL);
383   g_signal_connect (G_OBJECT (object),
384                     "test_object",
385                     G_CALLBACK (test_object_callback), 
386                     NULL);
387 }
388
389 static PyObject *
390 _wrap_connectcallbacks(PyObject * self, PyObject *args)
391 {
392     PyGObject *obj;
393
394     if (!PyArg_ParseTuple(args, "O", &obj))
395       return NULL;
396
397     connectcallbacks (G_OBJECT (obj->obj));
398
399     Py_INCREF(Py_None);
400     return Py_None;
401 }
402
403 static PyObject *
404 _wrap_test_value(PyObject *self, PyObject *args)
405 {
406   GValue tvalue = {0,}, *value = &tvalue;
407   PyObject *obj;
408
409   if (!PyArg_ParseTuple(args, "O", &obj))
410     return NULL;
411
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");
415     return NULL;
416   }
417   
418   return pyg_value_as_pyobject(value, FALSE);
419 }
420
421 static PyObject *
422 _wrap_test_value_array(PyObject *self, PyObject *args)
423 {
424   GValue tvalue = {0,}, *value = &tvalue;
425   PyObject *obj;
426
427   if (!PyArg_ParseTuple(args, "O", &obj))
428     return NULL;
429
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");
433     return NULL;
434   }
435   
436   return pyg_value_as_pyobject(value, FALSE);
437 }
438
439 static PyObject *
440 _wrap_test_gerror_exception(PyObject *self, PyObject *args)
441 {
442     PyObject *py_method;
443     PyObject *py_args;
444     PyObject *py_ret;
445     GError *err = NULL;
446
447     if (!PyArg_ParseTuple(args, "O", &py_method))
448         return NULL;
449
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);
454         return NULL;
455     }       
456
457     Py_DECREF(py_method);
458     Py_DECREF(py_args);
459     Py_DECREF(py_ret);
460
461     Py_INCREF(Py_None);
462     return Py_None;
463 }
464
465 static PyObject *
466 _wrap_test_owned_by_library_get_instance_list (PyObject *self)
467 {
468     PyObject *py_list, *py_obj;
469     GSList *list, *tmp;
470
471     list = test_owned_by_library_get_instance_list ();
472
473     if ((py_list = PyList_New (0)) == NULL) {
474         return NULL;
475     }
476     for (tmp = list; tmp != NULL; tmp = tmp->next) {
477         py_obj = pygobject_new (G_OBJECT (tmp->data));
478         if (py_obj == NULL) {
479             Py_DECREF (py_list);
480             return NULL;
481         }
482         PyList_Append (py_list, py_obj);
483         Py_DECREF (py_obj);
484     }
485     return py_list;
486 }
487
488 static PyObject *
489 _wrap_test_floating_and_sunk_get_instance_list (PyObject *self)
490 {
491     PyObject *py_list, *py_obj;
492     GSList *list, *tmp;
493
494     list = test_floating_and_sunk_get_instance_list ();
495
496     if ((py_list = PyList_New (0)) == NULL) {
497        return NULL;
498     }
499     for (tmp = list; tmp != NULL; tmp = tmp->next) {
500        py_obj = pygobject_new (G_OBJECT (tmp->data));
501        if (py_obj == NULL) {
502            Py_DECREF (py_list);
503            return NULL;
504        }
505        PyList_Append (py_list, py_obj);
506        Py_DECREF (py_obj);
507     }
508     return py_list;
509 }
510
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 },
522     { NULL, NULL }
523 };
524
525 PYGLIB_MODULE_START(testhelper, "testhelper")
526 {
527   PyObject *m, *d;
528   
529   g_thread_init(NULL);
530   pygobject_init(-1, -1, -1);
531
532   d = PyModule_GetDict(module);
533
534   if ((m = PyImport_ImportModule("gi._gobject")) != NULL) {
535     PyObject *moddict = PyModule_GetDict(m);
536     
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;
542     }
543   } else {
544     PyErr_SetString(PyExc_ImportError,
545                     "could not import gobject");
546     return PYGLIB_MODULE_ERROR_RETURN;
547   }
548
549   /* TestInterface */
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);
555
556
557   /* TestUnknown */
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,
562                            &PyTestUnknown_Type,
563                            Py_BuildValue("(O)",
564                            &PyGObject_Type,
565                            &PyTestInterface_Type));
566   pyg_set_object_has_new_constructor(TEST_TYPE_UNKNOWN);
567   //pyg_register_class_init(TEST_TYPE_UNKNOWN, __GtkUIManager_class_init);
568
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,
575                            Py_BuildValue("(O)",
576                            &PyGObject_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);
579
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,
586                            Py_BuildValue("(O)",
587                            &PyGObject_Type));
588   pyg_set_object_has_new_constructor(TEST_TYPE_FLOATING_WITHOUT_SINK_FUNC);
589
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,
597                            Py_BuildValue("(O)",
598                            &PyGObject_Type));
599   pyg_set_object_has_new_constructor(TEST_TYPE_OWNED_BY_LIBRARY);
600
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,
608                            Py_BuildValue("(O)",
609                            &PyGObject_Type));
610   pyg_set_object_has_new_constructor(TEST_TYPE_FLOATING_AND_SUNK);
611 }
612 PYGLIB_MODULE_END
613