|
12 | 12 | import os.path |
13 | 13 | import sys |
14 | 14 | import threading |
| 15 | +import tokenize |
15 | 16 | import traceback |
16 | 17 | from collections.abc import Callable |
17 | 18 | from dataclasses import dataclass |
|
21 | 22 | from coverage import env |
22 | 23 | from coverage.bytecode import TBranchTrails, always_jumps, branch_trails, bytes_to_lines |
23 | 24 | from coverage.debug import short_filename, short_stack |
24 | | -from coverage.exceptions import NoSource, NotPython |
| 25 | +from coverage.exceptions import NoSource |
25 | 26 | from coverage.misc import isolate_module |
26 | | -from coverage.parser import PythonParser |
| 27 | +from coverage.parser import multiline_map_from_text |
| 28 | +from coverage.python import get_python_source |
27 | 29 | from coverage.types import ( |
28 | 30 | AnyCallable, |
29 | 31 | TFileDisposition, |
@@ -223,6 +225,9 @@ def __init__(self) -> None: |
223 | 225 | # Map filename:__name__ -> set(id(code_object)) |
224 | 226 | self.filename_code_ids: dict[str, set[int]] = collections.defaultdict(set) |
225 | 227 |
|
| 228 | + # Map filename -> multiline map, so each file is parsed at most once. |
| 229 | + self.multiline_maps: dict[str, dict[TLineNo, TLineNo]] = {} |
| 230 | + |
226 | 231 | self.sysmon_on = False |
227 | 232 | self.lock = threading.Lock() |
228 | 233 |
|
@@ -458,7 +463,7 @@ def sysmon_branch_either( |
458 | 463 | if not code_info.branch_trails: |
459 | 464 | if self.stats is not None: |
460 | 465 | self.stats["branch_trails"] += 1 |
461 | | - multiline_map = get_multiline_map(code.co_filename) |
| 466 | + multiline_map = self.get_multiline_map(code.co_filename) |
462 | 467 | code_info.branch_trails = branch_trails(code, multiline_map=multiline_map) |
463 | 468 | code_info.always_jumps = always_jumps(code) |
464 | 469 | # log(f"branch_trails for {code}:\n{ppformat(code_info.branch_trails)}") |
@@ -495,20 +500,26 @@ def sysmon_branch_either( |
495 | 500 |
|
496 | 501 | return DISABLE |
497 | 502 |
|
| 503 | + def get_multiline_map(self, filename: str) -> dict[TLineNo, TLineNo]: |
| 504 | + """Get the multiline map for `filename`, computing it at most once.""" |
| 505 | + multiline_map = self.multiline_maps.get(filename) |
| 506 | + if multiline_map is None: |
| 507 | + multiline_map = self.multiline_maps[filename] = compute_multiline_map(filename) |
| 508 | + return multiline_map |
| 509 | + |
498 | 510 |
|
499 | | -@functools.lru_cache(maxsize=20) |
500 | | -def get_multiline_map(filename: str) -> dict[TLineNo, TLineNo]: |
501 | | - """Get a PythonParser for the given filename, cached.""" |
| 511 | +def compute_multiline_map(filename: str) -> dict[TLineNo, TLineNo]: |
| 512 | + """Tokenize `filename` and return its multiline map.""" |
502 | 513 | try: |
503 | | - parser = PythonParser(filename=filename) |
504 | | - parser.parse_source() |
505 | | - except NotPython: |
| 514 | + text = get_python_source(filename) |
| 515 | + except (OSError, NoSource): |
| 516 | + # This can happen if open() in python.py fails. |
| 517 | + return {} |
| 518 | + try: |
| 519 | + return multiline_map_from_text(text) |
| 520 | + except (tokenize.TokenError, IndentationError, SyntaxError): |
506 | 521 | # The file was not Python. This can happen when the code object refers |
507 | 522 | # to an original non-Python source file, like a Jinja template. |
508 | 523 | # In that case, just return an empty map, which might lead to slightly |
509 | 524 | # wrong branch coverage, but we don't have any better option. |
510 | 525 | return {} |
511 | | - except NoSource: |
512 | | - # This can happen if open() in python.py fails. |
513 | | - return {} |
514 | | - return parser.multiline_map |
|
0 commit comments