2020-04-16, 23:32
Hello,
First of all this is my first post in this forum so hello Kodi's community!
I have been banging my head on this for hours now so I give up and kindly ask for your help...
I thought I understood the tricky handling of byte and unicode strings in python but now I am completly lost.
Here is the code that is driving me nuts:
When I run this code with python 2.7.10 on MacOS I get this:
This is the expected behavior according to me because as you can see "s" is a unicode string so from what I understood there are 2 safe ways to print it using "format()":
But if I run the same code in my add-on I get this:
I just don't understand why I get this UnicodeEncodeError exception here.
I am using Kodi Leia v18.6 on MacOS (so python 2 is used) and it uses xbmc.python v2.26.0.
Also please note that the behavior is the same with xbmc.log() instead of print. I used print here just to shorten the code.
Do you have any idea what is wrong here?
Thank you in advance for your help!
First of all this is my first post in this forum so hello Kodi's community!
I have been banging my head on this for hours now so I give up and kindly ask for your help...
I thought I understood the tricky handling of byte and unicode strings in python but now I am completly lost.
Here is the code that is driving me nuts:
python:
# -*- coding: utf-8 -*-
if __name__ == "__main__":
s = u"string with àccénts"
print(u"Type of s = {}".format(type(s)))
print("s with repr = {}".format(repr(s)))
print("s with encode = {}".format(s.encode("utf-8")))
print(u"s = {}".format(s))
When I run this code with python 2.7.10 on MacOS I get this:
Code:
Type of s = <type 'unicode'>
s with repr = u'string with \xe0cc\xe9nts'
s with encode = string with àccénts
s = string with àccénts
This is the expected behavior according to me because as you can see "s" is a unicode string so from what I understood there are 2 safe ways to print it using "format()":
- Convert the string containing the literal to unicode with "u": print(u"s = {}".format(s))
- Use repr(): print("s with repr = {}".format(repr(s)))
But if I run the same code in my add-on I get this:
Code:
DEBUG: Type of s = <type 'unicode'>
DEBUG: s with repr = u'string with \xe0cc\xe9nts'
DEBUG: s with encode = string with àccénts
ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
- NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
Error Type: <type 'exceptions.UnicodeEncodeError'>
Error Contents: 'ascii' codec can't encode character u'\xe0' in position 16: ordinal not in range(128)
Traceback (most recent call last):
File "<my_plugin_path>/main.py", line 39, in <module>
print(u"s = {}".format(s))
File "<string>", line 7, in write
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe0' in position 16: ordinal not in range(128)
-->End of Python script error report<--
I just don't understand why I get this UnicodeEncodeError exception here.
I am using Kodi Leia v18.6 on MacOS (so python 2 is used) and it uses xbmc.python v2.26.0.
Also please note that the behavior is the same with xbmc.log() instead of print. I used print here just to shorten the code.
Do you have any idea what is wrong here?
Thank you in advance for your help!