在用 SWIG 包装 class 的 c++ Python 中用二进制数据填充 char*

Fill char* with binary data in a c++ Python wrapped class with SWIG

我用 swig 包装了一个 c++ class 供 python 使用。我的 class 是这样的:

public class DataHolder
{
public:
     char* BinaryData;
     long Length;
}

public class MyProcessor
{
public:
    int process(DataHolder& holder)
    {
          holder.BinaryData = assign some binary data;
          holder.Length = 1024;
    }
}

我想从 python 得到的是这样的:

import Module
from Module import *

proc = MyProcessor();
holder = DataHolder();

proc.process(holder);

#use holder.BinaryData to, for example, print

非常感谢任何帮助,非常感谢!

您的示例 C++ 代码不太合法(它看起来更像 Java!),但是一旦我修复了它并为 process 添加了一些真正的东西,我就能够包装它如你所愿。

与我的 previous answer 相比,主要变化是长度是从 class 内部读取的,而不是在容器上调用方法的结果。这有点难看,因为我不得不硬编码 C++ 'this' 对象的名称,$self 指的是 Python 对象。因此,我使类型映射仅在我们确定其合法且正确的受限情况下应用。

所以我最终的界面文件看起来像这样:

%module test

%typemap(out) char *DataHolder::BinaryData {
  Py_buffer *buf=(Py_buffer*)malloc(sizeof *buf);
  if (PyBuffer_FillInfo(buf, NULL, , arg1->Length, true, PyBUF_ND)) {
    // Error, handle
  }
  $result = PyMemoryView_FromBuffer(buf);
}

%inline %{
class DataHolder
{
public:
     char* BinaryData;
     long Length;
};

class MyProcessor
{
public:
    int process(DataHolder& holder)
    {
          static char val[] = "Hello[=10=]Binary[=10=]World\n!";
          holder.BinaryData = val;
          holder.Length = sizeof val;
          return 0;
    }
};
%}

我测试过的:

from test import *
proc = MyProcessor()
holder = DataHolder()

proc.process(holder)

data = holder.BinaryData
print(repr(data))
print(data.tobytes())

我在此处针对 Python3,但完全相同的代码应该适用于 Python 2.7。编译后 运行 这给出了:

swig2.0 -c++ -Wall -py3 -python test.i
g++ -Wall  -shared -o _test.so test_wrap.cxx -I/usr/include/python3.4
python3 run.py
<memory at 0xb7271f9c>
b'Hello\x00Binary\x00World\n!\x00'