python - 将手工包装方法添加到 Swig 输出

标签 python swig

我有一个 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/

相关文章:

Python web-socket 解释(读取)数据

c++ - 如何通过 SWIG 将 lua 嵌入到 C++ 中

python - SWIG python 初始化指向 NULL 的指针

java - 使用 Swig Controller 将数组从 C++ 传递到 Java,up 调用

java - 如何使用 swig 在 C 和 Java 之间传递包含数组的结构体?

python - PySpark 2.4 : TypeError: Column is not iterable (with F. col() 用法)

python - 使用 winpdb 调试远程脚本

Pythonic 的写法属于两组函数

c++ - 用于检测分号终止的 C++ for 和 while 循环的正则表达式

java - SWIG 重命名函数