| 1268 | } |
| 1269 | |
| 1270 | PyObject *PythonContext::outstream_write(PyObject *self, PyObject *args) |
| 1271 | { |
| 1272 | const char *text = NULL; |
| 1273 | |
| 1274 | if(!PyArg_ParseTuple(args, "z:write", &text)) |
| 1275 | return NULL; |
| 1276 | |
| 1277 | if(PyErr_Occurred()) |
| 1278 | return NULL; |
| 1279 | |
| 1280 | OutputRedirector *redirector = (OutputRedirector *)self; |
| 1281 | |
| 1282 | if(redirector) |
| 1283 | { |
| 1284 | PythonContext *context = redirector->context; |
| 1285 | // most likely this is NULL because the sys.stdout override is static and shared amongst |
| 1286 | // contexts. So look up the global variable that stores the context |
| 1287 | if(context == NULL) |
| 1288 | { |
| 1289 | PyFrameObject *frame = PyEval_GetFrame(); |
| 1290 | // inc reference count here so all frames can be decref'd whether they come from here or |
| 1291 | // PyFrame_GetBack |
| 1292 | Py_XINCREF(frame); |
| 1293 | |
| 1294 | while(frame) |
| 1295 | { |
| 1296 | PyObject *globals = PyFrame_GetGlobals(frame); |
| 1297 | if(globals) |
| 1298 | { |
| 1299 | OutputRedirector *global = |
| 1300 | (OutputRedirector *)PyDict_GetItemString(globals, "_renderdoc_internal"); |
| 1301 | if(global) |
| 1302 | context = global->context; |
| 1303 | } |
| 1304 | |
| 1305 | Py_XDECREF(globals); |
| 1306 | |
| 1307 | // first get the next frame without decrefing the current |
| 1308 | PyFrameObject *back = PyFrame_GetBack(frame); |
| 1309 | // now decref the old frame |
| 1310 | Py_XDECREF(frame); |
| 1311 | // and iterate on the next one, if we got one |
| 1312 | frame = back; |
| 1313 | |
| 1314 | if(context) |
| 1315 | { |
| 1316 | // release the last trailing ref, which we know is either NULL or a strong reference from |
| 1317 | // PyFrame_GetBack |
| 1318 | Py_XDECREF(frame); |
| 1319 | break; |
| 1320 | } |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | if(context) |
| 1325 | { |
| 1326 | context->addText(redirector->isStdError ? true : false, QString::fromUtf8(text)); |
| 1327 | } |
nothing calls this directly
no test coverage detected