File: vtkPVPythonInteractiveInterpretor.cxx

package info (click to toggle)
paraview 3.14.1-6
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 234,468 kB
  • sloc: cpp: 2,166,013; ansic: 801,575; xml: 58,068; tcl: 49,247; python: 43,091; java: 16,625; fortran: 12,224; sh: 11,722; yacc: 5,688; perl: 3,128; makefile: 2,228; lex: 1,311; lisp: 486; asm: 471; pascal: 228
file content (143 lines) | stat: -rw-r--r-- 4,320 bytes parent folder | download
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
/*=========================================================================

  Program:   ParaView
  Module:    vtkPVPythonInteractiveInterpretor.cxx

  Copyright (c) Kitware, Inc.
  All rights reserved.
  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html 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 "vtkPython.h"
#include "vtkPVPythonInteractiveInterpretor.h"

#include "vtkObjectFactory.h"

#include <string>

class vtkPVPythonInteractiveInterpretor::vtkInternal
{
public:
  PyObject* InteractiveConsole;
};

vtkStandardNewMacro(vtkPVPythonInteractiveInterpretor);
//----------------------------------------------------------------------------
vtkPVPythonInteractiveInterpretor::vtkPVPythonInteractiveInterpretor()
{
  this->Internal = new vtkInternal;
  this->Internal->InteractiveConsole = 0;
}

//----------------------------------------------------------------------------
vtkPVPythonInteractiveInterpretor::~vtkPVPythonInteractiveInterpretor()
{
  if (this->Internal->InteractiveConsole)
    {
    this->MakeCurrent();
    Py_DECREF(this->Internal->InteractiveConsole);
    this->Internal->InteractiveConsole = 0;
    this->ReleaseControl();
    }

  delete this->Internal;
}

//----------------------------------------------------------------------------
void vtkPVPythonInteractiveInterpretor::InitializeInternal()
{
  this->Superclass::InitializeInternal();

  // set up the code.InteractiveConsole instance that we'll use.
  const char* code = 
    "import code\n"
    "__vtkConsole=code.InteractiveConsole(locals())\n";

  // The cast is necessary because PyRun_SimpleString() hasn't always been 
  // const-correct
  PyRun_SimpleString(const_cast<char*>(code));

  // Now get the reference to __vtkConsole and save the pointer.
  PyObject* main_module = PyImport_AddModule((char*)"__main__");
  PyObject* global_dict = PyModule_GetDict(main_module);
  this->Internal->InteractiveConsole = PyDict_GetItemString(
    global_dict, "__vtkConsole");
  if (this->Internal->InteractiveConsole)
    {
    Py_INCREF(this->Internal->InteractiveConsole);
    }
  else
    {
    vtkErrorMacro("Failed to locate the InteractiveConsole object.");
    }
}

//----------------------------------------------------------------------------
bool vtkPVPythonInteractiveInterpretor::Push(const char* const code)
{
  bool ret_value = false;
  if (this->Internal->InteractiveConsole)
    {
    this->MakeCurrent();

    // 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++;
      }

    PyObject *res = PyObject_CallMethod(this->Internal->InteractiveConsole,
      (char*)"push", (char*)"z", buffer.c_str());
    if (res)
      {
      int status = 0;
      if (PyArg_Parse(res, (char*)"i", &status))
        {
        ret_value = (status>0);
        }
      Py_DECREF(res);
      }

    this->ReleaseControl();
    }
  return ret_value;
}

//----------------------------------------------------------------------------
void vtkPVPythonInteractiveInterpretor::ResetBuffer()
{
  if (this->Internal->InteractiveConsole)
    {
    this->MakeCurrent();
    const char* code = "__vtkConsole.resetbuffer()\n";

    // The cast is necessary because PyRun_SimpleString() hasn't always been 
    // const-correct
    PyRun_SimpleString(const_cast<char*>(code));
    this->ReleaseControl();
    }
}

//----------------------------------------------------------------------------
void vtkPVPythonInteractiveInterpretor::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}