pybind / pybind/pybind11

[BUG]: Unable to convert function return value to a Python type

Open
#3,751 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Required prerequisites
Problem description

Consider a module mymodule. From it, I am returning a reference to a member as follows

const GooseFEM::Vector& vector() const { return m_vector; }

with corresponding Python API

PYBIND11_MODULE(mymodule, m)
{
    py::class_<Myclass> cls(m, "Myclass");
    ...
    cls.def("vector", &Myclass::vector, "vector");
}

Now GooseFEM has its own (pybind11) Python API, so this works brilliantly.

Accept... Since a week or so, the package of GooseFEM (and other libraries, just making an example here) shipped from conda-forge is no longer playing nicely with my locally compiled library on Linux (macOS and Windows work fine).

I have no idea what is going on, but have been able to get a minimal reproducer: https://github.com/tdegeus/test_pybind (which include the failing CI on Linux, and passing CI on macOS and Windows).

Reproducible example code

See full working example, including CI: https://github.com/tdegeus/test_pybind

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <GooseFEM/GooseFEM.h>

namespace py = pybind11;

class Myclass
{
public:
    Myclass() = default;

    Myclass(size_t n) {
        m_mesh = GooseFEM::Mesh::Quad4::Regular(n, n);
        m_vector = GooseFEM::Vector(m_mesh.conn(), m_mesh.dofs());
    }

    const GooseFEM::Vector& vector() const
    {
        return m_vector;
    }

private:
    GooseFEM::Mesh::Quad4::Regular m_mesh;
    GooseFEM::Vector m_vector;
};

PYBIND11_MODULE(mymodule, m)
{
    m.doc() = "Foo";
    py::class_<Myclass> cls(m, "Myclass");
    cls.def(py::init<size_t>(), "Myclass", py::arg("n"));
    cls.def("vector", &Myclass::vector, "vector");
}

and Python code

import GooseFEM
import mymodule

a = mymodule.Myclass(3)
v = a.vector()
print(v)

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

Start with the linked test_pybind minimal reproducer and its failing Linux CI, then inspect the Myclass::vector binding and GooseFEM conversion shown in the issue. Compare the Linux result with macOS and Windows; done means the reproducer returns the vector successfully on Linux or the incompatibility is isolated and documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api
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.