| 2958 | |
| 2959 | |
| 2960 | void |
| 2961 | AppManager::initPython() |
| 2962 | { |
| 2963 | #ifdef NATRON_RUN_WITHOUT_PYTHON |
| 2964 | |
| 2965 | return; |
| 2966 | #endif |
| 2967 | //Disable user sites as they could conflict with Natron bundled packages. |
| 2968 | //If this is set, Python won’t add the user site-packages directory to sys.path. |
| 2969 | //See https://www.python.org/dev/peps/pep-0370/ |
| 2970 | qputenv("PYTHONNOUSERSITE", "1"); |
| 2971 | ++Py_NoUserSiteDirectory; |
| 2972 | |
| 2973 | // |
| 2974 | // set up paths, clear those that don't exist or are not valid |
| 2975 | // |
| 2976 | QString binPath = QCoreApplication::applicationDirPath(); |
| 2977 | binPath = QDir::toNativeSeparators(binPath); |
| 2978 | NATRON_PYTHON_NAMESPACE::setupPythonEnv(binPath.toStdString()); |
| 2979 | |
| 2980 | ///Must be called prior to Py_Initialize (calls PyImport_AppendInittab()) |
| 2981 | initBuiltinPythonModules(); |
| 2982 | |
| 2983 | #if PY_MAJOR_VERSION >= 3 |
| 2984 | _imp->mainModule = NATRON_PYTHON_NAMESPACE::initializePython3(_imp->commandLineArgsWide); |
| 2985 | #else |
| 2986 | _imp->mainModule = NATRON_PYTHON_NAMESPACE::initializePython2(_imp->commandLineArgsUtf8); |
| 2987 | #endif |
| 2988 | |
| 2989 | std::string err; |
| 2990 | std::string modulename = NATRON_ENGINE_PYTHON_MODULE_NAME; |
| 2991 | bool ok = NATRON_PYTHON_NAMESPACE::interpretPythonScript("import sys\nfrom math import *\nimport " + modulename, &err, 0); |
| 2992 | if (!ok) { |
| 2993 | throw std::runtime_error( tr("Error while loading python module %1: %2").arg( QString::fromUtf8( modulename.c_str() ) ).arg( QString::fromUtf8( err.c_str() ) ).toStdString() ); |
| 2994 | } |
| 2995 | |
| 2996 | ok = NATRON_PYTHON_NAMESPACE::interpretPythonScript(modulename + ".natron = " + modulename + ".PyCoreApplication()\n", &err, 0); |
| 2997 | assert(ok); |
| 2998 | if (!ok) { |
| 2999 | throw std::runtime_error( tr("Error while loading python module %1: %2").arg( QString::fromUtf8( modulename.c_str() ) ).arg( QString::fromUtf8( err.c_str() ) ).toStdString() ); |
| 3000 | } |
| 3001 | |
| 3002 | if ( !isBackground() ) { |
| 3003 | modulename = NATRON_GUI_PYTHON_MODULE_NAME; |
| 3004 | ok = NATRON_PYTHON_NAMESPACE::interpretPythonScript("import sys\nimport " + modulename, &err, 0); |
| 3005 | assert(ok); |
| 3006 | if (!ok) { |
| 3007 | throw std::runtime_error( tr("Error while loading python module %1: %2").arg( QString::fromUtf8( modulename.c_str() ) ).arg( QString::fromUtf8( err.c_str() ) ).toStdString() ); |
| 3008 | } |
| 3009 | |
| 3010 | ok = NATRON_PYTHON_NAMESPACE::interpretPythonScript(modulename + ".natron = " + |
| 3011 | modulename + ".PyGuiApplication()\n", &err, 0); |
| 3012 | assert(ok); |
| 3013 | if (!ok) { |
| 3014 | throw std::runtime_error( tr("Error while loading python module %1: %2").arg( QString::fromUtf8( modulename.c_str() ) ).arg( QString::fromUtf8( err.c_str() ) ).toStdString() ); |
| 3015 | } |
| 3016 | |
| 3017 | //redirect stdout/stderr |
nothing calls this directly
no test coverage detected