From 133fbcd57a75b96f9811be961e4b42c94f68ad4d Mon Sep 17 00:00:00 2001 From: David Halter Date: Thu, 10 Jan 2013 11:04:26 +0100 Subject: [PATCH] move module caching from builtin to cache --- jedi/builtin.py | 20 ++++---------------- jedi/cache.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/jedi/builtin.py b/jedi/builtin.py index a13b31ca..c48dfab0 100644 --- a/jedi/builtin.py +++ b/jedi/builtin.py @@ -47,18 +47,9 @@ class CachedModule(object): @property def parser(self): """ get the parser lazy """ - if not self._parser: - try: - timestamp, parser = cache.module_cache[self.path or self.name] - if not self.path or os.path.getmtime(self.path) <= timestamp: - self._parser = parser - else: - # In case there is already a module cached and this module - # has to be reparsed, we also need to invalidate the import - # caches. - cache.invalidate_star_import_cache(parser.module) - raise KeyError() - except KeyError: + if self._parser is None: + self._parser = cache.load_module(self.path, self.name) + if self._parser is None: self._load_module() return self._parser @@ -69,10 +60,7 @@ class CachedModule(object): source = self._get_source() p = self.path or self.name self._parser = fast_parser.FastParser(source, p) - p_time = None if not self.path else os.path.getmtime(self.path) - - if self.path or self.name: - cache.module_cache[self.path or self.name] = p_time, self._parser + cache.save_module(self.path, self.name, self._parser) class Parser(CachedModule): diff --git a/jedi/cache.py b/jedi/cache.py index 1e6f3f9d..4f7f0098 100644 --- a/jedi/cache.py +++ b/jedi/cache.py @@ -1,4 +1,6 @@ import time +import os + import settings # memoize caches will be deleted after every action @@ -87,6 +89,7 @@ def time_cache(time_add_setting): def _temp(key_func): dct = {} time_caches.append(dct) + def wrapper(optional_callable, *args, **kwargs): key = key_func(*args, **kwargs) value = None @@ -144,3 +147,41 @@ def invalidate_star_import_cache(module, only_main=False): for key, (t, mods) in list(star_import_cache.items()): if module in mods: invalidate_star_import_cache(key) + + +def load_module(path, name): + """ + Returns the module or None, if it fails. + """ + if path is None and name is None: + return None + + try: + timestamp, parser = module_cache[path or name] + if not path or os.path.getmtime(path) <= timestamp: + return parser + else: + # In case there is already a module cached and this module + # has to be reparsed, we also need to invalidate the import + # caches. + invalidate_star_import_cache(parser.module) + return None + except KeyError: + return load_pickle_module(path or name) + + +def save_module(path, name, parser): + if path is None and name is None: + return + + p_time = None if not path else os.path.getmtime(path) + module_cache[path or name] = p_time, parser + save_pickle_module(path or name) + + +def load_pickle_module(path): + return None + + +def save_pickle_module(path): + pass