is adler32 cross-kernel stable?

This commit is contained in:
Shay Palachy
2020-02-08 18:43:19 +02:00
parent 018f5eae90
commit d9a1a8cbd5
3 changed files with 11 additions and 6 deletions
+3 -2
View File
@@ -8,6 +8,7 @@
# Copyright (c) 2016, Shay Palachy <shaypal5@gmail.com>
import os
from zlib import adler32
import pickle # for local caching
from datetime import datetime
import threading
@@ -166,7 +167,7 @@ class _PickleCore(_BaseCore):
except ImportError: # pragma: no cover
pass
if hasattr(value, "tobytes"): # For numpy
return hash(value.tobytes())
return adler32(value.tobytes()) & 0xffffffff
if hasattr(value, "__iter__"): # For iterators
if isinstance(value, (list, tuple)):
hash_array = []
@@ -179,7 +180,7 @@ class _PickleCore(_BaseCore):
hash_array.append(key)
hash_array.append(self._hash_args(elem))
return tuple(hash_array)
return hash(value)
return adler32(pickle.dumps(value)) & 0xffffffff
def set_entry(self, key, func_res):
with self.lock:
+5 -2
View File
@@ -4,7 +4,9 @@
from cachier import cachier
from time import sleep
from random import random
from pickle import dump
from pickle import dump, dumps
from zlib import adler32
TEXT_VAL_TO_CHECK = 'foo'
TEXT_CACHE_FNAME = 'cachier_text_cache_temp.pkl'
@@ -14,11 +16,12 @@ TEXT_CACHE_FNAME = 'cachier_text_cache_temp.pkl'
def text_caching(text):
sleep(1)
print(text)
print(hash(text))
print(adler32(dumps(text)) & 0xffffffff)
return random()
if __name__ == '__main__':
text_caching.clear_cache()
return_val = text_caching(TEXT_VAL_TO_CHECK)
print(return_val)
with open(TEXT_CACHE_FNAME, 'wb+') as f:
+3 -2
View File
@@ -402,13 +402,14 @@ def test_pickle_core_custom_cache_dir():
def test_text_hashing():
text = TEXT_VAL_TO_CHECK
with open(TEXT_CACHE_FNAME, 'rb') as f:
first = load(f)
print('\npickled return val found for text cache text:')
print(first)
start_time = time()
second = text_caching(text)
print('calling with value:')
print(TEXT_VAL_TO_CHECK)
second = text_caching(TEXT_VAL_TO_CHECK)
print('second value returned:')
print(second)
call_time = time() - start_time