=== modified file 'twisted/python/test/test_util.py' --- twisted/python/test/test_util.py 2010-08-02 17:16:05 +0000 +++ twisted/python/test/test_util.py 2010-09-17 21:11:55 +0000 @@ -616,6 +616,149 @@ +class Hashable(util.FancyHashMixin): + compareAttributes = hashAttributes = ("value",) + def __init__(self, value): + self.value = value + + + +class DifferentHashable(util.FancyHashMixin): + compareAttributes = hashAttributes = ("value",) + def __init__(self, value): + self.value = value + + + +class PartialHashable(util.FancyHashMixin): + compareAttributes = ("valueOne", "valueTwo") + hashAttributes = ("valueOne",) + + def __init__(self, valueOne, valueTwo): + self.valueOne, self.valueTwo = valueOne, valueTwo + + + +class DifferentPartialHashable(util.FancyHashMixin): + compareAttributes = ("valueOne", "valueTwo") + hashAttributes = ("valueOne",) + + def __init__(self, valueOne, valueTwo): + self.valueOne, self.valueTwo = valueOne, valueTwo + + + +class FancyHashMixinTests(unittest.TestCase): + def test_equality(self): + h1, h2 = Hashable(1), Hashable(1) + self.assertEqual(h1, h1) + self.assertEqual(h1, h2) + + + def test_inequality(self): + h1, h2 = Hashable(1), Hashable(2) + self.assertNotEqual(h1, h2) + + + def test_inequality_differentClasses(self): + h1, h2 = Hashable(1), DifferentHashable(2) + self.assertNotEqual(h1, h2) + + + def test_hashConsistent(self): + h = Hashable(1) + self.assertEqual(hash(h), hash(h)) + + + def test_hashEqual(self): + h1, h2 = Hashable(1), Hashable(1) + self.assertEqual(hash(h1), hash(h2)) + + + def test_hashEqual_differentClasses(self): + h1, h2 = Hashable(1), DifferentHashable(1) + self.assertEqual(hash(h1), hash(h2)) + + + def test_hashNotEqual(self): + h1, h2 = Hashable(1), Hashable(2) + self.assertNotEqual(hash(h1), hash(h2)) + + + def test_hashNotEqual_differentClasses(self): + h1, h2 = Hashable(1), DifferentHashable(2) + self.assertNotEqual(hash(h1), hash(h2)) + + + def test_set(self): + s = set([Hashable(1), Hashable(1)]) + self.assertEqual(len(s), 1) + + + +class PartialHashableTests(unittest.TestCase): + def test_equality(self): + h1, h2 = PartialHashable(1, 2), PartialHashable(1, 2) + self.assertEqual(h1, h1) + self.assertEqual(h1, h2) + + + def test_inequality(self): + h1, h2 = PartialHashable(1, 2), PartialHashable(2, 1) + self.assertNotEqual(h1, h2) + + + def test_hashConsistent(self): + h = PartialHashable(1, 2) + self.assertEqual(hash(h), hash(h)) + + + def test_hashEqual(self): + h1, h2 = PartialHashable(1, 2), PartialHashable(1, 2) + self.assertEqual(hash(h1), hash(h2)) + + + def test_hashEqual_differentClasses(self): + h1, h2 = PartialHashable(1, 2), DifferentPartialHashable(1, 2) + self.assertEqual(hash(h1), hash(h2)) + + + def test_hashEqual_partial(self): + h1, h2 = PartialHashable(1, 1), PartialHashable(1, 2) + self.assertEqual(hash(h1), hash(h2)) + + + def test_hashEqual_partial_differentClasses(self): + h1, h2 = PartialHashable(1, 1), DifferentPartialHashable(1, 2) + self.assertEqual(hash(h1), hash(h2)) + + + def test_hashNotEqual(self): + h1, h2 = PartialHashable(1, 2), PartialHashable(2, 1) + self.assertNotEqual(hash(h1), hash(h2)) + + + def test_hashNotEqual_differentClasses(self): + h1, h2 = PartialHashable(1, 2), PartialHashable(2, 1) + self.assertNotEqual(hash(h1), hash(h2)) + + + def test_set(self): + s = set([PartialHashable(1, 2), PartialHashable(1, 2)]) + self.assertEqual(len(s), 1) + + + def test_set_equalHashes(self): + s = set([PartialHashable(1, 1), PartialHashable(1, 2)]) + self.assertEqual(len(s), 2) + + + def test_set_equalHashes_differentClasses(self): + s = set([PartialHashable(1, 2), DifferentPartialHashable(1, 2)]) + self.assertEqual(len(s), 2) + + + class RunAsEffectiveUserTests(unittest.TestCase): """ Test for the L{util.runAsEffectiveUser} function. === modified file 'twisted/python/util.py' --- twisted/python/util.py 2010-08-02 17:16:05 +0000 +++ twisted/python/util.py 2010-09-17 21:14:34 +0000 @@ -563,6 +563,18 @@ return not result + __hash__ = None + + + +class FancyHashMixin(FancyEqMixin): + compareAttributes = hashAttributes = () + + def __hash__(self): + values = tuple(getattr(self, name) for name in self.hashAttributes) + return hash(values) + + def dsu(list, key): """