[C++-sig] embeded python and winsound
Roman Yakovenko
roman.yakovenko at gmail.com
Fri May 18 20:26:19 CEST 2007
On 5/18/07, Irene Ladyko <iladyko at odesk.com> wrote:
> 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.
I guess the problem is related to location of winsound module. May be
you can move the module to one of the directories Python is looking at
or adjust sys.path variable.
By the way you can use Boost.Python functionality to simplify a lot your code:
http://boost.org/libs/python/doc/tutorial/doc/html/python/embedding.html
>
>
> 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.
>
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
>
--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
More information about the Cplusplus-sig
mailing list