Solved [Python] Confusion about unicode implementation in xbmcaddon Addon class
#1
I am currently processing some bug reports and faced a unicode-problem in my addon.
I use the xbmcaddon python module and its Addon class to get data and settings for the addon as recommended.
In the code documentation it is stated, that getSetting and getLocalizedString both return unicode strings. This is for Kodi 16 (jarvis), but I did not find one for Kodi 17 (krypton).

First thing I am not sure about:
I use Kodi 17.6, and still use getString probably as a relict from the past. The method getLocalizedString does not seem to exist.
Quote:AttributeError: 'module' object has no attribute 'getLocalizedString'

Second thing: I used both getString and getSetting in the same way, but getSetting seems to only return "normal" strings and getString returns unicode strings (which are a different python type).
I checked the variables in the python debugger in Eclipse. The German characters Ä,ä,ö,ü,ß are not standard ASCII.
python:
_Addon = xbmcaddon.Addon(u'service.watchedlist')
_Addon.getSetting("mysql_pass")
str: watchedlistäöüß
utils.getString(30022)
unicode: Gesehen-Änderungen laufend überwachen

My solution is to use .decode('utf-8') on all values returned by getSetting that contain user entered strings like directories.
This was no problem until I found out that I forgot the decode on mysql passwords that a user entered in kyrillic.

So my question is:
Is the decode-approach correct or is there another best practice? Or is this just an inaccuracy in the documentation?
Reply
#2
you can find the python documentation for kodi v17 here:
https://codedocs.xyz/xbmc/xbmc/group__python.html

as for getString vs getLocalizedString perhaps you need to have another look at your code ;-)
https://github.com/SchapplM/xbmc-addon-s...py#L70-L73

i think you are correct getSetting() does not return unicode, so decoding it would be the right solution.
Do not PM or e-mail Team-Kodi members directly asking for support.
Always read the Forum rules, Kodi online-manual, FAQ, Help and Search the forum before posting.
Reply
#3
(2017-12-26, 14:52)ronie Wrote: as for getString vs getLocalizedString perhaps you need to have another look at your code ;-)
https://github.com/SchapplM/xbmc-addon-s...py#L70-L73
indeed :-D
(edit: utils.getString is just a custom wrapper for Addon.getLocalizedString that I forgot about and did not see when posting the question.)

Thanks for the quick reply. Then I will just do the .decode directly in utils.getSetting.
Reply
#4
You are obviously mixing up methods from Kodi Python API and some third-party library or addon because I don't know what getString is. But you are correct: Addon.getSetting() returns a byte string (Python 2 "str" type) and Addon.getLocalizedString() returns a Unicode string (Python 2 "unicode" type).
Also, your assumption is correct too: textual data should be processed as unicode meaning that all byte strings should be decoded to unicode strings. There shouldn't be any mixing of byte and unicode strings in your code ("str" and "unicode" types). Unfortunately, Python 2 allows such mixing that makes it difficult to spot such mix-up. Also, current Kodi Python API accepts/returns byte and unicode strings inconsistently, that may potentially create huge problems for planned migration of addon subsystem to Python 3.
So my general recommendations are:
- Process your textual data only as unicode strings.
- Learn Kodi Python API and other libraries that you are using to know which string types their functions/methods accept and return so that to apply .encode() and .decode() when necessary. Unfortunately, there is no reliable information about which Kodi Python API functions/methods accept/return which string types, so the only reliable method is trial and error. Using a debugger also helps.
Reply

Logout Mark Read Team Forum Stats Members Help
[Python] Confusion about unicode implementation in xbmcaddon Addon class0