pythonserver.cpp 6.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA  02110-1301, USA.

    ---
    Copyright (C) 2015 Minh Ngo <minh@fedoraproject.org>
 */

21
#include "pythonserver.h"
22
#include <vector>
23
24
25

#include <Python.h>

26
using namespace std;
27

28
29
namespace
{
30
    string pyObjectToQString(PyObject* obj)
31
    {
32
#if PY_MAJOR_VERSION == 3
33
        return string(PyUnicode_AsUTF8(obj));
34
#elif PY_MAJOR_VERSION == 2
35
        return string(PyString_AsString(obj));
36
37
38
#else
    #warning Unknown Python version
#endif
39
40
41
    }
}

42
void PythonServer::login()
43
{
44
    Py_InspectFlag = 1;
45
46
    Py_Initialize();
    m_pModule = PyImport_AddModule("__main__");
47
    filePath = "python_cantor_worksheet";
48
49
}

50
51
52
53
54
void PythonServer::interrupt()
{
    PyErr_SetInterrupt();
}

55
void PythonServer::runPythonCommand(const string& command)
56
{
57
    PyObject* py_dict = PyModule_GetDict(m_pModule);
58
    m_error = false;
59
60
61
62
63
64
65
66
67
68
69
70
71
72

    const char* prepareCommand =
        "import sys;\n"\
        "class CatchOutPythonBackend:\n"\
        "  def __init__(self):\n"\
        "    self.value = ''\n"\
        "  def write(self, txt):\n"\
        "    self.value += txt\n"\
        "outputPythonBackend = CatchOutPythonBackend()\n"\
        "errorPythonBackend  = CatchOutPythonBackend()\n"\
        "sys.stdout = outputPythonBackend\n"\
        "sys.stderr = errorPythonBackend\n";
    PyRun_SimpleString(prepareCommand);

73
#if PY_MAJOR_VERSION == 3
74
    PyObject* compile = Py_CompileString(command.c_str(), filePath.c_str(), Py_single_input);
75
76
77
78
79
80
81
    // There are two reasons for the error:
    // 1) This code is not single expression, so we can't compile this with flag Py_single_input
    // 2) There are errors in the code
    if (PyErr_Occurred())
    {
        PyErr_Clear();
        // Try to recompile code as sequence of expressions
82
        compile = Py_CompileString(command.c_str(), filePath.c_str(), Py_file_input);
83
84
85
        if (PyErr_Occurred())
        {
            // We now know, that we have a syntax error, so print the traceback and exit
86
            m_error = true;
87
88
89
90
91
92
            PyErr_PrintEx(0);
            return;
        }
    }
    PyEval_EvalCode(compile, py_dict, py_dict);
#elif PY_MAJOR_VERSION == 2
93
94
95
    // Python 2.X don't check, that input string contains only one expression.
    // So for checking this, we compile string as file and as single expression and compare bytecode
    // FIXME?
96
    PyObject* codeFile = Py_CompileString(command.c_str(), filePath.c_str(), Py_file_input);
97
98
    if (PyErr_Occurred())
    {
99
        m_error = true;
100
101
102
        PyErr_PrintEx(0);
        return;
    }
103
    PyObject* codeSingle = Py_CompileString(command.c_str(), filePath.c_str(), Py_single_input);
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
    if (PyErr_Occurred())
    {
        // We have error with Py_single_input, but haven't error with Py_file_input
        // So, the code can't be compiled as singel input -> use file input right away
        PyErr_Clear();
        PyEval_EvalCode((PyCodeObject*)codeFile, py_dict, py_dict);
    }
    else
    {
        PyObject* bytecode1 = ((PyCodeObject*)codeSingle)->co_code;
        PyObject* bytecode2 = ((PyCodeObject*)codeFile)->co_code;

        if (PyObject_Length(bytecode1) >= PyObject_Length(bytecode2))
        {
            PyEval_EvalCode((PyCodeObject*)codeSingle, py_dict, py_dict);
        }
        else
        {
            PyEval_EvalCode((PyCodeObject*)codeFile, py_dict, py_dict);
        }
    }
125
126
127
128
#else
    #warning Unknown Python version
#endif
    if (PyErr_Occurred())
129
130
    {
        m_error = true;
131
        PyErr_PrintEx(0);
132
    }
133
134
}

