10 #include <QApplication>
12 #include <QRegularExpression>
17 mState = PyGILState_Ensure();
22 PyGILState_Release((PyGILState_STATE)mState);
26 :
QThread(parent), mScript(script), mSingleStatement(singleStatement),
27 mAbortRequested(false), mSpamCount(0)
41 py::dict tmp_context(py::globals());
46 pybind11::object rc = py::eval(
"threading.get_ident()", tmp_context, tmp_context);
47 mPythonThreadID = rc.cast<
unsigned long>();
49 mElapsedTimer.
start();
54 rc = py::eval<py::eval_single_statement>(mScript.
toStdString(), tmp_context, tmp_context);
56 rc = py::eval<py::eval_statements>(mScript.
toStdString(), tmp_context, tmp_context);
61 catch (py::error_already_set& e)
63 qDebug() <<
"AlreadySet";
76 QString(
"At line %1:").arg(line));
78 mErrorMessage +=
"\n";
82 catch (std::exception& e)
84 qDebug() <<
"Exception";
93 if (mElapsedTimer.
elapsed() > 100)
99 if (mSpamCount > 1000)
101 else if (mSpamCount < 10 || mElapsedTimer.
elapsed() > 1000)
104 mStdoutBuffer.
clear();
116 QString retval = mStdoutBuffer;
117 mStdoutBuffer.
clear();
133 mAbortRequested =
true;
141 qDebug() <<
"about to terminate thread..." << mPythonThreadID;
145 int nThreads = PyThreadState_SetAsyncExc(mPythonThreadID, PyExc_KeyboardInterrupt);
148 qDebug() <<
"Oh no! The Python interpreter doesn't know that thread.";
150 else if (nThreads > 1)
153 qDebug() <<
"Oh no! There seem to be multiple threads with the same ID!";
155 qDebug() <<
"thread terminated";
163 qDebug() <<
"Oh no! Function already locked waiting for input.";
172 throw std::runtime_error(std::string(
"Python script aborted by user"));
193 return mInput.
toInt();
199 return static_cast<Module*
>(mInput.
value<
void*>());
205 return static_cast<Gate*
>(mInput.
value<
void*>());
static void initializeScript(py::dict *context)
~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)
QRegularExpressionMatch match(const QString &subject, int offset, QRegularExpression::MatchType matchType, QRegularExpression::MatchOptions matchOptions) const const
QString captured(int nth) const const
int capturedLength(int nth) const const
int capturedStart(int nth) const const
bool hasMatch() const const
QString fromStdString(const std::string &str)
bool isEmpty() const const
QString & replace(int position, int n, QChar after)
int toInt(bool *ok, int base) const const
std::string toStdString() const const
int toInt(bool *ok) const const
QString toString() const const