diff --git a/qrenderdoc/Code/pyrenderdoc/PythonContext.cpp b/qrenderdoc/Code/pyrenderdoc/PythonContext.cpp index aa46df46a..c11b4f55d 100644 --- a/qrenderdoc/Code/pyrenderdoc/PythonContext.cpp +++ b/qrenderdoc/Code/pyrenderdoc/PythonContext.cpp @@ -130,6 +130,64 @@ static PyMethodDef OutputRedirector_methods[] = { PyObject *PythonContext::main_dict = NULL; +void FetchException(QString &typeStr, QString &valueStr, QList frames) +{ + PyObject *exObj = NULL, *valueObj = NULL, *tracebackObj = NULL; + + PyErr_Fetch(&exObj, &valueObj, &tracebackObj); + + PyErr_NormalizeException(&exObj, &valueObj, &tracebackObj); + + if(exObj && PyType_Check(exObj)) + { + PyTypeObject *type = (PyTypeObject *)exObj; + + typeStr = QString::fromUtf8(type->tp_name); + } + else + { + typeStr = ""; + } + + if(valueObj) + valueStr = ToQStr(valueObj); + + if(tracebackObj) + { + PyObject *tracebackModule = PyImport_ImportModule("traceback"); + + if(tracebackModule) + { + PyObject *func = PyObject_GetAttrString(tracebackModule, "format_tb"); + + if(func && PyCallable_Check(func)) + { + PyObject *args = Py_BuildValue("(N)", tracebackObj); + PyObject *formattedTB = PyObject_CallObject(func, args); + + if(formattedTB) + { + Py_ssize_t size = PyList_Size(formattedTB); + for(Py_ssize_t i = 0; i < size; i++) + { + PyObject *el = PyList_GetItem(formattedTB, i); + + frames << ToQStr(el); + } + + Py_DecRef(formattedTB); + } + + Py_DecRef(args); + } + } + } + + Py_DecRef(exObj); + Py_DecRef(valueObj); + Py_DecRef(tracebackObj); +} + void PythonContext::GlobalInit() { // must happen on the UI thread @@ -167,10 +225,11 @@ void PythonContext::GlobalInit() OutputRedirectorType.tp_name = "renderdoc_output_redirector"; OutputRedirectorType.tp_basicsize = sizeof(OutputRedirector); - OutputRedirectorType.tp_flags = Py_TPFLAGS_DEFAULT; + OutputRedirectorType.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE; OutputRedirectorType.tp_doc = "Output redirector, to be able to catch output to stdout and stderr"; OutputRedirectorType.tp_new = PyType_GenericNew; + OutputRedirectorType.tp_dealloc = &PythonContext::outstream_del; OutputRedirectorType.tp_methods = OutputRedirector_methods; OutputRedirector_methods[0].ml_meth = &PythonContext::outstream_write; @@ -280,25 +339,27 @@ PythonContext::PythonContext(QObject *parent) : QObject(parent) QString valueStr = ""; QList frames; - // set global output that point to this + // set global output that point to this context. It is responsible for deleting the context when + // it goes out of scope PyObject *redirector = PyObject_CallFunction((PyObject *)&OutputRedirectorType, ""); if(redirector) { - PyDict_SetItemString(context_namespace, "renderdoc_output_redirector_context_pointer", - redirector); + PyDict_SetItemString(context_namespace, "_renderdoc_internal", redirector); OutputRedirector *output = (OutputRedirector *)redirector; output->context = this; + Py_DECREF(redirector); } // release the GIL again PyGILState_Release(gil); } -PythonContext::~PythonContext() +void PythonContext::Finish() { PyGILState_STATE gil = PyGILState_Ensure(); + // release our external handle to globals. It'll now only be ref'd from inside Py_XDECREF(context_namespace); PyGILState_Release(gil); @@ -352,8 +413,12 @@ void PythonContext::executeString(const QString &filename, const QString &source PyEval_SetTrace(&PythonContext::traceEvent, traceContext); + m_State = PyGILState_GetThisThreadState(); + ret = PyEval_EvalCode(compiled, context_namespace, context_namespace); + m_State = NULL; + Py_XDECREF(thisobj); Py_XDECREF(traceContext); } @@ -366,62 +431,7 @@ void PythonContext::executeString(const QString &filename, const QString &source bool caughtException = (ret == NULL); if(caughtException) - { - PyObject *exObj = NULL, *valueObj = NULL, *tracebackObj = NULL; - - PyErr_Fetch(&exObj, &valueObj, &tracebackObj); - - PyErr_NormalizeException(&exObj, &valueObj, &tracebackObj); - - if(exObj && PyType_Check(exObj)) - { - PyTypeObject *type = (PyTypeObject *)exObj; - - typeStr = QString::fromUtf8(type->tp_name); - } - else - { - typeStr = tr("Unknown Exception"); - } - - if(valueObj) - valueStr = ToQStr(valueObj); - - if(tracebackObj) - { - PyObject *tracebackModule = PyImport_ImportModule("traceback"); - - if(tracebackModule) - { - PyObject *func = PyObject_GetAttrString(tracebackModule, "format_tb"); - - if(func && PyCallable_Check(func)) - { - PyObject *args = Py_BuildValue("(N)", tracebackObj); - PyObject *formattedTB = PyObject_CallObject(func, args); - - if(formattedTB) - { - Py_ssize_t size = PyList_Size(formattedTB); - for(Py_ssize_t i = 0; i < size; i++) - { - PyObject *el = PyList_GetItem(formattedTB, i); - - frames << ToQStr(el); - } - - Py_DecRef(formattedTB); - } - - Py_DecRef(args); - } - } - } - - Py_DecRef(exObj); - Py_DecRef(valueObj); - Py_DecRef(tracebackObj); - } + FetchException(typeStr, valueStr, frames); Py_XDECREF(ret); @@ -569,6 +579,18 @@ void PythonContext::setPyGlobal(const char *varName, PyObject *obj) emit exception("RuntimeError", QString("Failed to set variable '%1'").arg(varName), {}); } +void PythonContext::outstream_del(PyObject *self) +{ + OutputRedirector *redirector = (OutputRedirector *)self; + + if(redirector) + { + PythonContext *context = redirector->context; + + delete context; + } +} + PyObject *PythonContext::outstream_write(PyObject *self, PyObject *args) { const char *text = NULL; @@ -588,8 +610,8 @@ PyObject *PythonContext::outstream_write(PyObject *self, PyObject *args) PyObject *globals = PyEval_GetGlobals(); if(globals) { - OutputRedirector *global = (OutputRedirector *)PyDict_GetItemString( - globals, "renderdoc_output_redirector_context_pointer"); + OutputRedirector *global = + (OutputRedirector *)PyDict_GetItemString(globals, "_renderdoc_internal"); if(global) context = global->context; } @@ -625,3 +647,25 @@ int PythonContext::traceEvent(PyObject *obj, PyFrameObject *frame, int what, PyO return 0; } + +extern "C" PyThreadState *GetExecutingThreadState(PyObject *global_handle) +{ + OutputRedirector *redirector = (OutputRedirector *)global_handle; + if(redirector->context) + return redirector->context->GetExecutingThreadState(); + + return NULL; +} + +extern "C" void HandleException(PyObject *global_handle) +{ + QString typeStr; + QString valueStr = ""; + QList frames; + + FetchException(typeStr, valueStr, frames); + + OutputRedirector *redirector = (OutputRedirector *)global_handle; + if(redirector->context) + emit redirector->context->exception(typeStr, valueStr, frames); +} diff --git a/qrenderdoc/Code/pyrenderdoc/PythonContext.h b/qrenderdoc/Code/pyrenderdoc/PythonContext.h index 6d074364e..ba1c747d7 100644 --- a/qrenderdoc/Code/pyrenderdoc/PythonContext.h +++ b/qrenderdoc/Code/pyrenderdoc/PythonContext.h @@ -41,10 +41,15 @@ class PythonContext : public QObject private: Q_OBJECT + // don't allow destruction from outside, you must heap-allocate the context and let it delete + // itself when all references are done. This handles the case where e.g. some Async work is going + // on and needs to finish executing after the external code is done with the context + ~PythonContext() {} public: explicit PythonContext(QObject *parent = NULL); - ~PythonContext(); + void Finish(); + PyThreadState *GetExecutingThreadState() { return m_State; } static void GlobalInit(); static void GlobalShutdown(); @@ -88,6 +93,10 @@ private: // globals are set into and any scripts execute in PyObject *context_namespace = NULL; + // this is set during an execute, so we can identify when a callback happens within our execute or + // not + PyThreadState *m_State = NULL; + struct { QString file; @@ -97,6 +106,7 @@ private: void setQtGlobal_internal(const char *varName, const char *typeName, QObject *object); // Python callbacks + static void outstream_del(PyObject *self); static PyObject *outstream_write(PyObject *self, PyObject *args); static PyObject *outstream_flush(PyObject *self, PyObject *args); static int traceEvent(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg); @@ -109,4 +119,20 @@ template <> void PythonContext::setGlobal(const char *varName, QObject *object); template <> -void PythonContext::setGlobal(const char *varName, QWidget *object); \ No newline at end of file +void PythonContext::setGlobal(const char *varName, QWidget *object); + +// helper struct to handle dynamically allocating then calling Finish() + +struct PythonContextHandle +{ +public: + PythonContextHandle() { m_ctx = new PythonContext; } + ~PythonContextHandle() { m_ctx->Finish(); } + // don't allow copying + PythonContextHandle(const PythonContextHandle &) = delete; + PythonContextHandle &operator=(const PythonContextHandle &) = delete; + + PythonContext &ctx() { return *m_ctx; } +private: + PythonContext *m_ctx; +}; \ No newline at end of file diff --git a/qrenderdoc/Code/pyrenderdoc/pyconversion.h b/qrenderdoc/Code/pyrenderdoc/pyconversion.h index 7ba113183..bd92c9e1d 100644 --- a/qrenderdoc/Code/pyrenderdoc/pyconversion.h +++ b/qrenderdoc/Code/pyrenderdoc/pyconversion.h @@ -457,8 +457,11 @@ PyObject *ConvertToPy(PyObject *self, const T &in) return TypeConversion::ConvertToPy(self, in); } +// See renderdoc.i for implementation & explanation +extern "C" void HandleCallbackFailure(PyObject *global_handle, bool &failflag); + template -inline T get_return(const char *funcname, PyObject *result, bool &failflag) +inline T get_return(const char *funcname, PyObject *result, PyObject *global_handle, bool &failflag) { T val = T(); @@ -466,7 +469,7 @@ inline T get_return(const char *funcname, PyObject *result, bool &failflag) if(!SWIG_IsOK(res)) { - failflag = true; + HandleCallbackFailure(global_handle, failflag); PyErr_Format(PyExc_TypeError, "Expected a '%s' for return value of callback in %s", TypeName(), funcname); @@ -478,7 +481,7 @@ inline T get_return(const char *funcname, PyObject *result, bool &failflag) } template <> -inline void get_return(const char *funcname, PyObject *result, bool &failflag) +inline void get_return(const char *funcname, PyObject *result, PyObject *global_handle, bool &failflag) { Py_XDECREF(result); } @@ -519,33 +522,62 @@ struct varfunc } ~varfunc() { Py_XDECREF(args); } - rettype call(const char *funcname, PyObject *func, bool &failflag) + rettype call(const char *funcname, PyObject *func, PyObject *global_handle, bool &failflag) { if(!func || func == Py_None || !PyCallable_Check(func) || !args) { - failflag = true; + HandleCallbackFailure(global_handle, failflag); return rettype(); } PyObject *result = PyObject_Call(func, args, 0); if(result == NULL) - failflag = true; + HandleCallbackFailure(global_handle, failflag); Py_DECREF(args); - return get_return(funcname, result, failflag); + return get_return(funcname, result, global_handle, failflag); } int currentarg = 0; PyObject *args; }; +struct ScopedFuncCall +{ + ScopedFuncCall(PyObject *h) + { + handle = h; + Py_XINCREF(handle); + gil = PyGILState_Ensure(); + } + + ~ScopedFuncCall() + { + Py_XDECREF(handle); + PyGILState_Release(gil); + } + + PyObject *handle; + PyGILState_STATE gil; +}; + template funcType ConvertFunc(PyObject *self, const char *funcname, PyObject *func, bool &failflag) { - return [self, funcname, func, &failflag](auto... param) { + // add a reference to the global object so it stays alive while we execute, in case this is an + // async call + PyObject *global_internal_handle = NULL; + + PyObject *globals = PyEval_GetGlobals(); + if(globals) + global_internal_handle = PyDict_GetItemString(globals, "_renderdoc_internal"); + + return [global_internal_handle, self, funcname, func, &failflag](auto... param) { + ScopedFuncCall gil(global_internal_handle); + varfunc f(self, funcname, param...); - return f.call(funcname, func, failflag); + return f.call(funcname, func, global_internal_handle, failflag); }; } diff --git a/qrenderdoc/Code/pyrenderdoc/pyrenderdoc_module.cpp b/qrenderdoc/Code/pyrenderdoc/pyrenderdoc_module.cpp new file mode 100644 index 000000000..930b70ce4 --- /dev/null +++ b/qrenderdoc/Code/pyrenderdoc/pyrenderdoc_module.cpp @@ -0,0 +1,34 @@ +/****************************************************************************** + * The MIT License (MIT) + * + * Copyright (c) 2017 Baldur Karlsson + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + ******************************************************************************/ + +#include + +extern "C" PyThreadState *GetExecutingThreadState(PyObject *global_handle) +{ + return NULL; +} + +extern "C" void HandleException(PyObject *global_handle) +{ +} diff --git a/qrenderdoc/Code/pyrenderdoc/renderdoc.i b/qrenderdoc/Code/pyrenderdoc/renderdoc.i index f64e15ad6..f72fd4598 100644 --- a/qrenderdoc/Code/pyrenderdoc/renderdoc.i +++ b/qrenderdoc/Code/pyrenderdoc/renderdoc.i @@ -367,6 +367,34 @@ PyObject *PassObjectToPython(const char *type, void *obj) return SWIG_InternalNewPointerObj(obj, t, 0); } +// this is defined elsewhere for managing the opaque global_handle object +PyThreadState *GetExecutingThreadState(PyObject *global_handle); +void HandleException(PyObject *global_handle); + +// this function handles failures in callback functions. If we're synchronously calling the callback from within an execute scope, then we can assign to failflag and let the error propagate upwards. If we're not, then the callback is being executed on another thread with no knowledge of python, so we need to use the global handle to try and emit the exception through the context. None of this is multi-threaded because we're inside the GIL at all times +void HandleCallbackFailure(PyObject *global_handle, bool &fail_flag) +{ + // if there's no global handle assume we are not running in the usual environment, so there are no external-to-python threads + if(!global_handle) + { + fail_flag = true; + return; + } + + PyThreadState *current = PyGILState_GetThisThreadState(); + PyThreadState *executing = GetExecutingThreadState(global_handle); + + // we are executing synchronously, set the flag and return + if(current == executing) + { + fail_flag = true; + return; + } + + // in this case we are executing asynchronously, and must handle the exception manually as there's nothing above us that knows about python exceptions + HandleException(global_handle); +} + %} %header %{