diff options
author | Johan Dahlin <johan@src.gnome.org> | 2006-07-10 14:21:50 +0000 |
---|---|---|
committer | Johan Dahlin <johan@src.gnome.org> | 2006-07-10 14:21:50 +0000 |
commit | dc15570848f833402c31b149ee1c4c4f108394d8 (patch) | |
tree | 6c13496c03348aff39cc498188e0678ae709b4f3 /tests | |
parent | 3a70f2630f8514c841bb5ddeff9f0fad91a99605 (diff) | |
download | pygobject-dc15570848f833402c31b149ee1c4c4f108394d8.tar.gz |
Add Interface implementation test
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 1 | ||||
-rw-r--r-- | tests/test-unknown.c | 33 | ||||
-rw-r--r-- | tests/test-unknown.h | 11 | ||||
-rw-r--r-- | tests/test_interface.py | 25 | ||||
-rw-r--r-- | tests/testhelpermodule.c | 236 |
5 files changed, 296 insertions, 10 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index 8ccb3c39..fe627c2c 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -25,6 +25,7 @@ tests = \ test_conversion.py \ test_enum.py \ test_gtype.py \ + test_interface.py \ test_mainloop.py \ test_properties.py \ test_signal.py \ diff --git a/tests/test-unknown.c b/tests/test-unknown.c index 36a99041..2d7ef518 100644 --- a/tests/test-unknown.c +++ b/tests/test-unknown.c @@ -1,11 +1,5 @@ #include "test-unknown.h" -G_DEFINE_TYPE_WITH_CODE (TestUnknown, test_unknown, G_TYPE_OBJECT, - G_IMPLEMENT_INTERFACE (TEST_TYPE_INTERFACE, NULL)); - -static void test_unknown_init (TestUnknown *self) {} -static void test_unknown_class_init (TestUnknownClass *klass) {} - GType test_interface_get_type (void) { @@ -15,7 +9,7 @@ test_interface_get_type (void) { static const GTypeInfo info = { - sizeof (TestInterface), /* class_size */ + sizeof (TestInterfaceIface), /* class_size */ NULL, /* base_init */ NULL, /* base_finalize */ NULL, @@ -35,3 +29,28 @@ test_interface_get_type (void) return gtype; } + +void test_unknown_iface_method (TestInterface *iface) +{ + g_print ("C impl\n"); +} + +static void +test_unknown_test_interface_init (TestInterfaceIface *iface) +{ + iface->iface_method = test_unknown_iface_method; +} + +G_DEFINE_TYPE_WITH_CODE (TestUnknown, test_unknown, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (TEST_TYPE_INTERFACE, + test_unknown_test_interface_init)); + +static void test_unknown_init (TestUnknown *self) {} +static void test_unknown_class_init (TestUnknownClass *klass) {} + +void test_interface_iface_method (TestInterface *instance) +{ + TestInterfaceIface *iface = TEST_INTERFACE_GET_IFACE (instance); + + return (* iface->iface_method) (instance); +} diff --git a/tests/test-unknown.h b/tests/test-unknown.h index 7e5dde26..e0f51a2e 100644 --- a/tests/test-unknown.h +++ b/tests/test-unknown.h @@ -20,10 +20,15 @@ typedef struct { GType test_unknown_get_type (void); /* TestInterface */ -typedef struct +typedef struct _TestInterface TestInterface; +typedef struct _TestInterfaceIface TestInterfaceIface; + +struct _TestInterfaceIface { GTypeInterface g_iface; -} TestInterface; + /* VTable */ + void (* iface_method) (TestInterface *iface); +}; #define TEST_TYPE_INTERFACE (test_interface_get_type ()) #define TEST_INTERFACE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TEST_TYPE_INTERFACE, TestInterface)) @@ -31,3 +36,5 @@ typedef struct #define TEST_INTERFACE_GET_IFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_INTERFACE, TestInterfaceIface)) GType test_interface_get_type (void); + +void test_interface_iface_method (TestInterface *iface); diff --git a/tests/test_interface.py b/tests/test_interface.py new file mode 100644 index 00000000..2e45e1d9 --- /dev/null +++ b/tests/test_interface.py @@ -0,0 +1,25 @@ +import unittest + +import testmodule +from common import gobject, testhelper +from gobject import GObject, GInterface + +GUnknown = gobject.type_from_name("TestUnknown") +Unknown = GUnknown.pytype + +class MyUnknown(Unknown, testhelper.Interface): + def __init__(self): + Unknown.__init__(self) + self.called = False + + def do_iface_method(self): + self.called = True + Unknown.do_iface_method(self) + +gobject.type_register(MyUnknown) + +class TestIfaceImpl(unittest.TestCase): + def testMethodCall(self): + m = MyUnknown() + m.iface_method() + self.assertEqual(m.called, True) diff --git a/tests/testhelpermodule.c b/tests/testhelpermodule.c index b849344b..48f6f6bb 100644 --- a/tests/testhelpermodule.c +++ b/tests/testhelpermodule.c @@ -4,6 +4,13 @@ #include "test-thread.h" #include "test-unknown.h" +static PyTypeObject *_PyGObject_Type; +#define PyGObject_Type (*_PyGObject_Type) + +static PyObject * _wrap_TestInterface__do_iface_method(PyObject *cls, + PyObject *args, + PyObject *kwargs); + GType test_type_get_type(void) { @@ -88,6 +95,30 @@ static PyMethodDef testhelper_methods[] = { { NULL, NULL } }; +/* TestUnknown */ +static PyObject * +_wrap_test_interface_iface_method(PyGObject *self, PyObject *args, PyObject *kwargs) +{ + static char *kwlist[] = { NULL }; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs,":", kwlist)) + return NULL; + + test_interface_iface_method(TEST_INTERFACE(self->obj)); + + Py_INCREF(Py_None); + return Py_None; +} + +static const PyMethodDef _PyTestInterface_methods[] = { + { "iface_method", (PyCFunction)_wrap_test_interface_iface_method, METH_VARARGS|METH_KEYWORDS, + NULL }, + { "do_iface_method", (PyCFunction)_wrap_TestInterface__do_iface_method, METH_VARARGS|METH_KEYWORDS|METH_CLASS, + NULL }, + { NULL, NULL, 0, NULL } +}; + +/* TestInterface */ PyTypeObject PyTestInterface_Type = { PyObject_HEAD_INIT(NULL) 0, /* ob_size */ @@ -111,21 +142,224 @@ PyTypeObject PyTestInterface_Type = { (setattrofunc)0, /* tp_setattro */ (PyBufferProcs*)0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + NULL, /* Documentation string */ + (traverseproc)0, /* tp_traverse */ + (inquiry)0, /* tp_clear */ + (richcmpfunc)0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + (getiterfunc)0, /* tp_iter */ + (iternextfunc)0, /* tp_iternext */ + (struct PyMethodDef*)_PyTestInterface_methods, /* tp_methods */ + (struct PyMemberDef*)0, /* tp_members */ + (struct PyGetSetDef*)0, /* tp_getset */ + NULL, /* tp_base */ + NULL, /* tp_dict */ + (descrgetfunc)0, /* tp_descr_get */ + (descrsetfunc)0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)0, /* tp_init */ + (allocfunc)0, /* tp_alloc */ + (newfunc)0, /* tp_new */ + (freefunc)0, /* tp_free */ + (inquiry)0 /* tp_is_gc */ + + + +}; + +static PyObject * +_wrap_TestInterface__do_iface_method(PyObject *cls, PyObject *args, PyObject *kwargs) +{ + TestInterfaceIface *iface; + static char *kwlist[] = { "self", NULL }; + PyGObject *self; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs,"O!:TestInterface.iface_method", kwlist, &PyTestInterface_Type, &self)) + return NULL; + + iface = g_type_interface_peek(g_type_class_peek(pyg_type_from_object(cls)), + TEST_TYPE_INTERFACE); + if (iface->iface_method) + iface->iface_method(TEST_INTERFACE(self->obj)); + else { + PyErr_SetString(PyExc_NotImplementedError, + "interface method TestInterface.iface_method not implemented"); + return NULL; + } + Py_INCREF(Py_None); + return Py_None; +} + +PyTypeObject PyTestUnknown_Type = { + PyObject_HEAD_INIT(NULL) + 0, /* ob_size */ + "testhelper.Unknown", /* tp_name */ + sizeof(PyGObject), /* tp_basicsize */ + 0, /* tp_itemsize */ + /* methods */ + (destructor)0, /* tp_dealloc */ + (printfunc)0, /* tp_print */ + (getattrfunc)0, /* tp_getattr */ + (setattrfunc)0, /* tp_setattr */ + (cmpfunc)0, /* tp_compare */ + (reprfunc)0, /* tp_repr */ + (PyNumberMethods*)0, /* tp_as_number */ + (PySequenceMethods*)0, /* tp_as_sequence */ + (PyMappingMethods*)0, /* tp_as_mapping */ + (hashfunc)0, /* tp_hash */ + (ternaryfunc)0, /* tp_call */ + (reprfunc)0, /* tp_str */ + (getattrofunc)0, /* tp_getattro */ + (setattrofunc)0, /* tp_setattro */ + (PyBufferProcs*)0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ + NULL, /* Documentation string */ + (traverseproc)0, /* tp_traverse */ + (inquiry)0, /* tp_clear */ + (richcmpfunc)0, /* tp_richcompare */ + offsetof(PyGObject, weakreflist), /* tp_weaklistoffset */ + (getiterfunc)0, /* tp_iter */ + (iternextfunc)0, /* tp_iternext */ + (struct PyMethodDef*)0, /* tp_methods */ + (struct PyMemberDef*)0, /* tp_members */ + (struct PyGetSetDef*)0, /* tp_getset */ + NULL, /* tp_base */ + NULL, /* tp_dict */ + (descrgetfunc)0, /* tp_descr_get */ + (descrsetfunc)0, /* tp_descr_set */ + offsetof(PyGObject, inst_dict), /* tp_dictoffset */ + (initproc)0, /* tp_init */ + (allocfunc)0, /* tp_alloc */ + (newfunc)0, /* tp_new */ + (freefunc)0, /* tp_free */ + (inquiry)0 /* tp_is_gc */ +}; + + +static void +_wrap_TestInterface__proxy_do_iface_method(TestInterface *self) +{ + PyGILState_STATE __py_state; + PyObject *py_self; + PyObject *py_retval; + PyObject *py_args; + PyObject *py_method; + + __py_state = pyg_gil_state_ensure(); + py_self = pygobject_new((GObject *) self); + if (!py_self) { + if (PyErr_Occurred()) + PyErr_Print(); + pyg_gil_state_release(__py_state); + return; + } + py_args = PyTuple_New(0); + py_method = PyObject_GetAttrString(py_self, "do_iface_method"); + if (!py_method) { + if (PyErr_Occurred()) + PyErr_Print(); + Py_DECREF(py_args); + Py_DECREF(py_self); + pyg_gil_state_release(__py_state); + return; + } + py_retval = PyObject_CallObject(py_method, py_args); + if (!py_retval) { + if (PyErr_Occurred()) + PyErr_Print(); + Py_DECREF(py_method); + Py_DECREF(py_args); + Py_DECREF(py_self); + pyg_gil_state_release(__py_state); + return; + } + if (py_retval != Py_None) { + if (PyErr_Occurred()) + PyErr_Print(); + PyErr_SetString(PyExc_TypeError, "retval should be None"); + Py_DECREF(py_retval); + Py_DECREF(py_method); + Py_DECREF(py_args); + Py_DECREF(py_self); + pyg_gil_state_release(__py_state); + return; + } + + Py_DECREF(py_retval); + Py_DECREF(py_method); + Py_DECREF(py_args); + Py_DECREF(py_self); + pyg_gil_state_release(__py_state); +} + +static void +__TestInterface__interface_init(TestInterfaceIface *iface, + PyTypeObject *pytype) +{ + TestInterfaceIface *parent_iface = g_type_interface_peek_parent(iface); + PyObject *py_method; + + py_method = pytype ? PyObject_GetAttrString((PyObject *) pytype, + "do_iface_method") : NULL; + + if (py_method && !PyObject_TypeCheck(py_method, &PyCFunction_Type)) { + iface->iface_method = _wrap_TestInterface__proxy_do_iface_method; + } else { + PyErr_Clear(); + if (parent_iface) { + iface->iface_method = parent_iface->iface_method; + } + Py_XDECREF(py_method); + } +} + +static const GInterfaceInfo __TestInterface__iinfo = { + (GInterfaceInitFunc) __TestInterface__interface_init, + NULL, + NULL }; void inittesthelper () { PyObject *m, *d; + PyObject *module; init_pygobject(); g_thread_init(NULL); m = Py_InitModule ("testhelper", testhelper_methods); d = PyModule_GetDict(m); - + + if ((module = PyImport_ImportModule("gobject")) != NULL) { + PyObject *moddict = PyModule_GetDict(module); + + _PyGObject_Type = (PyTypeObject *)PyDict_GetItemString(moddict, "GObject"); + if (_PyGObject_Type == NULL) { + PyErr_SetString(PyExc_ImportError, + "cannot import name GObject from gobject"); + return ; + } + } else { + PyErr_SetString(PyExc_ImportError, + "could not import gobject"); + return ; + } + + /* TestInterface */ pyg_register_interface(d, "Interface", TEST_TYPE_INTERFACE, &PyTestInterface_Type); + pyg_register_interface_info(TEST_TYPE_INTERFACE, &__TestInterface__iinfo); + + + /* TestUnknown */ + pygobject_register_class(d, "Unknown", TEST_TYPE_UNKNOWN, + &PyTestUnknown_Type, + Py_BuildValue("(O)", + &PyGObject_Type, + &PyTestInterface_Type)); + pyg_set_object_has_new_constructor(TEST_TYPE_UNKNOWN); + //pyg_register_class_init(TEST_TYPE_UNKNOWN, __GtkUIManager_class_init); } |