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

Python exceptions for Processing mode function calls are not handled properly #234

Closed
hx2A opened this issue Feb 7, 2023 · 6 comments
Closed

Comments

@hx2A
Copy link
Collaborator

hx2A commented Feb 7, 2023

When should it display a Python stack trace? When should it throw a Java exception? Should the error handling behavior be user specified?

@villares has pointed out that select_folder(), select_file(), etc are very hard to debug. These should not be swallowing exceptions.

@hx2A
Copy link
Collaborator Author

hx2A commented Feb 19, 2023

@villares, can you provide an example where one of py5's select functions throws an exception in a callback and is hard to debug?

When I test with the below code:

import py5


def f(selection):
    x = 10 / 0
    py5.println(selection)


def setup():
    py5.size(200, 200, py5.P2D)


def mouse_pressed():
    py5.select_input('pick a file any file', f)


def draw():
    py5.rect(py5.mouse_x, py5.mouse_y, 10, 10)


py5.run_sketch()

I get this result:

% python test.py
py5 encountered an error in your code:File "test.py", line 5, in f
    4    def f(selection):
--> 5        x = 10 / 0
    6        py5.println(selection)

ZeroDivisionError: division by zero

The Sketch stops just like it would do for any other exception. There's also no additional Java stack trace to confuse anyone.

@hx2A
Copy link
Collaborator Author

hx2A commented Feb 19, 2023

If the callback function has the wrong number of parameters there is a proper error message but I think this can be improved with Python's inspect library to validate the callback when the select method is called.

@villares
Copy link
Collaborator

Hmmm, I could reproduce the silencing in Processing 3.5.4 Python mode (the pf function never reaches print):
image

But couldn't reproduce it in py5!
I even tried a different FileIO related Exception...

import py5

def pf(folder):
    with open('this file does not exist', 'r') as f:
        print(f)
    print(type(folder), folder)

def settings():
    py5.size(100, 100)
    
def key_pressed():
    py5.select_folder('hi', pf)
    
def draw():
    py5.circle(100, 100, py5.frame_count % 100)
    
py5.run_sketch()

Maybe something changed in Processing 4's core and I missed it?

Maybe this is a non-issue after all?!?

@hx2A
Copy link
Collaborator Author

hx2A commented Feb 19, 2023

Maybe something changed in Processing 4's core and I missed it?

Can you reproduce it in java Processing 4?

Maybe this is a non-issue after all?!?

py5 has its own exception handling that is totally different from what Processing 4 does. In this situation, Processing 4 having a difficulty with callback exception handling will not carry over to py5.

Java and Python each have their own way of handling exceptions and they cannot throw each other's exceptions. py5 needs its own exception handling mechanisms that work for both, cause the Sketch to stop at the appropriate time, and do not require a Java call to System.exit(0) to exit the window (which is what Processing does). Balancing all of these needs was a serious undertaking, and is one of the more complicated aspects of py5.

Separately, I've tested Processing mode exception handling and it seems to work fine. It will show you the Python stack trace and then throw a Java exception, which the user can catch and handle or allow to stop the Sketch as it would for a Java Processing Sketch.

@villares
Copy link
Collaborator

villares commented Feb 19, 2023

Python mode doesn't even start for me in Processing 4.
I tried this in Java... looks like something changed, to me:

image

PS: Maybe it is just "broken" in a different way now, because the console doesn't look like a traditional Processing error message 😆

@hx2A
Copy link
Collaborator Author

hx2A commented Feb 19, 2023

PS: Maybe it is just "broken" in a different way now, because the console doesn't look like a traditional Processing error message 😆

Yes, that's probably it. Managing exception handling for a callback is difficult to code, so I totally understand.

I am going to close this out but add a few things to the other issue related to the select methods.

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

No branches or pull requests

2 participants