| 1227 | } |
| 1228 | |
| 1229 | void PluginManager::addToPythonPath(EnergyPlusData &state, const fs::path &includePath, bool userDefinedPath) |
| 1230 | { |
| 1231 | if (includePath.empty()) { |
| 1232 | return; |
| 1233 | } |
| 1234 | |
| 1235 | // We use generic_string / generic_wstring here, which will always use a forward slash as directory separator even on windows |
| 1236 | // This doesn't handle the (very strange, IMHO) case were on unix you have backlashes (which are VALID filenames on Unix!) |
| 1237 | // Could use FileSystem::makeNativePath first to convert the backslashes to forward slashes on Unix |
| 1238 | PyObject *unicodeIncludePath; |
| 1239 | // ReSharper disable once CppRedundantTypenameKeyword |
| 1240 | if constexpr (std::is_same_v<typename fs::path::value_type, wchar_t>) { |
| 1241 | // ReSharper disable once CppDFAUnreachableCode |
| 1242 | const std::wstring ws = includePath.generic_wstring(); |
| 1243 | unicodeIncludePath = PyUnicode_FromWideChar(ws.c_str(), static_cast<Py_ssize_t>(ws.size())); // New reference |
| 1244 | } else { |
| 1245 | const std::string s = includePath.generic_string(); |
| 1246 | unicodeIncludePath = PyUnicode_FromString(s.c_str()); // New reference |
| 1247 | } |
| 1248 | if (unicodeIncludePath == nullptr) { |
| 1249 | ShowFatalError(state, EnergyPlus::format("ERROR converting the path \"{:g}\" for addition to the sys.path in Python", includePath)); |
| 1250 | } |
| 1251 | |
| 1252 | PyObject *sysPath = PySys_GetObject("path"); // Borrowed reference |
| 1253 | int const ret = PyList_Insert(sysPath, 0, unicodeIncludePath); |
| 1254 | Py_DECREF(unicodeIncludePath); |
| 1255 | |
| 1256 | if (ret != 0) { |
| 1257 | if (PyErr_Occurred() != nullptr) { |
| 1258 | PluginInstance::reportPythonError(state); |
| 1259 | } |
| 1260 | ShowFatalError(state, EnergyPlus::format("ERROR adding \"{:g}\" to the sys.path in Python", includePath)); |
| 1261 | } |
| 1262 | |
| 1263 | if (userDefinedPath) { |
| 1264 | ShowMessage(state, EnergyPlus::format("Successfully added path \"{:g}\" to the sys.path in Python", includePath)); |
| 1265 | } |
| 1266 | |
| 1267 | // PyRun_SimpleString)("print(' EPS : ' + str(sys.path))"); |
| 1268 | } |
| 1269 | #else |
| 1270 | std::vector<std::string> PluginManager::currentPythonPath() |
| 1271 | { |
nothing calls this directly
no test coverage detected