Author: Armin Rigo <ar...@tunes.org>
Branch: py3k
Changeset: r85900:2193158bc483
Date: 2016-07-29 00:07 +0200
http://bitbucket.org/pypy/pypy/changeset/2193158bc483/

Log:    More tests and more tweaks about issue #2346 (all previously
        passing, but it feels strange to have _hash_int() be allowed to
        return -1 and _hash_long() not)

diff --git a/pypy/objspace/descroperation.py b/pypy/objspace/descroperation.py
--- a/pypy/objspace/descroperation.py
+++ b/pypy/objspace/descroperation.py
@@ -450,14 +450,14 @@
             if space.int_w(w_result) == -1:
                 return space.wrap(-2)
             return w_result
-        elif isinstance(w_result, W_IntObject):
-            result = space.int_w(w_result)
-            if result == -1:
-                result = -2
-            return space.newint(result)
-        # a non W_IntObject int, assume long-like
-        assert isinstance(w_result, W_AbstractIntObject)
-        return w_result.descr_hash(space)
+        if not isinstance(w_result, W_IntObject):
+            # a non W_IntObject int, assume long-like
+            assert isinstance(w_result, W_AbstractIntObject)
+            w_result = w_result.descr_hash(space)
+        result = space.int_w(w_result)
+        if result == -1:
+            result = -2
+        return space.newint(result)
 
     def issubtype_w(space, w_sub, w_type):
         return space._type_issubtype(w_sub, w_type)
diff --git a/pypy/objspace/std/longobject.py b/pypy/objspace/std/longobject.py
--- a/pypy/objspace/std/longobject.py
+++ b/pypy/objspace/std/longobject.py
@@ -404,8 +404,7 @@
         while x >= HASH_MODULUS:
             x -= HASH_MODULUS
         i -= 1
-    x = intmask(intmask(x) * v.sign)
-    return -2 if x == -1 else x
+    return intmask(intmask(x) * v.sign)
 
 
 def newlong(space, bigint):
diff --git a/pypy/objspace/std/test/test_longobject.py 
b/pypy/objspace/std/test/test_longobject.py
--- a/pypy/objspace/std/test/test_longobject.py
+++ b/pypy/objspace/std/test/test_longobject.py
@@ -44,10 +44,10 @@
 class AppTestLong:
 
     def w__long(self, obj):
-        import sys
         # XXX: currently returns a W_LongObject but might return
         # W_IntObject in the future
-        return obj + sys.maxsize - sys.maxsize
+        huge = 1 << 65
+        return obj + huge - huge
 
     def test_trunc(self):
         import math
@@ -237,23 +237,30 @@
     def test_hash(self):
         import sys
         modulus = sys.hash_info.modulus
-        for x in ([self._long(i) for i in range(200)] +
-                  [self._long(1234567890123456789),
-                   1234567890123456789, 18446743523953737727,
+        def longhash(x):
+            return hash(self._long(x))
+        for x in (list(range(200)) +
+                  [1234567890123456789, 18446743523953737727,
                    987685321987685321987685321987685321987685321,
                    10**50]):
             y = x % modulus
-            assert hash(x) == hash(y)
-            assert hash(-x) == hash(-y)
-        assert hash(modulus - 1) == modulus - 1
-        assert hash(modulus) == 0
-        assert hash(modulus + 1) == 1
+            assert longhash(x) == longhash(y)
+            assert longhash(-x) == longhash(-y)
+        assert longhash(modulus - 1) == modulus - 1
+        assert longhash(modulus) == 0
+        assert longhash(modulus + 1) == 1
 
-        assert hash(-1) == -2
+        assert longhash(-1) == -2
         value = -(modulus + 1)
-        assert hash(value) == -2
-        assert hash(value * 2 + 1) == -2
-        assert hash(value * 4 + 3) == -2
+        assert longhash(value) == -2
+        assert longhash(value * 2 + 1) == -2
+        assert longhash(value * 4 + 3) == -2
+
+    def test_hash_2(self):
+        class AAA:
+            def __hash__(a):
+                return self._long(-1)
+        assert hash(AAA()) == -2
 
     def test_math_log(self):
         import math
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to