问题
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