[pygobject] Fix handling of unicode for GtkTreeModels
- From: Martin Pitt <martinpitt src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pygobject] Fix handling of unicode for GtkTreeModels
- Date: Mon, 17 Jan 2011 16:53:51 +0000 (UTC)
commit 7a0548dbfbdfe481f75315b6bc7824a9f1a8a87b
Author: Martin Pitt <martin pitt ubuntu com>
Date: Mon Jan 17 17:52:15 2011 +0100
Fix handling of unicode for GtkTreeModels
The code previously converted an unicode object into unicode, but in Python 2.X
models actually have to specify 'str' (GTK expects a gchararray). So encode to
UTF-8 instead to get what GTK expects.
Add corresponding test case.
gi/overrides/Gtk.py | 2 +-
tests/test_overrides.py | 12 +++++++++++-
2 files changed, 12 insertions(+), 2 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index 22ab382..efc3d33 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -707,7 +707,7 @@ class TreeModel(Gtk.TreeModel):
value = str(value)
elif sys.version_info < (3, 0):
if isinstance(value, unicode):
- value = unicode(value)
+ value = value.encode('UTF-8')
else:
raise ValueError('Expected string or unicode for row %i but got %s%s' % (i, value, type(value)))
else:
diff --git a/tests/test_overrides.py b/tests/test_overrides.py
index ffa2517..08930af 100644
--- a/tests/test_overrides.py
+++ b/tests/test_overrides.py
@@ -398,7 +398,7 @@ class TestGtk(unittest.TestCase):
test_pylist = [1,"2", "3"]
list_store = Gtk.ListStore(int, str, 'GIOverrideTreeAPITest', object, object, object)
- for i in range(95):
+ for i in range(94):
label = 'this is row #%d' % i
testobj = TestGtk.TestClass(self, i, label)
parent = list_store.append((i,
@@ -408,6 +408,16 @@ class TestGtk(unittest.TestCase):
test_pydict,
test_pylist))
+ # test automatic unicode->str conversion
+ i = 94
+ label = u'this is row #94'
+ treeiter = list_store.append((i,
+ label,
+ TestGtk.TestClass(self, i, label),
+ test_pyobj,
+ test_pydict,
+ test_pylist))
+
# add sorted items out of order to test insert* apis
i = 97
label = 'this is row #97'
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]