[pygobject] Simplify overrides and tests using the new GObject.Value override
- From: Martin Pitt <martinpitt src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pygobject] Simplify overrides and tests using the new GObject.Value override
- Date: Mon, 14 Jan 2013 09:44:11 +0000 (UTC)
commit 9cfba517e1a6dced5e66786b28ed5e101b7b4a29
Author: Martin Pitt <martinpitt gnome org>
Date: Mon Jan 14 10:36:36 2013 +0100
Simplify overrides and tests using the new GObject.Value override
The previous commit added support for constructing a GObject.Value with a given
GType and Python object conversion. Use this to simplify the Gtk override and
the tests that construct GValues.
See https://bugzilla.gnome.org/show_bug.cgi?id=677473
gi/overrides/Gtk.py | 88 ++----------------------------------------
tests/test_gi.py | 26 ++++---------
tests/test_overrides_gtk.py | 2 +-
tests/test_signal.py | 12 +----
4 files changed, 17 insertions(+), 111 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index 281a680..9618110 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -872,89 +872,11 @@ class TreeModel(Gtk.TreeModel):
self.set_value(treeiter, column, value)
def _convert_value(self, column, value):
- # we may need to convert to a basic type
- type_ = self.get_column_type(column)
-
- # Allow None to be used as an initialized but empty value.
- # https://bugzilla.gnome.org/show_bug.cgi?id=684094
- if value is None:
- value_container = GObject.Value()
- value_container.init(type_)
- return value_container
-
- if type_ == GObject.TYPE_STRING:
- if isinstance(value, str):
- value = str(value)
- elif sys.version_info < (3, 0):
- if isinstance(value, unicode):
- value = value.encode('UTF-8')
- else:
- raise ValueError('Expected string or unicode for column %i but got %s%s' % (column, value, type(value)))
- else:
- raise ValueError('Expected a string for column %i but got %s' % (column, type(value)))
- elif type_ == GObject.TYPE_FLOAT or type_ == GObject.TYPE_DOUBLE:
- if isinstance(value, float):
- value = float(value)
- else:
- raise ValueError('Expected a float for column %i but got %s' % (column, type(value)))
- elif type_ == GObject.TYPE_LONG or type_ == GObject.TYPE_INT:
- if isinstance(value, int):
- value = int(value)
- elif sys.version_info < (3, 0):
- if isinstance(value, long):
- value = long(value)
- else:
- raise ValueError('Expected an long for column %i but got %s' % (column, type(value)))
- else:
- raise ValueError('Expected an integer for column %i but got %s' % (column, type(value)))
- elif type_ == GObject.TYPE_BOOLEAN:
- cmp_classes = [int]
- if sys.version_info < (3, 0):
- cmp_classes.append(long)
-
- if isinstance(value, tuple(cmp_classes)):
- value = bool(value)
- else:
- raise ValueError('Expected a bool for column %i but got %s' % (column, type(value)))
- else:
- # use GValues directly to marshal to the correct type
- # standard object checks should take care of validation
- # so we don't have to do it here
- value_container = GObject.Value()
- value_container.init(type_)
- if type_ == GObject.TYPE_CHAR:
- value_container.set_char(value)
- value = value_container
- elif type_ == GObject.TYPE_UCHAR:
- value_container.set_uchar(value)
- value = value_container
- elif type_ == GObject.TYPE_UNICHAR:
- cmp_classes = [str]
- if sys.version_info < (3, 0):
- cmp_classes.append(unicode)
-
- if isinstance(value, tuple(cmp_classes)):
- value = ord(value[0])
-
- value_container.set_uint(value)
- value = value_container
- elif type_ == GObject.TYPE_UINT:
- value_container.set_uint(value)
- value = value_container
- elif type_ == GObject.TYPE_ULONG:
- value_container.set_ulong(value)
- value = value_container
- elif type_ == GObject.TYPE_INT64:
- value_container.set_int64(value)
- value = value_container
- elif type_ == GObject.TYPE_UINT64:
- value_container.set_uint64(value)
- value = value_container
- elif type_ == GObject.TYPE_PYOBJECT:
- value_container.set_boxed(value)
- value = value_container
-
- return value
+ '''Convert value to a GObject.Value of the expected type'''
+
+ if isinstance(value, GObject.Value):
+ return value
+ return GObject.Value(self.get_column_type(column), value)
def get(self, treeiter, *columns):
n_columns = self.get_n_columns()
diff --git a/tests/test_gi.py b/tests/test_gi.py
index ca6e3c9..80919fe 100644
--- a/tests/test_gi.py
+++ b/tests/test_gi.py
@@ -1223,32 +1223,24 @@ class TestGValue(unittest.TestCase):
def test_gvalue_in(self):
GIMarshallingTests.gvalue_in(42)
- value = GObject.Value()
- value.init(GObject.TYPE_INT)
- value.set_int(42)
+ value = GObject.Value(GObject.TYPE_INT, 42)
GIMarshallingTests.gvalue_in(value)
def test_gvalue_int64_in(self):
- value = GObject.Value()
- value.init(GObject.TYPE_INT64)
- value.set_int64(GObject.G_MAXINT64)
+ value = GObject.Value(GObject.TYPE_INT64, GObject.G_MAXINT64)
GIMarshallingTests.gvalue_int64_in(value)
def test_gvalue_in_with_type(self):
- value = GObject.Value()
- value.init(GObject.TYPE_STRING)
- value.set_string('foo')
+ value = GObject.Value(GObject.TYPE_STRING, 'foo')
GIMarshallingTests.gvalue_in_with_type(value, GObject.TYPE_STRING)
- value = GObject.Value()
- value.init(GIMarshallingTests.Flags.__gtype__)
- value.set_flags(GIMarshallingTests.Flags.VALUE1)
+ value = GObject.Value(GIMarshallingTests.Flags.__gtype__,
+ GIMarshallingTests.Flags.VALUE1)
GIMarshallingTests.gvalue_in_with_type(value, GObject.TYPE_FLAGS)
def test_gvalue_in_enum(self):
- value = GObject.Value()
- value.init(GIMarshallingTests.Enum.__gtype__)
- value.set_enum(GIMarshallingTests.Enum.VALUE3)
+ value = GObject.Value(GIMarshallingTests.Enum.__gtype__,
+ GIMarshallingTests.Enum.VALUE3)
GIMarshallingTests.gvalue_in_enum(value)
def test_gvalue_out(self):
@@ -1262,9 +1254,7 @@ class TestGValue(unittest.TestCase):
def test_gvalue_inout(self):
self.assertEqual('42', GIMarshallingTests.gvalue_inout(42))
- value = GObject.Value()
- value.init(GObject.TYPE_INT)
- value.set_int(42)
+ value = GObject.Value(int, 42)
self.assertEqual('42', GIMarshallingTests.gvalue_inout(value))
def test_gvalue_flat_array_in(self):
diff --git a/tests/test_overrides_gtk.py b/tests/test_overrides_gtk.py
index ddea148..7fa6fe1 100644
--- a/tests/test_overrides_gtk.py
+++ b/tests/test_overrides_gtk.py
@@ -1307,7 +1307,7 @@ class TestTreeModel(unittest.TestCase):
def set_row3():
model[0][:2] = ("0", 0)
- self.assertRaises(ValueError, set_row3)
+ self.assertRaises(TypeError, set_row3)
def test_tree_model_set_value_to_none(self):
# Tests allowing the usage of None to set an empty value on a model.
diff --git a/tests/test_signal.py b/tests/test_signal.py
index 723e411..9ae499d 100644
--- a/tests/test_signal.py
+++ b/tests/test_signal.py
@@ -436,9 +436,7 @@ class _TestCMarshaller:
self.assertEqual(rv, 42)
# explicit float
- v = GObject.Value()
- v.init(GObject.TYPE_FLOAT)
- v.set_float(1.234)
+ v = GObject.Value(GObject.TYPE_FLOAT, 1.234)
rv = self.obj.emit("test-gvalue", v)
self.assertAlmostEqual(rv, 1.234, 4)
@@ -447,9 +445,7 @@ class _TestCMarshaller:
self.assertAlmostEqual(rv, 1.234, 4)
# explicit int64
- v = GObject.Value()
- v.init(GObject.TYPE_INT64)
- v.set_int64(GObject.G_MAXINT64)
+ v = GObject.Value(GObject.TYPE_INT64, GObject.G_MAXINT64)
rv = self.obj.emit("test-gvalue", v)
self.assertEqual(rv, GObject.G_MAXINT64)
@@ -459,9 +455,7 @@ class _TestCMarshaller:
#self.assertEqual(rv, GObject.G_MAXINT64)
# explicit uint64
- v = GObject.Value()
- v.init(GObject.TYPE_UINT64)
- v.set_uint64(GObject.G_MAXUINT64)
+ v = GObject.Value(GObject.TYPE_UINT64, GObject.G_MAXUINT64)
rv = self.obj.emit("test-gvalue", v)
self.assertEqual(rv, GObject.G_MAXUINT64)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]