[C++-sig] embeded python and winsound

Irene Ladyko iladyko at odesk.com
Fri May 18 10:17:56 CEST 2007


Hello, c++-sig.

I use Python version 2.4.4 under win32.
I built python using pcbuild.sln. There is winsound module project in
it, so I built it too. After that I can run python.exe interpeter
directly and load winsound module in it. All be OK.
    D:\....>python.exe
    Python 2.4.4 (#71, May 17 2007, 16:53:33) [MSC v.1310 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    >>>import winsound
    >>>winsound.PlaySound("SystemExit", winsound.SND_ALIAS)

But I am interested in embeded python. I use this interpreter in
C++ code (MSVC7.1). And when I load winsound programmatically, the following error appears:
"No module named winsound". But such module as 'array' is imported
correctly:
          import array

What is wrong. Please, point me on my error.


Code:
#include <string>

#include "Python.h"


int main(int /*argc*/, char** /*argv*/)
{
        PyThreadState *m_threadState;
        
        // Initialize python
        Py_NoSiteFlag = 1;
        Py_Initialize();
        
        // Enable thread support.
        PyEval_InitThreads();

        // Delete default thread state.
        PyThreadState *l_default = PyThreadState_Get();
        Py_EndInterpreter(l_default);

        // Release global interpreter lock.
        PyEval_ReleaseLock();

        //start python
        PyEval_AcquireLock();
        m_threadState = Py_NewInterpreter();
        PyEval_ReleaseThread(m_threadState);

        //-------Load winsound module----------!!!!!!!!!!!!!!!!!!!!!!!!
        PyEval_AcquireThread(m_threadState);

        PyObject *l_module;
        PyObject *l_dict;
        PyObject *l_retValue;

        // we will use __main__ module as our global and local namespace.
        l_module = PyImport_AddModule("__main__");
        if (!l_module)
        {
                PyEval_ReleaseThread(m_threadState);
                return -1;
        }

        std::string l_str = "import winsound";
        l_dict = PyModule_GetDict(l_module);
        l_retValue = PyRun_StringFlags(
                        (char *)l_str.c_str(),
                        Py_file_input,
                        l_dict,
                        l_dict,
                        NULL);
        if (!l_retValue)
        {
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                // Enter into this block!!!!!!!!!!!!!!!!!!!
                PyEval_ReleaseThread(m_threadState);
                return -1;
        }
        Py_DECREF(l_retValue);

        PyEval_ReleaseThread(m_threadState);

        //End
        PyEval_AcquireThread(m_threadState);
        Py_EndInterpreter(m_threadState);
        PyEval_ReleaseLock();
        PyEval_AcquireLock();
        // Create default interpreter back.
        Py_NewInterpreter();
        Py_Finalize();

        return 0;
}

-- 
Best Regards, Irene Ladyko.




More information about the Cplusplus-sig mailing list