From 0624797375d676732fcb2c20d289386ba7d1a527 Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Thu, 24 Sep 2015 12:46:06 +0300 Subject: [PATCH] Bugfix: if you define a custom __eq__, you must define a __hash__ too Also, on Python 3 the stock object.__hash__ raises an error ("unhashable type"), and we have code that uses Host instances as dict keys. --- lib/ansible/inventory/host.py | 3 +++ test/units/inventory/test_host.py | 3 +++ 2 files changed, 6 insertions(+) diff --git a/lib/ansible/inventory/host.py b/lib/ansible/inventory/host.py index fa2433956d2..f6089f07ed9 100644 --- a/lib/ansible/inventory/host.py +++ b/lib/ansible/inventory/host.py @@ -41,6 +41,9 @@ class Host: def __ne__(self, other): return not self.__eq__(other) + def __hash__(self): + return hash(self.name) + def serialize(self): groups = [] for group in self.groups: diff --git a/test/units/inventory/test_host.py b/test/units/inventory/test_host.py index f9f500a63e6..078d4321b57 100644 --- a/test/units/inventory/test_host.py +++ b/test/units/inventory/test_host.py @@ -33,3 +33,6 @@ class TestHost(unittest.TestCase): # __ne__ is a separate method self.assertFalse(self.hostA != Host('a')) + def test_hashability(self): + # equality implies the hash values are the same + self.assertEqual(hash(self.hostA), hash(Host('a')))