[pypy-commit] pypy default: Merged in mad-marty/pypy/ep2016sprint (pull request #462)
rlamy
pypy.commits at gmail.com
Sat Jul 23 12:05:24 EDT 2016
Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch:
Changeset: r85832:8fd42a7c348e
Date: 2016-07-23 18:04 +0200
http://bitbucket.org/pypy/pypy/changeset/8fd42a7c348e/
Log: Merged in mad-marty/pypy/ep2016sprint (pull request #462)
Issue #2346 : Hashing of -1 did not return -2 as cpython
diff --git a/pypy/objspace/descroperation.py b/pypy/objspace/descroperation.py
--- a/pypy/objspace/descroperation.py
+++ b/pypy/objspace/descroperation.py
@@ -425,17 +425,25 @@
"'%T' objects are unhashable", w_obj)
w_result = space.get_and_call_function(w_hash, w_obj)
w_resulttype = space.type(w_result)
+
+ # issue 2346 : returns now -2 for hashing -1 like cpython
if space.is_w(w_resulttype, space.w_int):
+ if space.int_w(w_result) == -1:
+ return space.wrap(-2)
return w_result
- elif space.is_w(w_resulttype, space.w_long):
- return space.hash(w_result)
elif space.isinstance_w(w_result, space.w_int):
# be careful about subclasses of 'int'...
- return space.wrap(space.int_w(w_result))
+ int_result = space.int_w(w_result)
+ if int_result == -1:
+ int_result == -2
+ return space.wrap(int_result)
elif space.isinstance_w(w_result, space.w_long):
# be careful about subclasses of 'long'...
bigint = space.bigint_w(w_result)
- return space.wrap(bigint.hash())
+ h = bigint.hash()
+ if h == -1:
+ h = -2
+ return space.wrap(h)
else:
raise oefmt(space.w_TypeError,
"__hash__() should return an int or long")
diff --git a/pypy/objspace/std/specialisedtupleobject.py b/pypy/objspace/std/specialisedtupleobject.py
--- a/pypy/objspace/std/specialisedtupleobject.py
+++ b/pypy/objspace/std/specialisedtupleobject.py
@@ -62,6 +62,11 @@
value = getattr(self, 'value%s' % i)
if typetuple[i] == object:
y = space.int_w(space.hash(value))
+ elif typetuple[i] == int:
+ # mimic cpythons behavior of a hash value of -2 for -1
+ y = value
+ if y == -1:
+ y = -2
elif typetuple[i] == float:
# get the correct hash for float which is an
# integer & other less frequent cases
diff --git a/pypy/objspace/std/test/test_specialisedtupleobject.py b/pypy/objspace/std/test/test_specialisedtupleobject.py
--- a/pypy/objspace/std/test/test_specialisedtupleobject.py
+++ b/pypy/objspace/std/test/test_specialisedtupleobject.py
@@ -177,6 +177,10 @@
assert hash(a) == hash((1L, 2L)) == hash((1.0, 2.0)) == hash((1.0, 2L))
+ x = (-1, -1)
+ y = tuple([-1, -1])
+ assert hash(x) == hash(y)
+
def test_getitem(self):
t = (5, 3)
assert (t)[0] == 5
diff --git a/pypy/objspace/test/test_descriptor.py b/pypy/objspace/test/test_descriptor.py
--- a/pypy/objspace/test/test_descriptor.py
+++ b/pypy/objspace/test/test_descriptor.py
@@ -141,3 +141,31 @@
return myint(15)
assert hash(I()) == 15
assert type(hash(I())) is int
+
+ # check hashing of -1 to -2
+ class myint(int):
+ pass
+ class mylong(long):
+ pass
+ class myfloat(float):
+ pass
+ class myHashClass(object):
+ def __hash__(self):
+ return -1
+ class myHashClass2(object):
+ def __hash__(self):
+ return -1L
+ class myHashClass3(object):
+ def __hash__(self):
+ return -10**100
+
+ assert hash(-1) == -2
+ assert hash(-1L) == -2
+ assert hash(-1.0) == -2
+ assert hash(-1 + 0j) == -2
+ assert hash(myint(-1)) == -2
+ assert hash(mylong(-1)) == -2
+ assert hash(myfloat(-1.0)) == -2
+ assert hash(myHashClass()) == -2
+ assert hash(myHashClass2()) == -2
+ assert hash(myHashClass3()) == hash(-10**100)
More information about the pypy-commit
mailing list