Skip to content

Commit

Permalink
wip: no stack, works better
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Nov 5, 2023
1 parent b435d1e commit 19cbf8a
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 17 deletions.
15 changes: 12 additions & 3 deletions coverage/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from coverage.disposition import FileDisposition
from coverage.exceptions import ConfigError
from coverage.misc import human_sorted_items, isolate_module
from coverage.pep669_tracer import Pep669Tracer
from coverage.plugin import CoveragePlugin
from coverage.pytracer import PyTracer
from coverage.types import (
Expand Down Expand Up @@ -144,17 +145,24 @@ def __init__(
if HAS_CTRACER and not timid:
use_ctracer = True

#if HAS_CTRACER and self._trace_class is CTracer:
if use_ctracer:
if env.PYBEHAVIOR.pep669 and self.should_start_context is None:
self._trace_class = Pep669Tracer
self.file_disposition_class = FileDisposition
self.supports_plugins = False
self.packed_arcs = False
self.systrace = False
elif use_ctracer:
self._trace_class = CTracer
self.file_disposition_class = CFileDisposition
self.supports_plugins = True
self.packed_arcs = True
self.systrace = True
else:
self._trace_class = PyTracer
self.file_disposition_class = FileDisposition
self.supports_plugins = False
self.packed_arcs = False
self.systrace = True

# We can handle a few concurrency options here, but only one at a time.
concurrencies = set(self.concurrency)
Expand Down Expand Up @@ -275,6 +283,7 @@ def reset(self) -> None:

def _start_tracer(self) -> TTraceFn:
"""Start a new Tracer object, and store it in self.tracers."""
# TODO: for pep669, this doesn't return a TTraceFn
tracer = self._trace_class()
tracer.data = self.data
tracer.trace_arcs = self.branch
Expand Down Expand Up @@ -344,7 +353,7 @@ def start(self) -> None:

# Install our installation tracer in threading, to jump-start other
# threads.
if self.threading:
if self.systrace and self.threading:
self.threading.settrace(self._installation_trace)

def stop(self) -> None:
Expand Down
13 changes: 7 additions & 6 deletions coverage/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def exc_one_line(exc: Exception) -> str:
return "|".join(l.rstrip() for l in lines)


def short_stack(limit: Optional[int] = None, skip: int = 0) -> str:
def short_stack(limit: Optional[int] = None, skip: int = 0, full: bool = False) -> str:
"""Return a string summarizing the call stack.
The string is multi-line, with one line per stack frame. Each line shows
Expand Down Expand Up @@ -209,11 +209,12 @@ def short_stack(limit: Optional[int] = None, skip: int = 0) -> str:
]

stack: Iterable[inspect.FrameInfo] = inspect.stack()[limit:skip:-1]
for pat in BORING_PRELUDE:
stack = itertools.dropwhile(
(lambda fi, pat=pat: re.search(pat, fi.filename)), # type: ignore[misc]
stack
)
if not full:
for pat in BORING_PRELUDE:
stack = itertools.dropwhile(
(lambda fi, pat=pat: re.search(pat, fi.filename)), # type: ignore[misc]
stack
)
return "\n".join(f"{fi.function:>30s} : {fi.filename}:{fi.lineno}" for fi in stack)


Expand Down
4 changes: 4 additions & 0 deletions coverage/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ class PYBEHAVIOR:
# Changed in https://github.com/python/cpython/pull/101441
comprehensions_are_functions = (PYVERSION <= (3, 12, 0, "alpha", 7, 0))

# PEP669 Low Impact Monitoring: https://peps.python.org/pep-0669/
pep669 = bool(getattr(sys, "monitoring", None))


# Coverage.py specifics.

# Are we using the C-implemented trace function?
Expand Down
Loading

0 comments on commit 19cbf8a

Please sign in to comment.