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