title: Swig: SWIG_NewPointerObj and Typemaps
body: <p>I'm working on a Qt-Application and I use Swig to generate Python Bindings for my Public API.</p>
  
  <p>I trying to implement signal/slots in SWIG and also embedding python into my final app. In the end it always boils down to a simple function, which SWIG seems to miss or I haven't found yet:</p>
  
  <p><code>PyObject* createPyObject(QString typeName, bool passOwnership, void* obj)</code></p>
  
  <h2>Requirements</h2>
  
  <p>The function should create a PyObject from the passed <code>obj</code>. Whereas <code>typeName</code> is the type of the instance to which <code>obj</code> points to and <code>passOwnership</code> specifies whether or not the object should be owned by the Python Interpreter afterwards. The function should:  </p>
  
  <ol>
  <li>Apply the <strong>out</strong>-Typemap for the passed type, if it exists</li>
  <li>Otherwise wrap the object as a "Swig Proxy Object of Type ..." if possible</li>
  <li>And if that's also not possible it should return a "Swig Object of Type ..."</li>
  </ol>
  
  <h2>Example</h2>
  
  <p>Let's say I have three classes:  </p>
  
  <ul>
  <li><code>class A1</code> for which I have a <strong>out</strong>-Typemap to convert from instances of this class to  <em>Python-Integers</em>.</li>
  <li><code>class A2</code> for which I have a <strong>out</strong>-Typemap to convert from instances of this class to <em>Python-Tuples</em>.</li>
  <li><code>class B</code> which I have wrapped using a SWIG Interface file</li>
  <li><code>class C</code> which is completely unknown to SWIG.</li>
  </ul>
  
  <p>then the <code>createPyObject</code> should</p>
  
  <ul>
  <li>return a <code>PyIntObject*</code> if it's invoked like <code>createPyObj("A1",true,new A1())</code></li>
  <li>return a <code>PyTupleObject*</code> if it's invoked like <code>createPyObj("A2",true,new A2())</code></li>
  <li>return a <code>proxy of &lt;Swig Object of Type 'B*'&gt;</code> if it's invoked like <code>createPyObj("B",true,new B())</code></li>
  <li>return a <code>Swig Object of Type 'C*'</code> if it's invoked like <code>createPyObj("C",true,new C())</code></li>
  </ul>
  
  <p>So to summarize: 
  I want the returned PyObject to be constructed like as if it was a return value of a function which was wrapped using a SWIG interface file.</p>
  
  <h2>My current implementation</h2>
  
  <pre><code>PyObject* createPyObject(QString typeName, bool passOwnership, void* obj) {
  
      typeName += " *"; //Pointer symbol
      swig_type_info* info = SWIG_TypeQuery(typeName.toStdString().c_str());
  
      if (info == nullptr) return nullptr;
  
      return SWIG_NewPointerObj(obj, info, passOwnership ? SWIG_POINTER_OWN : 0);
  }
  </code></pre>
  
  <p>This works fine for types which require no typemap and are known to SWIG (Point 2 in the Requirements). But it doesn't work for types which require a typemap.</p>
  
  <p><strong>Could you provide me with a correct implementation of  <code>createPyObject</code> ?</strong> By correct I mean, one that meets all the requirements listed above.</p>