Menu

#445 win32com can be used only one time in embedded python

closed-wont-fix
nobody
com (105)
5
2009-09-14
2009-09-14
Cyrille Rey
No

Hello all

I embed python interpreters in a C++ program. For each script to execute I create a new interpreter. In this environment I can use the win32 extensions only one time. Here is a little piece of code to illustrate the problem:

int main(int argc, char **argv) { Py_Initialize(); Py_SetProgramName(argv[0]);

PyThreadState *save_tstate=PyThreadState_Swap(NULL); PyThreadState *state=Py_NewInterpreter();

int rc=PyRun_SimpleString("import sys\nimport win32com.client\nw=win32com.client.Dispatch("Word.Application")\nw.Visible = 1\n"); //this works well printf("%d\n",rc);

Py_EndInterpreter(state);

PyThreadState_Swap(save_tstate);

state=Py_NewInterpreter();

rc=PyRun_SimpleString("import sys\nimport win32com.client\nw=win32com.client.Dispatch("Word.Application")\nw.Visible = 1\n"); //this and all further atemps will fail printf("%d\n",rc);

Py_EndInterpreter(state); PyThreadState_Swap(save_tstate);

; }

Here is the output of this little program:

Traceback (most recent call last):
File "<string>", line 8 (13), in <module>
File "C:\cre\2009.2\techlog\PythonScripts\External261_x86\win32com\__init__.py", line 6, in <module>
import pythoncom
File "C:\cre\2009.2\techlog\PythonScripts\External261_x86\Lib\pythoncom.py", line 3, in <module>
File "C:\cre\2009.2\techlog\PythonScripts\External261_x86\Lib\pywintypes.py", line 20, in __import_pywin32_system_module__
TypeError: 'NoneType' object is not callable

I use

Python 2.6 compiled with MSC v.8 32 bit (Intel)] on win32

with

Pywin32- 214

Is there any way to overcome this problem?

Discussion

  • Mark Hammond

    Mark Hammond - 2009-09-14

    Sorry, this is a limitation of the PyGILState API used by pywin32; pywin32 will never be able to play with the multiple interpreter API, nor work correctly with multiple interpreter initializations and cleanups

     
  • Mark Hammond

    Mark Hammond - 2009-09-14
    • status: open --> closed-wont-fix