nextpnr/gui/pythontab.cc

101 lines
2.9 KiB
C++
Raw Normal View History

2018-06-15 00:53:32 +08:00
#include "pythontab.h"
2018-06-15 00:53:48 +08:00
#include <QGridLayout>
2018-06-15 00:53:32 +08:00
#include "emb.h"
#include "pybindings.h"
PythonTab::PythonTab(QWidget *parent) : QWidget(parent)
{
PyImport_ImportModule("emb");
// Add text area for Python output and input line
plainTextEdit = new QPlainTextEdit();
plainTextEdit->setReadOnly(true);
plainTextEdit->setMinimumHeight(100);
QFont f("unexistent");
f.setStyleHint(QFont::Monospace);
plainTextEdit->setFont(f);
2018-06-20 15:42:47 +08:00
lineEdit = new LineEditor();
2018-06-15 00:53:32 +08:00
lineEdit->setMinimumHeight(30);
lineEdit->setMaximumHeight(30);
lineEdit->setFont(f);
2018-06-15 00:53:32 +08:00
QGridLayout *mainLayout = new QGridLayout();
mainLayout->addWidget(plainTextEdit, 0, 0);
mainLayout->addWidget(lineEdit, 1, 0);
setLayout(mainLayout);
2018-06-20 15:42:47 +08:00
connect(lineEdit, SIGNAL(textLineInserted(QString)), this,
SLOT(editLineReturnPressed(QString)));
2018-06-15 00:53:32 +08:00
write = [this](std::string s) {
plainTextEdit->moveCursor(QTextCursor::End);
plainTextEdit->insertPlainText(s.c_str());
plainTextEdit->moveCursor(QTextCursor::End);
};
emb::set_stdout(write);
2018-06-20 15:42:47 +08:00
char buff[1024];
sprintf(buff, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
print(buff);
}
void PythonTab::print(std::string line)
{
plainTextEdit->moveCursor(QTextCursor::End);
plainTextEdit->insertPlainText(line.c_str());
plainTextEdit->moveCursor(QTextCursor::End);
2018-06-15 00:53:32 +08:00
}
void handle_system_exit() { exit(-1); }
2018-06-20 15:42:47 +08:00
int PythonTab::executePython(std::string &command)
2018-06-15 00:53:32 +08:00
{
PyObject *m, *d, *v;
m = PyImport_AddModule("__main__");
if (m == NULL)
return -1;
d = PyModule_GetDict(m);
v = PyRun_StringFlags(command.c_str(),
(command.empty() ? Py_file_input : Py_single_input),
d, d, NULL);
if (v == NULL) {
PyObject *exception, *v, *tb;
if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
handle_system_exit();
}
PyErr_Fetch(&exception, &v, &tb);
if (exception == NULL)
return 0;
PyErr_NormalizeException(&exception, &v, &tb);
if (tb == NULL) {
tb = Py_None;
Py_INCREF(tb);
}
PyException_SetTraceback(v, tb);
if (exception == NULL)
return 0;
PyErr_Clear();
PyObject *objectsRepresentation = PyObject_Str(v);
std::string errorStr =
PyUnicode_AsUTF8(objectsRepresentation) + std::string("\n");
2018-06-20 15:42:47 +08:00
print(errorStr);
2018-06-15 00:53:32 +08:00
Py_DECREF(objectsRepresentation);
Py_XDECREF(exception);
Py_XDECREF(v);
Py_XDECREF(tb);
return -1;
}
Py_DECREF(v);
return 0;
}
2018-06-20 15:42:47 +08:00
void PythonTab::editLineReturnPressed(QString text)
2018-06-15 00:53:32 +08:00
{
2018-06-20 15:42:47 +08:00
std::string input = text.toStdString();
print(std::string(">>> " + input + "\n"));
2018-06-15 00:53:32 +08:00
int error = executePython(input);
}