Create std::vector<py::handle> in C++ with pybind11

I'm trying to create a std::vector<py::handle> in C++ to unittest a C++ function that receives such a vector. I need to create a vector of integers (py::int_), floats (py::float_) and strings (py::str).

I've tried things like:

  auto int1 = py::int_(1);
  std::vector<py::handle> values = {py::handle(int1)};

Or:

  auto int1 = py::int_(1);
  std::vector<py::handle> values = {py::handle(int1.ptr())};

But this always SIGSEVs.

I'm new to pybind11, so I'm probably doing something very obviously wrong. Any idea how to do it? I couldn't find an obvious way in the docs.


Solution 1:

Found the problem. I needed to add a:

py::scoped_interpreter guard{};

At the beginning of each test.

Why?: Because I never ran the C++ code from the interpreter, but was purely testing the C++ code. So I needed to start a Python interpreter from C++ to be able to use py:: features.