Skip to content

Commit 8581a4d

Browse files
committed
Undefine __hash__ when __eq__ is overriden
1 parent 7dc3276 commit 8581a4d

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_class.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
*graalpython.lib-python.3.test.test_class.ClassTests.testBinaryOps
33
*graalpython.lib-python.3.test.test_class.ClassTests.testForExceptionsRaisedInInstanceGetattr2
44
*graalpython.lib-python.3.test.test_class.ClassTests.testGetSetAndDel
5+
*graalpython.lib-python.3.test.test_class.ClassTests.testHashStuff
56
*graalpython.lib-python.3.test.test_class.ClassTests.testInit
67
*graalpython.lib-python.3.test.test_class.ClassTests.testListAndDictOps
78
*graalpython.lib-python.3.test.test_class.ClassTests.testMisc

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__WEAKREF__;
7272
import static com.oracle.graal.python.nodes.SpecialMethodNames.DECODE;
7373
import static com.oracle.graal.python.nodes.SpecialMethodNames.__COMPLEX__;
74+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__EQ__;
75+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__HASH__;
7476
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INDEX__;
7577
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INT__;
7678
import static com.oracle.graal.python.nodes.SpecialMethodNames.__REPR__;
@@ -2332,6 +2334,16 @@ private PythonClass typeMetaclass(VirtualFrame frame, String name, PTuple bases,
23322334
}
23332335
}
23342336

2337+
// CPython masks the __hash__ method with None when __eq__ is overriden, but __hash__ is
2338+
// not
2339+
Object hashMethod = nslib.getItem(namespace.getDictStorage(), __HASH__);
2340+
if (hashMethod == null) {
2341+
Object eqMethod = nslib.getItem(namespace.getDictStorage(), __EQ__);
2342+
if (eqMethod != null) {
2343+
pythonClass.setAttribute(__HASH__, PNone.NONE);
2344+
}
2345+
}
2346+
23352347
boolean addDict = false;
23362348
if (slots == null) {
23372349
// takes care of checking if we may_add_dict and adds it if needed

0 commit comments

Comments
 (0)