[C++-sig] Py_Finalize() crashes when PyImport_Import() fails

SrMandrake ElMago srmandrake at yahoo.com
Mon Aug 25 04:09:06 CEST 2008


Hello,
I am triying to learn some Python to use it with C++. After some time I managed to do what I was attempting, but I just realized a couple of days ago that there is a problem when I call Py_Finalize() just before my program exits.
The problem is as follows:
First of all, I am using VC Express 2005, on Windows XP.
My program is able to do everything perfectly as long as the script to load is found.
When the script is not found, the program keeps going perfectly, but crashes when calling Py_Finalize() raising an exception and poping the following message.
Exception exceptions.ImportError: 'No module named test_11' in 'garbage collection' ignored
Fatal Python error: unexpected exception during garbage collection
Now, I know the easiest solution to the problem is to put the file where it is supposed to be :-), but what I'm actually looking for is a way to prevent the error from crashing the program because, at the end, I would like the user to type the name of the file (script) to load (and there may be a typo or the entered file name may not exist ) Thanks in advance for any help you can give me... 
Rod-
Here's the code...
#include
 
int main()
{
Py_Initialize ();
//"test_1" is the actual name of the file. I changed it to "test_11" on purpose to cause the crash...
const char* s = "test_11";
PyObject* pName = PyString_FromString ( s );
PyObject* pModule = PyImport_Import ( pName );
if ( !pModule )
{    
    std::cout << "Unable to open script " << s << std::endl;
    getchar ();
}
Py_XDECREF( pModule );
Py_XDECREF( pName );
Py_Finalize ();
getchar ();
return0;
} <Python.h>


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20080824/3e837939/attachment.htm>


More information about the Cplusplus-sig mailing list