Skip to content

Commit b53ae9f

Browse files
committed
Fix int.__hash__()
1 parent 620d042 commit b53ae9f

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5858
import com.oracle.graal.python.builtins.PythonBuiltins;
5959
import com.oracle.graal.python.builtins.modules.MathGuards;
60+
import com.oracle.graal.python.builtins.modules.SysModuleBuiltins;
6061
import com.oracle.graal.python.builtins.objects.PNone;
6162
import com.oracle.graal.python.builtins.objects.PNotImplemented;
6263
import com.oracle.graal.python.builtins.objects.array.PArray;
@@ -2646,25 +2647,40 @@ private static void validateIntegerSpec(PythonCore core, Spec spec) {
26462647
abstract static class HashNode extends PythonUnaryBuiltinNode {
26472648

26482649
@Specialization
2649-
int hash(int self) {
2650-
return self;
2650+
long hash(int self) {
2651+
return PythonObjectLibrary.hash(self);
26512652
}
26522653

26532654
@Specialization
26542655
long hash(long self) {
2655-
return self;
2656+
return PythonObjectLibrary.hash(self);
26562657
}
26572658

26582659
@Specialization
2660+
@TruffleBoundary
26592661
long hash(PInt self) {
2660-
return self.longValue();
2662+
return self.getValue().remainder(BigInteger.valueOf(SysModuleBuiltins.HASH_MODULUS)).longValue();
26612663
}
26622664

26632665
@Specialization
2666+
long hash(PythonNativeVoidPtr self,
2667+
@CachedLibrary(limit = "1") InteropLibrary lib) {
2668+
if (lib.isPointer(self.object)) {
2669+
try {
2670+
long ptrVal = lib.asPointer(self.object);
2671+
return PythonObjectLibrary.hash(ptrVal);
2672+
} catch (UnsupportedMessageException e) {
2673+
// fall through
2674+
}
2675+
}
2676+
return doHashCode(self.object);
2677+
}
2678+
26642679
@TruffleBoundary
2665-
long hash(PythonNativeVoidPtr self) {
2666-
return self.object.hashCode();
2680+
long doHashCode(Object o) {
2681+
return o.hashCode();
26672682
}
2683+
26682684
}
26692685

26702686
@Builtin(name = "bit_length", minNumOfPositionalArgs = 1)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/DefaultPythonIntegerExports.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ static Object getLazyPythonClass(@SuppressWarnings("unused") Integer value) {
9191

9292
@ExportMessage
9393
static long hash(Integer value) {
94-
return value;
94+
return hash(value.intValue());
9595
}
9696

9797
@Ignore
9898
static long hash(int value) {
99-
return value;
99+
return value == -1 ? -2 : value;
100100
}
101101

102102
@ExportMessage

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/DefaultPythonLongExports.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
package com.oracle.graal.python.builtins.objects.object;
4242

4343
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
44+
import com.oracle.graal.python.builtins.modules.SysModuleBuiltins;
4445
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
4546
import com.oracle.graal.python.builtins.objects.floats.PFloat;
4647
import com.oracle.graal.python.builtins.objects.function.PArguments;
@@ -113,12 +114,13 @@ static Object getLazyPythonClass(@SuppressWarnings("unused") Long value) {
113114

114115
@ExportMessage
115116
static long hash(Long value) {
116-
return value;
117+
return hash(value.longValue());
117118
}
118119

119120
@Ignore
120121
static long hash(long value) {
121-
return value;
122+
long h = value % SysModuleBuiltins.HASH_MODULUS;
123+
return h == -1 ? -2 : h;
122124
}
123125

124126
@ExportMessage

0 commit comments

Comments
 (0)