Skip to content

Commit b4e79d3

Browse files
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
1 parent cde5ef6 commit b4e79d3

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

memestra/caching.py

+16-9
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ def check_deprecated(data):
128128

129129

130130
class CacheKeyFactoryBase(object):
131+
132+
py_version = sys.version_info
133+
134+
class CacheKeyBase(object):
135+
def __init__(self):
136+
self._hasher = hashlib.sha256()
137+
self._hasher.update(CacheKeyFactoryBase.py_version)
138+
131139
def __init__(self, keycls):
132140
self.keycls = keycls
133141
self.created = dict()
@@ -154,14 +162,15 @@ class CacheKeyFactory(CacheKeyFactoryBase):
154162
Only the content of the module is taken into account
155163
'''
156164

157-
class CacheKey(object):
165+
class CacheKey(CacheKeyFactoryBase.CacheKeyBase):
158166

159167
def __init__(self, module_path, _):
168+
super(CacheKey, self).__init__()
160169

161170
with open(module_path, 'rb') as fd:
162171
module_content = fd.read()
163-
module_hash = hashlib.sha256(module_content).hexdigest()
164-
self.module_hash = module_hash
172+
self._hasher.update(module_content)
173+
self.module_hash = m.hexdigest()
165174

166175
@property
167176
def path(self):
@@ -179,7 +188,7 @@ class RecursiveCacheKeyFactory(CacheKeyFactoryBase):
179188
key.
180189
'''
181190

182-
class CacheKey(object):
191+
class CacheKey(CacheKeyFactoryBase.CacheKeyBase):
183192

184193
def __init__(self, module_path, factory):
185194
assert module_path not in factory.created or factory.created[module_path] is None
@@ -196,9 +205,7 @@ def __init__(self, module_path, factory):
196205
if factory.get(dep, 1) is not None:
197206
new_deps.append(dep)
198207

199-
module_hash = hashlib.sha256(module_content).hexdigest()
200-
201-
hashes = [module_hash]
208+
self._hasher.update(module_content)
202209

203210
for new_dep in sorted(new_deps):
204211
try:
@@ -207,9 +214,9 @@ def __init__(self, module_path, factory):
207214
# better?
208215
except UnicodeDecodeError:
209216
continue
210-
hashes.append(new_dep_key.module_hash)
217+
self._hasher.update(new_dep_key.module_hash)
211218

212-
self.module_hash = hashlib.sha256("".join(hashes).encode("ascii")).hexdigest()
219+
self.module_hash = self._hasher.hexdigest()
213220

214221
@property
215222
def path(self):

0 commit comments

Comments
 (0)