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

Improvements to callback handling for select_file(), select_folder(), etc #233

Closed
hx2A opened this issue Feb 7, 2023 · 6 comments · Fixed by #264
Closed

Improvements to callback handling for select_file(), select_folder(), etc #233

hx2A opened this issue Feb 7, 2023 · 6 comments · Fixed by #264

Comments

@hx2A
Copy link
Collaborator

hx2A commented Feb 7, 2023

No description provided.

@hx2A
Copy link
Collaborator Author

hx2A commented Feb 19, 2023

From #234:

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.

@hx2A
Copy link
Collaborator Author

hx2A commented Feb 19, 2023

And original goal of this issue:

Callbacks for select_file(), select_folder(), etc should be passed Path objects, not strings

@hx2A hx2A changed the title Callbacks for select_file(), select_folder(), etc should be passed Path objects, not strings Improvements to callback handling for select_file(), select_folder(), etc Feb 19, 2023
@hx2A
Copy link
Collaborator Author

hx2A commented Feb 19, 2023

I looked at the code for this and don't think it will be that hard to fix.

@hx2A
Copy link
Collaborator Author

hx2A commented Mar 19, 2023

Callbacks for select_file(), select_folder(), etc should be passed Path objects, not strings

@villares I have a fix for this. Now the callbacks will be passed Path objects or None (if the user hits cancel), as originally intended.

Next I will re-evaluate the error handling around this and see if I can make some improvements there.

@hx2A
Copy link
Collaborator Author

hx2A commented Mar 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.

This is done also. Now, if the callback has the wrong number of parameters or is keyword only, py5 will raise an error when py5's select method is called, not when the callback is called. py5 is validating the callback before proceeding with the select method functionality.

One more fix for the error handling: if an exception is thrown in the callback, there is an error message, but no stack trace (at least on my Mac). I'm confused why there is no stack trace. There should be.

@hx2A hx2A mentioned this issue Mar 19, 2023
@hx2A hx2A closed this as completed in #264 Mar 19, 2023
@hx2A hx2A reopened this Mar 19, 2023
@hx2A
Copy link
Collaborator Author

hx2A commented Mar 19, 2023

Now stack traces appear when the user's callback has an error. Example below:

py5 encountered an error in your code:

File "/Users/jim/INSTALL/anaconda3/envs/py5/lib/python3.8/site-packages/py5/sketch.py", line 933, in wrapped_callback_py5_no_prune
    return callback(selection if selection is None else Path(selection))
File "test.py", line 5, in f
    4    def f(selection):
--> 5        x = 10 / 0
    6        py5.println(type(selection))

ZeroDivisionError: division by zero

I can't seem to get rid of the wrapped_callback_py5_no_prune bit without also getting rid of the user's code. That's fine though, this is much better than the alternatives.

@hx2A hx2A closed this as completed Mar 19, 2023
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

Successfully merging a pull request may close this issue.

1 participant