summaryrefslogtreecommitdiff
path: root/gi/overrides
diff options
context:
space:
mode:
authorMartin Pitt <martinpitt@gnome.org>2013-01-14 10:36:36 +0100
committerMartin Pitt <martinpitt@gnome.org>2013-01-14 10:43:40 +0100
commit9cfba517e1a6dced5e66786b28ed5e101b7b4a29 (patch)
treef917000ab2ad769f8bc17f62f786c4cd46ea2d67 /gi/overrides
parentf62b98398177991bfdbe0b6753342e79e6cf170a (diff)
downloadpygobject-9cfba517e1a6dced5e66786b28ed5e101b7b4a29.tar.gz
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
Diffstat (limited to 'gi/overrides')
-rw-r--r--gi/overrides/Gtk.py88
1 files changed, 5 insertions, 83 deletions
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index 281a6803..9618110c 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()