Reduce the use of C++ exceptions #11282
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Summary
This is hopefully the last of the pyodide-related patches for a while.
Emscripten has only limited support for C++ exceptions. The support that is there doesn't seem to work fully correctly over the matplotlib code base, and even if it did work, it has a large performance penalty which will be hard to resolve. The best available option at the moment is to have C++ exception support turned off, which means a
throw
from C++ aborts the current WebAssembly frame, exiting back to Javascript without a chance for any C++catch
blocks to handle the exception. (This is weaker than an unhandled exception in "native" code which would terminate the application altogether). In most cases, this behavior is "good enough" when the exceptions are truly "exceptional". It's not ideal, since there isn't an opportunity to cleanup or provide friendly error messages from C++.However there is a very small handful of places in matplotlib where exception-handling is used for cases that are the normal course of business, such as dealing with an input differently based on the number of its dimensions. This grew out of the fact that a constructor can not return an error code, so must raise an exception to indicate a dimension mismatch. I think it's worth fixing these three places to just use the C-level Numpy API instead of the
numpy_cpp.h
abstraction, even though that's not strictly necessary for most platforms. There should be performance benefits for everyone, however, since it avoids interpreting the array-like input twice.PR Checklist