diff --git a/jedi/cache.py b/jedi/cache.py index ff7af271..1755bd45 100644 --- a/jedi/cache.py +++ b/jedi/cache.py @@ -6,6 +6,8 @@ memoize_caches = [] time_caches = [] +star_import_cache = {} + def clear_caches(): """ Jedi caches many things, that should be completed after each completion @@ -90,3 +92,40 @@ def time_cache(time_add_setting): def cache_get_in_function_call(stmt): module_path = stmt.get_parent_until().path return None if module_path is None else (module_path, stmt.start_pos) + + +def cache_star_import(func): + def wrapper(scope, *args, **kwargs): + try: + mods = star_import_cache[scope] + if mods[0] + settings.star_import_cache_validity > time.time(): + return mods[1] + except KeyError: + pass + # cache is too old and therefore invalid or not available + invalidate_star_import_cache(scope) + mods = func(scope, *args, **kwargs) + star_import_cache[scope] = time.time(), mods + + return mods + return wrapper + + +def invalidate_star_import_cache(module, only_main=False): + """ Important if some new modules are being reparsed """ + try: + t, mods = star_import_cache[module] + + del star_import_cache[module] + + for m in mods: + invalidate_star_import_cache(m, only_main=True) + except KeyError: + pass + + if not only_main: + # We need a list here because otherwise the list is being changed + # during the iteration in py3k: iteritems -> items. + for key, (t, mods) in list(star_import_cache.items()): + if module in mods: + invalidate_star_import_cache(key) diff --git a/jedi/imports.py b/jedi/imports.py index 501d6790..266fd879 100644 --- a/jedi/imports.py +++ b/jedi/imports.py @@ -4,7 +4,6 @@ import os import pkgutil import imp import sys -import time import builtin import modules @@ -12,13 +11,11 @@ import debug import parsing import evaluate import itertools -import settings +import cache # for debugging purposes only imports_processed = 0 -star_import_cache = {} - class ModuleNotFound(Exception): pass @@ -276,44 +273,7 @@ def strip_imports(scopes): return result -def cache_star_import(func): - def wrapper(scope, *args, **kwargs): - try: - mods = star_import_cache[scope] - if mods[0] + settings.star_import_cache_validity > time.time(): - return mods[1] - except KeyError: - pass - # cache is too old and therefore invalid or not available - invalidate_star_import_cache(scope) - mods = func(scope, *args, **kwargs) - star_import_cache[scope] = time.time(), mods - - return mods - return wrapper - - -def invalidate_star_import_cache(module, only_main=False): - """ Important if some new modules are being reparsed """ - try: - t, mods = star_import_cache[module] - - del star_import_cache[module] - - for m in mods: - invalidate_star_import_cache(m, only_main=True) - except KeyError: - pass - - if not only_main: - # We need a list here because otherwise the list is being changed - # during the iteration in py3k: iteritems -> items. - for key, (t, mods) in list(star_import_cache.items()): - if module in mods: - invalidate_star_import_cache(key) - - -@cache_star_import +@cache.cache_star_import def remove_star_imports(scope, ignored_modules=[]): """ Check a module for star imports: