[codex] Add copy-free matrix view hooks#184
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds an extension point for pybind11 argument declarations by generating typed arguments through a gtwrap::internal::PyArgPolicy<T> / py_arg<T>(name) hook (defaulting to pybind11::arg(name)), and tightens reference_internal return policy emission to only apply to const-reference method returns.
Changes:
- Generate
gtwrap::internal::PyArgPolicy+py_arg<T>(name)support code into produced pybind C++ and route generated named arguments (including defaulted args) throughpy_arg<T>(...). - Update expected generated pybind outputs to reflect
py_arg<T>(...)usage in.def(...)declarations. - Add/extend unit tests and fixtures for the new argument policy hook and for restricting
reference_internalto const-reference method returns.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
gtwrap/pybind_wrapper.py |
Implements the argument-policy hook injection and switches named argument emission to py_arg<T>(...); restricts reference_internal to const-ref method returns. |
tests/test_pybind_wrapper.py |
Adds coverage for the new arg-policy hook and the refined const-ref return policy behavior; allows passing a custom module template for targeted assertions. |
tests/fixtures/arg_policies.i |
New fixture exercising typed args + defaulted args for policy hook generation. |
tests/fixtures/return_policies.i |
New fixture validating reference_internal is emitted only for const-ref method returns. |
tests/expected/python/*_pybind.cpp |
Updates golden expected pybind C++ outputs to use gtwrap::internal::py_arg<T>(...) and include the generated hook support block. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Member
Author
|
I tested this on my copy of GTSAM and it works like a charm. I will merge and vendor in this new version of wrap and monitor the situation on the GTSAM CI. |
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.
Summary
gtwrap::internal::PyArgPolicy<T>/py_arg<T>(name)hook for pybind argument declarations.pybind11::arg(name)so existing wrappers continue to use normal pybind conversion..noconvert().reference_internalreturn policy to const reference method returns, avoiding it for mutable references.ConstMatrixView-style matrix view arguments by accepting MATLABdoublematrices and generatingunwrapMatrixView<T>(...)locals backed by the mxArray buffer.Why
Downstream projects need a wrap-level extension point for copy-free matrix-view arguments without making wrap depend on project-specific Python CLI configuration. This lets GTSAM specialize Python matrix-view arguments in a preamble and lets MATLAB pass compatible double matrices into const Eigen matrix views without the existing
unwrap<Matrix>copy.Impact
Ordinary Python and MATLAB arguments keep their existing behavior.
ConstMatrixViewarguments are treated as MATLAB double matrix inputs and are passed through generated wrappers as value-like view objects, not pointer/shared-pointer objects.Validation
conda run -n py312 python -m unittest discover testsgit diff --checkgit diff --cached --check/Applications/MATLAB_R2025b.app/bin/mex ... /tmp/gtwrap_matrix_view_mex.cpp -outdir /tmpmatlab -batch "addpath(/tmp); y = gtwrap_matrix_view_mex([1 3 5; 2 4 6]); assert(y == 2323); disp(y);"