pybind / pybind/pybind11

eigen: Passing non-referencable array to `py::cast(my_array, rvp::reference_internal, parent)` silently fails

Open
#1,631 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

I accidentally ran into an issue where our code had some confusing overloads; GetX(state) returned a reference to a vector, while GetX(state, specifier) returned a copy of a vector (due to non-contiguous sub-indices).

I had accidentally bound both as reference methods, like so:

cls
  .def("GetX",
    [](const State& state) {
      return py::cast(
          GetX(state), reference_internal, py::cast(&state, reference));
    })
  .def("GetX",
    [](const State& state, const Specifier& spec) {
      return py::cast(
          GetX(state, spec), reference_internal, py::cast(&state, reference));
    });

When testing, GetX(state) would return the values I wanted, but GetX(state, specifier) would return all zeros. Once I realized it was just a copy, I was able to fix this issue.

This seems like something that should be easily caught before it gets to eigen_array_cast, though it would require having knowledge of whether or not the type being passed in is (a) non-Map (which I would assume falls to the nominal dense type_caster) and (b) a pointer type, which should be caught between the indirection from cast([const] Type&[&], ...) to cast_impl(...).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Trace the cast_impl and eigen_array_cast entry points, including the nominal dense type_caster path described in the issue. Reproduce the non-referencable array case and inspect how reference_internal is handled between the reference overloads and eigen_array_cast. Done means the invalid reference case no longer silently produces incorrect values and has regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.