[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [stem/master] Type hashing produced inconsistant values
commit 7792a9bfc5f2782e7960044f2f2660af99e86d03
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date: Tue Jul 17 13:17:18 2018 -0700
Type hashing produced inconsistant values
Hashing classes (ie. "hash(type(my_obj))") sometimes provides inconsistant
values between interpreter invocations. Common builtin types (int, str, etc)
are consistant, but custom classes appear to vary.
Generally speaking this is fine. Hash value consistancy is only highly
important within an interpreter invocation. But full consistancy is nice and
easy to achieve.
Hashing the string representation of our module name instead.
---
stem/util/__init__.py | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/stem/util/__init__.py b/stem/util/__init__.py
index f1a7be6a..b812fc13 100644
--- a/stem/util/__init__.py
+++ b/stem/util/__init__.py
@@ -47,8 +47,6 @@ def _hash_value(val):
if not HASH_TYPES:
my_hash = 0
else:
- my_hash = hash(type(val))
-
# TODO: I hate doing this but until Python 2.x support is dropped we
# can't readily be strict about bytes vs unicode for attributes. This
# is because test assertions often use strings, and normalizing this
@@ -57,7 +55,11 @@ def _hash_value(val):
# This hack will go away when we drop Python 2.x support.
if _is_str(val):
- my_hash = hash(type(str))
+ my_hash = hash('str')
+ else:
+ # Hashing common builtins (ints, bools, etc) provide consistant values but many others vary their value on interpreter invokation.
+
+ my_hash = hash(str(type(val)))
if isinstance(val, (tuple, list)):
for v in val:
@@ -133,7 +135,7 @@ def _hash_attr(obj, *attributes, **kwargs):
# TODO: deal with this parent thing
- my_hash = hash(type(obj)) if kwargs.get('parent') is None else kwargs.get('parent').__hash__(obj)
+ my_hash = hash(str(type(obj))) if kwargs.get('parent') is None else kwargs.get('parent').__hash__(obj)
for attr in attributes:
val = getattr(obj, attr)
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits