static PyObject *dict_get(PyGRTDictObject *self, PyObject *arg) { PythonContext *ctx = PythonContext::get_and_check(); if (!ctx) return NULL; PyObject *def = NULL; char *key; if (!PyArg_ParseTuple(arg, "s|O", &key, &def)) return NULL; if (key) { if (self->dict->has_key(key)) return ctx->from_grt(self->dict->get(key)); else { if (def) { Py_INCREF(def); return def; } else { PyErr_SetString(PyExc_KeyError, base::strfmt("invalid key '%s'", key).c_str()); } } } Py_RETURN_NONE; }
static PyObject *list_item(PyGRTListObject *self, Py_ssize_t index) { PythonContext *ctx; if (!(ctx= PythonContext::get_and_check())) return NULL; if (index < 0 || index >= (int) self->list->count()) { PyErr_SetString(PyExc_IndexError, "list index out of range"); return NULL; } try { return ctx->from_grt(self->list->get(index)); } catch (grt::bad_item &exc) { PyErr_SetString(PyExc_IndexError, exc.what()); return NULL; } catch (std::exception &exc) { PyErr_SetString(PyExc_RuntimeError, exc.what()); return NULL; } }
static PyObject *dict_setdefault(PyGRTDictObject *self, PyObject *arg) { PythonContext *ctx = PythonContext::get_and_check(); if (!ctx) return NULL; PyObject *def = Py_None; char *key; if (!PyArg_ParseTuple(arg, "s|O", &key, &def)) return NULL; if (key) { if (self->dict->has_key(key)) return ctx->from_grt(self->dict->get(key)); else { if (def != Py_None) Py_INCREF(def); try { self->dict->set(key, ctx->from_pyobject(def)); } catch (grt::bad_item &exc) { PythonContext::set_python_error(exc); } catch (std::exception &exc) { PythonContext::set_python_error(exc); } return ctx->from_grt(self->dict->get(key)); } } Py_RETURN_NONE; }
static PyObject *dict_subscript(PyGRTDictObject *self, PyObject *key) { AutoPyObject tmp; if (PyUnicode_Check(key)) key = tmp = PyUnicode_AsUTF8String(key); if (!PyString_Check(key)) { PyErr_SetString(PyExc_KeyError, "grt.Dict key must be a string"); return NULL; } const char *k = PyString_AsString(key); PythonContext *ctx = PythonContext::get_and_check(); if (!ctx) return NULL; try { return ctx->from_grt(self->dict->get(k)); } catch (grt::bad_item &exc) { PythonContext::set_python_error(exc); return NULL; } catch (std::exception &exc) { PythonContext::set_python_error(exc); return NULL; } return NULL; }
static PyObject *dict_update(PyGRTDictObject *self, PyObject *arg) { PythonContext *ctx = PythonContext::get_and_check(); if (!ctx) return NULL; if (!arg) { PyErr_SetString(PyExc_ValueError, "dict argument required for update()"); return NULL; } TypeSpec type; type.base.type = DictType; type.content.type = AnyType; grt::DictRef value; try { value = grt::DictRef::cast_from(ctx->from_pyobject(arg, type)); } catch (std::exception &exc) { PythonContext::set_python_error(exc, "invalid argument"); return NULL; } if (value.is_valid()) { merge_contents(*self->dict, value, true); } else { PyErr_SetString(PyExc_ValueError, "invalid argument for update()"); return NULL; } Py_RETURN_NONE; }
void PythonShell::init() { _loader = dynamic_cast<PythonModuleLoader *>(grt::GRT::get()->get_module_loader(LanguagePython)); if (!_loader) throw std::runtime_error("Python module loader not initialized"); PythonContext *pycontext = _loader->get_python_context(); pycontext->refresh(); }
static PyObject *dict_values(PyGRTDictObject *self, PyObject *args) { if (args) { PyErr_SetString(PyExc_ValueError, "method takes no arguments"); return NULL; } PythonContext *ctx = PythonContext::get_and_check(); if (!ctx) return NULL; PyObject *list = PyList_New(self->dict->count()); Py_ssize_t i = 0; for (grt::DictRef::const_iterator iter = self->dict->begin(); iter != self->dict->end(); ++iter) PyList_SetItem(list, i++, ctx->from_grt(iter->second)); return list; }
PyObject *PythonContext::outstream_write(PyObject *self, PyObject *args) { const char *text = NULL; if(!PyArg_ParseTuple(args, "z:write", &text)) return NULL; if(PyErr_Occurred()) return NULL; OutputRedirector *redirector = (OutputRedirector *)self; if(redirector) { PythonContext *context = redirector->context; // most likely this is NULL because the sys.stdout override is static and shared amongst // contexts. So look up the global variable that stores the context if(context == NULL) { _frame *frame = PyEval_GetFrame(); while(frame) { PyObject *globals = frame->f_globals; if(globals) { OutputRedirector *global = (OutputRedirector *)PyDict_GetItemString(globals, "_renderdoc_internal"); if(global) context = global->context; } if(context) break; frame = frame->f_back; } } if(context) { context->addText(redirector->isStdError ? true : false, QString::fromUtf8(text)); } } Py_RETURN_NONE; }
static PyObject *dict_getattro(PyGRTDictObject *self, PyObject *attr_name) { AutoPyObject tmp; if (PyUnicode_Check(attr_name)) attr_name = tmp = PyUnicode_AsUTF8String(attr_name); if (PyString_Check(attr_name)) { const char *attrname = PyString_AsString(attr_name); PyObject *object; if ((object = PyObject_GenericGetAttr((PyObject *)self, attr_name))) return object; PyErr_Clear(); if (strcmp(attrname, "__members__") == 0) { PyObject *members = Py_BuildValue("[s]", "__contenttype__"); for (grt::DictRef::const_iterator iter = self->dict->begin(); iter != self->dict->end(); ++iter) { PyObject *tmp = PyString_FromString(iter->first.c_str()); PyList_Append(members, tmp); Py_DECREF(tmp); } return members; } else if (strcmp(attrname, "__methods__") == 0) { PyObject *methods = Py_BuildValue("[sssss]", "keys", "items", "values", "has_key", "update", "setdefault"); return methods; } else { if (self->dict->has_key(attrname)) { PythonContext *ctx = PythonContext::get_and_check(); if (!ctx) return NULL; return ctx->from_grt(self->dict->get(attrname)); } else PyErr_SetString(PyExc_AttributeError, base::strfmt("unknown attribute '%s'", attrname).c_str()); } } PyErr_SetString(PyExc_KeyError, "grt.Dict key must be a string"); return NULL; }
static int dict_ass_subscript(PyGRTDictObject *self, PyObject *key, PyObject *value) { AutoPyObject tmp; if (PyUnicode_Check(key)) key = tmp = PyUnicode_AsUTF8String(key); if (!PyString_Check(key)) { PyErr_SetString(PyExc_KeyError, "grt.Dict key must be a string"); return -1; } const char *k = PyString_AsString(key); PythonContext *ctx = PythonContext::get_and_check(); if (!ctx) return -1; try { if (value == NULL) self->dict->remove(k); else if (value == Py_None) self->dict->set(k, grt::ValueRef()); else { grt::ValueRef v = ctx->from_pyobject(value); if (!v.is_valid()) { PyErr_SetString(PyExc_ValueError, "grt.Dict may only be assigned other GRT or string/numeric values"); return -1; } self->dict->set(k, v); } return 0; } catch (grt::bad_item &exc) { PythonContext::set_python_error(exc); } catch (std::exception &exc) { PythonContext::set_python_error(exc); } return -1; }
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) { 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; }