2 #include "PyEmbedder.h"
4 #if defined(_DEBUG) && defined(SWIG_PYTHON_INTERPRETER_NO_DEBUG)
16 PyEmbedder::PyEmbedder(
void)
22 PyEmbedder::~PyEmbedder(
void)
29 {
Std_TraceMsg(0,
"Caught Error in desctructor of PyEmbedder\r\n",
"", -1,
false,
true);}
32 void PyEmbedder::InitPython()
35 if(!Py_IsInitialized())
37 Py_SetProgramName(
"AnimatLab");
40 if(!Py_IsInitialized())
41 THROW_ERROR(Al_Err_lFailedToInitPython, Al_Err_strFailedToInitPython);
43 Simulator *lpSim = ActiveSim();
45 THROW_ERROR(Al_Err_lSimNotDefined, Al_Err_strSimNotDefined);
47 std::string strExePath = lpSim->ExecutablePath();
51 std::string strInit =
"import os,sys,traceback,math\n"
52 "sys.path.append(\"" + strExePath +
"\")\n"
53 "os.chdir(\"" + strExePath +
"\")\n";
54 iRet = PyRun_SimpleString(strInit.c_str());
56 THROW_ERROR(Al_Err_lFailedToInitPython, Al_Err_strFailedToInitPython);
59 iRet = PyRun_SimpleString(
"import AnimatSimPy\n");
61 THROW_ERROR(Al_Err_lFailedToImportAnimatSimPy, Al_Err_strFailedToImportAnimatSimPy);
64 std::string strExcept =
"def animat_excepthook(type, value, tb):\n"
65 " strErr = \"\".join(traceback.format_exception(type, value, tb))\n"
66 " AnimatSimPy.SetLastScriptError(strErr)\n"
67 "sys.excepthook = animat_excepthook\n";
68 iRet = PyRun_SimpleString(strExcept.c_str());
70 THROW_ERROR(Al_Err_lFailedToInitPython, Al_Err_strFailedToInitPython);
86 void PyEmbedder::FinalizePython()
88 if(Py_IsInitialized())
92 void PyEmbedder::ResetPython()
98 bool PyEmbedder::ExecutePythonScript(
const std::string strPy,
bool bThrowError)
100 if(m_bPyInit && strPy.length() > 0)
102 int iRet = PyRun_SimpleString(strPy.c_str());
105 std::string strErr = GetLastScriptError();
108 THROW_PARAM_ERROR(Al_Err_lExecutingPythonScript, Al_Err_strExecutingPythonScript,
"Trace:\r\n", strErr);
110 Std_LogMsg(StdLogError, strErr,
"", -1,
true);
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.
void Std_LogMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bPrintHeader)
Logs a message,.
std::string Std_Replace(std::string strVal, std::string strFind, std::string strReplace)
Replaces a substring with another string.