向下转换为 pybind11 派生类
Downcast to pybind11 derived class
我正在使用pybind11的"覆盖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'
我确实有一个笨拙的解决方法,我基本上保留了一个"向下的地图"std::unordered_map<State*, py::object>
并注册每个创建的MyState
。但我宁愿不诉诸这样的事情。
我认为您可能遇到了这一系列问题:
https://github.com/pybind/pybind11/issues/1774
最终,因为你只是直接返回MyState
门,然后直接进入C++,Python解释器会失去对你的实例的跟踪,并继续垃圾回收对象的Python部分,这就是为什么你的对象最终被切片的原因。
潜在的解决方案:
- 存储对返回
MyState
的引用,至少足够长的时间让 Python 解释器再次获得引用。- 例如,将
return MyState(...)
更改为self._stashed_state = MyState(...); return self._stashed_state
- 例如,将
- 看看你是否可以以某种方式
incref
你的类的 Python 版本C++(哎呀,但它会起作用) - 查看上述问题中列出的解决方法(不记得所有解决方法)
- 使用我们的
pybind11
分支,它可以处理这个问题,但也拖入其他内容:RobotLocomotion/pybind11 概述
您可能还想发布现有问题之一,提及您也遇到了此问题(只是为了可以跟踪它)。
相关文章:
- 为什么使用 "this" 指针调用派生成员函数?
- Pybind11:将元组列表从Python传递到C++
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 在派生函数中指定void*参数
- 如何通过派生类函数更改基类中的向量
- 如何委托派生类使用其父构造函数?
- 如何使用单独文件中的派生类访问友元函数对象
- 派生类销毁的最佳实践是什么
- Windows/Cygwin - 不能使用 pybind11 - 犯错误
- 如何使用基类指针引用派生类成员
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 使用基类指针创建对象时,缺少派生类析构函数
- 如何引用基类的派生类?
- 存储模板类型以强制转换回派生<T>
- 需要从 istream 和 ostream 派生 iostream
- 用pybind11包装C++抽象类时出错
- 在 C++ 中用派生类型重写成员函数
- 使用Pybind11向Python公开Eigen::张量
- 具有多个类、派生类的C++正向声明
- 向下转换为 pybind11 派生类