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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPythonInterpreter.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#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/SystemInformation.hxx>
#include <vtksys/SystemTools.hxx>
#include <algorithm>
#include <csignal>
#include <sstream>
#include <string>
#include <vector>
#if PY_VERSION_HEX >= 0x03000000
#if defined(__APPLE__) && PY_VERSION_HEX < 0x03050000
extern "C"
{
extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char* s, Py_ssize_t size);
}
#endif
#endif
#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>
void strFree(T* foo)
{
delete[] foo;
}
template <class T>
class PoolT
{
std::vector<T*> Strings;
public:
~PoolT()
{
for (T* astring : this->Strings)
{
strFree(astring);
}
}
T* push_back(T* val)
{
this->Strings.push_back(val);
return val;
}
};
using StringPool = PoolT<char>;
#if PY_VERSION_HEX >= 0x03000000
template <>
void strFree(wchar_t* foo)
{
#if PY_VERSION_HEX >= 0x03050000
PyMem_RawFree(foo);
#else
PyMem_Free(foo);
#endif
}
using WCharStringPool = PoolT<wchar_t>;
#endif
#if PY_VERSION_HEX >= 0x03000000
wchar_t* vtk_Py_DecodeLocale(const char* arg, size_t* size)
{
(void)size;
#if PY_VERSION_HEX >= 0x03050000
return Py_DecodeLocale(arg, size);
#elif defined(__APPLE__)
return _Py_DecodeUTF8_surrogateescape(arg, strlen(arg));
#else
return _Py_char2wchar(arg, size);
#endif
}
#endif
#if PY_VERSION_HEX >= 0x03000000
char* vtk_Py_EncodeLocale(const wchar_t* arg, size_t* size)
{
(void)size;
#if PY_VERSION_HEX >= 0x03050000
return Py_EncodeLocale(arg, size);
#else
return _Py_wchar2char(arg, size);
#endif
}
#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(const_cast<char*>("path"));
#if PY_VERSION_HEX >= 0x03000000
PyObject* newpath = PyUnicode_FromString(pathtoadd);
#else
PyObject* newpath = PyString_FromString(pathtoadd);
#endif
// avoid adding duplicate paths.
if (PySequence_Contains(path, newpath) == 0)
{
PyList_Insert(path, 0, newpath);
}
Py_DECREF(newpath);
}
}
// 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;
vtkStandardNewMacro(vtkPythonInterpreter);
//------------------------------------------------------------------------------
vtkPythonInterpreter::vtkPythonInterpreter()
{
GlobalInterpreters->push_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*/)
{
if (Py_IsInitialized() == 0)
{
// guide the mechanism to locate Python standard library, if possible.
vtkPythonInterpreter::SetupPythonPrefix();
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(0, nullptr, 0);
#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
// 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(const_cast<char*>("stdout"), reinterpret_cast<PyObject*>(wrapperOut));
PySys_SetObject(const_cast<char*>("stderr"), reinterpret_cast<PyObject*>(wrapperErr));
PySys_SetObject(const_cast<char*>("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.
vtkPythonInterpreter::SetupVTKPythonPaths();
for (size_t cc = 0; cc < PythonPaths.size(); cc++)
{
vtkPrependPythonPath(PythonPaths[cc].c_str());
}
NotifyInterpreters(vtkCommand::EnterEvent);
return true;
}
return false;
}
#ifdef vtkPythonInterpreter_USE_DIRECTORY_COOKIE
static PyObject* DLLDirectoryCookie = nullptr;
static 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
//------------------------------------------------------------------------------
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)
{
if (programname)
{
// 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.
#if PY_VERSION_HEX >= 0x03000000
wchar_t* argv0 = vtk_Py_DecodeLocale(programname, nullptr);
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));
}
#else
static StringPool pool;
Py_SetProgramName(pool.push_back(vtksys::SystemTools::DuplicateString(programname)));
#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.
vtkPythonInterpreter::Initialize(1);
#if PY_VERSION_HEX >= 0x03000000
#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
// Need two copies of args, because programs might modify the first
wchar_t** argvWide = new wchar_t*[argc];
wchar_t** argvWide2 = new wchar_t*[argc];
int argcWide = 0;
for (int i = 0; i < argc; i++)
{
if (argv[i] && strcmp(argv[i], "--enable-bt") == 0)
{
vtksys::SystemInformation::SetStackTraceOnError(1);
continue;
}
if (argv[i] && strcmp(argv[i], "-V") == 0)
{
// print out VTK version and let argument pass to Py_Main(). At which point,
// Python will print its version and exit.
cout << vtkVersion::GetVTKSourceVersion() << endl;
}
argvWide[argcWide] = vtk_Py_DecodeLocale(argv[i], nullptr);
argvWide2[argcWide] = argvWide[argcWide];
if (argvWide[argcWide] == nullptr)
{
fprintf(stderr,
"Fatal vtkpython error: "
"unable to decode the command line argument #%i\n",
i + 1);
for (int k = 0; k < argcWide; k++)
{
PyMem_Free(argvWide2[k]);
}
delete[] argvWide;
delete[] argvWide2;
return 1;
}
argcWide++;
}
vtkPythonScopeGilEnsurer gilEnsurer(false, true);
int res = Py_Main(argcWide, argvWide);
for (int i = 0; i < argcWide; i++)
{
PyMem_Free(argvWide2[i]);
}
delete[] argvWide;
delete[] argvWide2;
return res;
#else
// process command line arguments to remove unhandled args.
std::vector<char*> newargv;
for (int i = 0; i < argc; ++i)
{
if (argv[i] && strcmp(argv[i], "--enable-bt") == 0)
{
vtksys::SystemInformation::SetStackTraceOnError(1);
continue;
}
if (argv[i] && strcmp(argv[i], "-V") == 0)
{
// print out VTK version and let argument pass to Py_Main(). At which point,
// Python will print its version and exit.
cout << vtkVersion::GetVTKSourceVersion() << endl;
}
newargv.push_back(argv[i]);
}
vtkPythonScopeGilEnsurer gilEnsurer(false, true);
return Py_Main(static_cast<int>(newargv.size()), &newargv[0]);
#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(const_cast<char*>(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::SetupPythonPrefix()
{
using systools = vtksys::SystemTools;
// Check Py_FrozenFlag global variable defined by Python to see if we're using
// frozen Python.
if (Py_FrozenFlag)
{
VTKPY_DEBUG_MESSAGE("`Py_FrozenFlag` is set. Skipping setting up of program path.");
return;
}
std::string pythonlib = vtkGetLibraryPathForSymbol(Py_SetProgramName);
if (pythonlib.empty())
{
VTKPY_DEBUG_MESSAGE("static Python build or `Py_SetProgramName` 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 Py_SetProgramName(" << newprogramname << ") to aid in setup of Python prefix.");
#if PY_VERSION_HEX >= 0x03000000
static WCharStringPool wpool;
Py_SetProgramName(wpool.push_back(vtk_Py_DecodeLocale(newprogramname.c_str(), nullptr)));
#else
static StringPool pool;
Py_SetProgramName(pool.push_back(systools::DuplicateString(newprogramname.c_str())));
#endif
}
//------------------------------------------------------------------------------
void vtkPythonInterpreter::SetupVTKPythonPaths()
{
// Check Py_FrozenFlag global variable defined by Python to see if we're using
// frozen Python.
if (Py_FrozenFlag)
{
VTKPY_DEBUG_MESSAGE("`Py_FrozenFlag` is set. Skipping locating of `vtk` package.");
return;
}
using systools = vtksys::SystemTools;
std::string vtklib = vtkGetLibraryPathForSymbol(GetVTKVersion);
if (vtklib.empty())
{
VTKPY_DEBUG_MESSAGE(
"`GetVTKVersion` library couldn't be found. Will use `Py_GetProgramName` next.");
}
if (vtklib.empty())
{
#if PY_VERSION_HEX >= 0x03000000
auto tmp = vtk_Py_EncodeLocale(Py_GetProgramName(), nullptr);
vtklib = tmp;
PyMem_Free(tmp);
#else
vtklib = 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(false, true);
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(), "vtkmodules/__init__.py");
#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::SetLogVerbosity(int val)
{
vtkPythonInterpreter::LogVerbosity = vtkLogger::ConvertToVerbosity(val);
}
//------------------------------------------------------------------------------
int vtkPythonInterpreter::GetLogVerbosity()
{
return vtkPythonInterpreter::LogVerbosity;
}
//------------------------------------------------------------------------------
int vtkPythonInterpreter::GetPythonVerboseFlag()
{
VTK_LEGACY_REPLACED_BODY(
vtkPythonInterpreter::GetPythonVerboseFlag, "VTK 9.0", vtkPythonInterpreter::GetLogVerbosity);
return vtkPythonInterpreter::LogVerbosity == vtkLogger::VERBOSITY_INFO ? 1 : 0;
}
|