diff options
author | Johan Dahlin <johan@src.gnome.org> | 2005-07-05 20:38:56 +0000 |
---|---|---|
committer | Johan Dahlin <johan@src.gnome.org> | 2005-07-05 20:38:56 +0000 |
commit | 8914f109dc0515d8927c76b29ec5b46317965b68 (patch) | |
tree | 682f2eb1919ba86d87a98d14a22616e201a12552 | |
parent | 4dd8caf2aae815a22e07f035839bf43739d03feb (diff) | |
download | pygobject-8914f109dc0515d8927c76b29ec5b46317965b68.tar.gz pygobject-8914f109dc0515d8927c76b29ec5b46317965b68.tar.xz pygobject-8914f109dc0515d8927c76b29ec5b46317965b68.zip |
Add GType.is_a and deprecate gobject.type_* Update tests and make
* gobject/gobjectmodule.c: (pyg_type_name), (pyg_type_from_name),
(pyg_type_parent), (pyg_type_is_a), (pyg_type_children),
(pyg_type_interfaces), (get_type_name_for_class), (initgobject):
* gobject/pygtype.c: (_wrap_g_type_is_a), (pyg_type_wrapper_init):
* tests/test_enum.py:
* tests/test_gtype.py:
* tests/test_unknown.py:
Add GType.is_a and deprecate gobject.type_*
Update tests
and make GType.is_a/gobject.type_is_a return a bool instead of int
-rw-r--r-- | gobject/gobjectmodule.c | 39 | ||||
-rw-r--r-- | gobject/pygtype.c | 43 | ||||
-rw-r--r-- | tests/test_enum.py | 23 | ||||
-rw-r--r-- | tests/test_gtype.py | 22 | ||||
-rw-r--r-- | tests/test_unknown.py | 7 |
5 files changed, 102 insertions, 32 deletions
diff --git a/gobject/gobjectmodule.c b/gobject/gobjectmodule.c index e24313e..d054635 100644 --- a/gobject/gobjectmodule.c +++ b/gobject/gobjectmodule.c @@ -238,6 +238,11 @@ pyg_type_name (PyObject *self, PyObject *args) GType type; const gchar *name; + if (PyErr_Warn(PyExc_DeprecationWarning, + "gobject.type_name is deprecated; " + "use GType.name instead")) + return NULL; + if (!PyArg_ParseTuple(args, "O:gobject.type_name", >ype)) return NULL; if ((type = pyg_type_from_object(gtype)) == 0) @@ -255,6 +260,11 @@ pyg_type_from_name (PyObject *self, PyObject *args) const gchar *name; GType type; + if (PyErr_Warn(PyExc_DeprecationWarning, + "gobject.type_from_name is deprecated; " + "use GType.from_name instead")) + return NULL; + if (!PyArg_ParseTuple(args, "s:gobject.type_from_name", &name)) return NULL; type = g_type_from_name(name); @@ -270,6 +280,11 @@ pyg_type_parent (PyObject *self, PyObject *args) PyObject *gtype; GType type, parent; + if (PyErr_Warn(PyExc_DeprecationWarning, + "gobject.type_parent is deprecated; " + "use GType.parent instead")) + return NULL; + if (!PyArg_ParseTuple(args, "O:gobject.type_parent", >ype)) return NULL; if ((type = pyg_type_from_object(gtype)) == 0) @@ -287,13 +302,18 @@ pyg_type_is_a (PyObject *self, PyObject *args) PyObject *gtype, *gparent; GType type, parent; + if (PyErr_Warn(PyExc_DeprecationWarning, + "gobject.type_is_a is deprecated; " + "use GType.is_a instead")) + return NULL; + if (!PyArg_ParseTuple(args, "OO:gobject.type_is_a", >ype, &gparent)) return NULL; if ((type = pyg_type_from_object(gtype)) == 0) return NULL; if ((parent = pyg_type_from_object(gparent)) == 0) return NULL; - return PyInt_FromLong(g_type_is_a(type, parent)); + return PyBool_FromLong(g_type_is_a(type, parent)); } static PyObject * @@ -303,6 +323,11 @@ pyg_type_children (PyObject *self, PyObject *args) GType type, *children; guint n_children, i; + if (PyErr_Warn(PyExc_DeprecationWarning, + "gobject.type_children is deprecated; " + "use GType.children instead")) + return NULL; + if (!PyArg_ParseTuple(args, "O:gobject.type_children", >ype)) return NULL; if ((type = pyg_type_from_object(gtype)) == 0) @@ -329,6 +354,11 @@ pyg_type_interfaces (PyObject *self, PyObject *args) GType type, *interfaces; guint n_interfaces, i; + if (PyErr_Warn(PyExc_DeprecationWarning, + "gobject.type_interfaces is deprecated; " + "use GType.interfaces instead")) + return NULL; + if (!PyArg_ParseTuple(args, "O:gobject.type_interfaces", >ype)) return NULL; if ((type = pyg_type_from_object(gtype)) == 0) @@ -962,7 +992,7 @@ get_type_name_for_class(PyTypeObject *class) gint i, name_serial; char name_serial_str[16]; PyObject *module; - char *type_name = NULL; + char *type_name; /* make name for new GType */ name_serial = 1; @@ -2512,8 +2542,6 @@ DL_EXPORT(void) initgobject(void) { PyObject *m, *d, *o, *tuple; - - PyGTypeWrapper_Type.ob_type = &PyType_Type; PyGParamSpec_Type.ob_type = &PyType_Type; m = Py_InitModule("gobject", pygobject_functions); @@ -2532,8 +2560,7 @@ initgobject(void) pyginterface_info_key = g_quark_from_static_string("PyGInterface::info"); pygpointer_class_key = g_quark_from_static_string("PyGPointer::class"); - PyType_Ready(&PyGTypeWrapper_Type); - PyDict_SetItemString(d, "GType", (PyObject *)&PyGTypeWrapper_Type); + REGISTER_TYPE(d, PyGTypeWrapper_Type, "GType"); PY_TYPE_OBJECT = g_boxed_type_register_static("PyObject", pyobject_copy, diff --git a/gobject/pygtype.c b/gobject/pygtype.c index 4e5bd33..843060f 100644 --- a/gobject/pygtype.c +++ b/gobject/pygtype.c @@ -215,6 +215,20 @@ _wrap_g_type_from_name(PyGTypeWrapper *_, PyObject *args) return pyg_type_wrapper_new(type); } +static PyObject* +_wrap_g_type_is_a(PyGTypeWrapper *self, PyObject *args) +{ + PyObject *gparent; + GType parent; + + if (!PyArg_ParseTuple(args, "O:GType.is_a", &gparent)) + return NULL; + else if ((parent = pyg_type_from_object(gparent)) == 0) + return NULL; + + return PyBool_FromLong(g_type_is_a(self->type, parent)); +} + static PyMethodDef _PyGTypeWrapper_methods[] = { { "is_interface", (PyCFunction)_wrap_g_type_is_interface, METH_NOARGS }, { "is_classed", (PyCFunction)_wrap_g_type_is_classed, METH_NOARGS }, @@ -226,9 +240,30 @@ static PyMethodDef _PyGTypeWrapper_methods[] = { { "is_value_type", (PyCFunction)_wrap_g_type_is_value_type, METH_NOARGS }, { "has_value_table", (PyCFunction)_wrap_g_type_has_value_table, METH_NOARGS }, { "from_name", (PyCFunction)_wrap_g_type_from_name, METH_VARARGS | METH_STATIC }, + { "is_a", (PyCFunction)_wrap_g_type_is_a, METH_VARARGS }, { NULL, 0, 0 } }; +static int +pyg_type_wrapper_init(PyGTypeWrapper *self, PyObject *args, PyObject *kwargs) +{ + static char *kwlist[] = { "object", NULL }; + PyObject *py_object; + GType type; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O:GType.__init__", + kwlist, &py_object)) + return -1; + + if (!(type = pyg_type_from_object(py_object))) + return -1; + + self->type = type; + + return 0; +} + PyTypeObject PyGTypeWrapper_Type = { PyObject_HEAD_INIT(NULL) 0, @@ -260,7 +295,13 @@ PyTypeObject PyGTypeWrapper_Type = { (iternextfunc)0, _PyGTypeWrapper_methods, 0, - _PyGTypeWrapper_getsets + _PyGTypeWrapper_getsets, + NULL, + NULL, + (descrgetfunc)0, + (descrsetfunc)0, + 0, + (initproc)pyg_type_wrapper_init }; /** diff --git a/tests/test_enum.py b/tests/test_enum.py index 052a5e9..45bc0dc 100644 --- a/tests/test_enum.py +++ b/tests/test_enum.py @@ -1,11 +1,12 @@ import unittest import warnings +from gobject import GEnum, GFlags, GObject, GType from common import gobject, atk, pango, gtk, gdk class EnumTest(unittest.TestCase): def testEnums(self): - assert issubclass(gobject.GEnum, int) + assert issubclass(GEnum, int) assert isinstance(atk.LAYER_OVERLAY, atk.Layer) assert isinstance(atk.LAYER_OVERLAY, int) assert 'LAYER_OVERLAY' in repr(atk.LAYER_OVERLAY) @@ -43,24 +44,24 @@ class EnumTest(unittest.TestCase): assert 'WINDOW_TOPLEVEL' in repr(wtype) def testAtkObj(self): - obj = atk.NoOpObject(gobject.GObject()) + obj = atk.NoOpObject(GObject()) assert obj.get_role() == atk.ROLE_INVALID def testGParam(self): win = gtk.Window() - enums = filter(lambda x: gobject.type_is_a(x.value_type, gobject.GEnum), + enums = filter(lambda x: GType.is_a(x.value_type, GEnum), gobject.list_properties(win)) assert enums enum = enums[0] assert hasattr(enum, 'enum_class') - assert issubclass(enum.enum_class, gobject.GEnum) + assert issubclass(enum.enum_class, GEnum) def testWeirdEnumValues(self): assert int(gdk.NOTHING) == -1 assert int(gdk.BUTTON_PRESS) == 4 def testParamSpec(self): - props = filter(lambda prop: gobject.type_is_a(prop.value_type, gobject.GEnum), + props = filter(lambda prop: GType.is_a(prop.value_type, GEnum), gobject.list_properties(gtk.Window)) assert len(props)>= 6 props = filter(lambda prop: prop.name == 'type', props) @@ -81,7 +82,7 @@ class EnumTest(unittest.TestCase): class FlagsTest(unittest.TestCase): def testFlags(self): - assert issubclass(gobject.GFlags, int) + assert issubclass(GFlags, int) assert isinstance(gdk.BUTTON_PRESS_MASK, gdk.EventMask) assert isinstance(gdk.BUTTON_PRESS_MASK, int) assert gdk.BUTTON_PRESS_MASK == 256 @@ -89,7 +90,7 @@ class FlagsTest(unittest.TestCase): assert gdk.BUTTON_PRESS_MASK != -256 assert gdk.BUTTON_PRESS_MASK != gdk.BUTTON_RELEASE_MASK - assert gdk.EventMask.__bases__[0] == gobject.GFlags + assert gdk.EventMask.__bases__[0] == GFlags assert len(gdk.EventMask.__flags_values__) == 22 def testComparisionWarning(self): @@ -104,19 +105,19 @@ class FlagsTest(unittest.TestCase): def testFlagOperations(self): a = gdk.BUTTON_PRESS_MASK - assert isinstance(a, gobject.GFlags) + assert isinstance(a, GFlags) assert a.first_value_name == 'GDK_BUTTON_PRESS_MASK' assert a.first_value_nick == 'button-press-mask' assert a.value_names == ['GDK_BUTTON_PRESS_MASK'], a.value_names assert a.value_nicks == ['button-press-mask'], a.value_names b = gdk.BUTTON_PRESS_MASK | gdk.BUTTON_RELEASE_MASK - assert isinstance(b, gobject.GFlags) + assert isinstance(b, GFlags) assert b.first_value_name == 'GDK_BUTTON_PRESS_MASK' assert b.first_value_nick == 'button-press-mask' assert b.value_names == ['GDK_BUTTON_PRESS_MASK', 'GDK_BUTTON_RELEASE_MASK'] assert b.value_nicks == ['button-press-mask', 'button-release-mask'] c = gdk.BUTTON_PRESS_MASK | gdk.BUTTON_RELEASE_MASK | gdk.ENTER_NOTIFY_MASK - assert isinstance(c, gobject.GFlags) + assert isinstance(c, GFlags) assert c.first_value_name == 'GDK_BUTTON_PRESS_MASK' assert c.first_value_nick == 'button-press-mask' assert c.value_names == ['GDK_BUTTON_PRESS_MASK', 'GDK_BUTTON_RELEASE_MASK', @@ -144,7 +145,7 @@ class FlagsTest(unittest.TestCase): warnings.resetwarnings() def testParamSpec(self): - props = filter(lambda x: gobject.type_is_a(x.value_type, gobject.GFlags), + props = filter(lambda x: GType.is_a(x.value_type, GFlags), gtk.container_class_list_child_properties(gtk.Table)) assert len(props) >= 2 klass = props[0].flags_class diff --git a/tests/test_gtype.py b/tests/test_gtype.py index bdaa2d1..eb89a87 100644 --- a/tests/test_gtype.py +++ b/tests/test_gtype.py @@ -1,28 +1,28 @@ import unittest -from common import gobject +from gobject import GType +from common import gobject, gtk class GTypeTest(unittest.TestCase): def checkType(self, expected, *objects): # First, double check so we get back what we sent - str = gobject.type_name(expected) # pyg_type_from_object - val = gobject.type_from_name(str) # pyg_type_wrapper_new - assert val == expected, \ - 'got %r while %r was expected' % (val, expected) + str = GType(expected).name # pyg_type_from_object + val = GType.from_name(str) # pyg_type_wrapper_new + self.assertEqual(val, expected, + 'got %r while %r was expected' % (val, expected)) # Then test the objects for object in objects: - str = gobject.type_name(expected) - val = gobject.type_from_name(str) - assert val == expected, \ - 'got %r while %r was expected' % (val, expected) + val = GType.from_name(GType(expected).name) + self.assertEqual(val, expected, + 'got %r while %r was expected' % + (val, expected)) def testBool(self): self.checkType(gobject.TYPE_BOOLEAN, 'gboolean', bool) def testInt(self): self.checkType(gobject.TYPE_INT, 'gint', int) - import gtk model = gtk.ListStore(str, int) iter = model.append() model.set(iter, 1, 100000000) @@ -61,7 +61,7 @@ class MyObject(gobject.GObject): class TypeNameTest(unittest.TestCase): def testTypeName(self): - self.assertEqual(gobject.type_name(MyObject), 'MyObject') + self.assertEqual(GType(MyObject).name, 'MyObject') if __name__ == '__main__': unittest.main() diff --git a/tests/test_unknown.py b/tests/test_unknown.py index 841a0fb..49e8c40 100644 --- a/tests/test_unknown.py +++ b/tests/test_unknown.py @@ -1,14 +1,15 @@ import unittest +from gobject import GType, new from common import gobject, testhelper -TestInterface = gobject.type_from_name('TestInterface') +TestInterface = GType.from_name('TestInterface') class TestUnknown(unittest.TestCase): def testFoo(self): obj = testhelper.get_unknown() - TestUnknownGType = gobject.type_from_name('TestUnknown') - TestUnknown = gobject.new(TestUnknownGType).__class__ + TestUnknownGType = GType.from_name('TestUnknown') + TestUnknown = new(TestUnknownGType).__class__ assert isinstance(obj, testhelper.Interface) assert isinstance(obj, TestUnknown) |