|
9 | 9 | import com.oracle.graal.python.builtins.objects.PNone;
|
10 | 10 | import com.oracle.graal.python.builtins.objects.ints.PInt;
|
11 | 11 | 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; |
12 | 15 | import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
|
13 | 16 | import com.oracle.graal.python.runtime.exception.PythonErrorType;
|
14 | 17 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
|
| 18 | +import com.oracle.truffle.api.dsl.Cached; |
15 | 19 | import com.oracle.truffle.api.dsl.GenerateNodeFactory;
|
16 | 20 | import com.oracle.truffle.api.dsl.NodeFactory;
|
17 | 21 | import com.oracle.truffle.api.dsl.Specialization;
|
| 22 | +import com.oracle.truffle.api.nodes.UnexpectedResultException; |
18 | 23 |
|
19 | 24 | @CoreFunctions(extendClasses = PRandom.class)
|
20 | 25 | public class RandomBuiltins extends PythonBuiltins {
|
@@ -53,10 +58,25 @@ public PNone seed(PRandom random, double inputSeed) {
|
53 | 58 | return PNone.NONE;
|
54 | 59 | }
|
55 | 60 |
|
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 | + } |
60 | 80 | return PNone.NONE;
|
61 | 81 | }
|
62 | 82 | }
|
|
0 commit comments