Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't catch JsonStore IOErrors #5259

Closed
JellyWX opened this issue Jul 5, 2017 · 9 comments
Closed

Can't catch JsonStore IOErrors #5259

JellyWX opened this issue Jul 5, 2017 · 9 comments

Comments

@JellyWX
Copy link

JellyWX commented Jul 5, 2017

Versions

  • Python: 2.7
  • OS: Ubuntu 16.04
  • Kivy: 1.10
  • Kivy installation method: PIP

I'm trying to use a try loop to catch errors when kivy attempts to create JsonStores. This is necessary on Android, since not all phones have a proper sdcard folder, yet some do. However, JsonStore can't be caught with either a catch-all except or an IOError specific except

Code and Logs

try:
  store = JsonStore('/sdcard/data.json')
except:
  try:
    store = JsonStore('/storage/emulated/0/data.json')
  except:
    store = JsonStore('data.json')

yields:

I python  :  IOError: [Errno 2] No such file or directory: '/sdcard/data.json'
I python  : Python for android ended.
@dessant dessant added Status: Needs-analysis Issue needs to be analyzed if it's real Platform: Android labels Jul 7, 2017
@inclement
Copy link
Member

Just to note, regardless of the details of the issue, the ideal way to resolve this issue is to ask Android for the right external files dir. I think something like the following should work:

from jnius import autoclass
Environment = autoclass('android.os.Environment')
external_storage_dir = Environment.getExternalStorageDirectory().getPath()

Even better, you can similarly get the external files directory that is dedicated to your application, does no require special permissions to write to, and will be cleared on uninstall.

@JellyWX
Copy link
Author

JellyWX commented Jul 7, 2017

good to know; i think sdcard is generally safe but this would be ideal (my android uses 'emui' and for some reason its unbound the sdcard folder :/). ill investigate that as an alternative

@JellyWX
Copy link
Author

JellyWX commented Jul 11, 2017

inclement's solution worked perfectly. saved me some processing power and some storage space. thanks

@KeyWeeUsr
Copy link
Contributor

@JellyWX could you please paste the path you got from Environment.getExternalStorageDirectory().getPath()? I'm curious what was the final path as at least one from

/storage/emulated/0
/storage/emulated/legacy
/sdcard

always worked for me.

@JellyWX
Copy link
Author

JellyWX commented Jul 22, 2017

@KeyWeeUsr I believe it returned

/storage/6537-3031

My phone is a Huawei model, and when I added an SD card to the storage, for some reason the modified OS unbound the sdcard folder and made the /storage/6357-3031 folder. I have no idea why... but luckily the getExternalStorage works perfectly (unlike the JsonStore itself, it seems. I'm fighting with it as I type this)

Sent from my HONOR BLN-L21 using FastHub

@KeyWeeUsr
Copy link
Contributor

Oh my... well, I've seen just slightly modified path (e.g. /storage/emulated/1, /storage/emulated/sdcard), but there was always some symlink to the real one. Quite lovely this one.

About JsonStore, I will try to look at it this weekend and see what's wrong with it.

@JellyWX
Copy link
Author

JellyWX commented Jul 22, 2017

@KeyWeeUsr thanks :) I was looking at it myself before, but got sidetracked and couldn't nail some of the code used. I'm having some issues where the store only updates in memory it seems, but sometimes it works sometimes not. If i make a reproducible example I'll file a different issue for it

Sent from my HONOR BLN-L21 using FastHub

@KeyWeeUsr
Copy link
Contributor

@JellyWX I can't really reproduce your issue. Pure j = JsonStore('somewhere') doesn't trigger anything for me both on py2 and py3 with the latest stable (1.10.0).

The only error I get is when I actually try to put something into the storage, therefore when I do:

j = JsonStore('somewhere')
j.put('something')  # error here

which throws an IOError on Android for me and when I try to catch the error, everything works just fine:

try:
    j.put('something') 
except:
    pass

which leads me to conclusion that you only tried to catch the error on a wrong place, which even appears to be true:

try:
    store = JsonStore('/sdcard/data.json')  # no error here, nothing to catch
except:
    try:
        store = JsonStore('/storage/emulated/0/data.json')
    except:
        store = JsonStore('data.json')

# missing line for the example
store.put('whatever')  # crash boom
# the same folder as the first "store" variable
I python  :  IOError: [Errno 2] No such file or directory: '/sdcard/data.json' 

@JellyWX
Copy link
Author

JellyWX commented Jul 22, 2017 via email

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

No branches or pull requests

4 participants