[tor-commits] [stem/master] Hash functions for descriptor classes

atagar at torproject.org atagar at torproject.org
Mon Oct 14 00:42:16 UTC 2013


commit 1ed6e2b7e553414736913b3cd440843d66ccea94
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Oct 13 12:40:46 2013 -0700

    Hash functions for descriptor classes
    
    Ok, finally found out what this python 2 vs 3 hashing oddity is. If you define
    an __eq__() method in python3 then it doesn't inherit its parent's __hash__().
    This makes some sense, though it's unintuitive as hell. Oh well...
    
    http://stackoverflow.com/a/1608882/1067192
---
 stem/descriptor/microdescriptor.py   |    3 +++
 stem/descriptor/networkstatus.py     |    3 +++
 stem/descriptor/server_descriptor.py |    6 ++++++
 stem/exit_policy.py                  |    2 +-
 4 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/stem/descriptor/microdescriptor.py b/stem/descriptor/microdescriptor.py
index 1f9e8cb..61dce83 100644
--- a/stem/descriptor/microdescriptor.py
+++ b/stem/descriptor/microdescriptor.py
@@ -296,6 +296,9 @@ class Microdescriptor(Descriptor):
 
     return method(str(self).strip(), str(other).strip())
 
+  def __hash__(self):
+    return hash(str(self).strip())
+
   def __eq__(self, other):
     return self._compare(other, lambda s, o: s == o)
 
diff --git a/stem/descriptor/networkstatus.py b/stem/descriptor/networkstatus.py
index a2b07fb..b5eb5b7 100644
--- a/stem/descriptor/networkstatus.py
+++ b/stem/descriptor/networkstatus.py
@@ -566,6 +566,9 @@ class NetworkStatusDocumentV3(NetworkStatusDocument):
 
     return method(str(self).strip(), str(other).strip())
 
+  def __hash__(self):
+    return hash(str(self).strip())
+
   def __eq__(self, other):
     return self._compare(other, lambda s, o: s == o)
 
diff --git a/stem/descriptor/server_descriptor.py b/stem/descriptor/server_descriptor.py
index 160dab7..c84e8b8 100644
--- a/stem/descriptor/server_descriptor.py
+++ b/stem/descriptor/server_descriptor.py
@@ -820,6 +820,9 @@ class RelayDescriptor(ServerDescriptor):
 
     return method(str(self).strip(), str(other).strip())
 
+  def __hash__(self):
+    return hash(str(self).strip())
+
   def __eq__(self, other):
     return self._compare(other, lambda s, o: s == o)
 
@@ -951,6 +954,9 @@ class BridgeDescriptor(ServerDescriptor):
 
     return method(str(self).strip(), str(other).strip())
 
+  def __hash__(self):
+    return hash(str(self).strip())
+
   def __eq__(self, other):
     return self._compare(other, lambda s, o: s == o)
 
diff --git a/stem/exit_policy.py b/stem/exit_policy.py
index 163f58b..0f033de 100644
--- a/stem/exit_policy.py
+++ b/stem/exit_policy.py
@@ -441,7 +441,7 @@ class MicroExitPolicy(ExitPolicy):
     return self._policy
 
   def __hash__(self):
-    return id(self)
+    return hash(str(self))
 
   def __eq__(self, other):
     if isinstance(other, MicroExitPolicy):





More information about the tor-commits mailing list