diff options
author | Raymond Hettinger <python@rcn.com> | 2009-03-03 10:52:49 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2009-03-03 10:52:49 (GMT) |
commit | b0d56afc84f4a7dacd8296e9203b051f019e2c3a (patch) | |
tree | 55898cd03e893b6e7694c8fef52731c3d2f36fb8 /Objects/dictobject.c | |
parent | ce57cdbea80c274646b0cbde50f05a9cb884bcf6 (diff) | |
download | cpython-b0d56afc84f4a7dacd8296e9203b051f019e2c3a.zip cpython-b0d56afc84f4a7dacd8296e9203b051f019e2c3a.tar.gz cpython-b0d56afc84f4a7dacd8296e9203b051f019e2c3a.tar.bz2 |
Give dict views a helpful __repr__.
Diffstat (limited to 'Objects/dictobject.c')
-rw-r--r-- | Objects/dictobject.c | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 10b6fd8..e235993 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -2563,6 +2563,21 @@ dictview_richcompare(PyObject *self, PyObject *other, int op) return result; } +static PyObject * +dictview_repr(dictviewobject *dv) +{ + PyObject *seq; + PyObject *result; + + seq = PySequence_List((PyObject *)dv); + if (seq == NULL) + return NULL; + + result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq); + Py_DECREF(seq); + return result; +} + /*** dict_keys ***/ static PyObject * @@ -2700,7 +2715,7 @@ PyTypeObject PyDictKeys_Type = { 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_reserved */ - 0, /* tp_repr */ + (reprfunc)dictview_repr, /* tp_repr */ &dictviews_as_number, /* tp_as_number */ &dictkeys_as_sequence, /* tp_as_sequence */ 0, /* tp_as_mapping */ @@ -2784,7 +2799,7 @@ PyTypeObject PyDictItems_Type = { 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_reserved */ - 0, /* tp_repr */ + (reprfunc)dictview_repr, /* tp_repr */ &dictviews_as_number, /* tp_as_number */ &dictitems_as_sequence, /* tp_as_sequence */ 0, /* tp_as_mapping */ @@ -2849,7 +2864,7 @@ PyTypeObject PyDictValues_Type = { 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_reserved */ - 0, /* tp_repr */ + (reprfunc)dictview_repr, /* tp_repr */ 0, /* tp_as_number */ &dictvalues_as_sequence, /* tp_as_sequence */ 0, /* tp_as_mapping */ |