Skip to content

Commit 264bbd3

Browse files
committed
refactor: more patch logging
1 parent 3ecdfaf commit 264bbd3

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

coverage/debug.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class NoDebugging(DebugControl):
115115
"""A replacement for DebugControl that will never try to do anything."""
116116
def __init__(self) -> None:
117117
# pylint: disable=super-init-not-called
118-
...
118+
pass
119119

120120
def should(self, option: str) -> bool:
121121
"""Should we write debug messages? Never."""
@@ -131,6 +131,12 @@ def write(self, msg: str, *, exc: BaseException | None = None) -> None:
131131
raise AssertionError("NoDebugging.write should never be called.")
132132

133133

134+
class DevNullDebug(NoDebugging):
135+
"""A DebugControl that won't write anywhere."""
136+
def write(self, msg: str, *, exc: BaseException | None = None) -> None:
137+
pass
138+
139+
134140
def info_header(label: str) -> str:
135141
"""Make a nice header string."""
136142
return "--{:-<60s}".format(" "+label+" ")

coverage/patch.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from typing import TYPE_CHECKING, Any, NoReturn
1414

1515
from coverage import env
16+
from coverage.debug import NoDebugging, DevNullDebug
1617
from coverage.exceptions import ConfigError, CoverageException
1718

1819
if TYPE_CHECKING:
@@ -29,6 +30,7 @@ def apply_patches(
2930
make_pth_file: bool = True,
3031
) -> None:
3132
"""Apply invasive patches requested by `[run] patch=`."""
33+
debug = debug if debug.should("patch") else DevNullDebug()
3234
for patch in sorted(set(config.patch)):
3335
if patch == "_exit":
3436
_patch__exit(cov, debug)
@@ -45,15 +47,13 @@ def apply_patches(
4547

4648
def _patch__exit(cov: Coverage, debug: TDebugCtl) -> None:
4749
"""Patch os._exit."""
48-
if debug.should("patch"):
49-
debug.write("Patching _exit")
50+
debug.write("Patching _exit")
5051

5152
old_exit = os._exit
5253

5354
def coverage_os_exit_patch(status: int) -> NoReturn:
5455
with contextlib.suppress(Exception):
55-
if debug.should("patch"):
56-
debug.write("Using _exit patch")
56+
debug.write("Using _exit patch")
5757
with contextlib.suppress(Exception):
5858
cov.save()
5959
old_exit(status)
@@ -66,14 +66,12 @@ def _patch_execv(cov: Coverage, config: CoverageConfig, debug: TDebugCtl) -> Non
6666
if env.WINDOWS:
6767
raise CoverageException("patch=execv isn't supported yet on Windows.")
6868

69-
if debug.should("patch"):
70-
debug.write("Patching execv")
69+
debug.write("Patching execv")
7170

7271
def make_execv_patch(fname: str, old_execv: Any) -> Any:
7372
def coverage_execv_patch(*args: Any, **kwargs: Any) -> Any:
7473
with contextlib.suppress(Exception):
75-
if debug.should("patch"):
76-
debug.write(f"Using execv patch for {fname}")
74+
debug.write(f"Using execv patch for {fname}")
7775
with contextlib.suppress(Exception):
7876
cov.save()
7977

@@ -105,13 +103,13 @@ def coverage_execv_patch(*args: Any, **kwargs: Any) -> Any:
105103

106104
def _patch_subprocess(config: CoverageConfig, debug: TDebugCtl, make_pth_file: bool) -> None:
107105
"""Write .pth files and set environment vars to measure subprocesses."""
108-
if debug.should("patch"):
109-
debug.write("Patching subprocess")
106+
debug.write("Patching subprocess")
110107

111108
if make_pth_file:
112-
pth_files = create_pth_files()
109+
pth_files = create_pth_files(debug)
113110
def delete_pth_files() -> None:
114111
for p in pth_files:
112+
debug.write(f"Deleting subprocess .pth file: {str(p)!r}")
115113
p.unlink(missing_ok=True)
116114
atexit.register(delete_pth_files)
117115
assert config.config_file is not None
@@ -133,14 +131,17 @@ def delete_pth_files() -> None:
133131
coverage.process_startup()
134132
"""
135133

136-
def create_pth_files() -> list[Path]:
134+
PTH_TEXT = f"import sys; exec({PTH_CODE!r})"
135+
136+
def create_pth_files(debug: TDebugCtl = NoDebugging()) -> list[Path]:
137137
"""Create .pth files for measuring subprocesses."""
138-
pth_text = rf"import sys; exec({PTH_CODE!r})"
139138
pth_files = []
140139
for pth_dir in site.getsitepackages():
141140
pth_file = Path(pth_dir) / f"subcover_{os.getpid()}.pth"
142141
try:
143-
pth_file.write_text(pth_text, encoding="utf-8")
142+
if debug.should("patch"):
143+
debug.write(f"Writing subprocess .pth file: {str(pth_file)!r}")
144+
pth_file.write_text(PTH_TEXT, encoding="utf-8")
144145
except OSError: # pragma: cant happen
145146
continue
146147
else:

0 commit comments

Comments
 (0)