[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2024-04-13 00:00:18 +00:00
parent 72ad6dc953
commit f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions

View file

@ -1,29 +1,25 @@
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
"""Monkey-patching to add multiprocessing support for coverage.py"""
from __future__ import annotations
import multiprocessing
import multiprocessing.process
import os
import os.path
import sys
import traceback
from typing import Any
from coverage.debug import DebugControl
# An attribute that will be set on the module to indicate that it has been
# monkey-patched.
PATCHED_MARKER = "_coverage$patched"
PATCHED_MARKER = '_coverage$patched'
OriginalProcess = multiprocessing.process.BaseProcess
original_bootstrap = OriginalProcess._bootstrap # type: ignore[attr-defined]
class ProcessWithCoverage(OriginalProcess): # pylint: disable=abstract-method
"""A replacement for multiprocess.Process that starts coverage."""
@ -37,12 +33,12 @@ class ProcessWithCoverage(OriginalProcess): # pylint: disable=abstract-m
cov.start()
_debug = cov._debug
assert _debug is not None
if _debug.should("multiproc"):
if _debug.should('multiproc'):
debug = _debug
if debug:
debug.write("Calling multiprocessing bootstrap")
debug.write('Calling multiprocessing bootstrap')
except Exception:
print("Exception during multiprocessing bootstrap init:", file=sys.stderr)
print('Exception during multiprocessing bootstrap init:', file=sys.stderr)
traceback.print_exc(file=sys.stderr)
sys.stderr.flush()
raise
@ -50,27 +46,29 @@ class ProcessWithCoverage(OriginalProcess): # pylint: disable=abstract-m
return original_bootstrap(self, *args, **kwargs)
finally:
if debug:
debug.write("Finished multiprocessing bootstrap")
debug.write('Finished multiprocessing bootstrap')
try:
cov.stop()
cov.save()
except Exception as exc:
if debug:
debug.write("Exception during multiprocessing bootstrap cleanup", exc=exc)
debug.write('Exception during multiprocessing bootstrap cleanup', exc=exc)
raise
if debug:
debug.write("Saved multiprocessing data")
debug.write('Saved multiprocessing data')
class Stowaway:
"""An object to pickle, so when it is unpickled, it can apply the monkey-patch."""
def __init__(self, rcfile: str) -> None:
self.rcfile = rcfile
def __getstate__(self) -> dict[str, str]:
return {"rcfile": self.rcfile}
return {'rcfile': self.rcfile}
def __setstate__(self, state: dict[str, str]) -> None:
patch_multiprocessing(state["rcfile"])
patch_multiprocessing(state['rcfile'])
def patch_multiprocessing(rcfile: str) -> None:
@ -90,7 +88,7 @@ def patch_multiprocessing(rcfile: str) -> None:
# Set the value in ProcessWithCoverage that will be pickled into the child
# process.
os.environ["COVERAGE_RCFILE"] = os.path.abspath(rcfile)
os.environ['COVERAGE_RCFILE'] = os.path.abspath(rcfile)
# When spawning processes rather than forking them, we have no state in the
# new process. We sneak in there with a Stowaway: we stuff one of our own
@ -107,7 +105,7 @@ def patch_multiprocessing(rcfile: str) -> None:
def get_preparation_data_with_stowaway(name: str) -> dict[str, Any]:
"""Get the original preparation data, and also insert our stowaway."""
d = original_get_preparation_data(name)
d["stowaway"] = Stowaway(rcfile)
d['stowaway'] = Stowaway(rcfile)
return d
spawn.get_preparation_data = get_preparation_data_with_stowaway