Unicode error on an Android Python 3 app

169 views
Skip to first unread message

Tyler

unread,
Oct 30, 2017, 2:46:40 PM10/30/17
to Kivy users support
Hi, I've been making an android app in python 3.5 and it's nearing completion but there's one problem that I'm having trouble with.
The app is partially text based and there're few screen manager screens that have a top navigation bar with a Label embedded  inside it and few buttons below. Text within the label/buttons contains few words with some diacritic signs (such as ščćđž).
The issue is that if I compile the app via buildozer and python3crystax I get the following Unicode error and the app crashes on startup.

UnicodeDecodeError: "ascii' codec can't decode byte 0xc5 in position 793: ordinal not in range(128)"

On the other had if I replace those characters with non diacritics the app builds and runs fine. In contrast to that if the build is made with python 2.7  instead of 3 even diacritic signs in the mentioned label/buttons work.
Interesting part is that that the full app gets some text from a database and inserts it in Recycle View buttons and a Page Layout, those contain several hundred words with diacritics and work/render perfectly fine in both python 2 and 3.
As far as I understand the Button widget inherits from the Label so I don't know why some Labels/Buttons  work and other not.

I've attached a small excerpt that depicts the problem, issue happens with both custom and default font.

How would one go about fixing this? and why does the error happen in the first place?

I'd really appreciate some help

Thanks




test.zip

Alexander Taylor

unread,
Oct 30, 2017, 7:51:11 PM10/30/17
to Kivy users support
I wonder if this is an encoding problem when the file is first read - I'm fairly sure that unicode should work in general (I've tested this in my own apps, and it's consistent with your note about text loaded from a database), but this is separate to the actual initial evaluation of the file.

I don't have any immediate ideas, but I'll try to test this and see if I can reproduce it. I suppose for now a workaround could be to load the text from a different file, e.g. as json or plain txt.

Geekademy

unread,
Oct 30, 2017, 11:00:45 PM10/30/17
to kivy-...@googlegroups.com
Bug says Windows, but it happens to me on Android as well:

https://github.com/kivy/kivy/issues/4003#issuecomment-334905146

There should be an encoding parameter on load_file, and it's ridiculous that it
defaults to ascii on Python 3!

Here's my workaround:

with open(filename, encoding='utf8') as f:
Builder.load_string(f.read())

(Make sure to disable the auto load for the .kv file, or the error will still be
thrown and make it look like this didn't work.)

ZenCODE

unread,
Oct 31, 2017, 1:43:12 AM10/31/17
to Kivy users support
Please post more of the error i.e. the part that says the line number and file the error occurs in.

ZenCODE

unread,
Oct 31, 2017, 3:03:35 AM10/31/17
to Kivy users support
Does this PR perhaps fix it?

https://github.com/kivy/buildozer/pull/582

Tyler

unread,
Oct 31, 2017, 6:59:24 AM10/31/17
to Kivy users support
Thanks everyone for responding so promptly!

@Geekademy: I've searched for the issue but somehow missed that thread, seems like this one is a duplicate :)  But your workaround works great, I did as instructed and the app ran smoothly on python 3, just had to put the code in the build method otherwise it wouldn't recognize some of the custom classes. Thank you very much for helping!

Tyler

unread,
Oct 31, 2017, 7:12:08 AM10/31/17
to kivy-...@googlegroups.com
@ZenCODE: Thanks for the PR, unfortunately even with the fix the app crashed in the same manner, here's the log:

Traceback (most recent call last):\r\n'b'I/python  ( 1223):    File "main.py", line 26, in <module>\r\nI/python  ( 1223):      TestApp().run()\r\nI/python  ( 1223):    File "/data/data/org.test.uniapp/files/app/crystax_python/site-packages/kivy/app.py", line 801, in run\r\nI/python  ( 1223):      self.load_kv(filename=self.kv_file)\r\n'b'I/python  ( 1223):    File "/data/data/org.test.uniapp/files/app/crystax_python/site-packages/kivy/app.py", line 598, in load_kv\r\n'b'I/python  ( 1223):      root = Builder.load_file(rfilename)\r\nI/python  ( 1223):    File "/data/data/org.test.uniapp/files/app/crystax_python/site-packages/kivy/lang/builder.py", line 290, in load_file\r\n'b'I/python  ( 1223):      data = fd.read()\r\nI/python  ( 1223):    File "/data/data/org.test.uniapp/files/app/crystax_python/stdlib.zip/encodings/ascii.py", line 26, in decode\r\nI/python  ( 1223):  UnicodeDecodeError: \'ascii\' codec can\'t decode byte 0xc5 in position 817: ordinal not in range(128)\r\n'b'I/python
log1.txt

Geekademy

unread,
Nov 1, 2017, 8:01:14 PM11/1/17
to kivy-...@googlegroups.com
The bug happens during the reading of the .kv file at runtime, in
Builder.load_file() though buildozer might have something to do with it, if
unlikely.


load_file assumes the .kv file is in ascii (on python3!) and there's no way to
tell it differently, other than opening it yourself.
Reply all
Reply to author
Forward
0 new messages