Skip to content

Commit 620d042

Browse files
committed
Expose hash-related constants in sys.hash_info
1 parent 4c7453f commit 620d042

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/PythonCextBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1466,7 +1466,7 @@ Object doUnicode(VirtualFrame frame, Object o, Object errorMarker) {
14661466
abstract static class PyHashImagNode extends PythonBuiltinNode {
14671467
@Specialization
14681468
long getHash() {
1469-
return PComplex.IMAG_MULTIPLIER;
1469+
return SysModuleBuiltins.HASH_IMAG;
14701470
}
14711471
}
14721472

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SysModuleBuiltins.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ public class SysModuleBuiltins extends PythonBuiltins {
109109
public static final String PLATFORM_WIN32 = "win32";
110110
public static final PNone FRAMEWORK = PNone.NONE;
111111
public static final int MAXSIZE = Integer.MAX_VALUE;
112+
public static final long HASH_MULTIPLIER = 1000003L;
113+
public static final int HASH_BITS = 61;
114+
public static final long HASH_MODULUS = (1L << HASH_BITS) - 1;
115+
public static final long HASH_INF = 314159;
116+
public static final long HASH_NAN = 0;
117+
public static final long HASH_IMAG = HASH_MULTIPLIER;
112118

113119
static {
114120
String compile_time;
@@ -155,6 +161,17 @@ public void initialize(PythonCore core) {
155161
2, // FLT_RADIX
156162
1 // FLT_ROUNDS
157163
}));
164+
builtinConstants.put("hash_info", core.factory().createTuple(new Object[]{
165+
"java", // algorithm
166+
0, // cutoff
167+
64, // hash_bits
168+
HASH_IMAG, // imag
169+
HASH_INF, // inf
170+
HASH_MODULUS, // modulus
171+
HASH_NAN, // nan
172+
0, // seed_bits
173+
64, // width
174+
}));
158175
builtinConstants.put("maxunicode", IntegerFormatter.LIMIT_UNICODE.intValue() - 1);
159176

160177
String os = getPythonOSName();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/complex/ComplexBuiltins.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
import com.oracle.graal.python.builtins.CoreFunctions;
8282
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
8383
import com.oracle.graal.python.builtins.PythonBuiltins;
84+
import com.oracle.graal.python.builtins.modules.SysModuleBuiltins;
8485
import com.oracle.graal.python.builtins.objects.PNone;
8586
import com.oracle.graal.python.builtins.objects.PNotImplemented;
8687
import com.oracle.graal.python.builtins.objects.floats.FloatBuiltins;
@@ -831,7 +832,7 @@ long hash(PComplex self) {
831832
// just like CPython
832833
long realHash = PythonObjectLibrary.hash(self.getReal());
833834
long imagHash = PythonObjectLibrary.hash(self.getImag());
834-
return realHash + PComplex.IMAG_MULTIPLIER * imagHash;
835+
return realHash + SysModuleBuiltins.HASH_IMAG * imagHash;
835836
}
836837
}
837838

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/complex/PComplex.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
import com.oracle.truffle.api.object.Shape;
3232

3333
public final class PComplex extends PythonBuiltinObject {
34-
/* Prime multiplier used in string and various other hashes in CPython. */
35-
public static final int IMAG_MULTIPLIER = 1000003; /* 0xf4243 */
3634

3735
private final double real;
3836
private final double imag;

graalpython/lib-graalpython/sys.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,7 @@ def make_hash_info_class():
131131
"seed_bits",
132132
"width"]
133133
)
134-
hash_info = make_hash_info_class()(
135-
("java", 0, 64, 0, float('inf').__hash__(), 7, float('nan').__hash__(), 0, 64)
136-
)
134+
hash_info = make_hash_info_class()(hash_info)
137135
del make_hash_info_class
138136

139137

0 commit comments

Comments
 (0)