From 3887e4cc9989a1e215e62d17938c0cd361ff9b61 Mon Sep 17 00:00:00 2001 From: Johan Dahlin Date: Mon, 25 Jul 2005 15:12:23 +0000 Subject: [PATCH] Clean up tests, we don't need a C binding to access tp_basicsize, clean up * tests/test_subtype.py: * tests/testhelpermodule.c: (test_type_get_type), (_wrap_get_unknown), (_wrap_test_g_object_new), (inittesthelper): Clean up tests, we don't need a C binding to access tp_basicsize, clean up the C tests, quite a bit aswell. --- tests/test_subtype.py | 75 +++++++++++++------------------ tests/testhelpermodule.c | 95 ++++++++++------------------------------ 2 files changed, 55 insertions(+), 115 deletions(-) diff --git a/tests/test_subtype.py b/tests/test_subtype.py index ea66fab..0de6074 100644 --- a/tests/test_subtype.py +++ b/tests/test_subtype.py @@ -1,55 +1,42 @@ import unittest -from common import gobject, gtk, testhelper import testmodule +from common import gobject, gtk, testhelper +from gobject import GObject, GInterface class TestSubType(unittest.TestCase): def testSubType(self): - t = type('testtype', (gobject.GObject, gobject.GInterface), {}) - self.assert_(issubclass(t, gobject.GObject)) - self.assert_(issubclass(t, gobject.GInterface)) - t = type('testtype2', (gobject.GObject, gtk.TreeModel), {}) - self.assert_(issubclass(t, gobject.GObject)) - self.assert_(issubclass(t, gtk.TreeModel)) - - def testTpBasicSize(self): - iface = testhelper.get_tp_basicsize(gobject.GInterface) - gobj = testhelper.get_tp_basicsize(gobject.GObject) + t = type('testtype', (GObject, GInterface), {}) + self.failUnless(issubclass(t, GObject)) + self.failUnless(issubclass(t, GInterface)) + t = type('testtype2', (GObject, gtk.TreeModel), {}) + self.failUnless(issubclass(t, GObject)) + self.failUnless(issubclass(t, gtk.TreeModel)) - widget = testhelper.get_tp_basicsize(gtk.Widget) - self.assert_(gobj == widget) - - treemodel = testhelper.get_tp_basicsize(gtk.TreeModel) - self.assert_(iface == treemodel) - - def testBuiltinContructorRefcount(self): - foo = gtk.Label() - self.assertEqual(foo.__grefcount__, 1) - - def testPyContructorRefcount(self): - foo = testmodule.PyLabel() - self.assertEqual(foo.__grefcount__, 1) - - def testBuiltinObjNewRefcount(self): - foo = gobject.new(gtk.Label) - self.assertEqual(foo.__grefcount__, 1) - - def testPyObjNewRefcount(self): - foo = gobject.new(testmodule.PyLabel) - self.assertEqual(foo.__grefcount__, 1) - - def testPyContructorPropertyChaining(self): - foo = testmodule.PyLabel() - self.assertEqual(foo.__grefcount__, 1) - - def testPyObjNewPropertyChaining(self): - foo = gobject.new(testmodule.PyLabel) - self.assertEqual(foo.props.label, "hello") - - def testCPyCSubclassing1(self): + def testTpBasicSize(self): + self.assertEqual(GObject.__basicsize__, + gtk.Widget.__basicsize__) + + self.assertEqual(GInterface.__basicsize__, + gtk.TreeModel.__basicsize__) + + def testLabel(self): + label = gtk.Label() + self.assertEqual(label.__grefcount__, 1) + label = gobject.new(gtk.Label) + self.assertEqual(label.__grefcount__, 1) + + def testPythonSubclass(self): + label = testmodule.PyLabel() + self.assertEqual(label.__grefcount__, 1) + self.assertEqual(label.props.label, "hello") + label = gobject.new(testmodule.PyLabel) + self.assertEqual(label.__grefcount__, 1) + self.assertEqual(label.props.label, "hello") + + def testCPyCSubclassing(self): obj = testhelper.create_test_type() self.assertEqual(obj.__grefcount__, 1) - - def testCPyCSubclassing1(self): refcount = testhelper.test_g_object_new() self.assertEqual(refcount, 2) + diff --git a/tests/testhelpermodule.c b/tests/testhelpermodule.c index 962a0dc..7684721 100644 --- a/tests/testhelpermodule.c +++ b/tests/testhelpermodule.c @@ -4,64 +4,38 @@ #include "test-thread.h" #include "test-unknown.h" - -static GType -py_label_get_type(void) -{ - static GType gtype = 0; - if (gtype == 0) { - PyObject *module; - if ((module = PyImport_ImportModule("testmodule")) != NULL) { - PyObject *moddict = PyModule_GetDict(module); - PyObject *py_label_type = PyDict_GetItemString(moddict, "PyLabel"); - if (py_label_type != NULL) - gtype = pyg_type_from_object(py_label_type); - } - } - if (gtype == 0) - g_warning("could not get PyLabel from testmodule"); - return gtype; -} - GType test_type_get_type(void) { static GType gtype = 0; - + GType parent_type; + if (gtype == 0) { - GTypeQuery q; - GTypeInfo type_info = { - 0, /* class_size */ - - (GBaseInitFunc) NULL, - (GBaseFinalizeFunc) NULL, - - (GClassInitFunc) NULL, - (GClassFinalizeFunc) NULL, - NULL, /* class_data */ - - 0, /* instance_size */ - 0, /* n_preallocs */ - (GInstanceInitFunc) NULL - }; - g_type_query(py_label_get_type(), &q); - type_info.class_size = q.class_size; - type_info.instance_size = q.instance_size; - gtype = g_type_register_static(py_label_get_type(), "TestType", &type_info, 0); + GTypeInfo *type_info; + GTypeQuery query; + + parent_type = g_type_from_name("PyLabel"); + if (parent_type == 0) + g_error("could not get PyLabel from testmodule"); + + type_info = (GTypeInfo *)g_new0(GTypeInfo, 1); + + g_type_query(parent_type, &query); + type_info->class_size = query.class_size; + type_info->instance_size = query.instance_size; + + gtype = g_type_register_static(parent_type, + "TestType", type_info, 0); + if (!gtype) + g_error("Could not register TestType"); } + return gtype; } #define TYPE_TEST (test_type_get_type()) -static PyObject * -_wrap_get_tp_basicsize (PyObject * self, PyObject * args) -{ - PyObject *item = PyTuple_GetItem(args, 0); - return PyInt_FromLong(((PyTypeObject*)item)->tp_basicsize); -} - static PyObject * _wrap_get_test_thread (PyObject * self) { @@ -81,7 +55,6 @@ _wrap_get_unknown (PyObject * self) GObject *obj; obj = g_object_new (TEST_TYPE_UNKNOWN, NULL); return pygobject_new(obj); - } static PyObject * @@ -101,14 +74,13 @@ _wrap_test_g_object_new (PyObject * self) GObject *obj; PyObject *rv; - obj = g_object_new(py_label_get_type(), NULL); + obj = g_object_new(g_type_from_name("PyLabel"), NULL); rv = PyInt_FromLong(obj->ref_count); /* should be == 2 at this point */ g_object_unref(obj); return rv; } static PyMethodDef testhelper_methods[] = { - { "get_tp_basicsize", _wrap_get_tp_basicsize, METH_VARARGS }, { "get_test_thread", (PyCFunction)_wrap_get_test_thread, METH_NOARGS }, { "get_unknown", (PyCFunction)_wrap_get_unknown, METH_NOARGS }, { "create_test_type", (PyCFunction)_wrap_create_test_type, METH_NOARGS }, @@ -138,27 +110,7 @@ PyTypeObject PyTestInterface_Type = { (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 */ - 0, /* tp_weaklistoffset */ - (getiterfunc)0, /* tp_iter */ - (iternextfunc)0, /* tp_iternext */ - 0, /* tp_methods */ - 0, /* tp_members */ - 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 */ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, }; void @@ -172,7 +124,8 @@ inittesthelper () d = PyModule_GetDict(m); - pyg_register_interface(d, "Interface", TEST_TYPE_INTERFACE, &PyTestInterface_Type); + pyg_register_interface(d, "Interface", TEST_TYPE_INTERFACE, + &PyTestInterface_Type); }