Skip to content

Commit a0dcc82

Browse files
committed
[GR-23207] Fix hash of bound methods
PullRequest: graalpython/1142
2 parents e130d3d + e18df3b commit a0dcc82

File tree

6 files changed

+23
-14
lines changed

6 files changed

+23
-14
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
*graalpython.lib-python.3.test.test_class.ClassTests.testBadTypeReturned
22
*graalpython.lib-python.3.test.test_class.ClassTests.testBinaryOps
33
*graalpython.lib-python.3.test.test_class.ClassTests.testConstructorErrorMessages
4+
*graalpython.lib-python.3.test.test_class.ClassTests.testDel
45
*graalpython.lib-python.3.test.test_class.ClassTests.testForExceptionsRaisedInInstanceGetattr2
56
*graalpython.lib-python.3.test.test_class.ClassTests.testGetSetAndDel
7+
*graalpython.lib-python.3.test.test_class.ClassTests.testHashComparisonOfMethods
68
*graalpython.lib-python.3.test.test_class.ClassTests.testHashStuff
79
*graalpython.lib-python.3.test.test_class.ClassTests.testInit
810
*graalpython.lib-python.3.test.test_class.ClassTests.testListAndDictOps

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

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
*/
2626
package com.oracle.graal.python.builtins.objects.method;
2727

28+
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
29+
import com.oracle.graal.python.builtins.objects.function.PArguments.ThreadState;
2830
import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
2931
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
3032
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
@@ -65,4 +67,9 @@ public String toString() {
6567
public boolean isCallable() {
6668
return true;
6769
}
70+
71+
@ExportMessage
72+
protected long hashWithState(@SuppressWarnings("unused") ThreadState state) {
73+
return PythonAbstractObject.systemHashCode(this.getSelf()) ^ PythonAbstractObject.systemHashCode(this.getFunction());
74+
}
6875
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
*/
2626
package com.oracle.graal.python.builtins.objects.method;
2727

28+
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
29+
import com.oracle.graal.python.builtins.objects.function.PArguments.ThreadState;
2830
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
2931
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
3032
import com.oracle.truffle.api.CompilerAsserts;
@@ -77,4 +79,9 @@ protected SourceSection getSourceLocation(@CachedLibrary("this.function") Intero
7779
protected boolean hasSourceLocation(@CachedLibrary("this.function") InteropLibrary lib) {
7880
return lib.hasSourceLocation(function);
7981
}
82+
83+
@ExportMessage
84+
protected long hashWithState(@SuppressWarnings("unused") ThreadState state) {
85+
return PythonAbstractObject.systemHashCode(this.getSelf()) ^ PythonAbstractObject.systemHashCode(this.getFunction());
86+
}
8087
}

graalpython/lib-python/3/test/test_class.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ def __delattr__(self, *args):
445445
del testme.cardinal
446446
self.assertCallStack([('__delattr__', (testme, "cardinal"))])
447447

448+
@support.impl_detail("finalization", graalvm=False)
448449
def testDel(self):
449450
x = []
450451

0 commit comments

Comments
 (0)