diff --git a/src/App/Application.cpp b/src/App/Application.cpp index 7475eebaa..585de9e6a 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -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()); diff --git a/src/Base/Interpreter.cpp b/src/Base/Interpreter.cpp index 11200a934..e54da64af 100644 --- a/src/Base/Interpreter.cpp +++ b/src/Base/Interpreter.cpp @@ -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: "; diff --git a/src/Base/Interpreter.h b/src/Base/Interpreter.h index 1e740fd15..9f32d68d3 100644 --- a/src/Base/Interpreter.h +++ b/src/Base/Interpreter.h @@ -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 diff --git a/src/Gui/Macro.cpp b/src/Gui/Macro.cpp index 81b25ddc4..067f44fa1 100644 --- a/src/Gui/Macro.cpp +++ b/src/Gui/Macro.cpp @@ -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) { diff --git a/src/Gui/PythonDebugger.cpp b/src/Gui/PythonDebugger.cpp index 8e8ecead1..48a688a35 100644 --- a/src/Gui/PythonDebugger.cpp +++ b/src/Gui/PythonDebugger.cpp @@ -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&) { }