embedding in COM object confusion

Achim Domma domma at procoders.net
Sun May 25 08:31:37 EDT 2003


Hi,

I tried to embed python in a COM object using boost.python. For my first
try, I called Py_Initialize()/Py_Finalize() for each COM method call. The
method in C++ looks like this:

STDMETHODIMP CMyClass::DoSomething(BSTR someData, FLOAT* result)
{
    Py_Initialize();
    handle<> main_module(borrowed( PyImport_AddModule("__main__") ));
    handle<> main_namespace(borrowed(
PyModule_GetDict(main_module.get()) ));
    handle<>( PyRun_String("print 'COM
test'\n",Py_file_input,main_namespace.get(), main_namespace.get()) );
    Py_Finalize();
    return S_OK;
}

I compiled the object and wrote a Testscript in Python which failed with:

Fatal Python error: PyThreadState_Get: no current thread
abnormal program termination

After a little bit of googling I found out, that it has something to do with
threads. I tried PyEval_InitThreads(), PyEval_AcquireLock(), ... but
couldn't get it to work. Then I wrote a testscript in Javascript and my
object works without problems. Is there any possibility to get the other
szenario to work? Should I create my own interpreter? Or what do I have to
do to coexist friendly with an existing Python in the same process?

Achim






More information about the Python-list mailing list