summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZackery Spytz <zspytz@gmail.com>2020-05-07 23:25:50 -0600
committerGitHub <noreply@github.com>2020-05-07 22:25:50 -0700
commit02fa0ea9c1073e4476c9bde3d7112f5dd964aa57 (patch)
tree77db423fdfaede444989d4f36cd7978f5b31652f
parentdb9163ceef31ba00ccb23226917f9c8e9142a0b8 (diff)
downloadcpython-git-02fa0ea9c1073e4476c9bde3d7112f5dd964aa57.tar.gz
bpo-40273: Reversible mappingproxy (FH-19513)
-rw-r--r--Doc/library/types.rst6
-rw-r--r--Lib/test/test_types.py9
-rw-r--r--Misc/NEWS.d/next/Library/2020-04-14-09-54-35.bpo-40273.IN73Ks.rst1
-rw-r--r--Objects/descrobject.c9
4 files changed, 25 insertions, 0 deletions
diff --git a/Doc/library/types.rst b/Doc/library/types.rst
index 4cb91c1a90..1d081e2c54 100644
--- a/Doc/library/types.rst
+++ b/Doc/library/types.rst
@@ -329,6 +329,12 @@ Standard names are defined for the following types:
Return a new view of the underlying mapping's values.
+ .. describe:: reversed(proxy)
+
+ Return a reverse iterator over the keys of the underlying mapping.
+
+ .. versionadded:: 3.9
+
Additional Utility Classes and Functions
----------------------------------------
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index f42238762d..28ebfb6e60 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -627,6 +627,7 @@ class MappingProxyTests(unittest.TestCase):
'__iter__',
'__len__',
'__or__',
+ '__reversed__',
'__ror__',
'copy',
'get',
@@ -768,6 +769,14 @@ class MappingProxyTests(unittest.TestCase):
self.assertEqual(set(view.values()), set(values))
self.assertEqual(set(view.items()), set(items))
+ def test_reversed(self):
+ d = {'a': 1, 'b': 2, 'foo': 0, 'c': 3, 'd': 4}
+ mp = self.mappingproxy(d)
+ del d['foo']
+ r = reversed(mp)
+ self.assertEqual(list(r), list('dcba'))
+ self.assertRaises(StopIteration, next, r)
+
def test_copy(self):
original = {'key1': 27, 'key2': 51, 'key3': 93}
view = self.mappingproxy(original)
diff --git a/Misc/NEWS.d/next/Library/2020-04-14-09-54-35.bpo-40273.IN73Ks.rst b/Misc/NEWS.d/next/Library/2020-04-14-09-54-35.bpo-40273.IN73Ks.rst
new file mode 100644
index 0000000000..50f547f56c
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-04-14-09-54-35.bpo-40273.IN73Ks.rst
@@ -0,0 +1 @@
+:class:`types.MappingProxyType` is now reversible.
diff --git a/Objects/descrobject.c b/Objects/descrobject.c
index c9754a11b8..c29cf7a4c4 100644
--- a/Objects/descrobject.c
+++ b/Objects/descrobject.c
@@ -1118,6 +1118,13 @@ mappingproxy_copy(mappingproxyobject *pp, PyObject *Py_UNUSED(ignored))
return _PyObject_CallMethodIdNoArgs(pp->mapping, &PyId_copy);
}
+static PyObject *
+mappingproxy_reversed(mappingproxyobject *pp, PyObject *Py_UNUSED(ignored))
+{
+ _Py_IDENTIFIER(__reversed__);
+ return _PyObject_CallMethodIdNoArgs(pp->mapping, &PyId___reversed__);
+}
+
/* WARNING: mappingproxy methods must not give access
to the underlying mapping */
@@ -1135,6 +1142,8 @@ static PyMethodDef mappingproxy_methods[] = {
PyDoc_STR("D.copy() -> a shallow copy of D")},
{"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS,
PyDoc_STR("See PEP 585")},
+ {"__reversed__", (PyCFunction)mappingproxy_reversed, METH_NOARGS,
+ PyDoc_STR("D.__reversed__() -> reverse iterator")},
{0}
};