Translate cpp errors#84
Open
kwabenantim wants to merge 5 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds configurable C++ exception translation to cppwg-generated pybind11 modules so that user-defined exception types (including those not derived from std::exception) surface as catchable Python exceptions instead of terminating the interpreter, addressing the crash scenario described in #83.
Changes:
- Introduces an
exceptionsconfig option, resolves exception metadata from parsed C++ declarations, and generatespy::register_exception_translator(...)code in each module. - Updates header collection generation (and per-module includes when
common_include_file: False) so exception class declarations are available for both parsing and compilation. - Fixes an explicit
free_functionsparsing bug and adds unit tests for the parser behavior.
Reviewed changes
Copilot reviewed 22 out of 22 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/test_package_info_parser.py | Adds regression coverage for explicit free_functions parsing and CPPWG_ALL behavior. |
| README.md | Documents the new exceptions configuration for exception translation. |
| examples/shapes/wrapper/wrapper_header_collection.cppwg.hpp | Ensures the new example exception header is available via the common include header. |
| examples/shapes/wrapper/primitives/_pyshapes_primitives.main.cppwg.cpp | Demonstrates generated per-module exception translator registration. |
| examples/shapes/wrapper/package_info.yaml | Adds an exceptions entry showing message_method configuration. |
| examples/shapes/wrapper/math_funcs/_pyshapes_math_funcs.main.cppwg.cpp | Demonstrates translator registration and exposes a throwing function to test translation. |
| examples/shapes/wrapper/geometry/_pyshapes_geometry.main.cppwg.cpp | Demonstrates translator registration in another module. |
| examples/shapes/src/py/tests/test_functions.py | Adds an example-level test asserting translation to RuntimeError (integration-style). |
| examples/shapes/src/cpp/math_funcs/ThrowingFunction.hpp | Adds a non-std::exception exception type and a function that throws it. |
| examples/cells/tests/test_cells.py | Adds an example-level test asserting translation for a std::runtime_error-derived exception using GetMessage(). |
| examples/cells/src/cpp/utils/SimulationException.hpp | Adds an exception type used for demonstrating translation configuration. |
| examples/cells/src/cpp/utils/PetscUtils.hpp | Adds a C++ entrypoint that throws the example exception. |
| examples/cells/src/cpp/utils/PetscUtils.cpp | Implements the throwing function and includes the new exception header. |
| examples/cells/dynamic/wrappers/wrapper_header_collection.cppwg.hpp | Ensures the example exception header is included for dynamic wrapper builds. |
| examples/cells/dynamic/wrappers/all/PetscUtils.cppwg.cpp | Exposes the throwing method to Python to exercise translation. |
| examples/cells/dynamic/wrappers/all/_pycells_all.main.cppwg.cpp | Demonstrates module-level translator registration when common_include_file: False. |
| examples/cells/dynamic/config.yaml | Adds an exceptions entry for the dynamic cells example. |
| cppwg/writers/module_writer.py | Generates exception-translator code and injects needed includes when not using the common include file. |
| cppwg/writers/header_collection_writer.py | Adds exception-class header inclusion so configured exceptions are parsed/introspectable. |
| cppwg/parsers/package_info_parser.py | Adds exceptions default config and fixes explicit free-function parsing bug. |
| cppwg/info/package_info.py | Stores exception config and resolves exception_info from parsed declarations for codegen. |
| cppwg/info/free_function_info.py | Improves failure mode when explicitly listed free functions can’t be found in parsed headers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+31
to
+38
| # The function's header was not parsed. For explicitly listed free | ||
| # functions, the header is only included when source_file or | ||
| # source_file_path is set in the config. | ||
| logger = logging.getLogger() | ||
| logger.error( | ||
| f"Could not find free function {self.name}. Set source_file or " | ||
| "source_file_path in the config so that its header is included." | ||
| ) |
| super().__init__(name, package_config) | ||
|
|
||
| self.common_include_file: bool = False | ||
| self.exceptions: List[str] = [] |
| * | ||
| * pybind11 cannot translate this automatically, so without a registered | ||
| * exception translator it would terminate the Python interpreter. It is used | ||
| * to exercise the package's exception_translation_code option. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Supports #83