summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2009-03-03 10:52:49 (GMT)
committerRaymond Hettinger <python@rcn.com>2009-03-03 10:52:49 (GMT)
commitb0d56afc84f4a7dacd8296e9203b051f019e2c3a (patch)
tree55898cd03e893b6e7694c8fef52731c3d2f36fb8
parentce57cdbea80c274646b0cbde50f05a9cb884bcf6 (diff)
downloadcpython-b0d56afc84f4a7dacd8296e9203b051f019e2c3a.zip
cpython-b0d56afc84f4a7dacd8296e9203b051f019e2c3a.tar.gz
cpython-b0d56afc84f4a7dacd8296e9203b051f019e2c3a.tar.bz2
Give dict views a helpful __repr__.
-rw-r--r--Lib/test/test_dict.py6
-rw-r--r--Misc/NEWS3
-rw-r--r--Objects/dictobject.c21
3 files changed, 24 insertions, 6 deletions
diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py
index bfc31af..308143d 100644
--- a/Lib/test/test_dict.py
+++ b/Lib/test/test_dict.py
@@ -36,16 +36,16 @@ class DictTest(unittest.TestCase):
k = d.keys()
self.assert_('a' in d)
self.assert_('b' in d)
-
self.assertRaises(TypeError, d.keys, None)
+ self.assertEqual(repr(dict(a=1).keys()), "dict_keys(['a'])")
def test_values(self):
d = {}
self.assertEqual(set(d.values()), set())
d = {1:2}
self.assertEqual(set(d.values()), {2})
-
self.assertRaises(TypeError, d.values, None)
+ self.assertEqual(repr(dict(a=1).values()), "dict_values([1])")
def test_items(self):
d = {}
@@ -53,8 +53,8 @@ class DictTest(unittest.TestCase):
d = {1:2}
self.assertEqual(set(d.items()), {(1, 2)})
-
self.assertRaises(TypeError, d.items, None)
+ self.assertEqual(repr(dict(a=1).items()), "dict_items([('a', 1)])")
def test_contains(self):
d = {}
diff --git a/Misc/NEWS b/Misc/NEWS
index d737f74..0a7a53b 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,9 @@ Core and Builtins
-----------------
=======
+
+- Give dict views an informative __repr__.
+
- Issue #5247: Improve error message when unknown format codes are
used when using str.format() with str, int, and float arguments.
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 */