import_cache: read files only once

This commit is contained in:
Sebastien Bourdeauducq 2021-11-11 20:42:49 +08:00
parent 1ea3cf48d6
commit 95eb218112
1 changed files with 11 additions and 10 deletions

View File

@ -24,16 +24,17 @@ linecache_getlines = None
def add_module_to_cache(module): def add_module_to_cache(module):
if hasattr(module, "__file__"): if hasattr(module, "__file__"):
fn = module.__file__ fn = module.__file__
try: if fn not in cache:
with tokenize.open(fn) as fp: try:
lines = fp.readlines() with tokenize.open(fn) as fp:
if lines and not lines[-1].endswith("\n"): lines = fp.readlines()
lines[-1] += "\n" if lines and not lines[-1].endswith("\n"):
cache[fn] = lines lines[-1] += "\n"
except: cache[fn] = lines
logger.warning("failed to add '%s' to cache", fn, exc_info=True) except:
else: logger.warning("failed to add '%s' to cache", fn, exc_info=True)
logger.debug("added '%s' to cache", fn) else:
logger.debug("added '%s' to cache", fn)
def hook_getlines(filename, module_globals=None): def hook_getlines(filename, module_globals=None):