When embedding python in C++ through PyBind11, I got stuck on the following issue. Consider I generate a shared_ptr instance of an object through C++ and I then want to handover this pointer to pybind11 to generate a "shadow" python binding for it.
Here is my initial, non-working attempt:
#include <stdio.h>
#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
using namespace std;
namespace py = pybind11;
class Pet
{
public:
Pet() {}
void bark(void) { printf("wow!\n"); }
};
PYBIND11_PLUGIN(Pets) {
py::module m("Pets", "Say hello to our pets");
py::class_<Pet, shared_ptr<Pet>>(m, "Pet")
.def("bark", &Pet::bark)
;
return m.ptr();
}
int main(int argc, char *argv[])
{
py::scoped_interpreter guard{};
shared_ptr<Pet> pet = make_shared<Pet>();
// How do Ι "assign" Pet.pet to the C++ pet? This compiles,
// but throws a run time exception:
py::globals()["pet"] = py::cast(pet);
py::exec("pet.bark()\n");
}
So my questions are:
- So how can I create a "shadow class" for C++ shared_ptr?
- How can I "assign" a C++ shared_ptr to a python variable?