From 146958ad785cf07cc7e9c8b32bbb6d735d2e3eec Mon Sep 17 00:00:00 2001
From: Marcelo Matus
Sometimes you may want to replace or modify the wrapper function +that SWIG creates in the proxy .py file. The Python module +in SWIG provides some features that enable you do do this. First, to +entirely replace a proxy function you can use +%feature("shadow"). For example:
+ +
+%module example
+
+// Rewrite bar() python code
+
+%feature("shadow") Foo::bar(int) %{
+def bar(*args):
+ #do something before
+ $action
+ #do something after
+%}
+
+class Foo {
+public:
+ int bar(int x);
+}
+
+where $action will be replaced by the call to +the C/C++ proper method. +
+ ++Often the proxy function created by SWIG is fine, but you simply want +to add code to it without touching the rest of the generated function +body. For these cases SWIG provides the pythonprepend and +pythonappend features which do exactly as their names suggest. The +pythonprepend feature will insert its value at the begining of the +proxy function, and pythonappend will insert code at the end of the +proxy, just before the return statement. +
+ + +
+%module example
+
+// Add python code to bar()
+
+%feature("pythonpreppend") Foo::bar(int) %{
+ #do something before C++ call
+%}
+
+%feature("pythonappend") Foo::bar(int) %{
+ #do something after C++ call
+%}
+
+
+class Foo {
+public:
+ int bar(int x);
+}
+
++Notes: Usually the pythonappend and pythonpreppend +features are safer to use than the shadow feature. Also, from +SWIG version 1.3.28 you can use the directive forms +%pythonappend and %pythonpreppend as follows:
+ + +
+%module example
+
+// Add python code to bar()
+
+%pythonpreppend Foo::bar(int) %{
+ #do something before C++ call
+%}
+
+%pythonappend Foo::bar(int) %{
+ #do something after C++ call
+%}
+
+
+class Foo {
+public:
+ int bar(int x);
+}
+
+