-----------------------------------------------------------------------------
| 164 | |
| 165 | //----------------------------------------------------------------------------- |
| 166 | void pqPythonManager::executeCode( |
| 167 | const QByteArray& code, const QVector<QByteArray>& pre_push, const QVector<QByteArray>& post_push) |
| 168 | { |
| 169 | pqPythonManagerStdIOHelper helper; |
| 170 | |
| 171 | // we capture messages from the script so that when the end up on the |
| 172 | // terminal they are grouped as single message, otherwise they get split at |
| 173 | // each "\n" since that how Python sends those messages over to us. |
| 174 | vtkNew<pqPythonManagerOutputWindow> owindow; |
| 175 | vtkSmartPointer<vtkOutputWindow> old = vtkOutputWindow::GetInstance(); |
| 176 | vtkOutputWindow::SetInstance(owindow); |
| 177 | const bool prevCapture = vtkPythonInterpreter::GetCaptureStdin(); |
| 178 | vtkPythonInterpreter::SetCaptureStdin(true); |
| 179 | |
| 180 | vtkNew<vtkPythonInteractiveInterpreter> interp; |
| 181 | interp->AddObserver(vtkCommand::SetOutputEvent, &helper, &pqPythonManagerStdIOHelper::rawOutput); |
| 182 | interp->AddObserver(vtkCommand::UpdateEvent, &helper, &pqPythonManagerStdIOHelper::rawInput); |
| 183 | for (const auto& instr : pre_push) |
| 184 | { |
| 185 | interp->Push(instr.data()); |
| 186 | } |
| 187 | interp->RunStringWithConsoleLocals(code.data()); |
| 188 | for (const auto& instr : post_push) |
| 189 | { |
| 190 | interp->Push(instr.data()); |
| 191 | } |
| 192 | vtkPythonInterpreter::SetCaptureStdin(prevCapture); |
| 193 | vtkOutputWindow::SetInstance(old); |
| 194 | interp->RemoveObservers(vtkCommand::UpdateEvent); |
| 195 | |
| 196 | auto txt = owindow->text(); |
| 197 | // vtkPythonInteractiveInterpreter always writes a newline, and if there |
| 198 | // was no other output from the script, then txt could contain only a |
| 199 | // newline here. If this is the case, do not display it to the user. |
| 200 | if (!txt.empty() && txt != "\n") |
| 201 | { |
| 202 | vtkOutputWindowDisplayText(txt.c_str()); |
| 203 | } |
| 204 | |
| 205 | auto errorText = owindow->errorText(); |
| 206 | if (!errorText.empty() && errorText != "\n") |
| 207 | { |
| 208 | vtkOutputWindowDisplayErrorText(errorText.c_str()); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | //----------------------------------------------------------------------------- |
| 213 | void pqPythonManager::executeScript(const QString& filename, vtkTypeUInt32 location) |