diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2015-02-01 17:38:26 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2015-02-01 17:38:26 -0500 |
commit | 5499a31ed1700a53f0ef8ca4713acae3fc81db34 (patch) | |
tree | 620100a0c6e00d871b0c36f74f4ac213ac104deb | |
parent | 33b358159048d8be3d8f7e2ba2b1435402a412bd (diff) | |
download | python-coveragepy-git-5499a31ed1700a53f0ef8ca4713acae3fc81db34.tar.gz |
Make process_startup idempotent, to fix #340.
-rw-r--r-- | .hgignore | 3 | ||||
-rw-r--r-- | CHANGES.txt | 5 | ||||
-rw-r--r-- | coverage/control.py | 29 |
3 files changed, 30 insertions, 7 deletions
@@ -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 c0166bb8..d51d34c3 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 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. |