[英]How do I use SWIG to return specific exceptions based on C function return type?
Here are examples of functions in my C API: 以下是我的C API中的函数示例:
int do_something(struct object *with_this)
struct object *get_something(struct object *from_this)
do_something
will return 0
for success or -1
and set errno
for failure. do_something
将为成功返回0
或为-1
并为失败设置errno
。 get_something
will return a valid pointer on success or NULL
and set errno
for failure. get_something
将在成功或NULL
时返回有效指针,并为失败设置errno
。
I'm using to SWIG 2.0 to generate python bindings. 我正在使用SWIG 2.0来生成python绑定。 For the
do_something
binding I'd like it to return an exception based on errno
if it fails and return the Python None
object if it succeeds. 对于
do_something
绑定,如果它失败,我希望它返回基于errno
的异常,如果成功,则返回Python None
对象。 For the get_something
binding I'd like it to return an exception based on errno
if it fails and the opaque object if it succeeds. 对于
get_something
绑定,我希望它在失败时返回基于errno
的异常,在成功时返回基于不透明的对象。
The question is how do I get SWIG to do all of this magic for me? 问题是我如何让SWIG为我完成所有这些魔术?
Currently I'm using SWIG 2.0. 目前,我正在使用SWIG 2.0。
I can use %exception
and define different exceptions per symbol, but I'd like it to be based on return type. 我可以使用
%exception
并为每个符号定义不同的异常,但是我希望它基于返回类型。 I'll have a lot of the API functions and I don't want to list them out. 我将有很多API函数,并且我不想将它们列出。 So I can do this for each symbol:
因此,我可以为每个符号执行此操作:
%exception do_something {
$action
if (result < 0) {
return PyErr_SetFromErrno(PyExc_RuntimeError);
}
}
%exception get_something {
$action
if (result == NULL) {
return PyErr_SetFromErrno(PyExc_RuntimeError);
}
}
It would be a lot better if I could do something like this (like you can do with %typemap
): 如果我可以做这样的事情(比如您可以使用
%typemap
),那就更好了:
%exception int {
$action
if (result < 0) {
return PyErr_SetFromErrno(PyExc_RuntimeError);
}
}
%exception struct object * {
$action
if (result == NULL) {
return PyErr_SetFromErrno(PyExc_RuntimeError);
}
}
Can I do something like this? 我可以做这样的事情吗? If I had something like this, then that would take care of the exceptions.
如果我有这样的事情,那将解决例外情况。 And I believe my
get_something
binding would be perfect. 而且我相信我的
get_something
绑定将是完美的。
My do_something
binding would still need something like this to get it to return the Python None object if I use %typemap
: 如果我使用
%typemap
我的do_something
绑定仍然需要这样的东西才能使其返回Python None对象:
%typemap(out) int {
$result = Py_BuildValue("");
}
Does the magic I'm looking for exist or am I going about this all wrong? 我正在寻找的魔术是否存在,或者我将这一切弄错了吗? Is there a better way to do this?
有一个更好的方法吗?
To help answer this question I took your functions and created the following test.h file that included enough real code to actually illustrate the problem: 为了帮助回答这个问题,我采用了您的功能,并创建了以下test.h文件,其中包含足够的实际代码来实际说明问题:
struct object { int bar; };
static int do_something(struct object *with_this) {
errno = EACCES;
return -1;
}
static struct object *get_something(struct object *from_this) {
errno = EAGAIN;
return NULL;
}
I'm fairly sure that %typemap(out)
is the place to write this code in, not %exception
, because the behaviour you want is determined by the return type, not the name of the function called. 我非常确定
%typemap(out)
是在其中写入此代码的位置,而不是%exception
,因为您想要的行为是由返回类型而不是所调用函数的名称确定的。 The fact that you also care about the value of the return further backs this up. 您还关心收益的事实进一步证明了这一点。 You can to some degree avoid repetition using
$typemap
to reference other typemaps within yours. 您可以在某种程度上避免重复使用
$typemap
来引用您自己的其他类型图。
So I wrote a little SWIG interface to illustrate this: 因此,我写了一些SWIG界面来说明这一点:
%module test
%{
#include <errno.h>
#include "test.h"
%}
%typemap(out) int do_something %{
if ($1 < 0) {
PyErr_SetFromErrno(PyExc_RuntimeError);
SWIG_fail;
}
$result = Py_None; // Return only controls exception
Py_INCREF($result);
%}
%typemap(out) struct object *get_something %{
if (!$1) {
PyErr_SetFromErrno(PyExc_RuntimeError);
SWIG_fail;
}
// Return passed through unless NULL
$typemap(out,$1_ltype);
%}
%include "test.h"
Which worked when tested: 在测试时有效:
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> test.do_something(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: (13, 'Permission denied')
>>> test.get_something(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: (11, 'Resource temporarily unavailable')
>>>
This only worked though because I explicitly named do_something
when matching the typemap, so the fallback without the named function is fine. 但这仅能工作,因为我在匹配类型映射时显式地命名了
do_something
,所以没有命名函数的后备很好。 If you just try to remove that restriction you'll get an error about recursive typemaps. 如果您只是尝试删除该限制,则会收到有关递归类型映射的错误。 You can work around that using
%apply
, eg 您可以使用
%apply
解决该问题,例如
%typemap(out) struct object * MY_NULL_OR_ERRNO %{
if (!$1) {
PyErr_SetFromErrno(PyExc_RuntimeError);
SWIG_fail;
}
$typemap(out,$1_ltype);
%}
%apply struct object * MY_NULL_OR_ERRNO { struct object *get_something, struct object *some_other_function };
If that's too painful you can of course just write the typemap by hand: 如果那太痛苦了,您当然可以手动编写类型图:
%typemap(out) struct object * %{
if (!$1) {
PyErr_SetFromErrno(PyExc_RuntimeError);
SWIG_fail;
}
$result = SWIG_NewPointerObj(SWIG_as_voidptr($1), $1_descriptor, $owner);
%}
Which seems the simplest option if you really want that everywhere a struct object*
gets returned. 如果您真的希望在任何地方都返回
struct object*
,这似乎是最简单的选择。
There are other possible solutions using %pythoncode
or %pythonappend
, but neither of them are really improvements over the above in my view. 还有其他可能使用
%pythoncode
或%pythonappend
解决方案,但在我看来,它们都不是对上述内容的真正改进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.