Skip to content

Translate cpp exceptions#84

Merged
kwabenantim merged 10 commits into
developfrom
83-handle-cpp-errors
Jun 26, 2026
Merged

Translate cpp exceptions#84
kwabenantim merged 10 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 thread cppwg/info/free_function_info.py
Comment thread cppwg/info/package_info.py Outdated
Comment thread examples/shapes/src/cpp/math_funcs/ThrowingFunction.hpp Outdated
@kwabenantim kwabenantim changed the title Translate cpp errors Translate cpp exceptions Jun 26, 2026

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

Copilot reviewed 26 out of 26 changed files in this pull request and generated 2 comments.

Comment thread cppwg/info/package_info.py Outdated
Comment thread examples/cells/src/cpp/utils/PetscUtils.cpp
kwabenantim and others added 2 commits June 26, 2026 14:48
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@kwabenantim kwabenantim merged commit a62eedd into develop Jun 26, 2026
13 checks passed
@kwabenantim kwabenantim deleted the 83-handle-cpp-errors branch June 26, 2026 14:19
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++ exceptions gracefully

3 participants