title: What Is The Cleanest Way to Call A Python Function From C++ with a SWIG Wrapped Object
body: <p>I have the following code, which implements a simple C++ class (ObjWithPyCallback) with a Python callback function. The idea is to call the Python function with "this" as the single argument. </p>
  
  <p>The problem is that since ObjWithPyCallback is a SWIG wrapped object I need the SWIG typeinfo in order to create a Python object. </p>
  
  <p>The problem with this is that it's inside of the SWIG generated file "ObjWithPyCallback_wrap.cxx". Can SWIG generate a header file? I have thus far not been able to make this happen. </p>
  
  <p>However, even with a header file there is a circular dependency between SWIG and my main implementation, which is annoying. I'd like to find a way to avoid it if at all possible. Ultimately ObjWithPyCallback ends up in a different shared library than the Python bindings.</p>
  
  <p>Is there a clean way to pull this off? I'm aware of <a href="https://stackoverflow.com/questions/1575802/python-callback-with-swig-wrapped-type">this post</a>, but it only addresses the mechanics of SWIG_NewPointerObj.</p>
  
  <p>Thanks in advance for any help!</p>
  
  <p>Here's the code:</p>
  
  <p>File: example.py</p>
  
  <pre><code>import cb
  
  def foo(x=None):
      print("Hello from Foo!")
      # I'd like x to be a reference to a ObjWithPyCallback object.
      print(x)
  
  o = cb.ObjWithPyCallback()
  o.setCallback(foo)
  o.call()
  </code></pre>
  
  <p>File: ObjWithPyCallback.h</p>
  
  <pre><code>#include &lt;Python.h&gt;
  
  class ObjWithPyCallback 
  {
     public:
  
        ObjWithPyCallback();
        void setCallback(PyObject *callback);
        void call();
  
        PyObject *callback_;
  };
  </code></pre>
  
  <p>File: ObjWithCallback.cpp</p>
  
  <pre><code>#include "ObjWithPyCallback.h"
  
  #include &lt;iostream&gt;
  
  ObjWithPyCallback::ObjWithPyCallback() : callback_(NULL) {}
  
  void ObjWithPyCallback::setCallback(PyObject* callback)
  {
     if (!PyCallable_Check(callback))
     {
        std::cerr &lt;&lt; "Object is not callable.\n";
     }
     else
     {
        if ( callback_ ) Py_XDECREF(callback_);
        callback_ = callback;
        Py_XINCREF(callback_);
     }
  }
  
  void ObjWithPyCallback::call()
  {
     if ( ! callback_ )
     {
        std::cerr &lt;&lt; "No callback is set.\n";
     }
     else
     {
        // I want to call "callback_(*this)", how to do this cleanly?
        PyObject *result = PyObject_CallFunction(callback_, "");
        if (result == NULL)
           std::cerr &lt;&lt; "Callback call failed.\n";
        else
           Py_DECREF(result);
     }
  }
  </code></pre>
  
  <p>File:: ObjWithPyCallback.i</p>
  
  <pre><code>%module cb
  %{
     #include "ObjWithPyCallback.h"
  %}
  
  %include "ObjWithPyCallback.h"
  </code></pre>