| 1 |
"""source_cache.py |
| 2 |
|
| 3 |
Used to cache and retrieve source code. |
| 4 |
This is especially useful when a custom REPL is used. |
| 5 |
|
| 6 |
Note that we monkeypatch Python's linecache.getlines. |
| 7 |
""" |
| 8 |
|
| 9 |
import inspect |
| 10 |
import linecache |
| 11 |
import time |
| 12 |
from typing import Any, Dict, Generator, List, Optional |
| 13 |
|
| 14 |
import stack_data |
| 15 |
|
| 16 |
old_getlines = linecache.getlines |
| 17 |
|
| 18 |
|
| 19 |
class Cache: |
| 20 |
"""Class used to store source of files and similar objects""" |
| 21 |
|
| 22 |
def __init__(self) -> None: |
| 23 |
self.local_cache: Dict[str, List[str]] = {} |
| 24 |
self.context = 4 |
| 25 |
|
| 26 |
def add(self, filename: str, source: str) -> None: |
| 27 |
"""Adds a source (received as a string) corresponding to a filename |
| 28 |
in the cache. |
| 29 |
|
| 30 |
The filename can be a true file name, or a fake one, like |
| 31 |
<friendly-console:42>, used for saving an REPL entry. |
| 32 |
These fake filenames might not be retrieved by Python's linecache |
| 33 |
which is why we keep a duplicate of anything we add to linecache.cache |
| 34 |
""" |
| 35 |
|
| 36 |
|
| 37 |
filename = str(filename) |
| 38 |
self.remove(filename) |
| 39 |
lines = [line + "\n" for line in source.splitlines()] |
| 40 |
entry = (len(source), time.time(), lines, filename) |
| 41 |
|
| 42 |
linecache.cache[filename] = entry |
| 43 |
self.local_cache[filename] = lines |
| 44 |
|
| 45 |
def remove(self, filename: str) -> None: |
| 46 |
"""Removes an entry from the cache if it can be found.""" |
| 47 |
if filename in self.local_cache: |
| 48 |
del self.local_cache[filename] |
| 49 |
if filename in linecache.cache: |
| 50 |
del linecache.cache[filename] |
| 51 |
|
| 52 |
stack_data.Source._class_local("__source_cache", {}).pop(filename, None) |
| 53 |
|
| 54 |
def get_source_lines( |
| 55 |
self, filename: str, module_globals: Optional[Dict[str, Any]] = None |
| 56 |
) -> List[str]: |
| 57 |
"""Given a filename, returns the corresponding source, either |
| 58 |
from the cache or from actually opening the file. |
| 59 |
|
| 60 |
If the filename corresponds to a true file, and the last time |
| 61 |
it was modified differs from the recorded value, a fresh copy |
| 62 |
is retrieved. |
| 63 |
|
| 64 |
The contents is stored as a string and returned as a list of lines, |
| 65 |
each line ending with a newline character. |
| 66 |
""" |
| 67 |
lines = old_getlines(filename, module_globals=module_globals) |
| 68 |
if not lines and filename in self.local_cache: |
| 69 |
lines = self.local_cache[filename] |
| 70 |
if not lines: |
| 71 |
lines = [] |
| 72 |
|
| 73 |
|
| 74 |
return lines + ["\n"] |
| 75 |
|
| 76 |
|
| 77 |
cache = Cache() |
| 78 |
|
| 79 |
|
| 80 |
linecache.getlines = cache.get_source_lines |
| 81 |
|
| 82 |
|
| 83 |
def _counter() -> Generator[int, None, None]: |
| 84 |
num = 0 |
| 85 |
while True: |
| 86 |
yield num |
| 87 |
num += 1 |
| 88 |
|
| 89 |
|
| 90 |
counter = _counter() |
| 91 |
|
| 92 |
|
| 93 |
def friendly_exec( |
| 94 |
source: Any, |
| 95 |
globals_: Optional[Dict[str, None]] = None, |
| 96 |
locals_: Optional[Dict[str, None]] = None, |
| 97 |
) -> None: |
| 98 |
"""A version of exec that uses a different filename each time |
| 99 |
instead of the Python default '<string>', and caches the source. |
| 100 |
This makes it possible to provide more help on code executed via 'exec'. |
| 101 |
""" |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
frame = inspect.getouterframes(inspect.currentframe())[1].frame |
| 111 |
true_globals = frame.f_globals |
| 112 |
true_locals = frame.f_locals |
| 113 |
if globals_ is None: |
| 114 |
globals_ = true_globals |
| 115 |
if locals_ is None: |
| 116 |
locals_ = true_locals |
| 117 |
else: |
| 118 |
if locals_ is None: |
| 119 |
locals_ = true_globals |
| 120 |
|
| 121 |
|
| 122 |
if not isinstance(source, str): |
| 123 |
return exec(source, globals_, locals_) |
| 124 |
|
| 125 |
filename = "<friendly-exec-%d>" % next(counter) |
| 126 |
cache.add(filename, source) |
| 127 |
code = compile(source, filename, "exec") |
| 128 |
return exec(code, globals_, locals_) |