<br><br><div class="gmail_quote">2009/12/8 Jadhav, Alok <span dir="ltr">&lt;<a href="mailto:alok.jadhav@credit-suisse.com">alok.jadhav@credit-suisse.com</a>&gt;</span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">







<div>


<p><font face="Arial" size="2">I am using SWIG to extend Python to C++.</font> <font face="Arial" size="2"></font> <font face="Arial" size="2"> But the questions I have consists of basic Python C API related. I hope this list will be able to help me. </font></p>


<p><font face="Arial" size="2">I am trying to solve a simple problem which many of you must have encountered. I wonder what I am doing different than others that I am facing a problem.</font></p>

<p><font face="Arial" size="2">Background</font>

<br><font face="Arial" size="2">-----------</font>
</p>

<p><font face="Arial" size="2">A c++ library is wrapped in my C++ Class. This new C++ class is extended in Python using SWIG. I have to call a python function from C++ code ( which is on a new thread spawned from C++ main thread).</font></p>


<p><font face="Arial" size="2">Issue</font>

<br><font face="Arial" size="2">-----</font>

<br><font face="Arial" size="2">I am able to call the function. No issues. I could pass basic data types such as int. No issues. But when I try to pass a C++ class object (eg RFAMessageWrapper) to my python function I get an exception. It is an Access violation exception. The memory could not be &quot;read&quot;.  I am not even able to catch this exception.</font></p>

<br>
<br>

<p><font face="Arial" size="2">Some code snippets</font>

<br><font face="Arial" size="2">--------------------</font>
</p>

<p><font face="Arial" size="2">C++ function which calls python function. ... </font>
</p>

<p><font face="Arial" size="2">[code]</font>

<br><font face="Arial" size="2">static void PythonCallBack(RFAMessage *msg, void *clientdata)</font>

<br><font face="Arial" size="2">{</font>

<br><font face="Arial" size="2">   PyObject *func, *arglist;</font>

<br><font face="Arial" size="2">   int blah = 1;</font>

<br><font face="Arial" size="2">  </font>

<br><font face="Arial" size="2">   PyGILState_STATE state; </font>

<br><font face="Arial" size="2">   state = PyGILState_Ensure();</font>

<br><font face="Arial" size="2">   func = (PyObject *) clientdata;               // Get Python function</font>

<br><font face="Arial" size="2">   RFAMessageWrapper msg2(msg);</font>

<br>        <font face="Arial" size="2">try {</font>

<br>                <font face="Arial" size="2">arglist = Py_BuildValue(&quot;(O)&quot;,msg2);             // Build argument list</font>

<br></p></div></blockquote><div>This is bad.   <font face="Arial" size="2">Py_BuildValue(&quot;(O)&quot;,</font> is expecting to find a PyObject, but <font face="Arial" size="2">msg2 is not a PyObject.<br><br>Additionally, PyObjects must be created with PyObject_New, and never stack allocated like msg2 is.<br>
</font><br>I cannot offer any solution, as it would be SWIG specific and I am not familiar with SWIG.<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><p>                <font face="Arial" size="2">//arglist = Py_BuildValue(&quot;(i)&quot;, blah);</font>

<br>                <font face="Arial" size="2">PyEval_CallObject(func,arglist);     // Call Python</font>

<br>        <font face="Arial" size="2">} catch (...) {</font>

<br>                <font face="Arial" size="2">cout&lt;&lt;&quot;Unknown exception...&quot;&lt;&lt;endl;</font>

<br>        <font face="Arial" size="2">}</font>

<br><font face="Arial" size="2">  Py_XDECREF(arglist);                           // Trash arglist</font>

<br><font face="Arial" size="2">   PyGILState_Release(state); </font>

<br><font face="Arial" size="2">}</font>

<br><font face="Arial" size="2">[/code]</font>
</p>
<br>

<p><font face="Arial" size="2">Python function call..</font>
</p>

<p><font face="Arial" size="2">[code]</font>

<br><font face="Arial" size="2">def callback_fun(msg):</font>

<br><font face="Arial" size="2">    try:</font>

<br><font face="Arial" size="2">        print &quot;RFAMessage received for service:&quot;+msg.service</font>

<br><font face="Arial" size="2">    except Exception:</font>

<br><font face="Arial" size="2">        print &quot;Exception handled&quot;</font>

<br><font face="Arial" size="2">[/code]</font>
</p>

<p><font face="Arial" size="2">Whenever I try to access msg (of type RFAMessageWrapper .. C++ object) I get exception. msg is not None. I checked that in if condition. even type(msg) raises exception.</font></p>

<p><font face="Arial" size="2">RFAMessageWrapper class has been wrapped properly by SWIG as I could create an object manually and use it in Python. But only when I pass it from C++ to python I get this</font> <font face="Arial" size="2">exception</font><font face="Arial" size="2">.</font></p>

<br>

<p><font face="Arial" size="2">I have spent long time to solve this issue but in vain. I hope I get some help from this Forum.</font>
</p>
<br>

<p><font face="Arial" size="2">Regards, </font>

<br><font face="Arial" size="2">Alok<span lang="ja"></span></font>
</p>

</div>
<br><p class="MsoNormal"><span style="font-size: 8pt; font-family: Courier;" lang="EN-US">==============================================================================<br>Please access the attached hyperlink for an important electronic communications disclaimer:<br>
<a href="http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html" target="_blank">http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html</a><br>==============================================================================</span></p>
<br>
<br>_______________________________________________<br>
Cplusplus-sig mailing list<br>
<a href="mailto:Cplusplus-sig@python.org">Cplusplus-sig@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/cplusplus-sig" target="_blank">http://mail.python.org/mailman/listinfo/cplusplus-sig</a><br></blockquote></div><br><br clear="all"><br>-- <br>Gustavo J. A. M. Carneiro<br>
INESC Porto, Telecommunications and Multimedia Unit<br>&quot;The universe is always one step beyond logic.&quot; -- Frank Herbert<br>