Skip to content

Commit b0287f9

Browse files
committed
give method and builtin method custom hashCodes
fixes #515497
1 parent db67d4c commit b0287f9

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

Lib/test/test_func_jy.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,27 @@ def test_functiontype_from_globals(self):
3030
self.assertEquals(sm_abc(), 789)
3131

3232

33+
class MethodHashCodeTestCase(unittest.TestCase):
34+
35+
def test_builtin_method_hashcode(self):
36+
foo = 'foo'
37+
self.assert_(foo.title is not foo.title)
38+
self.assertEqual(hash(foo.title), hash(foo.title))
39+
self.assertNotEqual(hash(foo.title), hash('bar'.title))
40+
41+
def test_method_hashcode(self):
42+
class Foo(object):
43+
def bar(self):
44+
pass
45+
foo = Foo()
46+
self.assert_(foo.bar is not foo.bar)
47+
self.assertEqual(hash(foo.bar), hash(foo.bar))
48+
self.assertNotEqual(hash(foo.bar), hash(Foo().bar))
49+
50+
3351
def test_main():
34-
test_support.run_unittest(FunctionTypeTestCase)
52+
test_support.run_unittest(FunctionTypeTestCase,
53+
MethodHashCodeTestCase)
3554

3655
if __name__ == '__main__':
3756
test_main()

src/org/python/core/PyBuiltinMethod.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import org.python.expose.ExposeAsSuperclass;
44

5-
65
public abstract class PyBuiltinMethod extends PyBuiltinFunction implements ExposeAsSuperclass {
76

7+
protected PyObject self;
8+
89
protected PyBuiltinMethod(PyType type, PyObject self, Info info) {
910
super(type, info);
1011
this.self = self;
1112
}
13+
1214
protected PyBuiltinMethod(PyObject self, Info info) {
1315
super(info);
1416
this.self = self;
@@ -26,5 +28,9 @@ public PyMethodDescr makeDescriptor(PyType t) {
2628
return new PyMethodDescr(t, this);
2729
}
2830

29-
protected PyObject self;
31+
@Override
32+
public int hashCode() {
33+
int hashCode = self == null ? 0 : self.hashCode();
34+
return hashCode ^ getClass().hashCode();
35+
}
3036
}

src/org/python/core/PyMethod.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ final int instancemethod___cmp__(PyObject other) {
136136
return 0;
137137
}
138138

139+
@Override
140+
public int hashCode() {
141+
int hashCode = im_self == null ? Py.None.hashCode() : im_self.hashCode();
142+
return hashCode ^ im_func.hashCode();
143+
}
144+
139145
@Override
140146
public PyObject getDoc() {
141147
return im_func.getDoc();

0 commit comments

Comments
 (0)