title: Passing python functions to SWIG wrapped C++ code
body: <p>I am trying to wrap a C++ library for python, using SWIG.  The library uses callback functions frequently, by passing <em>callback functions of certain type</em> to class methods.</p>
  
  <p>Now, after wrapping the code, I would like to create the callback logic from python.  Is this possible?  Here is an experiment I was doing to find it out .. does not work at the moment.</p>
  
  <p>The header and swig files are as follows:</p>
  
  <p>paska.h :</p>
  
  <pre><code>typedef void (handleri)(int code, char* codename);
  
  // handleri is now an alias to a function that eats int, string and returns void
  
  void wannabe_handleri(int i, char* blah);
  
  void handleri_eater(handleri* h);
  </code></pre>
  
  <p>paska.i :</p>
  
  <pre><code>%module paska
  
  %{ // this section is copied in the front of the wrapper file
  #define SWIG_FILE_WITH_INIT
  #include "paska.h"
  %}
  
  // from now on, what are we going to wrap ..
  
  %inline %{
  // helper functions here
  
  void wannabe_handleri(int i, char* blah) {
  };
  
  void handleri_eater(handleri* h) {
  };
  
  %}
  
  %include "paska.h"
  
  // in this case, we just put the actual .cpp code into the inline block ..
  </code></pre>
  
  <p>Finally, I test in python ..</p>
  
  <pre><code>import paska
  
  def testfunc(i, st):
    print i
    print st
  
  paska.handleri_eater(paska.wannabe_handleri(1,"eee")) # THIS WORKS!
  
  paska.handleri_eater(testfunc) # THIS DOES NOT WORK!
  </code></pre>
  
  <p>The last line throws me "TypeError: in method 'handleri_eater', argument 1 of type 'handleri *'"</p>
  
  <p>Is there any way to "cast" the python function to a type accepted by the SWIG wrapper?</p>