Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void ModifierWidgetPythonScript::openScript(QString fileName)
- {
- QFileInfo fileInfo(fileName);
- /*
- * Equivalent to:
- * import sys
- * del sys.modules['module']
- */
- if(py_->module)
- {
- PyObject* modules = PyImport_GetModuleDict();
- PyDict_DelItem(modules, PyUnicode_FromString(PyModule_GetName(py_->module)));
- }
- // Release references to previous module + callback functions
- py_->flushFunction.reset();
- py_->applyFunction.reset();
- py_->module.reset();
- /*
- * Need to first change the path to the directory where the script lies, by
- * adding the path to the script to sys.path. The following checks if the
- * path already exists in sys.path, and adds it if it doesnt.
- */
- std::wstring pathToAdd = fileInfo.absolutePath().toStdWString();
- PyObject* pySysPath = PySys_GetObject("path");
- PythonObject<> pyPathToAdd = PyUnicode_FromWideChar(pathToAdd.c_str(), wcslen(pathToAdd.c_str()));
- int pyLen = PyList_Size(pySysPath);
- for(int i = 0; i != pyLen; ++i)
- {
- PyObject* item = PyList_GET_ITEM(pySysPath, i);
- if(PyUnicode_Compare(item, pyPathToAdd) == 0)
- goto path_already_added;
- }
- PyList_Append(pySysPath, pyPathToAdd.steal());
- path_already_added:
- PythonObject<> scriptFile = PyUnicode_DecodeFSDefault(fileInfo.fileName().replace(".py", "").toUtf8().data());
- if(scriptFile == NULL)
- {
- defaultLogger.logError("Failed to load python script %s, file name to unicode conversion failed.", fileName.toLatin1().data());
- return;
- }
- py_->module = PyImport_Import(scriptFile);
- if(py_->module == NULL)
- {
- PythonInterpreter_logError(QString("Failed to import script " + fileName).toLatin1().data());
- return;
- }
- // other stuff
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement