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
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkPython.h"
#include "vtkPythonInteractiveInterpreter.h"
#include "vtkCommand.h"
#include "vtkObjectFactory.h"
#include "vtkPythonInterpreter.h"
#include "vtkSmartPointer.h"
#include <string>
VTK_ABI_NAMESPACE_BEGIN
class vtkPythonInteractiveInterpreter::vtkInternals
{
PyObject* InteractiveConsole;
PyObject* InteractiveConsoleLocals;
std::string PS1;
std::string PS2;
public:
vtkSmartPointer<vtkPythonInterpreter> Interpreter;
vtkInternals()
: InteractiveConsole(nullptr)
, InteractiveConsoleLocals(nullptr)
{
}
~vtkInternals() { this->CleanupPythonObjects(); }
PyObject* GetInteractiveConsolePyObject() { return this->InteractiveConsole; }
PyObject* GetInteractiveConsoleLocalsPyObject() { return this->InteractiveConsoleLocals; }
void CleanupPythonObjects()
{
if (this->InteractiveConsole)
{
vtkPythonScopeGilEnsurer gilEnsurer;
Py_XDECREF(this->InteractiveConsoleLocals);
Py_XDECREF(this->InteractiveConsole);
this->InteractiveConsole = nullptr;
this->InteractiveConsoleLocals = nullptr;
if (vtkPythonInterpreter::IsInitialized())
{
const char* code = "import gc; gc.collect()\n";
vtkPythonInterpreter::RunSimpleString(code);
}
}
}
PyObject* GetInteractiveConsole()
{
if (this->InteractiveConsole)
{
return this->InteractiveConsole;
}
vtkPythonInterpreter::Initialize();
vtkPythonScopeGilEnsurer gilEnsurer;
// set up the code.InteractiveConsole instance that we'll use.
const char* code = "import code\n"
"__vtkConsoleLocals={'__name__':'__vtkconsole__','__doc__':None}\n"
"__vtkConsole=code.InteractiveConsole(__vtkConsoleLocals)\n";
PyRun_SimpleString(code);
// Now get the reference to __vtkConsole and save the pointer.
PyObject* main_module = PyImport_AddModule("__main__");
PyObject* global_dict = PyModule_GetDict(main_module);
this->InteractiveConsole = PyDict_GetItemString(global_dict, "__vtkConsole");
this->InteractiveConsoleLocals = PyDict_GetItemString(global_dict, "__vtkConsoleLocals");
if (!this->InteractiveConsole || !this->InteractiveConsoleLocals)
{
vtkGenericWarningMacro(
"Failed to locate the InteractiveConsole/InteractiveConsoleLocals object.");
return nullptr;
}
Py_INCREF(this->InteractiveConsole);
Py_INCREF(this->InteractiveConsoleLocals);
PyRun_SimpleString("del __vtkConsole; del __vtkConsoleLocals");
// Maybe we need an API to enable developers to set the prompts.
PyObject* ps1 = PySys_GetObject("ps1");
if (!ps1)
{
ps1 = PyUnicode_FromString(">>> ");
PySys_SetObject("ps1", ps1);
Py_XDECREF(ps1);
}
PyObject* ps2 = PySys_GetObject("ps2");
if (!ps2)
{
ps2 = PyUnicode_FromString("... ");
PySys_SetObject("ps2", ps2);
Py_XDECREF(ps2);
}
return this->InteractiveConsole;
}
};
vtkStandardNewMacro(vtkPythonInteractiveInterpreter);
//------------------------------------------------------------------------------
vtkPythonInteractiveInterpreter::vtkPythonInteractiveInterpreter()
: Internals(new vtkPythonInteractiveInterpreter::vtkInternals())
{
this->Internals->Interpreter = vtkSmartPointer<vtkPythonInterpreter>::New();
this->Internals->Interpreter->AddObserver(
vtkCommand::AnyEvent, this, &vtkPythonInteractiveInterpreter::HandleEvents);
}
//------------------------------------------------------------------------------
vtkPythonInteractiveInterpreter::~vtkPythonInteractiveInterpreter()
{
delete this->Internals;
this->Internals = nullptr;
}
//------------------------------------------------------------------------------
void vtkPythonInteractiveInterpreter::HandleEvents(
vtkObject* vtkNotUsed(caller), unsigned long eventid, void* calldata)
{
if (eventid == vtkCommand::ExitEvent)
{
this->Internals->CleanupPythonObjects();
}
this->InvokeEvent(eventid, calldata);
}
//------------------------------------------------------------------------------
bool vtkPythonInteractiveInterpreter::Push(const char* code)
{
PyObject* console = this->Internals->GetInteractiveConsole();
if (!console)
{
return false;
}
// 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 = code ? code : "";
// replace "\r\n" with "\n"
std::string::size_type i = buffer.find("\r\n");
for (; i != std::string::npos; i = buffer.find("\r\n", i))
{
buffer.replace(i, 2, "\n");
i++;
}
// replace "\r" with "\n" (sometimes seen on Mac)
i = buffer.find('\r');
for (; i != std::string::npos; i = buffer.find('\r', i))
{
buffer.replace(i, 1, "\n");
i++;
}
vtkPythonScopeGilEnsurer gilEnsurer;
bool ret_value = false;
PyObject* res = PyObject_CallMethod(console, "push", "z", buffer.c_str());
if (res)
{
int status = 0;
if (PyArg_Parse(res, "i", &status))
{
ret_value = (status > 0);
}
Py_DECREF(res);
}
return ret_value;
}
//------------------------------------------------------------------------------
int vtkPythonInteractiveInterpreter::RunStringWithConsoleLocals(const char* script)
{
// The implementation of this method is modeled after
// PyRun_SimpleStringFlags() found in Python's pythonrun.c
this->Internals->GetInteractiveConsole(); // ensure the console is initialized
vtkPythonScopeGilEnsurer gilEnsurer;
PyObject* context = this->Internals->GetInteractiveConsoleLocalsPyObject();
PyObject* result = PyRun_String(script, Py_file_input, context, context);
if (result == nullptr)
{
PyErr_Print();
return -1;
}
Py_DECREF(result);
PyObject* f = PySys_GetObject("stdout");
if (f == nullptr || PyFile_WriteString("\n", f) != 0)
{
PyErr_Clear();
}
return 0;
}
//------------------------------------------------------------------------------
void vtkPythonInteractiveInterpreter::Reset()
{
this->Internals->CleanupPythonObjects();
}
//------------------------------------------------------------------------------
void* vtkPythonInteractiveInterpreter::GetInteractiveConsolePyObject()
{
return this->Internals->GetInteractiveConsolePyObject();
}
//------------------------------------------------------------------------------
void* vtkPythonInteractiveInterpreter::GetInteractiveConsoleLocalsPyObject()
{
return this->Internals->GetInteractiveConsoleLocalsPyObject();
}
//------------------------------------------------------------------------------
void vtkPythonInteractiveInterpreter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
VTK_ABI_NAMESPACE_END
|