Add hand-wrapped method to Swig output

喜夏-厌秋 提交于 2021-01-27 23:22:53

问题


I have a SWIG module where I want to add a hand-rolled method.

%module frob

%inline %{

    int Foo(int x, int y) { return x+y; }

    PyObject* Bar(PyObject* self, PyObject* args) {
        return PyString_FromString("Hello from Bar");
    }
%}

However, when I ran swig over it swig -python frob.i, I saw that SWIG actually wrapped both Foo and Bar as _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);
   // ... 
}

How do I tell SWIG to stop wrapping Bar for me, but just expose it in the PyMethodDef table?


回答1:


To exclude a function from being wrapped, use the %native directive.

%module "test"

/* Prototype */    
%native(DontWrapMeBro)
PyObject* DontWrapMeBro(PyObject* self, PyObject* args);

%{
  PyObject* DontWrapMeBro(PyObject* self, PyObject* args)
  {
    return PyString_AsString("Don't wrap me");
  }
%}


来源:https://stackoverflow.com/questions/12187658/add-hand-wrapped-method-to-swig-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!