Store the source in a private cache, and add a lazy entry in linecache that allows the source to be retrieved by 'filename'. Args: src (str): The module source to cache globals (dict): The module globals Returns: str: The cache key (and d
(self, src: str, globals: Dict[str, Any], co_fields=None)
| 36 | self.next_id = 0 |
| 37 | |
| 38 | def cache(self, src: str, globals: Dict[str, Any], co_fields=None): |
| 39 | """Store the source in a private cache, and add a lazy entry in linecache |
| 40 | that allows the source to be retrieved by 'filename'. |
| 41 | |
| 42 | Args: |
| 43 | src (str): The module source to cache |
| 44 | globals (dict): The module globals |
| 45 | |
| 46 | Returns: |
| 47 | str: The cache key (and dummy filename) generated for src. |
| 48 | """ |
| 49 | |
| 50 | key = self._get_key() |
| 51 | if co_fields: |
| 52 | key += f" from {co_fields['co_filename']}:{co_fields['co_firstlineno']} in {co_fields['co_name']}" |
| 53 | self.eval_cache[key] = src |
| 54 | |
| 55 | # Don't mutate globals so that this loader is only used |
| 56 | # to populate linecache, and doesn't interact with other modules |
| 57 | # that might check `__loader__` |
| 58 | globals_copy = globals.copy() |
| 59 | globals_copy["__file__"] = key |
| 60 | globals_copy["__name__"] = key |
| 61 | globals_copy["__loader__"] = self |
| 62 | linecache.lazycache(key, globals_copy) |
| 63 | |
| 64 | return key |
| 65 | |
| 66 | # Part of the loader protocol (PEP 302) |
| 67 | # linecache will use this method when trying to find source code |
no test coverage detected