Skip to content
Snippets Groups Projects
Commit 99e86323 authored by Pat Marion's avatar Pat Marion
Browse files

Add RunStringWithConsoleLocals() to vtkPythonInteractiveInterpreter

This method executes python source code in the context of the locals()
dictionary used by the InteractiveConsole instance owned by the
vtkPythonInteractiveInterpreter.

Change-Id: I86e194ebc2efb959570ed0d4b36b8dfb324052c8
parent 3531c085
Branches
Tags
No related merge requests found
......@@ -182,6 +182,31 @@ bool vtkPythonInteractiveInterpreter::Push(const char* const code)
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
PyObject* context = this->Internals->GetInteractiveConsoleLocalsPyObject();
PyObject* result = PyRun_String(const_cast<char*>(script), Py_file_input, context, context);
if (result == NULL)
{
PyErr_Print();
return -1;
}
Py_DECREF(result);
if (Py_FlushLine())
{
PyErr_Clear();
}
return 0;
}
//----------------------------------------------------------------------------
void vtkPythonInteractiveInterpreter::Reset()
{
......
......@@ -60,6 +60,14 @@ public:
// code.InteractiveConsole().
void Reset();
// Description:
// Executes the given python source code using the context given by the
// locals() object used by this interactive console. This is similar to
// using vtkPythonInterpreter::RunSimpleString(), except that method will
// execute code in the context of the __main__ module. Returns 0 on success
// or -1 if an exception was raised.
int RunStringWithConsoleLocals(const char* script);
// Description:
// Provides access to the internal PyObject instances used for the
// code.InteractiveConsole() as well as the dictionary for the locals of the
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment