[pypy-commit] pypy all_ordered_dicts: Write docstring for __pypy__.reversed_dict().

arigo noreply at buildbot.pypy.org
Tue Jan 13 19:35:28 CET 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: all_ordered_dicts
Changeset: r75322:0f19f34109da
Date: 2015-01-13 19:34 +0100
http://bitbucket.org/pypy/pypy/changeset/0f19f34109da/

Log:	Write docstring for __pypy__.reversed_dict().

diff --git a/pypy/module/__pypy__/interp_dict.py b/pypy/module/__pypy__/interp_dict.py
--- a/pypy/module/__pypy__/interp_dict.py
+++ b/pypy/module/__pypy__/interp_dict.py
@@ -32,6 +32,14 @@
         raise oefmt(space.w_TypeError, "unknown type of dict %s", type)
 
 def reversed_dict(space, w_obj):
+    """Enumerate the keys in a dictionary object in reversed order.
+
+    This is a __pypy__ function instead of being simply done by calling
+    reversed(), for CPython compatibility: dictionaries are only ordered
+    on PyPy.  You should use the collections.OrderedDict class for cases
+    where ordering is important.  That class implements __reversed__ by
+    calling __pypy__.reversed_dict().
+    """
     from pypy.objspace.std.dictmultiobject import W_DictMultiObject
     if not isinstance(w_obj, W_DictMultiObject):
         raise OperationError(space.w_TypeError, space.w_None)
diff --git a/rpython/rlib/objectmodel.py b/rpython/rlib/objectmodel.py
--- a/rpython/rlib/objectmodel.py
+++ b/rpython/rlib/objectmodel.py
@@ -757,8 +757,11 @@
 def reversed_dict(d):
     """Equivalent to reversed(ordered_dict), but works also for
     regular dicts."""
-    if not we_are_translated() and type(d) is dict:
-        d = list(d)
+    # note that there is also __pypy__.reversed_dict(), which we could
+    # try to use here if we're not translated and running on top of pypy,
+    # but that seems a bit pointless
+    if not we_are_translated():
+        d = d.keys()
     return reversed(d)
 
 


More information about the pypy-commit mailing list