SWIG:在Python和C之间传递字节

时间:2018-06-15 00:01:17

标签: python c swig python-c-api

我已经在网上搜索了一段时间,但在Python和C之间来回传递字节的正确方法是什么?我利用了SWIG文档中的示例:

C代码:

char* foo(char *buff, int size)
{
    return buff;
}

包装代码:

%include <pybuffer.i>

%pybuffer_binary(char *buff, int size);
char* foo(char *buff, int size);

错误:

example_wrap.c: In function ‘_wrap_foo’:
example_wrap.c:3385:19: error: ‘size’ undeclared (first use in this function)
     arg2 = (int) (size / sizeof(char));               
example_wrap.c:3385:19: note: each undeclared identifier is reported only once for each function it appears in
example_wrap.c:3387:12: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
   result = (char *)foo(arg1,arg2); 

编辑: 包括pybuffer.i:https://github.com/swig/swig/blob/master/Lib/python/pybuffer.i

0 个答案:

没有答案