chaquo / chaquopy

Chaquopy: the Python SDK for Android
https://chaquo.com/chaquopy/
MIT License
807 stars 132 forks source link

`sys.excepthook` not triggering on errors #1053

Closed matan-h closed 9 months ago

matan-h commented 9 months ago

When I try to use sys.excepthook it looks like it's not being called. For example, in this code:

import sys
def foo(exctype, value, tb):
    print('My Error Information')
    print('Type:', exctype)
    print('Value:', value)
    print('Traceback:', tb)
sys.excepthook = foo
name

I get back directly the crash without the excepthook:

D/AndroidRuntime: Shutting down VM
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
E/AndroidRuntime: Process: com.matan_h.ipython.bee_ipython, PID: 13550
E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.matan_h.ipython.bee_ipython/org.beeware.android.MainActivity}: com.chaquo.python.PyException: NameError: name 'name' is not defined

does chaquopy have any future plan to support sys.excepthook ?

mhsmith commented 9 months ago

As the sys.excepthook documentation says, it's called "just before the program exits". But this isn't applicable when Python is embedded inside another program, because Python doesn't own the main loop and it doesn't decide when to exit the program. Instead, in Chaquopy's case, it converts the Python exception into a Java exception and lets the calling code decide what to do with it.

Of course, if you're calling some code that you expect to throw an exception, then you can always wrap it in a try block and handle the exception however you want. So how would sys.excepthook be useful in the context of BeeWare?

matan-h commented 9 months ago

Of course, if you're calling some code that you expect to throw an exception, then you can always wrap it in a try block and handle the exception however you want. So how would sys.excepthook be useful in the context of BeeWare?

thats exactly what I do now in my brbug library : I have a "@brbug.catch_beeapp" on the class which do "try/except" on each method. but it could be easier for the user to call brbug.install to install excepthook.

mhsmith commented 9 months ago

I see; that looks like a useful tool. But since sys.excepthook is only relevant in standalone Python processes, I think the way you're currently doing it is the best option.

However, there might be a simpler way of dealing with the .pyc source code issue, because you can just disable .pyc compilation using the build_gradle_extra_content option. See:

matan-h commented 9 months ago

Thanks, that's cool, I didn't know about it. (and I'll mention it in my library), and it seems like kivy doesn't implement it either.

matan-h commented 9 months ago

Maybe it would be wise to implement it in toga_android (in the main_loop) or in briefcase-android-gradle-template (e.g. a default "try/except" in the mainactivity), since they are intended to a main python process, and the java there just to allow the python to run.

mhsmith commented 9 months ago

The main_loop method isn't actually a main loop at all on Android – it has that name for compatibility with the other platforms, but it just returns after initializing the app. iOS is similar.

All the event handlers are called from Android's own main loop, so the closest thing to what you're looking for would be the "Error in handler" blocks like this one.