------------------------------------------------------------------------------ * Add paths to VTK's Python modules. */
| 327 | * Add paths to VTK's Python modules. |
| 328 | */ |
| 329 | void SetupPythonPaths(bool isolated, std::string vtklib, const char* landmark) |
| 330 | { |
| 331 | // Check if we're using an isolated Python. |
| 332 | if (isolated) |
| 333 | { |
| 334 | VTKPY_DEBUG_MESSAGE("Isolated Python detected; skipping setting up of `vtk` package."); |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | using systools = vtksys::SystemTools; |
| 339 | if (vtklib.empty()) |
| 340 | { |
| 341 | VTKPY_DEBUG_MESSAGE( |
| 342 | "`GetVTKVersion` library couldn't be found. Will use `sys.executable` next."); |
| 343 | } |
| 344 | |
| 345 | if (vtklib.empty()) |
| 346 | { |
| 347 | #if PY_VERSION_HEX >= 0x03080000 |
| 348 | vtkPythonScopeGilEnsurer gilEnsurer; |
| 349 | PyObject* executable_path = PySys_GetObject("executable"); |
| 350 | if (executable_path != Py_None) |
| 351 | { |
| 352 | vtklib = PyUnicode_AsUTF8AndSize(executable_path, nullptr); |
| 353 | } |
| 354 | #else |
| 355 | vtklib = vtk_Py_WideToUTF8(Py_GetProgramName()); |
| 356 | #endif |
| 357 | } |
| 358 | |
| 359 | vtklib = systools::CollapseFullPath(vtklib); |
| 360 | const std::string vtkdir = systools::GetFilenamePath(vtklib); |
| 361 | |
| 362 | #if defined(_WIN32) && !defined(__CYGWIN__) && defined(VTK_BUILD_SHARED_LIBS) |
| 363 | // On Windows, based on how the executable is run, we end up failing to load |
| 364 | // pyd files due to inability to load dependent dlls. This seems to overcome |
| 365 | // the issue. |
| 366 | if (!vtkdir.empty()) |
| 367 | { |
| 368 | #if PY_VERSION_HEX >= 0x03080000 |
| 369 | vtkPythonScopeGilEnsurer gilEnsurer; |
| 370 | CloseDLLDirectoryCookie(); |
| 371 | PyObject* os = PyImport_ImportModule("os"); |
| 372 | if (os) |
| 373 | { |
| 374 | PyObject* add_dll_directory = PyObject_GetAttrString(os, "add_dll_directory"); |
| 375 | if (add_dll_directory && PyCallable_Check(add_dll_directory)) |
| 376 | { |
| 377 | PyObject* newpath = PyUnicode_FromString(vtkdir.c_str()); |
| 378 | DLLDirectoryCookie = PyObject_CallFunctionObjArgs(add_dll_directory, newpath, nullptr); |
| 379 | Py_XDECREF(newpath); |
| 380 | } |
| 381 | |
| 382 | Py_XDECREF(add_dll_directory); |
| 383 | } |
| 384 | |
| 385 | Py_XDECREF(os); |
| 386 | #else |
no test coverage detected