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

Improve error-handling in Python Extensions #6986

Merged
merged 4 commits into from
Aug 31, 2022
Merged

Conversation

steven-johnson
Copy link
Contributor

Currently, Python Extensions don't make any effort to override halide_error, so the default (which aborts) is generally used... this is very unfriendly. This modifies the standard Python Extension glue code to hook halide_error, saving the text in a thread local, and then throwing a Python exception after the extension's AOT call is finished (if an error occurred, of course).

Also does a drive-by default hooking of halide_print to ensure that it goes to whatever Python thinks that stdout is.

(Note that it would be really nice if we could use closures of some sort for halide_error, halide_print, etc so that we could save context in the actual Python module, rather than in a thread-local global var, but this currently isn't possible without nontrivial refactoring in the Halide runtime.)

Currently, Python Extensions don't make any effort to override `halide_error`, so the default (which aborts) is generally used... this is very unfriendly. This modifies the standard Python Extension glue code to hook halide_error, saving the text in a thread local, and then throwing a Python exception after the extension's AOT call is finished (if an error occurred, of course).

Also does a drive-by default hooking of `halide_print` to ensure that it goes to whatever Python thinks that `stdout` is.

(Note that it would be really nice if we could use closures of some sort for halide_error, halide_print, etc so that we could save context in the actual Python module, rather than in a thread-local global var, but this currently isn't possible without nontrivial refactoring in the Halide runtime.)
@steven-johnson steven-johnson added release_notes For changes that may warrant a note in README for official releases. python Issues related to Halide/Python interop labels Aug 31, 2022
Copy link
Member

@alexreinking alexreinking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@steven-johnson steven-johnson merged commit 95e37ee into main Aug 31, 2022
@steven-johnson steven-johnson deleted the srj/py-better-errors branch August 31, 2022 21:44
ardier pushed a commit to ardier/Halide-mutation that referenced this pull request Mar 3, 2024
* Improve error-handling in Python Extensions

Currently, Python Extensions don't make any effort to override `halide_error`, so the default (which aborts) is generally used... this is very unfriendly. This modifies the standard Python Extension glue code to hook halide_error, saving the text in a thread local, and then throwing a Python exception after the extension's AOT call is finished (if an error occurred, of course).

Also does a drive-by default hooking of `halide_print` to ensure that it goes to whatever Python thinks that `stdout` is.

(Note that it would be really nice if we could use closures of some sort for halide_error, halide_print, etc so that we could save context in the actual Python module, rather than in a thread-local global var, but this currently isn't possible without nontrivial refactoring in the Halide runtime.)

* Make Windows happy

* Remove dangling code bits

* Allow defeating of error-handler via HALIDE_PYTHON_EXTENSION_OMIT_ERROR_AND_PRINT_HANDLERS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
python Issues related to Halide/Python interop release_notes For changes that may warrant a note in README for official releases.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants