Swig、Python 和输出字符串

发布于 2024-10-07 04:56:05 字数 455 浏览 0 评论 0原文

我正在使用 Swig 包装一个 C 接口,如下所示:

int dosomething(char **str);

其中 str 是输出字符串。例如,在 C 中,它的调用方式如下:

char *str= NULL;
int val= dosomething(&str);
   ...
free(str);

在 Python 中,我希望能够这样调用它:

val,str = dosomething()

但是,python 不断报告

TypeError: dosomething() takes exactly 1 arguments (0 given)

我已尝试在类型映射中使用 OUTPUT,但无济于事。有什么想法吗?

I am using Swig to wrap a C interface that looks like this:

int dosomething(char **str);

where str is an output string. For example, from C its called like this:

char *str= NULL;
int val= dosomething(&str);
   ...
free(str);

In Python, I'd like to be able to call it like this:

val,str = dosomething()

However, python keeps reporting

TypeError: dosomething() takes exactly 1 arguments (0 given)

I've tried using OUTPUT in a typemap, but to no avail. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月朦胧 2024-10-14 04:56:05

尝试这个类型映射(我使用的是 SWIG 2.0.0):

%include <cstring.i>
%cstring_output_allocate(char **str, free(*$1));

文档: cstring.i

Try this typemap (I'm using SWIG 2.0.0):

%include <cstring.i>
%cstring_output_allocate(char **str, free(*$1));

Documentation: cstring.i

魔法唧唧 2024-10-14 04:56:05

为什么不正常使用它,传递一个参数,并将其包装在 python 中以返回一个元组?

Why not use it normally, passing a parameter, and wrap it inside python to return a tuple?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文