embedding in COM object confusion

Mark Hammond mhammond at skippinet.com.au
Sun May 25 19:49:26 EDT 2003


Achim Domma wrote:

> 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 am surprised that fails.  However, calling Py_Init/Fini like this is 
asking for some trouble.  I suggest you simply init once, and finalize 
once (if at all).  Yes, it *should* work, but...

If you really care, you could re-write this in C and turn it into a 
standalone test that fails, then upload it to a new bug on sourceforge.

If you still have problems, I suggest you build a debug version of 
Python, and use that.  This will allow you to break into the debugger, 
and see exactly where the error happens.

Also, make sure your COM object is using /MD (or /MDd for debug builds) 
to ensure you have the same C runtime lib as Python.

Finally, if you ever will need either threads or callbacks, I suggest 
you use Python 2.3 and the new PyGILState_ API to help manage the 
thread-state from your C code.

Mark.





More information about the Python-list mailing list