From 8bdfea3c5c319f868334d4a34b3b5d35a2843b0f Mon Sep 17 00:00:00 2001 From: serge-sans-paille Date: Fri, 6 Aug 2021 13:15:29 +0200 Subject: [PATCH] Integrate python version into the hash key This prevents subtle issues when the ast changes but gast / beniget / memestra don't get updated accordingly. Fix #61 --- memestra/caching.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/memestra/caching.py b/memestra/caching.py index 01df70e..ca4baac 100644 --- a/memestra/caching.py +++ b/memestra/caching.py @@ -128,6 +128,14 @@ def check_deprecated(data): class CacheKeyFactoryBase(object): + + py_version = ".".join(map(str, sys.version_info)).encode() + + class CacheKeyBase(object): + def __init__(self): + self._hasher = hashlib.sha256() + self._hasher.update(CacheKeyFactoryBase.py_version) + def __init__(self, keycls): self.keycls = keycls self.created = dict() @@ -154,14 +162,15 @@ class CacheKeyFactory(CacheKeyFactoryBase): Only the content of the module is taken into account ''' - class CacheKey(object): + class CacheKey(CacheKeyFactoryBase.CacheKeyBase): def __init__(self, module_path, _): + super(CacheKeyFactory.CacheKey, self).__init__() with open(module_path, 'rb') as fd: module_content = fd.read() - module_hash = hashlib.sha256(module_content).hexdigest() - self.module_hash = module_hash + self._hasher.update(module_content) + self.module_hash = self._hasher.hexdigest() @property def path(self): @@ -179,9 +188,10 @@ class RecursiveCacheKeyFactory(CacheKeyFactoryBase): key. ''' - class CacheKey(object): + class CacheKey(CacheKeyFactoryBase.CacheKeyBase): def __init__(self, module_path, factory): + super(RecursiveCacheKeyFactory.CacheKey, self).__init__() assert module_path not in factory.created or factory.created[module_path] is None with open(module_path, 'rb') as fd: @@ -196,9 +206,7 @@ def __init__(self, module_path, factory): if factory.get(dep, 1) is not None: new_deps.append(dep) - module_hash = hashlib.sha256(module_content).hexdigest() - - hashes = [module_hash] + self._hasher.update(module_content) for new_dep in sorted(new_deps): try: @@ -207,9 +215,9 @@ def __init__(self, module_path, factory): # better? except UnicodeDecodeError: continue - hashes.append(new_dep_key.module_hash) + self._hasher.update(new_dep_key.module_hash.encode()) - self.module_hash = hashlib.sha256("".join(hashes).encode("ascii")).hexdigest() + self.module_hash = self._hasher.hexdigest() @property def path(self):