mirror of https://github.com/python/cpython
Give dict views a helpful __repr__.
This commit is contained in:
parent
ce57cdbea8
commit
b0d56afc84
|
@ -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 = {}
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
|
|
@ -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 */
|
||||
|
|
Loading…
Reference in New Issue