[pypy-svn] r78297 - pypy/branch/set-object-cleanup/pypy/objspace/std

arigo at codespeak.net arigo at codespeak.net
Tue Oct 26 14:57:44 CEST 2010


Author: arigo
Date: Tue Oct 26 14:57:42 2010
New Revision: 78297

Modified:
   pypy/branch/set-object-cleanup/pypy/objspace/std/dictmultiobject.py
   pypy/branch/set-object-cleanup/pypy/objspace/std/dicttype.py
Log:
Interp-level implementation of dict.popitem().


Modified: pypy/branch/set-object-cleanup/pypy/objspace/std/dictmultiobject.py
==============================================================================
--- pypy/branch/set-object-cleanup/pypy/objspace/std/dictmultiobject.py	(original)
+++ pypy/branch/set-object-cleanup/pypy/objspace/std/dictmultiobject.py	Tue Oct 26 14:57:42 2010
@@ -836,6 +836,16 @@
         w_dict.delitem(w_key)
         return w_item
 
+def dict_popitem__DictMulti(space, w_dict):
+    # XXX should somehow use the same trick as CPython: saving the index
+    # of the last popped item in the hash table, so that the next call to
+    # popitem() can be more efficient, instead of always starting from the
+    # beginning of the hash table.
+    iterator = w_dict.iter()
+    w_key, w_value = iterator.next()
+    w_dict.delitem(w_key)
+    return space.newtuple([w_key, w_value])
+
 app = gateway.applevel('''
     def dictrepr(currently_in_repr, d):
         # Now we only handle one implementation of dicts, this one.

Modified: pypy/branch/set-object-cleanup/pypy/objspace/std/dicttype.py
==============================================================================
--- pypy/branch/set-object-cleanup/pypy/objspace/std/dicttype.py	(original)
+++ pypy/branch/set-object-cleanup/pypy/objspace/std/dicttype.py	Tue Oct 26 14:57:42 2010
@@ -53,29 +53,6 @@
 # This can return when multimethods have been fixed
 #dict_str        = StdObjSpace.str
 
-# default application-level implementations for some operations
-# most of these (notably not popitem and update*) are overwritten
-# in dictmultiobject
-# gateway is imported in the stdtypedef module
-app = gateway.applevel('''
-
-    # in the following functions we use dict.__setitem__ instead of
-    # d[k]=...  because when a subclass of dict override __setitem__,
-    # CPython does not call it when doing builtin operations.  The
-    # same for other operations.
-
-    def popitem(d):
-        for k in dict.iterkeys(d):
-            break
-        else:
-            raise KeyError("popitem(): dictionary is empty")
-        v = dict.__getitem__(d, k)
-        dict.__delitem__(d, k)
-        return k, v
-''', filename=__file__)
-
-dict_popitem__ANY            = app.interphook("popitem")
-
 register_all(vars(), globals())
 
 @gateway.unwrap_spec(ObjSpace, W_Root, W_Root, W_Root)



More information about the Pypy-commit mailing list