1313from typing import TYPE_CHECKING , Any , NoReturn
1414
1515from coverage import env
16+ from coverage .debug import NoDebugging , DevNullDebug
1617from coverage .exceptions import ConfigError , CoverageException
1718
1819if 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
4648def _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
106104def _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