| 184 | } |
| 185 | |
| 186 | void addToPythonPath(EnergyPlusData &state, const fs::path &includePath, bool userDefinedPath) |
| 187 | { |
| 188 | if (includePath.empty()) { |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | // We use generic_string / generic_wstring here, which will always use a forward slash as directory separator even on windows |
| 193 | // This doesn't handle the (very strange, IMHO) case were on unix you have backlashes (which are VALID filenames on Unix!) |
| 194 | // Could use FileSystem::makeNativePath first to convert the backslashes to forward slashes on Unix |
| 195 | PyObject *unicodeIncludePath = nullptr; |
| 196 | if constexpr (std::is_same_v<typename fs::path::value_type, wchar_t>) { |
| 197 | const std::wstring ws = includePath.generic_wstring(); |
| 198 | unicodeIncludePath = PyUnicode_FromWideChar(ws.c_str(), static_cast<Py_ssize_t>(ws.size())); // New reference |
| 199 | } else { |
| 200 | const std::string s = includePath.generic_string(); |
| 201 | unicodeIncludePath = PyUnicode_FromString(s.c_str()); // New reference |
| 202 | } |
| 203 | if (unicodeIncludePath == nullptr) { |
| 204 | EnergyPlus::ShowFatalError( |
| 205 | state, EnergyPlus::format("ERROR converting the path \"{}\" for addition to the sys.path in Python", includePath.generic_string())); |
| 206 | } |
| 207 | |
| 208 | PyObject *sysPath = PySys_GetObject("path"); // Borrowed reference |
| 209 | int const ret = PyList_Insert(sysPath, 0, unicodeIncludePath); |
| 210 | Py_DECREF(unicodeIncludePath); |
| 211 | |
| 212 | if (ret != 0) { |
| 213 | if (PyErr_Occurred() != nullptr) { |
| 214 | reportPythonError(state); |
| 215 | } |
| 216 | EnergyPlus::ShowFatalError(state, EnergyPlus::format("ERROR adding \"{}\" to the sys.path in Python", includePath.generic_string())); |
| 217 | } |
| 218 | |
| 219 | if (userDefinedPath) { |
| 220 | EnergyPlus::ShowMessage(state, |
| 221 | EnergyPlus::format("Successfully added path \"{}\" to the sys.path in Python", includePath.generic_string())); |
| 222 | } |
| 223 | |
| 224 | // PyRun_SimpleString)("print(' EPS : ' + str(sys.path))"); |
| 225 | } |
| 226 | |
| 227 | void initPython(EnergyPlusData &state, fs::path const &pathToPythonPackages) |
| 228 | { |
no test coverage detected