Seed the cache for filename with module_globals. The module loader will be asked for the source only when getlines is called, not immediately. If there is an entry in the cache already, it is not altered. :return: True if a lazy load is registered in the cache, oth
(filename, module_globals)
| 145 | |
| 146 | |
| 147 | def lazycache(filename, module_globals): |
| 148 | """Seed the cache for filename with module_globals. |
| 149 | |
| 150 | The module loader will be asked for the source only when getlines is |
| 151 | called, not immediately. |
| 152 | |
| 153 | If there is an entry in the cache already, it is not altered. |
| 154 | |
| 155 | :return: True if a lazy load is registered in the cache, |
| 156 | otherwise False. To register such a load a module loader with a |
| 157 | get_source method must be found, the filename must be a cacheable |
| 158 | filename, and the filename must not be already cached. |
| 159 | """ |
| 160 | if filename in cache: |
| 161 | if len(cache[filename]) == 1: |
| 162 | return True |
| 163 | else: |
| 164 | return False |
| 165 | if not filename or (filename.startswith('<') and filename.endswith('>')): |
| 166 | return False |
| 167 | # Try for a __loader__, if available |
| 168 | if module_globals and '__name__' in module_globals: |
| 169 | spec = module_globals.get('__spec__') |
| 170 | name = getattr(spec, 'name', None) or module_globals['__name__'] |
| 171 | loader = getattr(spec, 'loader', None) |
| 172 | if loader is None: |
| 173 | loader = module_globals.get('__loader__') |
| 174 | get_source = getattr(loader, 'get_source', None) |
| 175 | |
| 176 | if name and get_source: |
| 177 | get_lines = functools.partial(get_source, name) |
| 178 | cache[filename] = (get_lines,) |
| 179 | return True |
| 180 | return False |
no test coverage detected