importing multiarraymodule.c in Python embedding

I have run into an interesting issue with multiarraymodule.c with regards to embedding Python in a C application on Windows XP. In the application, the following abbreviated sequence was executed 1) Py_Initialize 2) numpy is imported 3) Py_Finalize 4) Py_Initialize 5) attempt to import numpy results in an error. I have tracked this down to two things 1) Python does not call the FreeLibrary windows API on C extension modules when shutting down 2) multiarraymodule.c has a static variable _multiarray_module_loaded which, when set, bypasses the call to Py_InitModule The error at step 5 above occurs because the multiarraymodule DLL is loaded at step 2, the _multiarray_module_loaded variable is set at step 2, and Py_InitModule is bypassed at step 5. Since Py_InitModule is not called, multiarraymodule is not placed in the module dictionary (i.e. sys.modules) and an error occurs in importdl.h which checks if the module is there. The obvious solution is to not execute the sequence above. This issue does seem to highlight a weakness in the implementation of multiarraymodule.c. Could someone comment on the need for static variable _multiarray_module_loaded? Is there a more robust way to achieve the goal? Thanks. Greg

Hi, it seems that I have the same trouble, but using Python under Linux:
I have run into an interesting issue with multiarraymodule.c with regards to embedding Python in a C application on Windows XP. In the application, the following abbreviated sequence was executed
1) Py_Initialize
2) numpy is imported
3) Py_Finalize
4) Py_Initialize
5) attempt to import numpy results in an error.
As an example I wrote a small C program that embeds Python. The steps are the same as above: 1) Python is initialized (Py_Initialize) 2) Numpy is "manually" imported (import numpy) under an interactive loop (PyRun_InteractiveLoop) 3) Python is finalized (Py_Finalize) 4) Python is initialized again (Py_Initialize) 5) Attempt to import numpy now fails:
import numpy Traceback (most recent call last): File "<STDIN>", line 1, in <module> File "/usr/lib/python2.5/site-packages/numpy/__init__.py", line 36, in <module> import core File "/usr/lib/python2.5/site-packages/numpy/core/__init__.py", line 5, in <module> import multiarray SystemError: dynamic module not initialized properly
I'm using Numpy 1.0 and Python 2.5 under a Fedora Core 6: Python 2.5 (r25:51908, Nov 15 2006, 14:24:03) [GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2 I have not searched further why this happens. I embed Python in a larger project and let the choice to the user to start a Python session if he needs it. Numpy is automatically imported right after Python initialization (because it is needed in my context), so that if user starts a session, ends it, and starts a new one, the initialization automatically fails at the second time. The solution at the present I found is preventing Python to be finalized (Py_Finalize). I hope this can help. Thanks. Sebastien
participants (2)
-
Sebastien Bardeau
-
Steele, Greg