[pygobject] Convert all strings to utf-8 encoding when retrieving from TreeModel
- From: Sebastian Polsterl <sebp src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pygobject] Convert all strings to utf-8 encoding when retrieving from TreeModel
- Date: Mon, 14 Nov 2011 23:18:24 +0000 (UTC)
commit 654711d0f940d7480d0f1cdb25a3dc9996f7a706
Author: Sebastian PÃlsterl <sebp k-d-w org>
Date: Tue Nov 8 12:38:12 2011 +0100
Convert all strings to utf-8 encoding when retrieving from TreeModel
https://bugzilla.gnome.org/show_bug.cgi?id=663610
gi/overrides/Gtk.py | 15 +++++++++++++++
tests/compathelper.py | 2 ++
tests/test_overrides.py | 31 ++++++++++++++++++++++++++++++-
3 files changed, 47 insertions(+), 1 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index 7945cc8..e0d038a 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -797,6 +797,13 @@ class TreeModel(Gtk.TreeModel):
self.set_value(treeiter, i, value)
+ def _decode_value(self, treeiter, column, value):
+ type_ = self.get_column_type(column)
+ if type_ == GObject.TYPE_STRING and sys.version_info < (3, 0):
+ value = value.decode('UTF-8')
+
+ return value
+
def _convert_value(self, treeiter, column, value):
if value is None:
return
@@ -965,6 +972,10 @@ class ListStore(Gtk.ListStore, TreeModel, TreeSortable):
value = self._convert_value(treeiter, column, value)
Gtk.ListStore.set_value(self, treeiter, column, value)
+ def get_value(self, treeiter, column):
+ value = Gtk.ListStore.get_value(self, treeiter, column)
+ return self._decode_value(treeiter, column, value)
+
def set(self, treeiter, *args):
def _set_lists(columns, values):
@@ -1180,6 +1191,10 @@ class TreeStore(Gtk.TreeStore, TreeModel, TreeSortable):
value = self._convert_value(treeiter, column, value)
Gtk.TreeStore.set_value(self, treeiter, column, value)
+ def get_value(self, treeiter, column):
+ value = Gtk.TreeStore.get_value(self, treeiter, column)
+ return self._decode_value(treeiter, column, value)
+
def set(self, treeiter, *args):
def _set_lists(columns, values):
diff --git a/tests/compathelper.py b/tests/compathelper.py
index 2465747..b061335 100644
--- a/tests/compathelper.py
+++ b/tests/compathelper.py
@@ -62,8 +62,10 @@ if sys.version_info >= (3, 0):
'''
_unicode = lambda s: str(s)
+ _unichr = chr
else:
_long = long
_basestring = basestring
_bytes = str
_unicode = lambda s: unicode(s, 'UTF-8')
+ _unichr = unichr
diff --git a/tests/test_overrides.py b/tests/test_overrides.py
index 8974273..e875d30 100644
--- a/tests/test_overrides.py
+++ b/tests/test_overrides.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
# -*- Mode: Python; py-indent-offset: 4 -*-
# vim: tabstop=4 shiftwidth=4 expandtab
@@ -7,7 +8,7 @@ import sys
import os
sys.path.insert(0, "../")
-from compathelper import _long, _unicode, _bytes
+from compathelper import _long, _unicode, _bytes, _unichr
from gi.repository import GLib
from gi.repository import GObject
@@ -1261,6 +1262,34 @@ class TestGtk(unittest.TestCase):
model[0] = (3, "three", -0.3)
+ def test_tree_model_unicode(self):
+ model = Gtk.ListStore(str)
+
+ text = (_unicode("just\na\tstring"),
+ _unicode("Tr") + _unichr(0xf6) + _unicode("del"),
+ _unicode("N") + _unichr(0xe1) + _unicode("m") + _unichr(0xe8),
+ _unichr(0xff) + _unichr(0xdf) + _unicode("er"),
+ _unichr(0x152) + _unichr(0x180) + _unichr(0x1d25))
+
+ for item in text:
+ model.append([item])
+
+ def byte_compare(have, expected):
+ if sys.version_info < (3, 0):
+ self.assertTrue(isinstance(have, unicode))
+ else:
+ self.assertTrue(isinstance(have, str))
+
+ self.assertEqual(len(have), len(expected))
+ for i in range(len(expected)):
+ a = have[i]
+ b = expected[i]
+ self.assertEqual(a, b)
+ self.assertEqual(ord(a), ord(b))
+
+ for i in range(len(model)):
+ byte_compare(model[i][0], text[i])
+
def test_tree_row_slice(self):
model = Gtk.ListStore(int, str, float)
model.append([1, "one", -0.1])
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]