135
string PythonServer::getError() const
136
{
137
138
    PyObject *errorPython = PyObject_GetAttrString(m_pModule, "errorPythonBackend");
    PyObject *error = PyObject_GetAttrString(errorPython, "value");
139

140
    return pyObjectToQString(error);
141
142
}

143
string PythonServer::getOutput() const
144
{
145
146
    PyObject *outputPython = PyObject_GetAttrString(m_pModule, "outputPythonBackend");
    PyObject *output = PyObject_GetAttrString(outputPython, "value");
147

148
    return pyObjectToQString(output);
149
150
}

151
void PythonServer::setFilePath(const string& path, const string& dir)
152
{
153
154
    PyRun_SimpleString(("import sys; sys.argv = ['" + path + "']").c_str());
    if (path.length() == 0) // New session, not from file
155
156
157
158
159
160
    {
        PyRun_SimpleString("import sys; sys.path.insert(0, '')");
    }
    else
    {
        this->filePath = path;
161
162
        PyRun_SimpleString(("import sys; sys.path.insert(0, '" + dir + "')").c_str());
        PyRun_SimpleString(("__file__ = '"+path+"'").c_str());
163
    }
164
}
165

166
string PythonServer::variables(bool parseValue) const
167
{
168
    PyRun_SimpleString(
169
170
171
172
        "try: \n"
        "   import numpy \n"
        "   __cantor_numpy_internal__ = numpy.get_printoptions()['threshold'] \n"
        "   numpy.set_printoptions(threshold=100000000) \n"
Nikita Sirgienko's avatar
Nikita Sirgienko committed
173
#if PY_MAJOR_VERSION == 3
174
        "except ModuleNotFoundError: \n"
Nikita Sirgienko's avatar
Nikita Sirgienko committed
175
176
177
#elif PY_MAJOR_VERSION == 2
        "except ImportError: \n"
#endif
178
        "   pass \n"
179
    );
180
181
182
183
184
185

    PyRun_SimpleString("__tmp_globals__ = globals()");
    PyObject* globals = PyObject_GetAttrString(m_pModule,"__tmp_globals__");
    PyObject *key, *value;
    Py_ssize_t pos = 0;

186
    vector<string> vars;
187
    while (PyDict_Next(globals, &pos, &key, &value)) {
188
189
        const string& keyString = pyObjectToQString(key);
        if (keyString.substr(0, 2) == string("__"))
190
191
            continue;

192
193
194
        if (keyString == string("CatchOutPythonBackend")
            || keyString == string("errorPythonBackend")
            || keyString == string("outputPythonBackend"))
195
196
197
198
199
200
201
202
203
204
205
            continue;

        if (PyModule_Check(value))
            continue;

        if (PyFunction_Check(value))
            continue;

        if (PyType_Check(value))
            continue;

206
        string valueString;
207
208
209
        if (parseValue)
            valueString = pyObjectToQString(PyObject_Repr(value));

210
        vars.push_back(keyString + char(17) + valueString);
211
212
    }

213
214
215
216
217
    PyRun_SimpleString(
        "try: \n"
        "   import numpy \n"
        "   numpy.set_printoptions(threshold=__cantor_numpy_internal__) \n"
        "   del __cantor_numpy_internal__ \n"
Nikita Sirgienko's avatar
Nikita Sirgienko committed
218
#if PY_MAJOR_VERSION == 3
219
        "except ModuleNotFoundError: \n"
Nikita Sirgienko's avatar
Nikita Sirgienko committed
220
221
222
#elif PY_MAJOR_VERSION == 2
        "except ImportError: \n"
#endif
223
224
225
        "   pass \n"
    );

226
227
228
229
230
231

    string result;
    for (const string& s : vars)
        result += s + char(18);
    result += char(18);
    return result;
232
233
}

234
235
236
237
238
bool PythonServer::isError() const
{
    return m_error;
}

239