summaryrefslogtreecommitdiff
path: root/coverage/control.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2015-02-01 17:38:26 -0500
committerNed Batchelder <ned@nedbatchelder.com>2015-02-01 17:38:26 -0500
commit5499a31ed1700a53f0ef8ca4713acae3fc81db34 (patch)
tree620100a0c6e00d871b0c36f74f4ac213ac104deb /coverage/control.py
parent33b358159048d8be3d8f7e2ba2b1435402a412bd (diff)
downloadpython-coveragepy-git-5499a31ed1700a53f0ef8ca4713acae3fc81db34.tar.gz
Make process_startup idempotent, to fix #340.
Diffstat (limited to 'coverage/control.py')
-rw-r--r--coverage/control.py29
1 files changed, 24 insertions, 5 deletions
diff --git a/coverage/control.py b/coverage/control.py
index f422d7c0..81653d31 100644
--- a/coverage/control.py
+++ b/coverage/control.py
@@ -1062,11 +1062,30 @@ def process_startup():
"""
cps = os.environ.get("COVERAGE_PROCESS_START")
- if cps:
- cov = Coverage(config_file=cps, auto_data=True)
- cov.start()
- cov._warn_no_data = False
- cov._warn_unimported_source = False
+ if not cps:
+ # No request for coverage, nothing to do.
+ return
+
+ # This function can be called more than once in a process. This happens
+ # because some virtualenv configurations make the same directory visible
+ # twice in sys.path. This means that the .pth file will be found twice,
+ # and executed twice, executing this function twice. We set a global
+ # flag (an attribute on this function) to indicate that coverage has
+ # already been started, so we can avoid doing it twice.
+ #
+ # https://bitbucket.org/ned/coveragepy/issue/340/keyerror-subpy has more
+ # details.
+
+ if hasattr(process_startup, "done"):
+ # We've annotated this function before, so we must have already
+ # started coverage in this process. Nothing to do.
+ return
+
+ process_startup.done = True
+ cov = Coverage(config_file=cps, auto_data=True)
+ cov.start()
+ cov._warn_no_data = False
+ cov._warn_unimported_source = False
# A hack for debugging testing in sub-processes.