[tor-commits] [stem/master] Proper hash function for ExitPolicy
atagar at torproject.org
atagar at torproject.org
Sun Sep 7 19:31:10 UTC 2014
commit d76ced9fbc1408472de5a08b15789bbb1bccc6c0
Author: Damian Johnson <atagar at torproject.org>
Date: Fri Sep 5 08:52:37 2014 -0700
Proper hash function for ExitPolicy
We skipped on implementing hash() out of concern of a circular dependency from
@lru_cache on _get_rules(). But _get_rules() no longer uses @lru_cache there
due to pickleability so we can now implement hash().
---
stem/exit_policy.py | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/stem/exit_policy.py b/stem/exit_policy.py
index 86722af..1f72b60 100644
--- a/stem/exit_policy.py
+++ b/stem/exit_policy.py
@@ -235,6 +235,7 @@ class ExitPolicy(object):
self._input_rules = rules
self._rules = None
+ self._hash = None
# Result when no rules apply. According to the spec policies default to 'is
# allowed', but our microdescriptor policy subclass might want to change
@@ -435,12 +436,16 @@ class ExitPolicy(object):
return ', '.join([str(rule) for rule in self._get_rules()])
def __hash__(self):
- # TODO: It would be nice to provide a real hash function, but doing so is
- # tricky due to how we lazily load the rules. Like equality checks a proper
- # hash function would need to call _get_rules(), but that's behind
- # @lru_cache which calls hash() forming a circular dependency.
+ if self._hash is None:
+ my_hash = 0
+
+ for rule in self._get_rules():
+ my_hash *= 1024
+ my_hash += hash(rule)
+
+ self._hash = my_hash
- return id(self)
+ return self._hash
def __eq__(self, other):
if isinstance(other, ExitPolicy):
More information about the tor-commits
mailing list