Skip to content

Commit e92571b

Browse files
committed
Carry static context information into subprocesses
Fixes nedbat#1079
1 parent 7ff93a9 commit e92571b

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

coverage/control.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,10 @@ def _init_for_start(self):
434434
raise CoverageException( # pragma: only jython
435435
"multiprocessing is not supported on this Python"
436436
)
437-
patch_multiprocessing(rcfile=self.config.config_file)
437+
patch_multiprocessing(
438+
rcfile=self.config.config_file,
439+
static_context=self.config.context or ""
440+
)
438441

439442
dycon = self.config.dynamic_context
440443
if not dycon or dycon == "none":

coverage/multiproc.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ def _bootstrap(self, *args, **kwargs): # pylint: disable=signature-diff
3232
"""Wrapper around _bootstrap to start coverage."""
3333
try:
3434
from coverage import Coverage # avoid circular import
35-
cov = Coverage(data_suffix=True)
35+
cov = Coverage(
36+
data_suffix=True,
37+
context=os.environ.get("COVERAGE_STATIC_CONTEXT") or None
38+
)
3639
cov._warn_preimported_source = False
3740
cov.start()
3841
debug = cov._debug
@@ -55,18 +58,19 @@ def _bootstrap(self, *args, **kwargs): # pylint: disable=signature-diff
5558

5659
class Stowaway(object):
5760
"""An object to pickle, so when it is unpickled, it can apply the monkey-patch."""
58-
def __init__(self, rcfile):
61+
def __init__(self, rcfile, static_context):
5962
self.rcfile = rcfile
63+
self.static_context = static_context
6064

6165
def __getstate__(self):
62-
return {'rcfile': self.rcfile}
66+
return {'rcfile': self.rcfile, "static_context": self.static_context}
6367

6468
def __setstate__(self, state):
65-
patch_multiprocessing(state['rcfile'])
69+
patch_multiprocessing(state['rcfile'], state["static_context"])
6670

6771

68-
@contract(rcfile=str)
69-
def patch_multiprocessing(rcfile):
72+
@contract(rcfile=str, static_context=str)
73+
def patch_multiprocessing(rcfile, static_context):
7074
"""Monkey-patch the multiprocessing module.
7175
7276
This enables coverage measurement of processes started by multiprocessing.
@@ -87,6 +91,7 @@ def patch_multiprocessing(rcfile):
8791
# Set the value in ProcessWithCoverage that will be pickled into the child
8892
# process.
8993
os.environ["COVERAGE_RCFILE"] = os.path.abspath(rcfile)
94+
os.environ["COVERAGE_STATIC_CONTEXT"] = static_context
9095

9196
# When spawning processes rather than forking them, we have no state in the
9297
# new process. We sneak in there with a Stowaway: we stuff one of our own
@@ -103,7 +108,7 @@ def patch_multiprocessing(rcfile):
103108
def get_preparation_data_with_stowaway(name):
104109
"""Get the original preparation data, and also insert our stowaway."""
105110
d = original_get_preparation_data(name)
106-
d['stowaway'] = Stowaway(rcfile)
111+
d['stowaway'] = Stowaway(rcfile, static_context)
107112
return d
108113

109114
spawn.get_preparation_data = get_preparation_data_with_stowaway

0 commit comments

Comments
 (0)