Ticket #435 (new Enhancement)
Python bindings don't support unicode
| Reported by: | eternalmoonlight | Owned by: | billiob |
|---|---|---|---|
| Priority: | Major | Milestone: | |
| Component: | python-bindings | Keywords: | |
| Cc: | Blocked By: | ||
| Blocking: |
Description
Elementary (and probably the rest of EFL) expects that all text passed to the API is represented by UTF-8 byte strings. For example, you can't pass an unicode to elementary.Label.set_label. You need to do label.set_label(uni_text.encode('utf-8')).
The best way to deal with unicode in a Python program is to use the unicode type everywhere, and it's a great discomfort when your GUI API doesn't support it, and you have to manually encode everything at the output, or wrap the whole API yourself.
Attachments
Change History
comment:1 Changed 2 years ago by billiob
- Owner changed from raster to billiob
- Component changed from elementary to python-bindings
Changed 12 months ago by Ulrich Eckhardt <doomster@…>
-
attachment
python-evas-unicode-strings-2011-05-15.patch
added
Patches to unittests
comment:2 Changed 12 months ago by Ulrich Eckhardt <doomster@…>
See attached patch to the unit tests for how I would like this to behave. There are two goals:
- Unicode strings are transparently encoded as UTF-8 before being passed on.
- The string read back is the same as the one that was set.
The first goal depends on whether the C-bindings really expect UTF-8. While it works here and is reasonable to expect in modern systems, there is actually no place in Evas.h where I could find this explicitly stated. This lack of documentation might be worth another TRAC ticket.
The second goal means that the result type of text_get() depends on the previous text_set() argument and that it is not always the 'str' type as currently documented. This is not so easy to achieve, as the original Python text is not stored anywhere currently. If text_set and text_get are the only ways to access the text, I would simply store the text as a field in the in the Python object.
def text_get(self):
return self._text
def text_set(self, value):
"Change text to be used"
if isinstance(value, str):
evas_object_text_text_set(self.obj, value)
elif isinstance(value, unicode):
evas_object_text_text_set(self.obj, value.encode("UTF-8"))
else:
raise TypeError("expected str or unicode object")
self._text = value
This is how I would do this, but I'm guessing how the Cython syntax is supposed to be. I also haven't tried this yet, so don't expect working and stable code here.
comment:3 Changed 12 months ago by billiob
I've committed your patch as rev 60101. Maybe we should discuss that matter on the mailing list.
comment:4 Changed 7 months ago by TAsn
We should adjust the bindings to check the parameter passed...
If the parameter is a String, just pass it forward.
If it's a unicode string, convert to UTF-8 and pass it forward.
We shouldn't force either, python is all about letting people have fun without needing to .encode("UTF-8") everything.
So I agree with Ulrich, we should do it in the bindings.
