Skip to content

Commit b8cf170

Browse files
committed
Fix __hash__ of bound methods
1 parent 7b7a22f commit b8cf170

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
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
@@ -4,6 +4,7 @@
44
*graalpython.lib-python.3.test.test_class.ClassTests.testDel
55
*graalpython.lib-python.3.test.test_class.ClassTests.testForExceptionsRaisedInInstanceGetattr2
66
*graalpython.lib-python.3.test.test_class.ClassTests.testGetSetAndDel
7+
*graalpython.lib-python.3.test.test_class.ClassTests.testHashComparisonOfMethods
78
*graalpython.lib-python.3.test.test_class.ClassTests.testHashStuff
89
*graalpython.lib-python.3.test.test_class.ClassTests.testInit
910
*graalpython.lib-python.3.test.test_class.ClassTests.testListAndDictOps

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/method/AbstractMethodBuiltins.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__SELF__;
3535
import static com.oracle.graal.python.nodes.SpecialMethodNames.__CALL__;
3636
import static com.oracle.graal.python.nodes.SpecialMethodNames.__EQ__;
37+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__HASH__;
3738

3839
import java.util.List;
3940

@@ -43,6 +44,7 @@
4344
import com.oracle.graal.python.builtins.PythonBuiltins;
4445
import com.oracle.graal.python.builtins.modules.BuiltinFunctions.GetAttrNode;
4546
import com.oracle.graal.python.builtins.objects.PNone;
47+
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
4648
import com.oracle.graal.python.builtins.objects.function.PKeyword;
4749
import com.oracle.graal.python.builtins.objects.module.PythonModule;
4850
import com.oracle.graal.python.builtins.objects.object.PythonObject;
@@ -54,6 +56,7 @@
5456
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5557
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
5658
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
59+
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
5760
import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode;
5861
import com.oracle.graal.python.util.PythonUtils;
5962
import com.oracle.truffle.api.CompilerDirectives;
@@ -184,6 +187,20 @@ boolean eq(@SuppressWarnings("unused") Object self, @SuppressWarnings("unused")
184187
}
185188
}
186189

190+
@Builtin(name = __HASH__, minNumOfPositionalArgs = 1)
191+
@GenerateNodeFactory
192+
public abstract static class HashNode extends PythonUnaryBuiltinNode {
193+
@Specialization
194+
Object doGeneric(PMethod self) {
195+
return PythonAbstractObject.systemHashCode(self.getSelf()) ^ PythonAbstractObject.systemHashCode(self.getFunction());
196+
}
197+
198+
@Specialization
199+
Object doGeneric(PBuiltinMethod self) {
200+
return PythonAbstractObject.systemHashCode(self.getSelf()) ^ PythonAbstractObject.systemHashCode(self.getFunction());
201+
}
202+
}
203+
187204
@Builtin(name = __MODULE__, minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, isGetter = true, isSetter = true)
188205
@GenerateNodeFactory
189206
abstract static class GetModuleNode extends PythonBuiltinNode {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/method/BuiltinMethodBuiltins.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
package com.oracle.graal.python.builtins.objects.method;
2828

2929
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__TEXT_SIGNATURE__;
30+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__OBJCLASS__;
3031
import static com.oracle.graal.python.nodes.SpecialMethodNames.__REDUCE__;
3132
import static com.oracle.graal.python.nodes.SpecialMethodNames.__REPR__;
3233
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
@@ -38,6 +39,7 @@
3839
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
3940
import com.oracle.graal.python.builtins.PythonBuiltins;
4041
import com.oracle.graal.python.builtins.objects.PNone;
42+
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
4143
import com.oracle.graal.python.builtins.objects.function.AbstractFunctionBuiltins;
4244
import com.oracle.graal.python.builtins.objects.function.PFunction;
4345
import com.oracle.graal.python.builtins.objects.module.PythonModule;
@@ -46,7 +48,6 @@
4648
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetNameNode;
4749
import com.oracle.graal.python.nodes.ErrorMessages;
4850
import com.oracle.graal.python.nodes.SpecialAttributeNames;
49-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__OBJCLASS__;
5051
import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
5152
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5253
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
@@ -106,7 +107,7 @@ Object reprBuiltinMethod(VirtualFrame frame, PBuiltinMethod self,
106107
@Cached("createGetAttributeNode()") GetAttributeNode getNameNode,
107108
@Cached("create()") GetNameNode getTypeNameNode) {
108109
String typeName = getTypeNameNode.execute(lib.getLazyPythonClass(self.getSelf()));
109-
return strFormat("<built-in method %s of %s object at 0x%x>", getNameNode.executeObject(frame, self.getFunction()), typeName, hashCode(self));
110+
return strFormat("<built-in method %s of %s object at 0x%x>", getNameNode.executeObject(frame, self.getFunction()), typeName, PythonAbstractObject.systemHashCode(self.getSelf()));
110111
}
111112

112113
@Specialization(guards = "!isBuiltinFunction(self)", limit = "3")
@@ -115,12 +116,7 @@ Object reprBuiltinMethod(VirtualFrame frame, PMethod self,
115116
@Cached("createGetAttributeNode()") GetAttributeNode getNameNode,
116117
@Cached("create()") GetNameNode getTypeNameNode) {
117118
String typeName = getTypeNameNode.execute(lib.getLazyPythonClass(self.getSelf()));
118-
return strFormat("<built-in method %s of %s object at 0x%x>", getNameNode.executeObject(frame, self.getFunction()), typeName, hashCode(self));
119-
}
120-
121-
@TruffleBoundary(allowInlining = true)
122-
private static int hashCode(Object self) {
123-
return self.hashCode();
119+
return strFormat("<built-in method %s of %s object at 0x%x>", getNameNode.executeObject(frame, self.getFunction()), typeName, PythonAbstractObject.systemHashCode(self.getSelf()));
124120
}
125121

126122
@TruffleBoundary

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/method/MethodBuiltins.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4545
import com.oracle.graal.python.builtins.PythonBuiltins;
4646
import com.oracle.graal.python.builtins.objects.PNone;
47+
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
4748
import com.oracle.graal.python.builtins.objects.function.PKeyword;
4849
import com.oracle.graal.python.builtins.objects.object.ObjectBuiltins;
4950
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
@@ -135,12 +136,7 @@ Object reprMethod(VirtualFrame frame, PMethod self,
135136
@Cached("createGetAttributeNode()") GetAttributeNode getNameAttrNode,
136137
@Cached GetNameNode getTypeNameNode) {
137138
String typeName = getTypeNameNode.execute(lib.getLazyPythonClass(self.getSelf()));
138-
return strFormat("<bound method %s of %s object at 0x%x>", getNameAttrNode.executeObject(frame, self.getFunction()), typeName, hashCode(self));
139-
}
140-
141-
@TruffleBoundary(allowInlining = true)
142-
private static int hashCode(PMethod self) {
143-
return self.hashCode();
139+
return strFormat("<bound method %s of %s object at 0x%x>", getNameAttrNode.executeObject(frame, self.getFunction()), typeName, PythonAbstractObject.systemHashCode(self.getSelf()));
144140
}
145141

146142
@TruffleBoundary

0 commit comments

Comments
 (0)