html builder: rename get_object_hash to get_stable hash

Also amend the docstring so that it is easier to understand the need for the function.
This commit is contained in:
Georg Brandl
2013-03-29 11:17:34 +01:00
parent 663599abe9
commit 519be8ed6e

View File

@@ -53,20 +53,16 @@ INVENTORY_FILENAME = 'objects.inv'
LAST_BUILD_FILENAME = 'last_build'
def get_object_hash(obj):
def get_stable_hash(obj):
"""
In python3.3, unicode(dict_instance) retun another string per process.
get_object_hash(dict_instance) return same hash for same dict_instance.
Return a stable hash for a Python data structure. We can't just use
the md5 of str(obj) since for example dictionary items are enumerated
in unpredictable order due to hash randomization in newer Pythons.
"""
if isinstance(obj, dict):
return get_object_hash(list(obj.items()))
return get_stable_hash(list(obj.items()))
elif isinstance(obj, (list, tuple)):
obj = sorted(get_object_hash(o) for o in obj)
else: # int or other objects
pass
obj = sorted(get_stable_hash(o) for o in obj)
return md5(unicode(obj).encode('utf8')).hexdigest()
@@ -173,8 +169,8 @@ class StandaloneHTMLBuilder(Builder):
cfgdict = dict((name, self.config[name])
for (name, desc) in self.config.values.iteritems()
if desc[1] == 'html')
self.config_hash = get_object_hash(cfgdict)
self.tags_hash = get_object_hash(sorted(self.tags))
self.config_hash = get_stable_hash(cfgdict)
self.tags_hash = get_stable_hash(sorted(self.tags))
old_config_hash = old_tags_hash = ''
try:
fp = open(path.join(self.outdir, '.buildinfo'))