[pypy-commit] pypy jvm-improvements: Fix compute_unique_id to support built-ins in ootype.

benol noreply at buildbot.pypy.org
Tue Jan 17 10:10:22 CET 2012


Author: Michal Bendowski <michal at bendowski.pl>
Branch: jvm-improvements
Changeset: r51376:143a2edf9601
Date: 2012-01-11 22:29 +0100
http://bitbucket.org/pypy/pypy/changeset/143a2edf9601/

Log:	Fix compute_unique_id to support built-ins in ootype.

	Otherwise the translation fails because it doesn't know how to apply
	compute_unique_id to a String. In the jvm backend this is
	implemented by System.identityHashCode() which can be applied to our
	representations of built-ins equally well as for instances.

diff --git a/pypy/rlib/objectmodel.py b/pypy/rlib/objectmodel.py
--- a/pypy/rlib/objectmodel.py
+++ b/pypy/rlib/objectmodel.py
@@ -420,7 +420,7 @@
                   vobj.concretetype.TO._gckind == 'gc')
         else:
             from pypy.rpython.ootypesystem import ootype
-            ok = isinstance(vobj.concretetype, ootype.Instance)
+            ok = isinstance(vobj.concretetype, (ootype.Instance, ootype.BuiltinType))
         if not ok:
             from pypy.rpython.error import TyperError
             raise TyperError("compute_unique_id() cannot be applied to"
diff --git a/pypy/rpython/ootypesystem/ootype.py b/pypy/rpython/ootypesystem/ootype.py
--- a/pypy/rpython/ootypesystem/ootype.py
+++ b/pypy/rpython/ootypesystem/ootype.py
@@ -1377,6 +1377,9 @@
     def _cast_to_object(self):
         return make_object(self)
 
+    def _identityhash(self):
+        return hash(self)
+
 class _string(_builtin_type):
 
     def __init__(self, STRING, value = ''):
diff --git a/pypy/rpython/test/test_rbuiltin.py b/pypy/rpython/test/test_rbuiltin.py
--- a/pypy/rpython/test/test_rbuiltin.py
+++ b/pypy/rpython/test/test_rbuiltin.py
@@ -463,6 +463,20 @@
             assert x1 == intmask(x0)
             assert x3 == intmask(x2)
 
+    def test_id_on_builtins(self):
+        from pypy.rlib.objectmodel import compute_unique_id
+        from pypy.rlib.rstring import StringBuilder, UnicodeBuilder
+        def fn():
+            return (compute_unique_id("foo"),
+                    compute_unique_id(u"bar"),
+                    compute_unique_id([1]),
+                    compute_unique_id({"foo": 3}),
+                    compute_unique_id(StringBuilder()),
+                    compute_unique_id(UnicodeBuilder()))
+        res = self.interpret(fn, [])
+        for id in self.ll_unpack_tuple(res, 6):
+            assert isinstance(id, (int, r_longlong))
+
     def test_cast_primitive(self):
         from pypy.rpython.annlowlevel import LowLevelAnnotatorPolicy
         def llf(u):


More information about the pypy-commit mailing list