0000528: Need a way to reset the Python instance

git-svn-id: https://free-cad.svn.sourceforge.net/svnroot/free-cad/trunk@5250 e8eeb9e2-ec13-0410-a4a9-efa5cf37419d
This commit is contained in:
wmayer 2011-12-10 13:42:32 +00:00
parent 847a9aad5e
commit 1884ee3881
5 changed files with 35 additions and 9 deletions

View File

@ -1166,7 +1166,7 @@ void Application::processCmdLineFiles(void)
Application::_pcSingleton->openDocument(File.filePath().c_str());
}
else if (File.hasExtension("fcscript")||File.hasExtension("fcmacro")) {
Base::Interpreter().runFile(File.filePath().c_str());
Base::Interpreter().runFile(File.filePath().c_str(), false);
}
else if (File.hasExtension("py")) {
Base::Interpreter().loadModule(File.fileNamePure().c_str());

View File

@ -210,7 +210,7 @@ void InterpreterSingleton::runInteractiveString(const char *sCmd)
Py_DECREF(presult);
}
void InterpreterSingleton::runFile(const char*pxFileName)
void InterpreterSingleton::runFile(const char*pxFileName, bool local)
{
#ifdef FC_OS_WIN32
FileInfo fi(pxFileName);
@ -222,11 +222,37 @@ void InterpreterSingleton::runFile(const char*pxFileName)
PyGILStateLocker locker;
//std::string encoding = PyUnicode_GetDefaultEncoding();
//PyUnicode_SetDefaultEncoding("utf-8");
int ret = PyRun_SimpleFile(fp, pxFileName);
//PyUnicode_SetDefaultEncoding(encoding.c_str());
fclose(fp);
if (ret != 0)
throw PyException();
if (local) {
PyObject *module, *dict;
module = PyImport_AddModule("__main__");
dict = PyModule_GetDict(module);
dict = PyDict_Copy(dict);
if (PyDict_GetItemString(dict, "__file__") == NULL) {
PyObject *f = PyString_FromString(pxFileName);
if (f == NULL)
return;
if (PyDict_SetItemString(dict, "__file__", f) < 0) {
Py_DECREF(f);
return;
}
Py_DECREF(f);
}
PyObject *result = PyRun_File(fp, pxFileName, Py_file_input, dict, dict);
fclose(fp);
Py_DECREF(dict);
if (!result)
throw PyException();
Py_DECREF(result);
}
else {
int ret = PyRun_SimpleFile(fp, pxFileName);
fclose(fp);
if (ret != 0)
throw PyException();
}
}
else {
std::string err = "Unknown file: ";

View File

@ -147,7 +147,7 @@ public:
/// Run a statement on the python interpreter and gives back a string with the representation of the result.
void runInteractiveString(const char *psCmd);
/// Run file (script) on the python interpreter
void runFile(const char*pxFileName);
void runFile(const char*pxFileName, bool local);
/// Run a statement with arguments on the python interpreter
void runStringArg(const char * psCom,...);
/// runs a python object method with no return value and no arguments

View File

@ -221,7 +221,7 @@ void MacroManager::run(MacroType eType,const char *sName)
PythonRedirector std_out("stdout",new OutputStdout);
PythonRedirector std_err("stderr",new OutputStderr);
//The given path name is expected to be Utf-8
Base::Interpreter().runFile(sName);
Base::Interpreter().runFile(sName, true);
}
catch (const Base::Exception& e)
{

View File

@ -414,7 +414,7 @@ void PythonDebugger::runFile(const QString& fn)
{
try {
RunningState state(d->running);
Base::Interpreter().runFile((const char*)fn.toUtf8());
Base::Interpreter().runFile((const char*)fn.toUtf8(), true);
}
catch (const Base::PyException&) {
}