From b904b28187c7eed1e2784fe3dc669437009f22ca Mon Sep 17 00:00:00 2001 From: baldurk Date: Tue, 18 Apr 2017 13:43:40 +0100 Subject: [PATCH] Add support for aborting python execution at the next traceback call --- qrenderdoc/Code/pyrenderdoc/PythonContext.cpp | 20 ++++++++++++++----- qrenderdoc/Code/pyrenderdoc/PythonContext.h | 4 ++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/qrenderdoc/Code/pyrenderdoc/PythonContext.cpp b/qrenderdoc/Code/pyrenderdoc/PythonContext.cpp index 33e5406b0..e10a6206c 100644 --- a/qrenderdoc/Code/pyrenderdoc/PythonContext.cpp +++ b/qrenderdoc/Code/pyrenderdoc/PythonContext.cpp @@ -442,6 +442,8 @@ void PythonContext::executeString(const QString &filename, const QString &source PyEval_SetTrace(&PythonContext::traceEvent, traceContext); + m_Abort = false; + m_State = PyGILState_GetThisThreadState(); ret = PyEval_EvalCode(compiled, context_namespace, context_namespace); @@ -674,18 +676,26 @@ PyObject *PythonContext::outstream_flush(PyObject *self, PyObject *args) int PythonContext::traceEvent(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg) { + PyObject *thisobj = PyDict_GetItemString(obj, "thisobj"); + + uint64_t thisuint64 = PyLong_AsUnsignedLongLong(thisobj); + uintptr_t thisint = (uintptr_t)thisuint64; + PythonContext *context = (PythonContext *)thisint; + PyObject *compiled = PyDict_GetItemString(obj, "compiled"); if(compiled == (PyObject *)frame->f_code && what == PyTrace_LINE) { - PyObject *thisobj = PyDict_GetItemString(obj, "thisobj"); - - uint64_t thisuint64 = PyLong_AsUnsignedLongLong(thisobj); - uintptr_t thisint = (uintptr_t)thisuint64; - PythonContext *context = (PythonContext *)thisint; + context->location.line = PyFrame_GetLineNumber(frame); emit context->traceLine(context->location.file, context->location.line); } + if(context->shouldAbort()) + { + PyErr_SetString(PyExc_SystemExit, "Execution aborted."); + return -1; + } + return 0; } diff --git a/qrenderdoc/Code/pyrenderdoc/PythonContext.h b/qrenderdoc/Code/pyrenderdoc/PythonContext.h index a73f56582..6a0c802e1 100644 --- a/qrenderdoc/Code/pyrenderdoc/PythonContext.h +++ b/qrenderdoc/Code/pyrenderdoc/PythonContext.h @@ -80,6 +80,8 @@ public: static PyObject *QWidgetToPy(QWidget *widget) { return QtObjectToPython("QWidget", widget); } static QWidget *QWidgetFromPy(PyObject *widget); + void abort() { m_Abort = true; } + bool shouldAbort() { return m_Abort; } QString currentFile() { return location.file; } int currentLine() { return location.line; } signals: @@ -115,6 +117,8 @@ private: int line = 0; } location; + bool m_Abort = false; + static PyObject *QtObjectToPython(const char *typeName, QObject *object); // Python callbacks