我有以下代码:
old_lib.h:
struct DUMMY
{
// some members
};
module.cpp:
#include <boost/python.hpp>
#include "old_lib.h"
namespace py = boost::python;
void foo(py::object const& p)
{
// How to get rid of ctypes.addressof()?
static py::object ctypes_addressof = py::import("ctypes").attr("addressof");
DUMMY *ptr = *reinterpret_cast<DUMMY**>(uintptr_t(py::extract<uintptr_t>(ctypes_addressof(p))));
}
BOOST_PYTHON_MODULE(module)
{
py::def("foo", &foo, args("p"));
}
old_python.py:
import ctypes
class DUMMY(ctypes.Structure):
_pack_ = 4
PDUMMY = POINTER(DUMMY)
# some old stuff using PDUMMY
new_python.py:
from module import foo
from old_python import PDUMMY
x = PDUMMY()
# some calls to old python (e.g. initialization of x)
foo(x)
假设python代码是不可更改的。
如何摆脱ctypes.addressof()?我想,调用它比仅提取值更昂贵,特别是在频繁调用foo()的情况下。 UPD:换句话说,如何在不使用ctypes.addressof()的情况下获取ctypes._Pointer实例的值?
有没有办法将参数类型指定为'foo(PDUMMY p)'并将转换器从ctypes.POINTER写入PDUMMY?