diff --git a/Doc/Manual/Python.html b/Doc/Manual/Python.html index b73ab1ea3..4c1aa318f 100644 --- a/Doc/Manual/Python.html +++ b/Doc/Manual/Python.html @@ -2986,6 +2986,101 @@ what can be done without having to rely on any of the more advanced customization features.

+

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);
+}
+
+
+ + + +

29.6.3 Class extension with %extend

diff --git a/Lib/python/pyuserdir.swg b/Lib/python/pyuserdir.swg index 0e983fb7b..53962acd2 100644 --- a/Lib/python/pyuserdir.swg +++ b/Lib/python/pyuserdir.swg @@ -169,3 +169,20 @@ These methods "may be called" if needed. #define %nokwargs %feature("kwargs", "0") #define %clearkwargs %feature("kwargs", "") +/* ------------------------------------------------------------------------- */ +/* + Add python code to the proxy/shadow code + + %pythonpreppend - Add code before the C++ function is called + %pythonappend - Add code after the C++ function is called +*/ + +#define %pythonpreppend %feature("pythonpreppend") +#define %clearpythonpreppend %feature("pythonpreppend","") + +#define %pythonappend %feature("pythonappend") +#define %clearpythonappend %feature("pythonappend","") + + + +