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; }
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 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; }