Skip to content

open() build-in function don't work as expected  #706

Closed

Description

@Tungsteno74

Hello,
first of all sorry for my bad english, i do not use it often.

Said that, i found the following two possible bugs:

The first concerns the carriage return that on Android is recognizes only the LF and then when meets the other 2 types it give trouble.
So in practice, if you want to create a file directly from Android or on a Linux machine should be no problem, but when you create the file with an OS that uses another kind of carriage return and then transferred to Android system then you will have that problem.

The second problem concerns the special characters as, for example, the "copyright" © which returns the following error traceback:

04-12 23:48:21.160 25138-25158/? I/python: /data/data/opentest.test.opentest/files/page.html
04-12 23:48:21.160 25138-25158/? I/python: -----
04-12 23:48:21.180 25138-25158/? I/python:  Traceback (most recent call last):
04-12 23:48:21.180 25138-25158/? I/python:    File "main.py", line 15, in <module>
04-12 23:48:21.180 25138-25158/? I/python:      mainApp().run()
04-12 23:48:21.180 25138-25158/? I/python:    File "/data/data/opentest.test.opentest/files/lib/python2.7/site-packages/kivy/app.py", line 802, in run
04-12 23:48:21.190 25138-25158/? I/python:      root = self.build()
04-12 23:48:21.190 25138-25158/? I/python:    File "main.py", line 12, in build
04-12 23:48:21.190 25138-25158/? I/python:      open_test(filepath)
04-12 23:48:21.190 25138-25158/? I/python:    File "/data/data/opentest.test.opentest/files/opentest.py", line 20, in open_test
04-12 23:48:21.190 25138-25158/? I/python:      print fli
04-12 23:48:21.190 25138-25158/? I/python:    File "<string>", line 5, in write
04-12 23:48:21.190 25138-25158/? I/python:  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 70238: ordinal not in range(128)
04-12 23:48:21.220 25138-25158/? I/python: Python for android ended.

This problem may be related to this type of implementation of the code http://stackoverflow.com/a/5245904, but in this case the workarounds proposed do not solve (at least in my opinion) the compatibily problems.

Both of the above problems occur only in the Android platform, which instead in others Python implementation that i have tested (windows/linux) are all easily managed.

I used buildozer toolkit to build the following test code:

main.py

from kivy.app import App  
from kivy.uix.button import Button  
from opentest import *  

#filepath = "/home/valerio/PROGETTI/opentest/page.html" #test in xubuntu  
filepath = "/data/data/opentest.test.opentest/files/page.html" #test in android 4.4.2  
#filepath = "C:\Users\valerio\Desktop\open\testpage.html" #test in windows vista  

class mainApp(App):  
    def build(self):  
        open_test(filepath)  
        return Button(text='Hello World')  

mainApp().run()  

opentest.py

def open_test(filepath):  

    fli = ""  
    print filepath  
    print "-----"  
    with open(filepath,"rb") as file:  
#       print file.read()  
        fli = file.read()  
#       for f in file:  
#           fli += f  
#       while True:  
#           lin = file.readline()  
#           if lin != "":  
#               fli += lin  
#           else:  
#               break  
#   print open(filepath,"rb").read()  
    print fli  
    print "-----"  

page.html

<!DOCTYPE html>  
<!-- This file is needed for test the Android platform -->  
<html><head><title>test title</title></head><body>test body<div>©test div</div>  

<div>test div 2</div></body></html>  

It would be great if you could solve it in the upcoming milestone or at least in the dev branch.

thanks

Activity

  1. kived commented on Apr 14, 2016

    @kived
    Contributor

    This is not an issue with python-for-android.

    Line endings: different platforms write different line endings. You have to take steps to deal with this, just like you would in any other Python program. A common fix is to use universal newlines ('U' flag to open()).

    Copyright symbol: this is an encoding issue. ASCII does not have a copyright symbol (that's extended ASCII, which is not what we're talking about). You should decode the text yourself, since Python will assume ASCII. For example, if the file is in UTF-8, you can do something like fli = file.read().decode('utf8'). If you don't know the encoding, you can make an educated guess or use something like chardet, just note that character encoding detection is not reliable.

  2. Tungsteno74 commented on Apr 15, 2016

    @Tungsteno74
    Author

    The decode doesn't work. I've checked this link
    https://pythonhosted.org/kitchen/unicode-frustrations.html#frustration-3-inconsistent-treatment-of-output
    and I did what they say (I/O encoding/decoding), but in any case it does not work on Android (give me always the same encoding error).

    Anyhow this doesn't solve the compatibily break.
    How tells PEP 20: There should be one-- and preferably only one --obvious way to do it

    On other platforms, this is not necessary. I tested it and work properly (at least for me).

    Please, @kived reopen this post and give a try yourself or at least tell me where is the right section for open a new issue for that kind of problem.

    thanks again for you work.

  3. kived commented on Apr 15, 2016

    @kived
    Contributor

    The reason I closed this is that your issue has nothing to do with python-for-android. This is the same for any Python application. And using decode() as I described above works perfectly on Android:

    https://gist.github.com/kived/b72eae147581d58a4f66c8c11a973e29

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions