[C++-sig] Getting the same python instance back in boost::python?
Nicholas Devenish
ndevenish at gmail.com
Thu Jun 27 12:32:42 EDT 2019
I want to get the same python instance back for subsequent calls to
the C++ function e.g. I want this to work for the C++ structure below:
import ident_ext
o = ident_ext.Owner()
assert o.get() is o.get()
How can I get this behaviour in boost::python? It looked like call
policies was the right sort of place to look, but can't get that to
behave how I want. I can get this to work in pybind11 but not boost
(and unfortunately our project is heavily dependent on boost::python).
My working c++ is:
#include <boost/python.hpp>
#include <iostream>
using namespace boost::python;
class Something {
public:
void name(void) { std::cout << this << std::endl; }
};
class Owner {
Something *_shared = nullptr;
public:
Something &get() {
if (_shared == nullptr) {
_shared = new Something();
}
return *_shared;
}
};
BOOST_PYTHON_MODULE(ident_ext) {
class_<Something>("Something").def("name", &Something::name);
class_<Owner>("Owner").def("get", &Owner::get, return_internal_reference<>());
}
More information about the Cplusplus-sig
mailing list