Embedded and extending python 2.5: trouble with __main__ inPyRun_SimpleFileExFlags?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Carl Douglas

    #1

    Embedded and extending python 2.5: trouble with __main__ inPyRun_SimpleFileExFlags?

    Hi Python fans,

    I am developing a DLL that is loaded by a host application on windows.
    I'm using python 2.5.

    My DLL uses an embedded python interpreter which can access the host
    application through an API which I have exposed using SWIG 1.3.31.

    Therefore I have both extended and embedded Python at once: the SWIG
    generated code is statically linked into my DLL, and one of the
    functions in my DLL executes PyRun_SimpleFil e.

    Using python25_d.dll, I stepped through some python code to understand
    what is happening, and it appears that PyRun_SimpleFil e is returning
    -1 because python cannot create the module __main__.

    Here's the code from pythonrun.c:

    int
    PyRun_SimpleFil eExFlags(FILE *fp, const char *filename, int closeit,
    PyCompilerFlags *flags)
    {
    PyObject *m, *d, *v;
    const char *ext;

    m = PyImport_AddMod ule("__main__") ;
    if (m == NULL)
    return -1;
    ..
    ..
    ..
    }

    BTW, PyImport_AddMod ule fails because deeper down a dictionary check fails:

    if (!PyDict_Check( op))
    return NULL;


    Can someone suggest what I need to do to get this to work?

    Here are the relevant lines from my code:

    if (GetOpenFileNam e(&ofn)==TRUE)
    {
    Py_Initialize() ;
    init_mymodule() ; // SWIG generated method

    PyObject* PyFileObject = PyFile_FromStri ng(ofn.lpstrFil e, "r");

    if (PyRun_SimpleFi le(PyFile_AsFil e(PyFileObject) , ofn.lpstrFile)= =-1)
    {
    MessageBox(NULL , "error running script", "Python", MB_ICONERROR);
    }

    Py_DECREF(PyFil eObject);

    Py_Finalize();
    }

    Thanks.
Working...