Chui Tey
Chui Tey

Reputation: 5574

Add hand-wrapped method to Swig output

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?

Upvotes: 2

Views: 622

Answers (1)

Chui Tey
Chui Tey

Reputation: 5574

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");
  }
%}

Upvotes: 2

Related Questions