[pypy-commit] pypy default: Support (without crashing obscurely) dictionaries with keys that

arigo noreply at buildbot.pypy.org
Sun Oct 13 07:21:26 CEST 2013


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r67340:23d01e38f49e
Date: 2013-10-13 07:20 +0200
http://bitbucket.org/pypy/pypy/changeset/23d01e38f49e/

Log:	Support (without crashing obscurely) dictionaries with keys that are
	shorter integer types from rffi.

diff --git a/rpython/rtyper/lltypesystem/rdict.py b/rpython/rtyper/lltypesystem/rdict.py
--- a/rpython/rtyper/lltypesystem/rdict.py
+++ b/rpython/rtyper/lltypesystem/rdict.py
@@ -162,6 +162,9 @@
                 fasthashfn = None
             else:
                 fasthashfn = self.key_repr.get_ll_fasthash_function()
+                if getattr(self.key_repr.get_ll_eq_function(),
+                           'no_direct_compare', False):
+                    entrymeths['no_direct_compare'] = True
             if fasthashfn is None:
                 entryfields.append(("f_hash", lltype.Signed))
                 entrymeths['hash'] = ll_hash_from_cache
diff --git a/rpython/rtyper/rint.py b/rpython/rtyper/rint.py
--- a/rpython/rtyper/rint.py
+++ b/rpython/rtyper/rint.py
@@ -251,14 +251,15 @@
         raise TyperError("not an integer: %r" % (value,))
 
     def get_ll_eq_function(self):
+        if self._opprefix is None:
+            return ll_eq_shortint
         return None
-    get_ll_gt_function = get_ll_eq_function
-    get_ll_lt_function = get_ll_eq_function
-    get_ll_ge_function = get_ll_eq_function
-    get_ll_le_function = get_ll_eq_function
 
     def get_ll_ge_function(self):
         return None
+    get_ll_gt_function = get_ll_ge_function
+    get_ll_lt_function = get_ll_ge_function
+    get_ll_le_function = get_ll_ge_function
 
     def get_ll_hash_function(self):
         if (sys.maxint == 2147483647 and
@@ -390,6 +391,10 @@
 def ll_hash_long_long(n):
     return intmask(intmask(n) + 9 * intmask(n >> 32))
 
+def ll_eq_shortint(n, m):
+    return intmask(n) == intmask(m)
+ll_eq_shortint.no_direct_compare = True
+
 def ll_check_chr(n):
     if 0 <= n <= 255:
         return
diff --git a/rpython/rtyper/test/test_rdict.py b/rpython/rtyper/test/test_rdict.py
--- a/rpython/rtyper/test/test_rdict.py
+++ b/rpython/rtyper/test/test_rdict.py
@@ -999,6 +999,16 @@
         res = f()
         assert res == 1
 
+    def test_dict_with_SHORT_keys(self):
+        def func(x):
+            d = {}
+            d[rffi.cast(rffi.SHORT, 42)] = 123
+            d[rffi.cast(rffi.SHORT, -43)] = 321
+            return d[rffi.cast(rffi.SHORT, x)]
+
+        assert self.interpret(func, [42]) == 123
+        assert self.interpret(func, [2**16 - 43]) == 321
+
     def test_nonnull_hint(self):
         def eq(a, b):
             return a == b


More information about the pypy-commit mailing list