summaryrefslogtreecommitdiff
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
commit142760d61d30328f091636ba1fe9e3957685e0b6 (patch)
treeee2955a04c9a05e2b945ece6afc2e9deb42086a6
parentd59824c976c876fe00b8b05223041fdf4b9c43db (diff)
downloadpython-coveragepy-142760d61d30328f091636ba1fe9e3957685e0b6.tar.gz
Make process_startup idempotent, to fix #340.
-rw-r--r--.hgignore3
-rw-r--r--CHANGES.txt5
-rw-r--r--coverage/control.py29
3 files changed, 30 insertions, 7 deletions
diff --git a/.hgignore b/.hgignore
index 4105847..c13de37 100644
--- a/.hgignore
+++ b/.hgignore
@@ -23,8 +23,7 @@ dist
htmlcov
MANIFEST
setuptools-*.egg
-.tox
-.tox_kits
+.tox*
.noseids
# Stuff in the test directory.
diff --git a/CHANGES.txt b/CHANGES.txt
index c0166bb..d51d34c 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -14,7 +14,12 @@ Latest
- A new warning is possible, if a desired file isn't measured because it was
imported before coverage was started (`issue 353`_).
+- The `coverage.process_startup` function now will start coverage measurement
+ only once, no matter how many times it is called. This fixes problems due
+ to unusual virtualenv configurations (`issue 340`_).
+
.. _issue 117: https://bitbucket.org/ned/coveragepy/issue/117/enable-coverage-measurement-of-code-run-by
+.. _issue 340: https://bitbucket.org/ned/coveragepy/issue/340/keyerror-subpy
.. _issue 353: https://bitbucket.org/ned/coveragepy/issue/353/40a3-introduces-an-unexpected-third-case
diff --git a/coverage/control.py b/coverage/control.py
index f422d7c..81653d3 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.