From ada4d52d117be767b18848afbdde5555d18afd12 Mon Sep 17 00:00:00 2001 From: sweeneyde Date: Sun, 7 Jun 2020 03:39:06 -0400 Subject: [PATCH 1/4] add mapping attr for dict views --- Lib/test/test_dict.py | 13 +++++++++++++ Objects/dictobject.c | 13 ++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py index 6b8596fff6a9f7..408c193873a53b 100644 --- a/Lib/test/test_dict.py +++ b/Lib/test/test_dict.py @@ -105,6 +105,19 @@ def test_items(self): self.assertRaises(TypeError, d.items, None) self.assertEqual(repr(dict(a=1).items()), "dict_items([('a', 1)])") + def test_views_mapping(self): + d = {} + self.assertIs(d.keys().mapping, d) + self.assertIs(d.items().mapping, d) + self.assertIs(d.values().mapping, d) + + class MyDict(dict): + pass + my_dict = MyDict() + self.assertIs(my_dict.keys().mapping, my_dict) + self.assertIs(my_dict.items().mapping, my_dict) + self.assertIs(my_dict.values().mapping, my_dict) + def test_contains(self): d = {} self.assertNotIn('a', d) diff --git a/Objects/dictobject.c b/Objects/dictobject.c index c4d5da51f31933..61daae674f33a9 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -117,6 +117,7 @@ converting the dict to the combined table. #include "pycore_pystate.h" // _PyThreadState_GET() #include "dict-common.h" #include "stringlib/eq.h" // unicode_eq() +#include "structmember.h" // PyMemberDef /*[clinic input] class dict "PyDictObject *" "&PyDict_Type" @@ -4448,6 +4449,12 @@ static PyNumberMethods dictviews_as_number = { (binaryfunc)dictviews_or, /*nb_or*/ }; +static PyMemberDef dictview_members[] = { + {"mapping", T_OBJECT, offsetof(_PyDictViewObject, dv_dict), + READONLY, "dictionary that this view refers to"}, + {NULL} /* Sentinel */ +}; + static PyObject* dictviews_isdisjoint(PyObject *self, PyObject *other) { @@ -4545,7 +4552,7 @@ PyTypeObject PyDictKeys_Type = { (getiterfunc)dictkeys_iter, /* tp_iter */ 0, /* tp_iternext */ dictkeys_methods, /* tp_methods */ - 0, + .tp_members = dictview_members, }; static PyObject * @@ -4651,7 +4658,7 @@ PyTypeObject PyDictItems_Type = { (getiterfunc)dictitems_iter, /* tp_iter */ 0, /* tp_iternext */ dictitems_methods, /* tp_methods */ - 0, + .tp_members = dictview_members, }; static PyObject * @@ -4732,7 +4739,7 @@ PyTypeObject PyDictValues_Type = { (getiterfunc)dictvalues_iter, /* tp_iter */ 0, /* tp_iternext */ dictvalues_methods, /* tp_methods */ - 0, + .tp_members = dictview_members, }; static PyObject * From 2af3c5d67db2e252bd96c854735496e80367ede3 Mon Sep 17 00:00:00 2001 From: sweeneyde Date: Sun, 7 Jun 2020 04:31:20 -0400 Subject: [PATCH 2/4] document dictview.mapping --- Doc/library/stdtypes.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 2082b849fd05b0..8f5ca98c67a51c 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -4622,6 +4622,11 @@ support membership tests: .. versionchanged:: 3.8 Dictionary views are now reversible. +.. describe:: dictview.mapping + + Return the dictionary from which the view was derived. If ``d`` is a + dictionary, then ``d is d.keys().mapping``, ``d is d.values().mapping``, + and ``d is d.items().mapping``. Keys views are set-like since their entries are unique and hashable. If all values are hashable, so that ``(key, value)`` pairs are unique and hashable, From cbc2de03d4a6fb332719cf3ebd9390b6dacf06fe Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 7 Jun 2020 08:58:45 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Core and Builtins/2020-06-07-08-58-42.bpo-40890.pXK3wX.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-06-07-08-58-42.bpo-40890.pXK3wX.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-06-07-08-58-42.bpo-40890.pXK3wX.rst b/Misc/NEWS.d/next/Core and Builtins/2020-06-07-08-58-42.bpo-40890.pXK3wX.rst new file mode 100644 index 00000000000000..f4a3ef085be6f4 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-06-07-08-58-42.bpo-40890.pXK3wX.rst @@ -0,0 +1 @@ +Each dictionary view now has a ``mapping`` attribute that references the original dictionary. Patch contributed by Dennis Sweeney. \ No newline at end of file From cec69cc58c38f4c957112480acdb40b138f929c3 Mon Sep 17 00:00:00 2001 From: Dennis Sweeney <36520290+sweeneyde@users.noreply.github.com> Date: Sun, 7 Jun 2020 05:28:48 -0400 Subject: [PATCH 4/4] Simplify wording of documentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Rémi Lapeyre --- Doc/library/stdtypes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 8f5ca98c67a51c..631178e2e8cef8 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -4624,7 +4624,7 @@ support membership tests: .. describe:: dictview.mapping - Return the dictionary from which the view was derived. If ``d`` is a + Return the dictionary the view refers to. If ``d`` is a dictionary, then ``d is d.keys().mapping``, ``d is d.values().mapping``, and ``d is d.items().mapping``.