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

2024-04-25 23:53:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我在网上搜索了一段时间,但是在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); 

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


Tags: 代码insizefooexamplefunctionbuffint