| 200 | } |
| 201 | |
| 202 | bool prepend_sys_path_once(PyObject* const sys_path, |
| 203 | const std::filesystem::path& path, |
| 204 | const char* label) { |
| 205 | if (!sys_path || path.empty()) { |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | const auto path_utf8 = lfs::core::path_to_utf8(path); |
| 210 | PyObject* const py_path = PyUnicode_FromString(path_utf8.c_str()); |
| 211 | if (!py_path) { |
| 212 | LOG_WARN("Failed to create Python path string for {}: {}", label, path_utf8); |
| 213 | PyErr_Clear(); |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | const int contains = PySequence_Contains(sys_path, py_path); |
| 218 | if (contains < 0) { |
| 219 | LOG_WARN("Failed to inspect sys.path while adding {}: {}", label, path_utf8); |
| 220 | PyErr_Clear(); |
| 221 | Py_DECREF(py_path); |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | if (contains == 0) { |
| 226 | if (PyList_Insert(sys_path, 0, py_path) != 0) { |
| 227 | LOG_WARN("Failed to prepend {} to sys.path: {}", label, path_utf8); |
| 228 | PyErr_Clear(); |
| 229 | Py_DECREF(py_path); |
| 230 | return false; |
| 231 | } |
| 232 | LOG_INFO("Added {} to Python path: {}", label, path_utf8); |
| 233 | } |
| 234 | |
| 235 | Py_DECREF(py_path); |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | #ifdef LFS_DEV_PYTHON_SOURCE_DIR |
| 240 | void prepend_dev_python_source_path(PyObject* const sys_path) { |
no test coverage detected