pythonserver.cpp 7 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

23
24
#include <QFileInfo>
#include <QDir>
25
26
#include <Python.h>

27
PythonServer::PythonServer(QObject* parent) : QObject(parent), m_pModule(nullptr)
28
29
30
{
}

31
32
33
34
namespace
{
    QString pyObjectToQString(PyObject* obj)
    {
35
#if PY_MAJOR_VERSION == 3
36
        return QString::fromUtf8(PyUnicode_AsUTF8(obj));
37
38
39
40
41
#elif PY_MAJOR_VERSION == 2
        return QString::fromLocal8Bit(PyString_AsString(obj));
#else
    #warning Unknown Python version
#endif
42
43
44
    }
}

45
void PythonServer::login()
46
{
47
    Py_InspectFlag = 1;
48
49
    Py_Initialize();
    m_pModule = PyImport_AddModule("__main__");
Laurent Montel's avatar
Laurent Montel committed
50
    filePath = QStringLiteral("python_cantor_worksheet");
51
52
}

53
54
55
56
57
void PythonServer::interrupt()
{
    PyErr_SetInterrupt();
}

58
void PythonServer::runPythonCommand(const QString& command) const
59
{
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    PyObject* py_dict = PyModule_GetDict(m_pModule);

    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);

75
#if PY_MAJOR_VERSION == 3
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    PyObject* compile = Py_CompileString(command.toStdString().c_str(), filePath.toStdString().c_str(), Py_single_input);
    // 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
        compile = Py_CompileString(command.toStdString().c_str(), filePath.toStdString().c_str(), Py_file_input);
        if (PyErr_Occurred())
        {
            // We now know, that we have a syntax error, so print the traceback and exit
            PyErr_PrintEx(0);
            return;
        }
    }
    PyEval_EvalCode(compile, py_dict, py_dict);
#elif PY_MAJOR_VERSION == 2
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
    // 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?
    PyObject* codeFile = Py_CompileString(command.toStdString().c_str(), filePath.toStdString().c_str(), Py_file_input);
    if (PyErr_Occurred())
    {
        PyErr_PrintEx(0);
        return;
    }
    PyObject* codeSingle = Py_CompileString(command.toStdString().c_str(), filePath.toStdString().c_str(), Py_single_input);
    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
129
#else
    #warning Unknown Python version
#endif
    if (PyErr_Occurred())
        PyErr_PrintEx(0);
130
131
}

132
QString PythonServer::getError() const
133
{
134
135
    PyObject *errorPython = PyObject_GetAttrString(m_pModule, "errorPythonBackend");
    PyObject *error = PyObject_GetAttrString(errorPython, "value");
136

137
    return pyObjectToQString(error);
138
139
}

140
QString PythonServer::getOutput() const
141
{
142
143
    PyObject *outputPython = PyObject_GetAttrString(m_pModule, "outputPythonBackend");
    PyObject *output = PyObject_GetAttrString(outputPython, "value");
144

145
    return pyObjectToQString(output);
146
147
}

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

164
QString PythonServer::variables(bool parseValue) const
165
{
166
    PyRun_SimpleString(
167
168
169
170
        "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
171
#if PY_MAJOR_VERSION == 3
172
        "except ModuleNotFoundError: \n"
Nikita Sirgienko's avatar
Nikita Sirgienko committed
173
174
175
#elif PY_MAJOR_VERSION == 2
        "except ImportError: \n"
#endif
176
        "   pass \n"
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

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

    QStringList vars;
    while (PyDict_Next(globals, &pos, &key, &value)) {
        const QString& keyString = pyObjectToQString(key);
        if (keyString.startsWith(QLatin1String("__")))
            continue;

        if (keyString == QLatin1String("CatchOutPythonBackend")
            || keyString == QLatin1String("errorPythonBackend")
            || keyString == QLatin1String("outputPythonBackend"))
            continue;

        if (PyModule_Check(value))
            continue;

        if (PyFunction_Check(value))
            continue;

        if (PyType_Check(value))
            continue;

204
205
206
207
        QString valueString;
        if (parseValue)
            valueString = pyObjectToQString(PyObject_Repr(value));

208

209
        vars.append(keyString + QChar(17) + valueString);
210
211
    }

212
213
214
215
216
    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
217
#if PY_MAJOR_VERSION == 3
218
        "except ModuleNotFoundError: \n"
Nikita Sirgienko's avatar
Nikita Sirgienko committed
219
220
221
#elif PY_MAJOR_VERSION == 2
        "except ImportError: \n"
#endif
222
223
224
225
        "   pass \n"
    );

    return vars.join(QChar(18))+QChar(18);
226
227
}

228