[pypy-commit] pypy identity-dict-strategy: IdentityDictStrategy cannot inherit from ObjectDictStrategy, else translation fails

antocuni noreply at buildbot.pypy.org
Wed Jul 20 15:00:01 CEST 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: identity-dict-strategy
Changeset: r45773:a37a1c0b4a30
Date: 2011-07-20 12:26 +0200
http://bitbucket.org/pypy/pypy/changeset/a37a1c0b4a30/

Log:	IdentityDictStrategy cannot inherit from ObjectDictStrategy, else
	translation fails

diff --git a/pypy/objspace/std/dictmultiobject.py b/pypy/objspace/std/dictmultiobject.py
--- a/pypy/objspace/std/dictmultiobject.py
+++ b/pypy/objspace/std/dictmultiobject.py
@@ -1,4 +1,5 @@
 import py, sys
+from pypy.tool.sourcetools import func_with_new_name
 from pypy.objspace.std.model import registerimplementation, W_Object
 from pypy.objspace.std.register_all import register_all
 from pypy.objspace.std.settype import set_typedef as settypedef
@@ -414,7 +415,7 @@
         return self.unerase(w_dict.dstorage).keys()
 
 
-class IdentityDictStrategy(ObjectDictStrategy):
+class IdentityDictStrategy(AbstractTypedStrategy, DictStrategy):
     """
     Strategy for custom instances which compares by identity (i.e., the
     default unless you override __hash__, __eq__ or __cmp__).  The storage is
@@ -422,6 +423,16 @@
     semantics.
     """
 
+    erase, unerase = rerased.new_erasing_pair("identitydict")
+    erase = staticmethod(erase)
+    unerase = staticmethod(unerase)
+
+    def wrap(self, unwrapped):
+        return unwrapped
+
+    def unwrap(self, wrapped):
+        return wrapped
+
     def is_correct_type(self, w_obj):
         w_type = self.space.type(w_obj)
         return w_type.compares_by_identity()
@@ -429,6 +440,15 @@
     def get_empty_storage(self):
         return self.erase({})
 
+    def _never_equal_to(self, w_lookup_type):
+        return False
+
+    def iter(self, w_dict):
+        return IdentityDictIteratorImplementation(self.space, self, w_dict)
+
+    def keys(self, w_dict):
+        return self.unerase(w_dict.dstorage).keys()
+
 
 class StringDictStrategy(AbstractTypedStrategy, DictStrategy):
 
@@ -542,6 +562,13 @@
             return None, None
 
 
+class IdentityDictIteratorImplementation(IteratorImplementation):
+    __init__ = func_with_new_name(
+        ObjectIteratorImplementation.__init__.im_func, '__init__')
+
+    next_entry = func_with_new_name(
+        ObjectIteratorImplementation.next_entry.im_func, 'next_entry')
+
 init_signature = Signature(['seq_or_map'], None, 'kwargs')
 init_defaults = [None]
 
diff --git a/pypy/objspace/std/test/test_dictmultiobject.py b/pypy/objspace/std/test/test_dictmultiobject.py
--- a/pypy/objspace/std/test/test_dictmultiobject.py
+++ b/pypy/objspace/std/test/test_dictmultiobject.py
@@ -1162,3 +1162,10 @@
         assert d[y] == 1
         assert not self.uses_identity_strategy(d)
 
+    def test_iter(self):
+        class X(object):
+            pass
+        x = X()
+        d = {x: 1}
+        assert self.uses_identity_strategy(d)
+        assert list(iter(d)) == [x]


More information about the pypy-commit mailing list