diff options
author | Dennis Sweeney <36520290+sweeneyde@users.noreply.github.com> | 2020-06-12 17:19:25 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-12 17:19:25 (GMT) |
commit | 3ee0e48b0376a710c08eec6f30e4181563b192a2 (patch) | |
tree | 8a0ea5514e2b1bfb3617f434271c85465d805463 /Objects | |
parent | 0d3350daa8123a3e16d4a534b6e873eb12c10d7c (diff) | |
download | cpython-3ee0e48b0376a710c08eec6f30e4181563b192a2.zip cpython-3ee0e48b0376a710c08eec6f30e4181563b192a2.tar.gz cpython-3ee0e48b0376a710c08eec6f30e4181563b192a2.tar.bz2 |
bpo-40890: Add `mapping` property to dict views (GH-20749)
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/dictobject.c | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 1bb8cfd..48e96a0 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -4122,6 +4122,23 @@ _PyDictView_New(PyObject *dict, PyTypeObject *type) return (PyObject *)dv; } +static PyObject * +dictview_mapping(PyObject *view) +{ + assert(view != NULL); + assert(PyDictKeys_Check(view) + || PyDictValues_Check(view) + || PyDictItems_Check(view)); + PyObject *mapping = (PyObject *)((_PyDictViewObject *)view)->dv_dict; + return PyDictProxy_New(mapping); +} + +static PyGetSetDef dictview_getset[] = { + {"mapping", (getter)dictview_mapping, (setter)NULL, + "dictionary that this view refers to", NULL}, + {0} +}; + /* TODO(guido): The views objects are not complete: * support more set operations @@ -4635,7 +4652,7 @@ PyTypeObject PyDictKeys_Type = { (getiterfunc)dictkeys_iter, /* tp_iter */ 0, /* tp_iternext */ dictkeys_methods, /* tp_methods */ - 0, + .tp_getset = dictview_getset, }; static PyObject * @@ -4741,7 +4758,7 @@ PyTypeObject PyDictItems_Type = { (getiterfunc)dictitems_iter, /* tp_iter */ 0, /* tp_iternext */ dictitems_methods, /* tp_methods */ - 0, + .tp_getset = dictview_getset, }; static PyObject * @@ -4822,7 +4839,7 @@ PyTypeObject PyDictValues_Type = { (getiterfunc)dictvalues_iter, /* tp_iter */ 0, /* tp_iternext */ dictvalues_methods, /* tp_methods */ - 0, + .tp_getset = dictview_getset, }; static PyObject * |