Skip to content

Commit 5e3165e

Browse files
committed
deal with unhashable types on Python side which still have identity
1 parent da6af5f commit 5e3165e

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/PythonAbstractObject.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,15 +2026,23 @@ public Object getMetaObject(@Shared("getClassThis") @Cached GetClassNode getClas
20262026
}
20272027

20282028
@ExportMessage
2029-
public int identityHashCode(@CachedLibrary("this") PythonObjectLibrary objectLib) {
2030-
return Long.hashCode(objectLib.hash(this));
2029+
public int identityHashCode(
2030+
@CachedLibrary("this") PythonObjectLibrary objectLib) {
2031+
if (objectLib.isHashable(this)) {
2032+
return Long.hashCode(objectLib.hash(this));
2033+
} else {
2034+
// everything in Python has an identity, but not everything provides a __hash__ method
2035+
return System.identityHashCode(this);
2036+
}
20312037
}
20322038

20332039
@ExportMessage
20342040
public TriState isIdenticalOrUndefined(Object other,
20352041
@CachedLibrary(limit = "3") InteropLibrary otherLib,
20362042
@CachedLibrary("this") PythonObjectLibrary objectLib) {
2037-
if (otherLib.hasIdentity(other)) {
2043+
if (this == other) {
2044+
return TriState.TRUE;
2045+
} else if (otherLib.hasIdentity(other)) {
20382046
return objectLib.isSame(this, other) ? TriState.TRUE : TriState.FALSE;
20392047
} else {
20402048
return TriState.UNDEFINED;

0 commit comments

Comments
 (0)