【发布时间】:2017-09-12 08:29:31
【问题描述】:
我正在使用 boost::python 导出一些需要 void* 的 C++ 函数。他们在内部将其解释为原始内存(字节数组)。想想read 和write 一些非常特殊用途的设备。
如何将 Python bytearray 传递给这样的函数?
我尝试过使用ctypes.c_void_p.from_buffer(mybytearray),但这与函数的签名不匹配。
这是最小的例子:
#include <boost/python.hpp>
void fun_voidp(void*) {}
BOOST_PYTHON_MODULE(tryit) {
using namespace boost::python;
def("fun_voidp", fun_voidp);
}
在 python 端:
import tryit
import ctypes
b = bytearray(100)
tryit.fun_voidp(b) # fail
tryit.fun_voidp(ctypes.c_void_p.from_buffer(b)) # fail
tryit.fun_voidp(ctypes.c_void_p.from_buffer(b).value) # fail
【问题讨论】: