Cache the script with _linecache_, compile it and return the _locals_.
(
script: str,
filename: str,
globs: dict[str, Any] | None,
locals: Mapping[str, object] | None = None,
)
| 228 | |
| 229 | |
| 230 | def _linecache_and_compile( |
| 231 | script: str, |
| 232 | filename: str, |
| 233 | globs: dict[str, Any] | None, |
| 234 | locals: Mapping[str, object] | None = None, |
| 235 | ) -> dict[str, Any]: |
| 236 | """ |
| 237 | Cache the script with _linecache_, compile it and return the _locals_. |
| 238 | """ |
| 239 | |
| 240 | locs = {} if locals is None else locals |
| 241 | |
| 242 | # In order of debuggers like PDB being able to step through the code, |
| 243 | # we add a fake linecache entry. |
| 244 | count = 1 |
| 245 | base_filename = filename |
| 246 | while True: |
| 247 | linecache_tuple = ( |
| 248 | len(script), |
| 249 | None, |
| 250 | script.splitlines(True), |
| 251 | filename, |
| 252 | ) |
| 253 | old_val = linecache.cache.setdefault(filename, linecache_tuple) |
| 254 | if old_val == linecache_tuple: |
| 255 | break |
| 256 | |
| 257 | filename = f"{base_filename[:-1]}-{count}>" |
| 258 | count += 1 |
| 259 | |
| 260 | _compile_and_eval(script, globs, locs, filename) |
| 261 | |
| 262 | return locs |
| 263 | |
| 264 | |
| 265 | def _make_attr_tuple_class(cls_name: str, attr_names: list[str]) -> type: |
no test coverage detected