Re: Embedded and extending python 2.5: trouble with __main__ inPyRun_SimpleFileExFlags?

Discussion in 'Python' started by Carl Douglas, Mar 5, 2007.

  1. Carl Douglas

    Carl Douglas Guest

    I am going to answer this question myself, but perhaps this will be
    useful for someone else. My original C++ code was fine, the
    PyRun_SimpleFile was failing due to errors in my python code. To get
    a console and display the python interpreter errors I allocated a
    console, and redirected stdout and stderr:

    BOOL APIENTRY DllMain( HMODULE hModule,
    DWORD ul_reason_for_call,
    LPVOID lpReserved
    )
    {
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    AllocConsole();
    fpStdOut = freopen( "CONOUT$", "wt", stdout);
    fpStdErr = freopen( "CONOUT$", "wt", stderr);
    fprintf(stdout, "Console opened\n");
    Py_Initialize();
    init_mymodule(); // SWIG generated method
    break;
    case DLL_PROCESS_DETACH:
    fclose(fpStdErr);
    fclose(fpStdOut);
    FreeConsole();
    Py_Finalize();
    break;
    //case DLL_THREAD_ATTACH:
    //case DLL_THREAD_DETACH:
    }
    return TRUE;
    }

    Then, I can show the Python interpreter errors in the console using
    PyErr_Print() in my C++ code:

    // Display the Open dialog box.
    if (GetOpenFileName(&ofn)==TRUE)
    {
    // Thank you: http://www.ragestorm.net/tutorial?id=21#8
    PyObject* PyFileObject = PyFile_FromString(ofn.lpstrFile, "r");
    if (PyFileObject == NULL)
    {
    PyErr_Print();
    PyErr_Clear();
    return false; // Let the user know the error.
    }

    if (PyRun_SimpleFile(PyFile_AsFile(PyFileObject), ofn.lpstrFile) == -1)
    {
    PyErr_Print();
    PyErr_Clear();
    }

    Py_DECREF(PyFileObject);
    }



    On 2/19/07, Carl Douglas <> wrote:
    > 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_SimpleFile.
    >
    > Using python25_d.dll, I stepped through some python code to understand
    > what is happening, and it appears that PyRun_SimpleFile is returning
    > -1 because python cannot create the module __main__.
    >
    > Here's the code from pythonrun.c:
    >
    > int
    > PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
    > PyCompilerFlags *flags)
    > {
    > PyObject *m, *d, *v;
    > const char *ext;
    >
    > m = PyImport_AddModule("__main__");
    > if (m == NULL)
    > return -1;
    > .
    > .
    > .
    > }
    >
    > BTW, PyImport_AddModule 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 (GetOpenFileName(&ofn)==TRUE)
    > {
    > Py_Initialize();
    > init_mymodule(); // SWIG generated method
    >
    > PyObject* PyFileObject = PyFile_FromString(ofn.lpstrFile, "r");
    >
    > if (PyRun_SimpleFile(PyFile_AsFile(PyFileObject), ofn.lpstrFile)==-1)
    > {
    > MessageBox(NULL, "error running script", "Python", MB_ICONERROR);
    > }
    >
    > Py_DECREF(PyFileObject);
    >
    > Py_Finalize();
    > }
    >
    > Thanks.
    >
     
    Carl Douglas, Mar 5, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Townsend

    Extending Embedded Python

    Richard Townsend, Sep 24, 2005, in forum: Python
    Replies:
    1
    Views:
    282
    Fredrik Lundh
    Sep 25, 2005
  2. Torsten Bronger
    Replies:
    3
    Views:
    1,991
    Thomas Heller
    Mar 9, 2006
  3. Vertilka
    Replies:
    1
    Views:
    670
    Fredrik Lundh
    Jan 4, 2007
  4. Carl Douglas
    Replies:
    0
    Views:
    349
    Carl Douglas
    Feb 19, 2007
  5. vishnu
    Replies:
    0
    Views:
    375
    vishnu
    Mar 31, 2007
Loading...

Share This Page