pythonserver.cpp 3.53 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 <Python.h>

25
PythonServer::PythonServer(QObject* parent)
26
27
28
29
    : QObject(parent)
{
}

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

44
void PythonServer::login()
45
46
47
{
    Py_Initialize();
    m_pModule = PyImport_AddModule("__main__");
48
    filePath = QLatin1String("python_cantor_worksheet");
49
50
}

51
void PythonServer::runPythonCommand(const QString& command) const
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
    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);

    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;
        }
    }
#if PY_MAJOR_VERSION == 3
    PyEval_EvalCode(compile, py_dict, py_dict);
#elif PY_MAJOR_VERSION == 2
    PyEval_EvalCode((PyCodeObject*)compile, py_dict, py_dict);
#else
    #warning Unknown Python version
#endif
    if (PyErr_Occurred())
        PyErr_PrintEx(0);
93
94
}

95
QString PythonServer::getError() const
96
{
97
98
    PyObject *errorPython = PyObject_GetAttrString(m_pModule, "errorPythonBackend");
    PyObject *error = PyObject_GetAttrString(errorPython, "value");
99

100
    return pyObjectToQString(error);
101
102
}

103
QString PythonServer::getOutput() const
104
{
105
106
    PyObject *outputPython = PyObject_GetAttrString(m_pModule, "outputPythonBackend");
    PyObject *output = PyObject_GetAttrString(outputPython, "value");
107

108
    return pyObjectToQString(output);
109
110
}

111
112
113
114
115
void PythonServer::setFilePath(const QString& path)
{
    this->filePath = path;
    PyRun_SimpleString(("__file__ = '"+path.toStdString()+"'").c_str());
}
116

117