1
0
forked from VimPlug/jedi

move module caching from builtin to cache

This commit is contained in:
David Halter
2013-01-10 11:04:26 +01:00
parent cc3168ee0a
commit 133fbcd57a
2 changed files with 45 additions and 16 deletions

View File

@@ -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):

View File

@@ -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