diff --git a/lib/python/Components/Input.py b/lib/python/Components/Input.py
index 680e95d..99a9465 100644
--- a/lib/python/Components/Input.py
+++ b/lib/python/Components/Input.py
@@ -62,11 +62,11 @@ class Input(VariableText, HTMLComponent, GUIComponent, NumericalTextInput):
self.currPos = 0
self.Text = u""
else:
- self.Text = text.decode("utf-8", "ignore").decode("utf-8")
+ self.Text = text.decode("utf-8", "ignore")
self.update()
def getText(self):
- return self.Text.encode("utf-8")
+ return self.Text
def createWidget(self, parent):
if self.allmarked:
@@ -152,7 +152,8 @@ class Input(VariableText, HTMLComponent, GUIComponent, NumericalTextInput):
self.update()
def insertChar(self, ch, pos=False, owr=False, ins=False):
- self.Text = self.Text.decode("utf-8", "ignore").decode("utf-8")
+ if isinstance(ch, str):
+ ch = ch.decode("utf-8","ignore")
if not pos:
pos = self.currPos
if ins and not self.maxSize:
@@ -168,15 +169,15 @@ class Input(VariableText, HTMLComponent, GUIComponent, NumericalTextInput):
if not self.maxSize:
self.Text = self.Text[0:pos] + self.Text[pos + 1:]
elif self.overwrite:
- self.Text = self.Text[0:pos] + " " + self.Text[pos + 1:]
+ self.Text = self.Text[0:pos] + u" " + self.Text[pos + 1:]
else:
- self.Text = self.Text[0:pos] + self.Text[pos + 1:] + " "
+ self.Text = self.Text[0:pos] + self.Text[pos + 1:] + u" "
def deleteAllChars(self):
if self.maxSize:
- self.Text = " " * len(self.Text)
+ self.Text = u" " * len(self.Text)
else:
- self.Text = ""
+ self.Text = u""
self.currPos = 0
def tab(self):
@@ -186,7 +187,7 @@ class Input(VariableText, HTMLComponent, GUIComponent, NumericalTextInput):
self.deleteAllChars()
self.allmarked = False
else:
- self.insertChar(" ", self.currPos, False, True);
+ self.insertChar(u" ", self.currPos, False, True);
self.innerright()
self.update()
This patch fixes all encoding issues for me
Explanation:
self.Text = text.decode("utf-8", "ignore").decode("utf-8")
- setText function gets encoded utf-8 string, when we decode it we get unicode, if we again decode it, it will be first implicitly encoded by "ascii" encoding and then decoded to unicode. If we have some special chars which are not in ascii then there it raises UnicodeEncodeError
>>> u"ľ".decode("utf-8")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u013e' in position 0: ordinal not in range(128)
So once we have unicode we don't need to decode it again, since all we get is again unicode, but with implicitly used "ascii" encoding when encoding to string
def insertChar(self, ch, pos=False, owr=False, ins=False):
- self.Text = self.Text.decode("utf-8", "ignore").decode("utf-8")
+ if isinstance(ch, str):
+ ch = ch.decode("utf-8","ignore")
we have to protect input methods, since we are working with unicode, in case we get string, we have to decode it first to unicode
For example VirtualKeyboard Screen is on okClicked passing by InputText.char method string
def getText(self):
- return self.Text.encode("utf-8")
+ return self.Text
VirtualKeyboard is expecting that we pass unicode:
def ok(self):
self.close(self["text"].getText().encode("UTF-8"))
If we pass string, then we are encoding string, which means we are implicitly decoding this string by "ascii" to unicode and then again to string, which could cause UnicodeDecodeError
>>> "č".encode("utf-8")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 0: ordinal not in range(128)
Othes changes are cosmetics. What do you think
Edited by mx3L, 26 February 2015 - 23:46.