[pypy-svn] r72347 - in pypy/trunk/pypy/lib: . test2

xoraxax at codespeak.net xoraxax at codespeak.net
Thu Mar 18 05:05:13 CET 2010


Author: xoraxax
Date: Thu Mar 18 05:05:11 2010
New Revision: 72347

Added:
   pypy/trunk/pypy/lib/identity_dict.py
   pypy/trunk/pypy/lib/test2/test_identitydict.py
      - copied, changed from r72343, pypy/trunk/pypy/module/__pypy__/test/test_identitydict.py
Log:
Add identity_dict to pypy/lib.

Added: pypy/trunk/pypy/lib/identity_dict.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/lib/identity_dict.py	Thu Mar 18 05:05:11 2010
@@ -0,0 +1,56 @@
+try:
+    from __pypy__ import identity_dict as idict
+except ImportError:
+    idict = None
+
+from UserDict import DictMixin
+
+
+class IdentityDictPurePython(object, DictMixin):
+    def __init__(self):
+        self._dict = {}
+        self._keys = {} # id(obj) -> obj
+
+    def __getitem__(self, arg):
+        return self._dict[id(arg)]
+
+    def __setitem__(self, arg, val):
+        self._keys[id(arg)] = arg
+        self._dict[id(arg)] = val
+
+    def __delitem__(self, arg):
+        del self._keys[id(arg)]
+        del self._dict[id(arg)]
+
+    def keys(self):
+        return self._keys.values()
+
+    def __contains__(self, arg):
+        return id(arg) in self._dict
+
+
+class IdentityDictPyPy(object, DictMixin):
+    def __init__(self):
+        self._dict = idict()
+
+    def __getitem__(self, arg):
+        return self._dict[arg]
+
+    def __setitem__(self, arg, val):
+        self._dict[arg] = val
+
+    def __delitem__(self, arg):
+        del self._dict[arg]
+
+    def keys(self):
+        return self._dict.keys()
+
+    def __contains__(self, arg):
+        return arg in self._dict
+
+
+if idict is None:
+    identity_dict = IdentityDictPurePython
+else:
+    identity_dict = IdentityDictPyPy
+

Copied: pypy/trunk/pypy/lib/test2/test_identitydict.py (from r72343, pypy/trunk/pypy/module/__pypy__/test/test_identitydict.py)
==============================================================================
--- pypy/trunk/pypy/module/__pypy__/test/test_identitydict.py	(original)
+++ pypy/trunk/pypy/lib/test2/test_identitydict.py	Thu Mar 18 05:05:11 2010
@@ -1,13 +1,11 @@
 import py
-from pypy.conftest import gettestobjspace
+from pypy.lib.identity_dict import identity_dict, IdentityDictPurePython
 
-class AppTestIdentityDict:
-    def setup_class(cls):
-        cls.space = gettestobjspace(usemodules=['__pypy__'])
+class TestIdentityDictNative:
+    identity_dict = identity_dict
 
     def test_numbers(self):
-        from __pypy__ import identity_dict
-        d = identity_dict()
+        d = self.identity_dict()
         d[0] = 1
         d[0.0] = 2
         d[long(0)] = 3
@@ -18,8 +16,7 @@
         assert not d
 
     def test_get(self):
-        from __pypy__ import identity_dict
-        d = identity_dict()
+        d = self.identity_dict()
         d[None] = 1
 
         assert d.get(None, 42) == 1
@@ -28,9 +25,7 @@
         assert d.get(1, 42) == 42
 
     def test_unhashable(self):
-        from __pypy__ import identity_dict
-
-        d = identity_dict()
+        d = self.identity_dict()
         d[[]] = 1
         d[[]] = 2
         a = []
@@ -43,8 +38,7 @@
         raises(KeyError, d.__getitem__, [])
 
     def test_keys(self):
-        from __pypy__ import identity_dict
-        d = identity_dict()
+        d = self.identity_dict()
         d[[]] = 1
         d[[]] = 2
         d[[]] = 3
@@ -53,9 +47,12 @@
         assert sorted(d.values()) == [1, 2, 3]
 
     def test_in(self):
-        from __pypy__ import identity_dict
-        d = identity_dict()
+        d = self.identity_dict()
         d[None] = 1
 
         assert None in d
         assert [] not in d
+
+
+class TestIdentityDictPurePython(TestIdentityDictNative):
+    identity_dict = IdentityDictPurePython



More information about the Pypy-commit mailing list