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

@@ -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: ";