aboutsummaryrefslogtreecommitdiffstats
path: root/gui/pythontab.cc
diff options
context:
space:
mode:
authorMiodrag Milanovic <mmicko@gmail.com>2018-06-14 18:53:32 +0200
committerMiodrag Milanovic <mmicko@gmail.com>2018-06-14 20:04:22 +0200
commit4e82ed46d209d05508c7af24cfe135c78ee353db (patch)
tree8af990b5b75b5cb1f443a3ef1fc4be67f28e9556 /gui/pythontab.cc
parent9c0640240fdb2df2b1a39602e0750fdfd4fb541e (diff)
downloadnextpnr-4e82ed46d209d05508c7af24cfe135c78ee353db.tar.gz
nextpnr-4e82ed46d209d05508c7af24cfe135c78ee353db.tar.bz2
nextpnr-4e82ed46d209d05508c7af24cfe135c78ee353db.zip
Split to classes
Diffstat (limited to 'gui/pythontab.cc')
-rw-r--r--gui/pythontab.cc90
1 files changed, 90 insertions, 0 deletions
diff --git a/gui/pythontab.cc b/gui/pythontab.cc
new file mode 100644
index 00000000..8f620958
--- /dev/null
+++ b/gui/pythontab.cc
@@ -0,0 +1,90 @@
+#include "pythontab.h"
+#include "emb.h"
+#include "pybindings.h"
+#include <QGridLayout>
+
+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);
+ lineEdit = new QLineEdit();
+ lineEdit->setMinimumHeight(30);
+ lineEdit->setMaximumHeight(30);
+
+ QGridLayout *mainLayout = new QGridLayout();
+ mainLayout->addWidget(plainTextEdit, 0, 0);
+ mainLayout->addWidget(lineEdit, 1, 0);
+ setLayout(mainLayout);
+
+ connect(lineEdit, SIGNAL(returnPressed()), this,
+ SLOT(editLineReturnPressed()));
+
+ write = [this](std::string s) {
+ plainTextEdit->moveCursor(QTextCursor::End);
+ plainTextEdit->insertPlainText(s.c_str());
+ plainTextEdit->moveCursor(QTextCursor::End);
+ };
+ emb::set_stdout(write);
+}
+
+void handle_system_exit() { exit(-1); }
+
+int PythonTab::executePython(std::string command)
+{
+ 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");
+ plainTextEdit->moveCursor(QTextCursor::End);
+ plainTextEdit->insertPlainText(errorStr.c_str());
+ plainTextEdit->moveCursor(QTextCursor::End);
+ Py_DECREF(objectsRepresentation);
+ Py_XDECREF(exception);
+ Py_XDECREF(v);
+ Py_XDECREF(tb);
+ return -1;
+ }
+ Py_DECREF(v);
+ return 0;
+}
+
+void PythonTab::editLineReturnPressed()
+{
+ std::string input = lineEdit->text().toStdString();
+ plainTextEdit->moveCursor(QTextCursor::End);
+ plainTextEdit->insertPlainText(std::string(">>> " + input + "\n").c_str());
+ plainTextEdit->moveCursor(QTextCursor::End);
+ plainTextEdit->update();
+ lineEdit->clear();
+ int error = executePython(input);
+}