<div><div>Thanks Gabriel,</div><div><br></div><div>I've managed to get it working and so far stable...</div><div><br></div><div>What wasn't working reliably:</div><div><br></div><div>mycppclass</div><div>   mycppclass::mycppclass()</div>
<div>       m_mypymodule = PyImport_Import(pModuleName)</div><div><br></div><div>  mycppclass::~ mycppclass()</div><div>      Py_XDECREF(m_mypymodule)</div><div><br></div><div>  mycppclass::callpy(funcname, args...)</div>
<div>      PyTuple_SetItem * args</div><div>      PyCallable_Check(func)</div><div>      PyObject_CallObject(func)</div><div><br></div><div>Current working version:</div><div><br></div><div><div>mycppclass</div><div>   mycppclass::mycppclass()</div>
<div>   {}</div><div><br></div><div>  mycppclass::~ mycppclass()</div><div>  {}</div><div><br></div><div>  mycppclass::callpy(funcname, args...)</div><div>      m_mypymodule = PyImport_Import(pModuleName)</div><div><br></div>
<div>      pyargs = PyTuple_SetItem * args</div><div>      PyCallable_Check(func)</div><div>      PyObject_CallObject(func,pyargs)</div><div><br></div><div>      Py_XDECREF(m_mypymodule)</div><div><br></div><div>So now the module is being imported each function call (luckily I don't have to worry about performance)</div>
<div><br></div><div>I assume this means that the internal representation of the imported module is being corrupted by something. I found another person with a similar issue here:</div><div><a href="http://mail.python.org/pipermail/python-dev/2004-March/043306.html">http://mail.python.org/pipermail/python-dev/2004-March/043306.html</a> - that is a long time ago but another multi-threaded app.</div>
<div><br></div><div class="gmail_quote">I'm happy to use the working method but I'd like to understand what is going on a bit more. Can anyone shed any further light?</div><div class="gmail_quote"><br></div><div class="gmail_quote">
Regards,</div><div class="gmail_quote">Paul.</div><div class="gmail_quote"><br></div><div class="gmail_quote">On Tue, Feb 2, 2010 at 11:59 AM, Gabriel Genellina <span dir="ltr"><<a href="mailto:gagsl-py2@yahoo.com.ar">gagsl-py2@yahoo.com.ar</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
En Mon, 01 Feb 2010 18:21:56 -0300, Paul <<a href="mailto:gobladoome@gmail.com" target="_blank">gobladoome@gmail.com</a>> escribió:<div><div></div><div class="h5"><br><br><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
I'm extending some old Visual Studio 6 code to add embedded python<br>scripting. It works fine most of the time but some python function calls do<br>not work as expected.<br><br>The C++ code is a multithreaded MFC application. I was assuming that it was<br>
GIL issues but I have tried using the manual locking (PyEval_SaveThread &<br>PyEval_RestoreThread) and what seems to be the current method<br>(PyGILState_Ensure & PyGILState_Release)<br><br>Here's the error I'm getting:<br>
<br> Traceback (most recent call last):<br> File "...scripts\receipt_parser.py", line 296, in<br>get_giftcard_purchase_value<br>   details = extract_transaction_details_section(test)<br> File "...scripts\receipt_parser.py", line 204, in<br>
extract_transaction_details_section<br>   for line in base_details:<br>TypeError: expected string or Unicode object, NoneType found<br><br>base_details is a list of strings (I can just define it like<br>'base_details=["1","2","3"...]' on the line previous) and the code runs fine<br>
when run from standard interpreter. Many other function calls work fine from<br>the embedded app.<br>I create and then Py_DECREF the function parameters and the return value<br>after each function call. The module import is created at C++ object<br>
constructor and then Py_DECREF'd in the desctuctor<br></blockquote><br></div></div>Usually, errors in reference count handling prevent objects from being destroyed (a memory leak) or generate a GPF when accessing an now-inexistent object. In principle I'd look elsewhere.<div class="im">
<br><br><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
Anyone else had issues of this kind?<br></blockquote><br></div>Hard to tell without more info. base_details is built in C++ code? Is it actually a list, or a subclass defined by you?<br>A common error is to forget to check *every* API function call for errors, so errors get unnoticed for a while but are reported on the next check, which may happen in an entirely unrelated function.<div class="im">
<br><br><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
My next try will be to use<br>sub-interpreters per thread.<br></blockquote><br></div>I would not do that - I'd try to *simplify* the code to test, not make it more complicated.<br>Does it work in a single-threaded application?<br>
<br>-- <br><font color="#888888">Gabriel Genellina</font><div><div></div><div class="h5"><br><br>-- <br><a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div><div><br></div></div></blockquote></div></div></div>