@@ -32,7 +32,10 @@ def _bootstrap(self, *args, **kwargs): # pylint: disable=signature-diff
32
32
"""Wrapper around _bootstrap to start coverage."""
33
33
try :
34
34
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
+ )
36
39
cov ._warn_preimported_source = False
37
40
cov .start ()
38
41
debug = cov ._debug
@@ -55,18 +58,19 @@ def _bootstrap(self, *args, **kwargs): # pylint: disable=signature-diff
55
58
56
59
class Stowaway (object ):
57
60
"""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 ):
59
62
self .rcfile = rcfile
63
+ self .static_context = static_context
60
64
61
65
def __getstate__ (self ):
62
- return {'rcfile' : self .rcfile }
66
+ return {'rcfile' : self .rcfile , "static_context" : self . static_context }
63
67
64
68
def __setstate__ (self , state ):
65
- patch_multiprocessing (state ['rcfile' ])
69
+ patch_multiprocessing (state ['rcfile' ], state [ "static_context" ] )
66
70
67
71
68
- @contract (rcfile = str )
69
- def patch_multiprocessing (rcfile ):
72
+ @contract (rcfile = str , static_context = str )
73
+ def patch_multiprocessing (rcfile , static_context ):
70
74
"""Monkey-patch the multiprocessing module.
71
75
72
76
This enables coverage measurement of processes started by multiprocessing.
@@ -87,6 +91,7 @@ def patch_multiprocessing(rcfile):
87
91
# Set the value in ProcessWithCoverage that will be pickled into the child
88
92
# process.
89
93
os .environ ["COVERAGE_RCFILE" ] = os .path .abspath (rcfile )
94
+ os .environ ["COVERAGE_STATIC_CONTEXT" ] = static_context
90
95
91
96
# When spawning processes rather than forking them, we have no state in the
92
97
# new process. We sneak in there with a Stowaway: we stuff one of our own
@@ -103,7 +108,7 @@ def patch_multiprocessing(rcfile):
103
108
def get_preparation_data_with_stowaway (name ):
104
109
"""Get the original preparation data, and also insert our stowaway."""
105
110
d = original_get_preparation_data (name )
106
- d ['stowaway' ] = Stowaway (rcfile )
111
+ d ['stowaway' ] = Stowaway (rcfile , static_context )
107
112
return d
108
113
109
114
spawn .get_preparation_data = get_preparation_data_with_stowaway
0 commit comments