Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def for_filename(cls, filename, module_globals=None, use_cache=True):
source_cache = cls._class_local('__source_cache', {})
if use_cache:
try:
return source_cache[filename]
except KeyError:
pass
if isinstance(filename, Path):
filename = str(filename)
if not use_cache:
linecache.checkcache(filename)
lines = tuple(linecache.getlines(filename, module_globals))
result = source_cache[filename] = cls._for_filename_and_lines(filename, lines)
return result