Author: pizi
Branch: ep2016sprint
Changeset: r85825:1e1b649f6407
Date: 2016-07-23 15:06 +0200
http://bitbucket.org/pypy/pypy/changeset/1e1b649f6407/
Log: 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,16 +425,31 @@
"'%T' objects are unhashable", w_obj)
w_result = space.get_and_call_function(w_hash, w_obj)
w_resulttype = space.type(w_result)
+
if space.is_w(w_resulttype, space.w_int):
+ # bug 2346 (return -2 for a hashvalue of -1)
+ if space.int_w(w_result) == -1:
+ return space.wrap(-2)
return w_result
elif space.is_w(w_resulttype, space.w_long):
+ # bug 2346 (return -2 for a hashvalue of -1)
+ if space.bigint_w(w_result) == -1:
+ return space.hash(space.wrap(-2))
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))
+ # return space.wrap(space.int_w(w_result))
+ int_result = space.int_w(w_result)
+ # bug 2346 (return -2 for a hashvalue of -1)
+ 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)
+ # bug 2346 (return -2 for a hashvalue of -1)
+ if bigint == -1:
+ bigint = -2
return space.wrap(bigint.hash())
else:
raise oefmt(space.w_TypeError,
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,15 @@
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
+
+ assert hash(-1) == -2
+ assert hash(-1L) == -2
+ assert hash(myint(-1)) == -2
+ assert hash(mylong(-1)) == -2
+
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit