11# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
22# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
33
4- """Invasive patches for coverage.py"""
4+ """Invasive patches for coverage.py. """
55
66from __future__ import annotations
77
88import atexit
9+ import contextlib
910import os
1011import site
1112from pathlib import Path
1718if TYPE_CHECKING :
1819 from coverage import Coverage
1920 from coverage .config import CoverageConfig
21+ from coverage .types import TDebugCtl
2022
2123
22- def apply_patches (cov : Coverage , config : CoverageConfig , * , make_pth_file : bool = True ) -> None :
24+ def apply_patches (
25+ cov : Coverage ,
26+ config : CoverageConfig ,
27+ debug : TDebugCtl ,
28+ * ,
29+ make_pth_file : bool = True ,
30+ ) -> None :
2331 """Apply invasive patches requested by `[run] patch=`."""
2432
2533 for patch in sorted (set (config .patch )):
2634 if patch == "_exit" :
35+ if debug .should ("patch" ):
36+ debug .write ("Patching _exit" )
2737
2838 def make_exit_patch (
2939 old_exit : Callable [[int ], NoReturn ],
3040 ) -> Callable [[int ], NoReturn ]:
3141 def coverage_os_exit_patch (status : int ) -> NoReturn :
32- try :
42+ with contextlib .suppress (Exception ):
43+ if debug .should ("patch" ):
44+ debug .write ("Using _exit patch" )
45+ with contextlib .suppress (Exception ):
3346 cov .save ()
34- except : # pylint: disable=bare-except
35- pass
3647 old_exit (status )
3748
3849 return coverage_os_exit_patch
@@ -43,12 +54,16 @@ def coverage_os_exit_patch(status: int) -> NoReturn:
4354 if env .WINDOWS :
4455 raise CoverageException ("patch=execv isn't supported yet on Windows." )
4556
57+ if debug .should ("patch" ):
58+ debug .write ("Patching execv" )
59+
4660 def make_execv_patch (fname : str , old_execv : Any ) -> Any :
4761 def coverage_execv_patch (* args : Any , ** kwargs : Any ) -> Any :
48- try :
62+ with contextlib .suppress (Exception ):
63+ if debug .should ("patch" ):
64+ debug .write (f"Using execv patch for { fname } " )
65+ with contextlib .suppress (Exception ):
4966 cov .save ()
50- except : # pylint: disable=bare-except
51- pass
5267
5368 if fname .endswith ("e" ):
5469 # Assume the `env` argument is passed positionally.
@@ -62,9 +77,7 @@ def coverage_execv_patch(*args: Any, **kwargs: Any) -> Any:
6277 # When testing locally, we need to honor the pyc file location
6378 # or they get written to the .tox directories and pollute the
6479 # next run with a different core.
65- if (
66- cache_prefix := os .getenv ("PYTHONPYCACHEPREFIX" )
67- ) is not None :
80+ if (cache_prefix := os .getenv ("PYTHONPYCACHEPREFIX" )) is not None :
6881 new_env ["PYTHONPYCACHEPREFIX" ] = cache_prefix
6982
7083 # Without this, it fails on PyPy and Ubuntu.
@@ -78,6 +91,9 @@ def coverage_execv_patch(*args: Any, **kwargs: Any) -> Any:
7891 os .execve = make_execv_patch ("execve" , os .execve )
7992
8093 elif patch == "subprocess" :
94+ if debug .should ("patch" ):
95+ debug .write ("Patching subprocess" )
96+
8197 if make_pth_file :
8298 pth_files = create_pth_files ()
8399 def make_deleter (pth_files : list [Path ]) -> Callable [[], None ]:
0 commit comments