向下转换为 pybind11 派生类
Posted
技术标签:
【中文标题】向下转换为 pybind11 派生类【英文标题】:Downcast to pybind11 derived class 【发布时间】:2019-05-29 20:12:38 【问题描述】:我正在使用 pybind11 的“Overriding virtual functions in Python”功能来创建继承自 C++ 抽象类的 Python 类。我有一个 C++ 类 State
,它在 Python 中被子类化为 MyState
。在这种情况下,我有一些MyState
对象丢失了其类型信息,Python 认为它是State
。我需要在 Python 代码中将其向下转换回 MyState
,但我不知道这样做的好方法。
这是 C++ 示例代码:
#include <memory>
#include <pybind11/pybind11.h>
namespace py = pybind11;
// ========== State ==========
class State
public:
virtual ~State() = default;
virtual void dump() = 0;
;
using StatePtr = std::shared_ptr<State>;
class PyState : public State
public:
using State::State;
void dump() override
PYBIND11_OVERLOAD_PURE(void, State, dump);
;
// ========== Machine ==========
class Machine
public:
virtual ~Machine() = default;
virtual StatePtr begin() = 0;
virtual StatePtr step(const StatePtr&) = 0;
;
using MachinePtr = std::shared_ptr<Machine>;
class PyMachine : public Machine
public:
using Machine::Machine;
StatePtr begin() override
PYBIND11_OVERLOAD_PURE(StatePtr, Machine, begin);
StatePtr step(const StatePtr& state) override
PYBIND11_OVERLOAD_PURE(StatePtr, Machine, step, state);
;
// ========== run ==========
void run(const MachinePtr& machine)
StatePtr state = machine->begin();
for (int i = 0; i < 5; ++i)
state = machine->step(state);
state->dump();
// ========== pybind11 ==========
PYBIND11_MODULE(example, m)
py::class_<State, StatePtr, PyState>(m, "State").def(py::init<>());
py::class_<Machine, MachinePtr, PyMachine>(m, "Machine")
.def(py::init<>())
.def("begin", &Machine::begin)
.def("step", &Machine::step);
m.def("run", &run, "Run the machine");
还有 Python 代码:
#!/usr/bin/env python3
from example import Machine, State, run
class MyState(State):
def __init__(self, x):
State.__init__(self)
self.x = x
def dump(self):
print(self.x)
class MyMachine(Machine):
def __init__(self):
Machine.__init__(self)
def begin(self):
return MyState(0)
def step(self, state):
# problem: when called from C++, `state` is an `example.State`
# instead of `MyState`. In order to access `state.x` we need
# some way to downcast it...
return MyState(state.x + 1)
machine = MyMachine()
print("running machine with python")
state = machine.begin()
for _ in range(5):
state = machine.step(state)
state.dump()
print("running machine with C++")
run(machine) # error
错误信息:
running machine with python
1
2
3
4
5
running machine with C++
Traceback (most recent call last):
File "<string>", line 38, in <module>
File "<string>", line 36, in __run
File "/usr/local/fbcode/platform007/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/local/fbcode/platform007/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/data/users/jcai/fbsource/fbcode/buck-out/dev/gen/experimental/jcai/pybind/run_example#link-tree/run_example.py", line 38, in <module>
run(machine) # error
File "/data/users/jcai/fbsource/fbcode/buck-out/dev/gen/experimental/jcai/pybind/run_example#link-tree/run_example.py", line 26, in step
return MyState(state.x + 1)
AttributeError: 'example.State' object has no attribute 'x'
我确实有一个 hacky 解决方法,我基本上保留一个“向下映射”std::unordered_map<State*, py::object>
并使用它注册每个创建的 MyState
。但我不想诉诸此类事情。
【问题讨论】:
【参考方案1】:我认为您可能正遭受这一系列问题的困扰:
https://github.com/pybind/pybind11/issues/1774
最终,因为您只是直接返回 MyState
,然后直接进入 C++,Python 解释器会丢失您的实例,并继续垃圾收集对象的 Python 部分,这这就是为什么你的对象最终会变得有点sliced。
可能的解决方案:
存储对您的返回MyState
的引用,至少足够长,以便 Python 解释器再次获得引用。
例如将 return MyState(...)
更改为 self._stashed_state = MyState(...); return self._stashed_state
看看你是否能以某种方式 incref
在你的 C++ 类的 Python 版本上(糟糕,但它会工作)
查看上述问题中列出的解决方法(不记得所有问题)
使用我们的pybind11
fork,它可以处理这个问题,但也可以拖入其他东西:overview for RobotLocomotion/pybind11
您可能还想发布现有问题之一,提及您也遇到了此问题(以便可以跟踪它)。
【讨论】:
以上是关于向下转换为 pybind11 派生类的主要内容,如果未能解决你的问题,请参考以下文章