Skip to content

Commit e8dd14f

Browse files
committed
properly call __hash__ when setting an object as seed
1 parent ca9012b commit e8dd14f

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/random/PRandom.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
66
import com.oracle.graal.python.builtins.objects.type.PythonClass;
7+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
78

89
public class PRandom extends PythonBuiltinObject {
910
private static class PythonRandom extends Random {
@@ -22,10 +23,12 @@ public PRandom(PythonClass cls) {
2223
super(cls);
2324
}
2425

26+
@TruffleBoundary
2527
public void setSeed(long seed) {
2628
javaRandom.setSeed(seed);
2729
}
2830

31+
@TruffleBoundary
2932
public long getSeed() {
3033
return javaRandom.getSeed();
3134
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/random/RandomBuiltins.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@
99
import com.oracle.graal.python.builtins.objects.PNone;
1010
import com.oracle.graal.python.builtins.objects.ints.PInt;
1111
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
12+
import com.oracle.graal.python.nodes.PGuards;
13+
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
14+
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
1215
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
1316
import com.oracle.graal.python.runtime.exception.PythonErrorType;
1417
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
18+
import com.oracle.truffle.api.dsl.Cached;
1519
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
1620
import com.oracle.truffle.api.dsl.NodeFactory;
1721
import com.oracle.truffle.api.dsl.Specialization;
22+
import com.oracle.truffle.api.nodes.UnexpectedResultException;
1823

1924
@CoreFunctions(extendClasses = PRandom.class)
2025
public class RandomBuiltins extends PythonBuiltins {
@@ -53,10 +58,25 @@ public PNone seed(PRandom random, double inputSeed) {
5358
return PNone.NONE;
5459
}
5560

56-
@Specialization
57-
@TruffleBoundary
58-
public PNone seed(PRandom random, Object inputSeed) {
59-
random.setSeed(System.identityHashCode(inputSeed));
61+
@Specialization(rewriteOn = UnexpectedResultException.class)
62+
public PNone seedObject(PRandom random, Object inputSeed,
63+
@Cached("create(__HASH__)") LookupAndCallUnaryNode callHash) throws UnexpectedResultException {
64+
long hash = callHash.executeLong(inputSeed);
65+
random.setSeed(hash);
66+
return PNone.NONE;
67+
}
68+
69+
@Specialization(replaces = "seedObject")
70+
public PNone seedNonLong(PRandom random, Object inputSeed,
71+
@Cached("create(__HASH__)") LookupAndCallUnaryNode callHash) {
72+
Object object = callHash.executeObject(inputSeed);
73+
if (PGuards.isInteger(object)) {
74+
random.setSeed(((Number) object).intValue());
75+
} else if (PGuards.isPInt(object)) {
76+
random.setSeed(((PInt) object).intValue());
77+
} else {
78+
throw raise(PythonErrorType.TypeError, "__hash__ method should return an integer");
79+
}
6080
return PNone.NONE;
6181
}
6282
}

0 commit comments

Comments
 (0)