------------------------------------------------------------------------------
| 59 | |
| 60 | //------------------------------------------------------------------------------ |
| 61 | vtkMatplotlibMathTextUtilities::Availability vtkMatplotlibMathTextUtilities::CheckMPLAvailability() |
| 62 | { |
| 63 | if (vtkMatplotlibMathTextUtilities::MPLMathTextAvailable != NOT_TESTED) |
| 64 | { |
| 65 | // Already tested. Nothing to do now. |
| 66 | return vtkMatplotlibMathTextUtilities::MPLMathTextAvailable; |
| 67 | } |
| 68 | |
| 69 | // Enable startup debugging output. This will be set to true when |
| 70 | // VTK_MATPLOTLIB_DEBUG is defined in the process environment. |
| 71 | bool debug = (vtksys::SystemTools::GetEnv("VTK_MATPLOTLIB_DEBUG") != nullptr); |
| 72 | |
| 73 | #if VTK_MODULE_ENABLE_VTK_PythonInterpreter |
| 74 | if (!Py_IsInitialized()) |
| 75 | { |
| 76 | // Initialize the python interpreter if needed |
| 77 | vtkMplStartUpDebugMacro("Initializing Python, if not already."); |
| 78 | vtkPythonInterpreter::Initialize(); |
| 79 | } |
| 80 | #endif |
| 81 | if (!Py_IsInitialized()) |
| 82 | { |
| 83 | // Don't store the result; it might be available if Python is initialized |
| 84 | // elsewhere later. |
| 85 | vtkMplStartUpDebugMacro("Python is not available."); |
| 86 | return UNAVAILABLE; |
| 87 | } |
| 88 | |
| 89 | vtkMplStartUpDebugMacro("Attempting to import matplotlib."); |
| 90 | |
| 91 | vtkPythonScopeGilEnsurer gilEnsurer; |
| 92 | if (PyErr_Occurred() || !PyImport_ImportModule("matplotlib") || PyErr_Occurred()) |
| 93 | { |
| 94 | // FIXME: Check if we need this. Wouldn't pipe-ing the stdout/stderr make |
| 95 | // this unnecessary? |
| 96 | |
| 97 | // Fetch the exception info. Note that value and traceback may still be |
| 98 | // nullptr after the call to PyErr_Fetch(). |
| 99 | PyObject* type = nullptr; |
| 100 | PyObject* value = nullptr; |
| 101 | PyObject* traceback = nullptr; |
| 102 | PyErr_Fetch(&type, &value, &traceback); |
| 103 | vtkSmartPyObject tracebackStr; |
| 104 | if (traceback) |
| 105 | { |
| 106 | vtkSmartPyObject tb_module = PyImport_ImportModule("traceback"); |
| 107 | if (tb_module) |
| 108 | { |
| 109 | vtkSmartPyObject format_tb = PyObject_GetAttrString(tb_module, "format_tb"); |
| 110 | if (format_tb) |
| 111 | { |
| 112 | vtkSmartPyObject tracebacklist = |
| 113 | PyObject_CallFunction(format_tb, const_cast<char*>("O"), traceback); |
| 114 | Py_ssize_t tbsz = PySequence_Length(tracebacklist); |
| 115 | tracebackStr = PyUnicode_FromString(""); |
| 116 | for (Py_ssize_t i = 0; i < tbsz; ++i) |
| 117 | { |
| 118 | vtkSmartPyObject item = PySequence_GetItem(tracebacklist, i); |
no test coverage detected