我有一个 SWIG 模块,我想在其中添加手动方法。
%module frob
%inline %{
int Foo(int x, int y) { return x+y; }
PyObject* Bar(PyObject* self, PyObject* args) {
return PyString_FromString("Hello from Bar");
}
%}
但是,当我对其运行 swig swig -python frob.i
时,我发现 SWIG 实际上将 Foo 和 Bar 都包装为 _wrap_Foo、_wrap_Bar。
SWIGINTERN PyObject *_wrap_Foo(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
// ...
result = (int)Foo(arg1,arg2);
// ...
}
SWIGINTERN PyObject *_wrap_Bar(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
// ...
result = (PyObject *)Bar(arg1,arg2);
// ...
}
如何告诉 SWIG 停止为我包装 Bar,而只是将其公开在 PyMethodDef 表中?
最佳答案
要排除函数被包装,请使用 %native 指令。
%module "test"
/* Prototype */
%native(DontWrapMeBro)
PyObject* DontWrapMeBro(PyObject* self, PyObject* args);
%{
PyObject* DontWrapMeBro(PyObject* self, PyObject* args)
{
return PyString_AsString("Don't wrap me");
}
%}
关于python - 将手工包装方法添加到 Swig 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12187658/