1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkPythonInterpreter.h"
#include "vtkPython.h" // this must be the first include.
#include "vtkBuild.h"
#include "vtkCommand.h"
#include "vtkLogger.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkOutputWindow.h"
#include "vtkPythonStdStreamCaptureHelper.h"
#include "vtkResourceFileLocator.h"
#include "vtkVersion.h"
#include "vtkWeakPointer.h"
#include "vtksys/Encoding.h"
#include <vtksys/Encoding.hxx>
#include <vtksys/SystemInformation.hxx>
#include <vtksys/SystemTools.hxx>
#include <algorithm>
#include <csignal>
#include <memory>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#if defined(_WIN32) && !defined(__CYGWIN__)
#define VTK_PATH_SEPARATOR "\\"
#else
#define VTK_PATH_SEPARATOR "/"
#endif
#define VTKPY_DEBUG_MESSAGE(x) \
vtkVLog(vtkLogger::ConvertToVerbosity(vtkPythonInterpreter::GetLogVerbosity()), x)
#define VTKPY_DEBUG_MESSAGE_VV(x) \
vtkVLog(vtkLogger::ConvertToVerbosity(vtkPythonInterpreter::GetLogVerbosity() + 1), x)
#if defined(_WIN32) && !defined(__CYGWIN__) && defined(VTK_BUILD_SHARED_LIBS) && \
PY_VERSION_HEX >= 0x03080000
#define vtkPythonInterpreter_USE_DIRECTORY_COOKIE
#endif
namespace
{
template <class T>
class PoolT
{
std::vector<T*> Strings;
public:
~PoolT()
{
for (T* astring : this->Strings)
{
PyMem_RawFree(astring);
}
}
T* push_back(T* val)
{
this->Strings.push_back(val);
return val;
}
T* pop_last()
{
if (this->Strings.empty())
{
return nullptr;
}
T* last = *this->Strings.rbegin();
this->Strings.pop_back();
return last;
}
};
using StringPool = PoolT<char>;
using WCharStringPool = PoolT<wchar_t>;
wchar_t* vtk_Py_UTF8ToWide(const char* arg)
{
wchar_t* result = nullptr;
if (arg != nullptr)
{
size_t length = vtksysEncoding_mbstowcs(nullptr, arg, 0);
if (length > 0)
{
result = (wchar_t*)PyMem_RawMalloc(sizeof(wchar_t) * (length + 1));
vtksysEncoding_mbstowcs(result, arg, length + 1);
}
}
return result;
}
#if PY_VERSION_HEX < 0x03080000
std::string vtk_Py_WideToUTF8(const wchar_t* arg)
{
std::string result;
size_t length = vtksysEncoding_wcstombs(nullptr, arg, 0);
if (length > 0)
{
std::vector<char> chars(length + 1);
vtksysEncoding_wcstombs(chars.data(), arg, length + 1);
result.assign(chars.data(), length);
}
return result;
}
#endif
std::vector<vtkWeakPointer<vtkPythonInterpreter>>* GlobalInterpreters;
std::vector<std::string> PythonPaths;
void NotifyInterpreters(unsigned long eventid, void* calldata = nullptr)
{
std::vector<vtkWeakPointer<vtkPythonInterpreter>>::iterator iter;
for (iter = GlobalInterpreters->begin(); iter != GlobalInterpreters->end(); ++iter)
{
if (iter->GetPointer())
{
iter->GetPointer()->InvokeEvent(eventid, calldata);
}
}
}
inline void vtkPrependPythonPath(const char* pathtoadd)
{
VTKPY_DEBUG_MESSAGE("adding module search path " << pathtoadd);
vtkPythonScopeGilEnsurer gilEnsurer;
PyObject* path = PySys_GetObject("path");
PyObject* newpath = PyUnicode_FromString(pathtoadd);
// avoid adding duplicate paths.
if (PySequence_Contains(path, newpath) == 0)
{
PyList_Insert(path, 0, newpath);
}
Py_DECREF(newpath);
}
}
VTK_ABI_NAMESPACE_BEGIN
// Schwarz counter idiom for GlobalInterpreters object
static unsigned int vtkPythonInterpretersCounter;
vtkPythonGlobalInterpreters::vtkPythonGlobalInterpreters()
{
if (vtkPythonInterpretersCounter++ == 0)
{
GlobalInterpreters = new std::vector<vtkWeakPointer<vtkPythonInterpreter>>();
}
}
vtkPythonGlobalInterpreters::~vtkPythonGlobalInterpreters()
{
if (--vtkPythonInterpretersCounter == 0)
{
delete GlobalInterpreters;
GlobalInterpreters = nullptr;
}
}
bool vtkPythonInterpreter::InitializedOnce = false;
bool vtkPythonInterpreter::CaptureStdin = false;
bool vtkPythonInterpreter::RedirectOutput = true;
bool vtkPythonInterpreter::ConsoleBuffering = false;
std::string vtkPythonInterpreter::StdErrBuffer;
std::string vtkPythonInterpreter::StdOutBuffer;
int vtkPythonInterpreter::LogVerbosity = vtkLogger::VERBOSITY_TRACE;
std::vector<std::pair<std::string, std::string>> vtkPythonInterpreter::UserPythonPaths;
std::vector<void (*)()> vtkPythonInterpreter::AtExitCallbacks;
#if PY_VERSION_HEX >= 0x03000000
struct CharDeleter
{
void operator()(wchar_t* str) { PyMem_RawFree(str); }
};
#endif
vtkStandardNewMacro(vtkPythonInterpreter);
//------------------------------------------------------------------------------
vtkPythonInterpreter::vtkPythonInterpreter()
{
GlobalInterpreters->emplace_back(this);
}
//------------------------------------------------------------------------------
vtkPythonInterpreter::~vtkPythonInterpreter()
{
// We need to check that GlobalInterpreters has not been deleted yet. It can be
// deleted prior to a call to this destructor if another static object with a
// reference to a vtkPythonInterpreter object deletes that object after
// GlobalInterpreters has been destructed. It all depends on the destruction order
// of the other static object and GlobalInterpreters.
if (!GlobalInterpreters)
{
return;
}
std::vector<vtkWeakPointer<vtkPythonInterpreter>>::iterator iter;
for (iter = GlobalInterpreters->begin(); iter != GlobalInterpreters->end(); ++iter)
{
if (*iter == this)
{
GlobalInterpreters->erase(iter);
break;
}
}
}
//------------------------------------------------------------------------------
bool vtkPythonInterpreter::IsInitialized()
{
return (Py_IsInitialized() != 0);
}
//------------------------------------------------------------------------------
void vtkPythonInterpreter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//------------------------------------------------------------------------------
bool vtkPythonInterpreter::Initialize(int initsigs /*=0*/)
{
return vtkPythonInterpreter::InitializeWithArgs(initsigs, 0, nullptr);
}
#if PY_VERSION_HEX >= 0x03080000
static WCharStringPool PythonProgramName;
#endif
//------------------------------------------------------------------------------
// Ensure that Python is pre-initialized enough for VTK to do its
// initialization. Must be called before any `PyMem_*` calls are made.
static bool vtkPythonPreConfig()
{
// Guard against doing this multiple times.
static bool done = false;
if (done)
{
return false;
}
done = true;
#if PY_VERSION_HEX >= 0x03080000
PyStatus status;
PyPreConfig preconfig;
PyPreConfig_InitPythonConfig(&preconfig);
preconfig.allocator = PYMEM_ALLOCATOR_NOT_SET;
preconfig.utf8_mode = 1;
status = Py_PreInitialize(&preconfig);
if (PyStatus_Exception(status))
{
Py_ExitStatusException(status);
}
return preconfig.isolated;
#else
return Py_FrozenFlag;
#endif
}
//------------------------------------------------------------------------------
namespace
{
/**
* Since vtkPythonInterpreter is often used outside CPython executable, e.g.
* vtkpython, the default logic to locate Python standard libraries used by
* Python (which depends on the executable path) may fail or pickup incorrect
* Python libs. This methods address the issue by setting program name to help
* guide Python's default prefix/exec_prefix searching logic.
*/
void SetupPythonPrefix(bool isolated)
{
using systools = vtksys::SystemTools;
// Check if we're using an isolated Python.
if (isolated)
{
VTKPY_DEBUG_MESSAGE("Isolated Python detected; skipping setting up of program path.");
return;
}
std::string pythonlib = vtkGetLibraryPathForSymbol(Py_InitializeEx);
if (pythonlib.empty())
{
VTKPY_DEBUG_MESSAGE("static Python build or `Py_InitializeEx` library couldn't be found. "
"Set `PYTHONHOME` if Python standard library fails to load.");
return;
}
const std::string newprogramname =
systools::GetFilenamePath(pythonlib) + VTK_PATH_SEPARATOR "vtkpython";
VTKPY_DEBUG_MESSAGE("calling vtkPythonInterpreter::SetProgramName("
<< newprogramname << ") to aid in setup of Python prefix.");
vtkPythonInterpreter::SetProgramName(newprogramname.c_str());
}
#ifdef vtkPythonInterpreter_USE_DIRECTORY_COOKIE
PyObject* DLLDirectoryCookie = nullptr;
void CloseDLLDirectoryCookie()
{
if (DLLDirectoryCookie)
{
if (PyObject_HasAttrString(DLLDirectoryCookie, "close"))
{
PyObject* result = PyObject_CallMethod(DLLDirectoryCookie, "close", nullptr);
Py_XDECREF(result);
}
Py_XDECREF(DLLDirectoryCookie);
DLLDirectoryCookie = nullptr;
}
}
#endif
//------------------------------------------------------------------------------
/**
* Add paths to VTK's Python modules.
*/
void SetupPythonPaths(bool isolated, std::string vtklib, const char* landmark)
{
// Check if we're using an isolated Python.
if (isolated)
{
VTKPY_DEBUG_MESSAGE("Isolated Python detected; skipping setting up of `vtk` package.");
return;
}
using systools = vtksys::SystemTools;
if (vtklib.empty())
{
VTKPY_DEBUG_MESSAGE(
"`GetVTKVersion` library couldn't be found. Will use `sys.executable` next.");
}
if (vtklib.empty())
{
#if PY_VERSION_HEX >= 0x03080000
vtkPythonScopeGilEnsurer gilEnsurer;
PyObject* executable_path = PySys_GetObject("executable");
if (executable_path != Py_None)
{
vtklib = PyUnicode_AsUTF8AndSize(executable_path, nullptr);
}
#else
vtklib = vtk_Py_WideToUTF8(Py_GetProgramName());
#endif
}
vtklib = systools::CollapseFullPath(vtklib);
const std::string vtkdir = systools::GetFilenamePath(vtklib);
#if defined(_WIN32) && !defined(__CYGWIN__) && defined(VTK_BUILD_SHARED_LIBS)
// On Windows, based on how the executable is run, we end up failing to load
// pyd files due to inability to load dependent dlls. This seems to overcome
// the issue.
if (!vtkdir.empty())
{
#if PY_VERSION_HEX >= 0x03080000
vtkPythonScopeGilEnsurer gilEnsurer;
CloseDLLDirectoryCookie();
PyObject* os = PyImport_ImportModule("os");
if (os)
{
PyObject* add_dll_directory = PyObject_GetAttrString(os, "add_dll_directory");
if (add_dll_directory && PyCallable_Check(add_dll_directory))
{
PyObject* newpath = PyUnicode_FromString(vtkdir.c_str());
DLLDirectoryCookie = PyObject_CallFunctionObjArgs(add_dll_directory, newpath, nullptr);
Py_XDECREF(newpath);
}
Py_XDECREF(add_dll_directory);
}
Py_XDECREF(os);
#else
std::string env_path;
if (systools::GetEnv("PATH", env_path))
{
env_path = vtkdir + ";" + env_path;
}
else
{
env_path = vtkdir;
}
systools::PutEnv(std::string("PATH=") + env_path);
#endif
}
#endif
#if defined(VTK_BUILD_SHARED_LIBS)
vtkPythonInterpreter::PrependPythonPath(vtkdir.c_str(), landmark);
#else
// since there may be other packages not zipped (e.g. mpi4py), we added path to _vtk.zip
// to the search path as well.
vtkPythonInterpreter::PrependPythonPath(vtkdir.c_str(), "_vtk.zip", /*add_landmark*/ false);
vtkPythonInterpreter::PrependPythonPath(vtkdir.c_str(), "_vtk.zip", /*add_landmark*/ true);
#endif
}
}
//------------------------------------------------------------------------------
void vtkPythonInterpreter::AddUserPythonPath(const char* libraryPath, const char* landmark)
{
vtkPythonInterpreter::UserPythonPaths.emplace_back(libraryPath, landmark);
}
int vtkPythonInterpreter::AddAtExitCallback(void (*func)())
{
if (Py_IsInitialized() == 0)
{
vtkPythonInterpreter::AtExitCallbacks.emplace_back(func);
return 1;
}
return Py_AtExit(func);
}
//------------------------------------------------------------------------------
bool vtkPythonInterpreter::InitializeWithArgs(
int initsigs, int argc, char* argv[], const char* programName)
{
bool isolated = vtkPythonPreConfig();
if (Py_IsInitialized() == 0)
{
if (programName)
{
vtkPythonInterpreter::SetProgramName(programName);
}
else
{
// If no program name is specified,
// guide the mechanism to locate Python standard library, if possible.
SetupPythonPrefix(isolated);
}
bool signals_installed = initsigs != 0;
// Need two copies of args, because programs might modify the first
using OwnedWideString = std::unique_ptr<wchar_t, CharDeleter>;
std::vector<wchar_t*> argvForPython;
std::vector<OwnedWideString> argvCleanup;
for (int i = 0; i < argc; i++)
{
OwnedWideString argCopy(vtk_Py_UTF8ToWide(argv[i]), CharDeleter());
if (argCopy == nullptr)
{
fprintf(stderr,
"Fatal vtkpython error: "
"unable to decode the command line argument #%i\n",
i + 1);
return false;
}
argvForPython.push_back(argCopy.get());
argvCleanup.emplace_back(std::move(argCopy));
}
argvForPython.push_back(nullptr);
#if PY_VERSION_HEX < 0x03080000
Py_InitializeEx(initsigs);
// setup default argv. Without this, code snippets that check `sys.argv` may
// fail when run in embedded VTK Python environment.
PySys_SetArgvEx(argc, argvForPython.data(), 0);
isolated = Py_FrozenFlag;
#else
PyConfig config;
PyStatus status;
PyConfig_InitPythonConfig(&config);
config.install_signal_handlers = initsigs;
config.program_name = PythonProgramName.pop_last();
status = PyConfig_SetArgv(&config, argc, argvForPython.data());
if (PyStatus_IsError(status))
{
PyConfig_Clear(&config);
return false;
}
status = Py_InitializeFromConfig(&config);
if (PyStatus_IsError(status))
{
PyConfig_Clear(&config);
return false;
}
isolated = config.pathconfig_warnings == 0;
PyConfig_Clear(&config);
#endif
#ifdef VTK_PYTHON_FULL_THREADSAFE
#if PY_VERSION_HEX < 0x03090000
// In Python 3.9 and higher, PyEval_ThreadsInitialized() and
// PyEval_InitThreads() are deprecated and do nothing.
// GIL initialization is handled by Py_InitializeEx().
int threadInit = PyEval_ThreadsInitialized();
if (!threadInit)
{
PyEval_InitThreads(); // initialize and acquire GIL
}
#endif
// Always release GIL, as it has been acquired either by PyEval_InitThreads
// prior to Python 3.7 or by Py_InitializeEx in Python 3.7 and after
PyEval_SaveThread();
#endif
#ifdef SIGINT
if (signals_installed)
{
// Put default SIGINT handler back after Py_Initialize/Py_InitializeEx.
signal(SIGINT, SIG_DFL);
}
#endif
}
if (!vtkPythonInterpreter::InitializedOnce)
{
vtkPythonInterpreter::InitializedOnce = true;
// HACK: Calling PyRun_SimpleString for the first time for some reason results in
// a "\n" message being generated which is causing the error dialog to
// popup. So we flush that message out of the system before setting up the
// callbacks.
vtkPythonInterpreter::RunSimpleString("");
// Redirect Python's stdout and stderr and stdin - GIL protected operation
if (vtkPythonInterpreter::RedirectOutput)
{
// Setup handlers for stdout/stdin/stderr.
vtkPythonStdStreamCaptureHelper* wrapperOut = NewPythonStdStreamCaptureHelper(false);
vtkPythonStdStreamCaptureHelper* wrapperErr = NewPythonStdStreamCaptureHelper(true);
vtkPythonScopeGilEnsurer gilEnsurer;
PySys_SetObject("stdout", reinterpret_cast<PyObject*>(wrapperOut));
PySys_SetObject("stderr", reinterpret_cast<PyObject*>(wrapperErr));
PySys_SetObject("stdin", reinterpret_cast<PyObject*>(wrapperOut));
Py_DECREF(wrapperOut);
Py_DECREF(wrapperErr);
}
// We call this before processing any of Python paths added by the
// application using `PrependPythonPath`. This ensures that application
// specified paths are preferred to the ones `vtkPythonInterpreter` adds.
std::string vtklib = vtkGetLibraryPathForSymbol(GetVTKVersion);
SetupPythonPaths(isolated, vtklib, "vtkmodules/__init__.py");
// Used to setup additional user python paths added by `AddUserPythonPath` method.
for (const auto& pair : vtkPythonInterpreter::UserPythonPaths)
{
if (!pair.first.empty())
{
SetupPythonPaths(isolated, pair.first, pair.second.c_str());
}
}
for (size_t cc = 0; cc < PythonPaths.size(); cc++)
{
vtkPrependPythonPath(PythonPaths[cc].c_str());
}
for (auto* func : vtkPythonInterpreter::AtExitCallbacks)
{
if (Py_AtExit(func))
{
return false;
}
}
vtkPythonInterpreter::AtExitCallbacks.clear();
NotifyInterpreters(vtkCommand::EnterEvent);
return true;
}
return false;
}
//------------------------------------------------------------------------------
void vtkPythonInterpreter::Finalize()
{
if (Py_IsInitialized() != 0)
{
NotifyInterpreters(vtkCommand::ExitEvent);
vtkPythonScopeGilEnsurer gilEnsurer(false, true);
#ifdef vtkPythonInterpreter_USE_DIRECTORY_COOKIE
CloseDLLDirectoryCookie();
#endif
// Py_Finalize will take care of releasing gil
Py_Finalize();
}
}
//------------------------------------------------------------------------------
void vtkPythonInterpreter::SetProgramName(const char* programname)
{
vtkPythonPreConfig();
if (programname)
{
#if PY_VERSION_HEX >= 0x03080000
if (wchar_t* argv0 = vtk_Py_UTF8ToWide(programname))
{
PythonProgramName.push_back(argv0);
}
else
{
fprintf(stderr,
"Fatal vtkpython error: "
"unable to decode the program name\n");
wchar_t* empty = (wchar_t*)PyMem_RawMalloc(sizeof(wchar_t));
empty[0] = 0;
PythonProgramName.push_back(empty);
}
#else
// From Python Docs: The argument should point to a zero-terminated character
// string in static storage whose contents will not change for the duration of
// the program's execution. No code in the Python interpreter will change the
// contents of this storage.
wchar_t* argv0 = vtk_Py_UTF8ToWide(programname);
if (argv0 == nullptr)
{
fprintf(stderr,
"Fatal vtkpython error: "
"unable to decode the program name\n");
static wchar_t empty[1] = { 0 };
argv0 = empty;
Py_SetProgramName(argv0);
}
else
{
static WCharStringPool wpool;
Py_SetProgramName(wpool.push_back(argv0));
}
#endif
}
}
//------------------------------------------------------------------------------
void vtkPythonInterpreter::PrependPythonPath(const char* dir)
{
if (!dir)
{
return;
}
std::string out_dir = dir;
#if defined(_WIN32) && !defined(__CYGWIN__)
// Convert slashes for this platform.
std::replace(out_dir.begin(), out_dir.end(), '/', '\\');
#endif
if (Py_IsInitialized() == 0)
{
// save path for future use.
PythonPaths.push_back(out_dir);
return;
}
// Append the path to the python sys.path object.
vtkPrependPythonPath(out_dir.c_str());
}
//------------------------------------------------------------------------------
void vtkPythonInterpreter::PrependPythonPath(
const char* anchor, const char* landmark, bool add_landmark)
{
const std::vector<std::string> prefixes = {
VTK_PYTHON_SITE_PACKAGES_SUFFIX
#if defined(__APPLE__)
// if in an App bundle, the `sitepackages` dir is <app_root>/Contents/Python
,
"Contents/Python"
#endif
,
"."
};
vtkNew<vtkResourceFileLocator> locator;
locator->SetLogVerbosity(vtkPythonInterpreter::GetLogVerbosity() + 1);
std::string path = locator->Locate(anchor, prefixes, landmark);
if (!path.empty())
{
if (add_landmark)
{
path = path + "/" + landmark;
}
vtkPythonInterpreter::PrependPythonPath(path.c_str());
}
}
//------------------------------------------------------------------------------
int vtkPythonInterpreter::PyMain(int argc, char** argv)
{
vtksys::SystemTools::EnableMSVCDebugHook();
int count_v = 0;
for (int cc = 0; cc < argc; ++cc)
{
if (argv[cc] && strcmp(argv[cc], "-v") == 0)
{
++count_v;
}
if (argv[cc] && strcmp(argv[cc], "-vv") == 0)
{
count_v += 2;
}
}
if (count_v > 0)
{
// change the vtkPythonInterpreter's log verbosity. We only touch it
// if the command line arguments explicitly requested a certain verbosity.
vtkPythonInterpreter::SetLogVerbosity(vtkLogger::VERBOSITY_INFO);
vtkLogger::SetStderrVerbosity(vtkLogger::ConvertToVerbosity(count_v - 1));
}
vtkLogger::Init(argc, argv, nullptr); // since `-v` and `-vv` are parsed as Python verbosity flags
// and not log verbosity flags.
// Need two copies of args, because the first array may be modified elsewhere.
using OwnedCString = std::unique_ptr<char, decltype(&std::free)>;
std::vector<char*> argvForPython;
std::vector<OwnedCString> argvCleanup;
for (int i = 0; i < argc; i++)
{
if (!argv[i])
{
continue;
}
if (strcmp(argv[i], "--enable-bt") == 0)
{
vtksys::SystemInformation::SetStackTraceOnError(1);
continue;
}
if (strcmp(argv[i], "-V") == 0)
{
// print out VTK version and let argument pass to Py_RunMain(). At which
// point, Python will print its version and exit.
cout << vtkVersion::GetVTKSourceVersion() << endl;
}
OwnedCString argCopy(strdup(argv[i]), &std::free);
if (argCopy == nullptr)
{
fprintf(stderr,
"Fatal vtkpython error: "
"unable to copy the command line argument #%i\n",
i + 1);
return 1;
}
argvForPython.push_back(argCopy.get());
argvCleanup.emplace_back(std::move(argCopy));
}
int argvForPythonSize = static_cast<int>(argvForPython.size());
argvForPython.push_back(nullptr);
vtkPythonInterpreter::InitializeWithArgs(1, argvForPythonSize, argvForPython.data());
#if PY_VERSION_HEX >= 0x03070000 && PY_VERSION_HEX < 0x03080000
// Python 3.7.0 has a bug where Py_InitializeEx (called above) followed by
// Py_Main (at the end of this block) causes a crash. Gracefully exit with
// failure if we're using 3.7.0 and suggest getting the newest 3.7.x release.
// See <https://gitlab.kitware.com/vtk/vtk/-/issues/17434> for details.
{
bool is_ok = true;
vtkPythonScopeGilEnsurer gilEnsurer(false, true);
PyObject* sys = PyImport_ImportModule("sys");
if (sys)
{
// XXX: Check sys.implementation.name == 'cpython'?
PyObject* version_info = PyObject_GetAttrString(sys, "version_info");
if (version_info)
{
PyObject* major = PyObject_GetAttrString(version_info, "major");
PyObject* minor = PyObject_GetAttrString(version_info, "minor");
PyObject* micro = PyObject_GetAttrString(version_info, "micro");
auto py_number_cmp = [](PyObject* obj, long expected) {
return obj && PyLong_Check(obj) && PyLong_AsLong(obj) == expected;
};
// Only 3.7.0 has this issue. Any failures to get the version
// information is OK; we'll just crash later anyways if the version is
// bad.
is_ok = !py_number_cmp(major, 3) || !py_number_cmp(minor, 7) || !py_number_cmp(micro, 0);
Py_XDECREF(micro);
Py_XDECREF(minor);
Py_XDECREF(major);
}
Py_XDECREF(version_info);
}
Py_XDECREF(sys);
if (!is_ok)
{
std::cerr << "Python 3.7.0 has a known issue that causes a crash with a "
"specific API usage pattern. This has been fixed in 3.7.1 and all "
"newer 3.7.x Python releases. Exiting now to avoid the crash."
<< std::endl;
return 1;
}
}
#endif
#if PY_VERSION_HEX < 0x03080000
// Need two copies of args, because programs might modify the first
using OwnedWideString = std::unique_ptr<wchar_t, CharDeleter>;
std::vector<wchar_t*> argvForPythonWide;
std::vector<OwnedWideString> argvCleanupWide;
for (size_t i = 0; i < argvCleanup.size(); i++)
{
OwnedWideString argCopy(vtk_Py_UTF8ToWide(argvCleanup[i].get()), CharDeleter());
if (argCopy == nullptr)
{
fprintf(stderr,
"Fatal vtkpython error: "
"unable to decode the command line argument #%zu\n",
i + 1);
return 1;
}
argvForPythonWide.push_back(argCopy.get());
argvCleanupWide.emplace_back(std::move(argCopy));
}
int argvForPythonWideSize = static_cast<int>(argvForPythonWide.size());
argvForPythonWide.push_back(nullptr);
vtkPythonScopeGilEnsurer gilEnsurer(false, true);
return Py_Main(argvForPythonWideSize, argvForPythonWide.data());
#else
vtkPythonScopeGilEnsurer gilEnsurer(false, true);
return Py_RunMain();
#endif
}
//------------------------------------------------------------------------------
int vtkPythonInterpreter::RunSimpleString(const char* script)
{
vtkPythonInterpreter::Initialize(1);
vtkPythonInterpreter::ConsoleBuffering = true;
// The embedded python interpreter cannot handle DOS line-endings, see
// http://sourceforge.net/tracker/?group_id=5470&atid=105470&func=detail&aid=1167922
std::string buffer = script ? script : "";
buffer.erase(std::remove(buffer.begin(), buffer.end(), '\r'), buffer.end());
// The cast is necessary because PyRun_SimpleString() hasn't always been const-correct
int pyReturn;
{
vtkPythonScopeGilEnsurer gilEnsurer;
pyReturn = PyRun_SimpleString(buffer.c_str());
}
vtkPythonInterpreter::ConsoleBuffering = false;
if (!vtkPythonInterpreter::StdErrBuffer.empty())
{
vtkOutputWindow::GetInstance()->DisplayErrorText(vtkPythonInterpreter::StdErrBuffer.c_str());
NotifyInterpreters(
vtkCommand::ErrorEvent, const_cast<char*>(vtkPythonInterpreter::StdErrBuffer.c_str()));
vtkPythonInterpreter::StdErrBuffer.clear();
}
if (!vtkPythonInterpreter::StdOutBuffer.empty())
{
vtkOutputWindow::GetInstance()->DisplayText(vtkPythonInterpreter::StdOutBuffer.c_str());
NotifyInterpreters(
vtkCommand::SetOutputEvent, const_cast<char*>(vtkPythonInterpreter::StdOutBuffer.c_str()));
vtkPythonInterpreter::StdOutBuffer.clear();
}
return pyReturn;
}
//------------------------------------------------------------------------------
void vtkPythonInterpreter::SetCaptureStdin(bool val)
{
vtkPythonInterpreter::CaptureStdin = val;
}
//------------------------------------------------------------------------------
bool vtkPythonInterpreter::GetCaptureStdin()
{
return vtkPythonInterpreter::CaptureStdin;
}
//------------------------------------------------------------------------------
void vtkPythonInterpreter::SetRedirectOutput(bool redirect)
{
vtkPythonInterpreter::RedirectOutput = redirect;
}
//------------------------------------------------------------------------------
bool vtkPythonInterpreter::GetRedirectOutput()
{
return vtkPythonInterpreter::RedirectOutput;
}
//------------------------------------------------------------------------------
void vtkPythonInterpreter::WriteStdOut(const char* txt)
{
if (vtkPythonInterpreter::ConsoleBuffering)
{
vtkPythonInterpreter::StdOutBuffer += std::string(txt);
}
else
{
vtkOutputWindow::GetInstance()->DisplayText(txt);
NotifyInterpreters(vtkCommand::SetOutputEvent, const_cast<char*>(txt));
}
}
//------------------------------------------------------------------------------
void vtkPythonInterpreter::FlushStdOut() {}
//------------------------------------------------------------------------------
void vtkPythonInterpreter::WriteStdErr(const char* txt)
{
if (vtkPythonInterpreter::ConsoleBuffering)
{
vtkPythonInterpreter::StdErrBuffer += std::string(txt);
}
else
{
vtkOutputWindow::GetInstance()->DisplayErrorText(txt);
NotifyInterpreters(vtkCommand::ErrorEvent, const_cast<char*>(txt));
}
}
//------------------------------------------------------------------------------
void vtkPythonInterpreter::FlushStdErr() {}
//------------------------------------------------------------------------------
vtkStdString vtkPythonInterpreter::ReadStdin()
{
if (!vtkPythonInterpreter::CaptureStdin)
{
vtkStdString string;
cin >> string;
return string;
}
vtkStdString string;
NotifyInterpreters(vtkCommand::UpdateEvent, &string);
return string;
}
//------------------------------------------------------------------------------
void vtkPythonInterpreter::SetLogVerbosity(int val)
{
vtkPythonInterpreter::LogVerbosity = vtkLogger::ConvertToVerbosity(val);
}
//------------------------------------------------------------------------------
int vtkPythonInterpreter::GetLogVerbosity()
{
return vtkPythonInterpreter::LogVerbosity;
}
#if defined(_WIN32)
//------------------------------------------------------------------------------
vtkWideArgsConverter::vtkWideArgsConverter(int argc, wchar_t* wargv[])
{
this->Argc = argc;
for (int i = 0; i < argc; i++)
{
std::string str = vtksys::Encoding::ToNarrow(wargv[i]);
char* cstr = vtksys::SystemTools::DuplicateString(str.c_str());
Args.push_back(cstr);
MemCache.push_back(cstr);
}
Args.push_back(nullptr);
}
//------------------------------------------------------------------------------
vtkWideArgsConverter::~vtkWideArgsConverter()
{
for (auto cstr : MemCache)
{
delete[] cstr;
}
}
#endif
VTK_ABI_NAMESPACE_END
|