[pypy-commit] pypy list-strategies: implement id to match the new is behaviour.

cfbolz noreply at buildbot.pypy.org
Thu Sep 29 14:36:22 CEST 2011


Author: Carl Friedrich Bolz <cfbolz at gmx.de>
Branch: list-strategies
Changeset: r47677:cb61b577bb9f
Date: 2011-09-29 11:30 +0200
http://bitbucket.org/pypy/pypy/changeset/cb61b577bb9f/

Log:	implement id to match the new is behaviour.

diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py
--- a/pypy/objspace/std/objspace.py
+++ b/pypy/objspace/std/objspace.py
@@ -472,6 +472,30 @@
                     self.unicode_w(w_one) is self.unicode_w(w_two))
         return w_one is w_two
 
+    def id(self, w_obj):
+        from pypy.rlib import objectmodel, rbigint
+        from pypy.rlib.rstruct import ieee
+        w_type = self.type(w_obj)
+        if w_type is self.w_int:
+            tag = 1
+            return self.or_(self.lshift(w_obj, self.wrap(3)), self.wrap(tag))
+        elif w_type is self.w_long:
+            tag = 3
+            return self.or_(self.lshift(w_obj, self.wrap(3)), self.wrap(tag))
+        elif w_type is self.w_float:
+            tag = 5
+            val = ieee.float_pack(self.float_w(w_obj), 8)
+            w_obj = self.newlong_from_rbigint(rbigint.rbigint.fromrarith_int(val))
+            return self.or_(self.lshift(w_obj, self.wrap(3)), self.wrap(tag))
+        # XXX complex?
+        elif w_type is self.w_str:
+            res = objectmodel.compute_unique_id(self.str_w(w_obj))
+        elif w_type is self.w_unicode:
+            res = objectmodel.compute_unique_id(self.unicode_w(w_obj))
+        else:
+            res = objectmodel.compute_unique_id(w_obj)
+        return self.wrap(res)
+
     def is_true(self, w_obj):
         # a shortcut for performance
         # NOTE! this method is typically overridden by builtinshortcut.py.
diff --git a/pypy/objspace/std/test/test_obj.py b/pypy/objspace/std/test/test_obj.py
--- a/pypy/objspace/std/test/test_obj.py
+++ b/pypy/objspace/std/test/test_obj.py
@@ -16,6 +16,9 @@
         def w_unwrap_wrap_unicode(space, w_obj):
             return space.wrap(space.unicode_w(w_obj))
         cls.w_unwrap_wrap_unicode = space.wrap(gateway.interp2app(w_unwrap_wrap_unicode))
+        def w_unwrap_wrap_str(space, w_obj):
+            return space.wrap(space.str_w(w_obj))
+        cls.w_unwrap_wrap_str = space.wrap(gateway.interp2app(w_unwrap_wrap_str))
 
     def test_hash_builtin(self):
         if not self.cpython_behavior:
@@ -130,3 +133,19 @@
         u = u"a"
         assert self.unwrap_wrap_unicode(u) is u
 
+    def test_id(self):
+        assert id(1) == (1 << 3) + 1
+        assert id(1l) == (1 << 3) + 3
+        class myint(int):
+            pass
+        assert id(myint(1)) != id(1)
+
+        assert id(1.0) & 7 == 5
+        assert id(-0.0) != id(0.0)
+        assert hex(id(2.0)) == '0x20000000000000005L'
+        assert id(0.0) == 5
+
+        u = u"a"
+        assert id(self.unwrap_wrap_unicode(u)) == id(u)
+        s = "a"
+        assert id(self.unwrap_wrap_str(s)) == id(s)


More information about the pypy-commit mailing list