10 #include <QApplication>
16 mState = PyGILState_Ensure();
21 PyGILState_Release((PyGILState_STATE)mState);
25 :
QThread(parent), mScript(script), mSingleStatement(singleStatement),
26 mAbortRequested(false), mSpamCount(0)
40 py::dict tmp_context(py::globals());
41 PythonContext::initializeScript(&tmp_context);
45 pybind11::object rc = py::eval(
"threading.get_ident()", tmp_context, tmp_context);
46 mPythonThreadID = rc.cast<
unsigned long>();
48 mElapsedTimer.
start();
53 rc = py::eval<py::eval_single_statement>(mScript.
toStdString(), tmp_context, tmp_context);
55 rc = py::eval<py::eval_statements>(mScript.
toStdString(), tmp_context, tmp_context);
60 catch (py::error_already_set& e)
62 qDebug() <<
"AlreadySet";
67 catch (std::exception& e)
69 qDebug() <<
"Exception";
78 if (mElapsedTimer.
elapsed() > 100)
84 if (mSpamCount > 1000)
86 else if (mSpamCount < 10 || mElapsedTimer.
elapsed() > 1000)
89 mStdoutBuffer.
clear();
101 QString retval = mStdoutBuffer;
102 mStdoutBuffer.
clear();
118 mAbortRequested =
true;
126 qDebug() <<
"about to terminate thread..." << mPythonThreadID;
130 int nThreads = PyThreadState_SetAsyncExc(mPythonThreadID, PyExc_KeyboardInterrupt);
133 qDebug() <<
"Oh no! The Python interpreter doesn't know that thread.";
135 else if (nThreads > 1)
138 qDebug() <<
"Oh no! There seem to be multiple threads with the same ID!";
140 qDebug() <<
"thread terminated";
148 qDebug() <<
"Oh no! Function already locked waiting for input.";
157 throw std::runtime_error(std::string(
"Python script aborted by user"));
178 return mInput.
toInt();
184 return static_cast<Module*
>(mInput.
value<
void*>());
190 return static_cast<Gate*
>(mInput.
value<
void*>());
~PythonMutex()
Release GIL.
void stdError(QString txt)
Gate * handleGateInput(const QString &prompt)
void handleStdout(const QString &output) override
void setInput(const QVariant &inp)
std::string handleStringInput(const QString &prompt, const QString &defval)
int handleNumberInput(const QString &prompt, int defval)
bool getInput(InputType type, QString prompt, QVariant defaultValue)
std::string handleConsoleInput(const QString &prompt)
Module * handleModuleInput(const QString &prompt)
void handleError(const QString &output) override
void stdOutput(QString txt)
void requireInput(int type, QString prompt, QVariant defaultValue)
PythonThread(const QString &script, bool singleStatement, QObject *parent=nullptr)
std::string handleFilenameInput(const QString &prompt, const QString &filetype)
qint64 elapsed() const const
bool tryLock(int timeout)
QString fromStdString(const std::string &str)
bool isEmpty() const const
std::string toStdString() const const
int toInt(bool *ok) const const
QString toString() const const