diff options
-rw-r--r-- | Doc/library/types.rst | 6 | ||||
-rw-r--r-- | Lib/test/test_types.py | 9 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2020-04-14-09-54-35.bpo-40273.IN73Ks.rst | 1 | ||||
-rw-r--r-- | Objects/descrobject.c | 9 |
4 files changed, 25 insertions, 0 deletions
diff --git a/Doc/library/types.rst b/Doc/library/types.rst index 4cb91c1..1d081e2 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 f422387..28ebfb6 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 0000000..50f547f --- /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 c9754a1..c29cf7a 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} }; |