Skip to content

Translate cpp errors#84

Open
kwabenantim wants to merge 5 commits into
developfrom
83-handle-cpp-errors
Open

Translate cpp errors#84
kwabenantim wants to merge 5 commits into
developfrom
83-handle-cpp-errors

Conversation

@kwabenantim

Copy link
Copy Markdown
Member

Supports #83

@kwabenantim kwabenantim linked an issue Jun 26, 2026 that may be closed by this pull request
@kwabenantim kwabenantim requested a review from Copilot June 26, 2026 01:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 exceptions config option, resolves exception metadata from parsed C++ declarations, and generates py::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_functions parsing 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."
)
Comment thread cppwg/info/package_info.py Outdated
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.
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 this pull request may close these issues.

Handle C++ crashes gracefully

3 participants