diff --git a/parso/cache.py b/parso/cache.py index cc9e4ee..d3e4b8d 100644 --- a/parso/cache.py +++ b/parso/cache.py @@ -72,7 +72,7 @@ class _NodeCacheItem(object): self.change_time = change_time -def load_module(grammar, path, cache_path=None): +def load_module(grammar_hash, path, cache_path=None): """ Returns a module or None, if it fails. """ @@ -87,11 +87,11 @@ def load_module(grammar, path, cache_path=None): if p_time <= module_cache_item.change_time: return module_cache_item.node except KeyError: - return _load_from_file_system(grammar, path, p_time, cache_path=cache_path) + return _load_from_file_system(grammar_hash, path, p_time, cache_path=cache_path) -def _load_from_file_system(grammar, path, p_time, cache_path=None): - cache_path = _get_hashed_path(grammar, path, cache_path=cache_path) +def _load_from_file_system(grammar_hash, path, p_time, cache_path=None): + cache_path = _get_hashed_path(grammar_hash, path, cache_path=cache_path) try: try: if p_time > os.path.getmtime(cache_path): @@ -118,7 +118,7 @@ def _load_from_file_system(grammar, path, p_time, cache_path=None): return module_cache_item.node -def save_module(grammar, path, module, lines, pickling=True, cache_path=None): +def save_module(grammar_hash, path, module, lines, pickling=True, cache_path=None): try: p_time = None if path is None else os.path.getmtime(path) except OSError: @@ -128,11 +128,11 @@ def save_module(grammar, path, module, lines, pickling=True, cache_path=None): item = _NodeCacheItem(module, lines, p_time) parser_cache[path] = item if pickling and path is not None: - _save_to_file_system(grammar, path, item) + _save_to_file_system(grammar_hash, path, item) -def _save_to_file_system(grammar, path, item, cache_path=None): - with open(_get_hashed_path(grammar, path, cache_path=cache_path), 'wb') as f: +def _save_to_file_system(grammar_hash, path, item, cache_path=None): + with open(_get_hashed_path(grammar_hash, path, cache_path=cache_path), 'wb') as f: pickle.dump(item, f, pickle.HIGHEST_PROTOCOL) @@ -151,11 +151,11 @@ def clear_cache(cache_path=None): parser_cache.clear() -def _get_hashed_path(grammar, path, cache_path=None): +def _get_hashed_path(grammar_hash, path, cache_path=None): directory = _get_cache_directory_path(cache_path=cache_path) file_hash = hashlib.sha256(path.encode("utf-8")).hexdigest() - return os.path.join(directory, '%s-%s.pkl' % (grammar._sha256, file_hash)) + return os.path.join(directory, '%s-%s.pkl' % (grammar_hash, file_hash)) def _get_cache_directory_path(cache_path=None): diff --git a/parso/grammar.py b/parso/grammar.py index b97d0e7..3732313 100644 --- a/parso/grammar.py +++ b/parso/grammar.py @@ -77,7 +77,7 @@ class Grammar(object): # With the current architecture we cannot load from cache if the # code is given, because we just load from cache if it's not older than # the latest change (file last modified). - module_node = load_module(self, path, cache_path=cache_path) + module_node = load_module(self._sha256, path, cache_path=cache_path) if module_node is not None: return module_node @@ -99,7 +99,7 @@ class Grammar(object): old_lines = module_cache_item.lines if old_lines == lines: # TODO remove this line? I think it's not needed. (dave) - save_module(self, path, module_node, lines, pickling=False, + save_module(self._sha256, path, module_node, lines, pickling=False, cache_path=cache_path) return module_node @@ -107,7 +107,7 @@ class Grammar(object): old_lines=old_lines, new_lines=lines ) - save_module(self, path, new_node, lines, pickling=cache, + save_module(self._sha256, path, new_node, lines, pickling=cache, cache_path=cache_path) return new_node @@ -126,7 +126,7 @@ class Grammar(object): remove_last_newline(root_node) if cache or diff_cache: - save_module(self, path, root_node, lines, pickling=cache, + save_module(self._sha256, path, root_node, lines, pickling=cache, cache_path=cache_path) return root_node diff --git a/test/test_cache.py b/test/test_cache.py index 9fba3d5..2704502 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -37,20 +37,20 @@ def test_modulepickling_change_cache_dir(tmpdir): path_1 = 'fake path 1' path_2 = 'fake path 2' - grammar = load_grammar() - _save_to_file_system(grammar, path_1, item_1, cache_path=dir_1) + hashed_grammar = load_grammar()._sha256 + _save_to_file_system(hashed_grammar, path_1, item_1, cache_path=dir_1) parser_cache.clear() - cached = load_stored_item(grammar, path_1, item_1, cache_path=dir_1) + cached = load_stored_item(hashed_grammar, path_1, item_1, cache_path=dir_1) assert cached == item_1.node - _save_to_file_system(grammar, path_2, item_2, cache_path=dir_2) - cached = load_stored_item(grammar, path_1, item_1, cache_path=dir_2) + _save_to_file_system(hashed_grammar, path_2, item_2, cache_path=dir_2) + cached = load_stored_item(hashed_grammar, path_1, item_1, cache_path=dir_2) assert cached is None -def load_stored_item(grammar, path, item, cache_path): +def load_stored_item(hashed_grammar, path, item, cache_path): """Load `item` stored at `path` in `cache`.""" - item = _load_from_file_system(grammar, path, item.change_time - 1, cache_path) + item = _load_from_file_system(hashed_grammar, path, item.change_time - 1, cache_path) return item @@ -77,11 +77,11 @@ def test_modulepickling_simulate_deleted_cache(tmpdir): with open(path, 'w'): pass - save_module(grammar, path, module, []) - assert load_module(grammar, path) == module + save_module(grammar._sha256, path, module, []) + assert load_module(grammar._sha256, path) == module - unlink(_get_hashed_path(grammar, path)) + unlink(_get_hashed_path(grammar._sha256, path)) parser_cache.clear() - cached2 = load_module(grammar, path) + cached2 = load_module(grammar._sha256, path) assert cached2 is None