Commit 654711d0 authored by Sebastian Pölsterl's avatar Sebastian Pölsterl

Convert all strings to utf-8 encoding when retrieving from TreeModel

https://bugzilla.gnome.org/show_bug.cgi?id=663610
parent 4f637212
......@@ -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):
......
......@@ -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
# -*- 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])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment