From 8d65b505d832ba4d6e4ad8a612c69377303a1772 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Tue, 12 May 2009 21:12:09 -0400 Subject: Give the singleton module interface a way to keep the old behavior of auto-loading and -saving data as needed. --- CHANGES.txt | 6 ++++-- coverage/__init__.py | 2 +- coverage/control.py | 14 +++++++++++++- test/test_coverage.py | 3 +-- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 76fcb3cf..be8505eb 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -17,8 +17,10 @@ Version 3.0b3 installed after compiling are now located correctly. Thanks, Detlev Offenbach. -- Removed the automatic saving of data on process exit. If you needed this, - please get in touch with me so I can understand why! +- When using the object api (that is, constructing a coverage() object), data + is no longer saved automatically on process exit. If you needed this, + please get in touch with me so I can understand why! The module-level + interface still uses automatic saving. Version 3.0b2, 30 April 2009 diff --git a/coverage/__init__.py b/coverage/__init__.py index c787b341..bce40406 100644 --- a/coverage/__init__.py +++ b/coverage/__init__.py @@ -33,7 +33,7 @@ def _singleton_method(name): """Singleton wrapper around a coverage method.""" global _the_coverage if not _the_coverage: - _the_coverage = coverage() + _the_coverage = coverage(auto_data=True) return getattr(_the_coverage, name)(*args, **kwargs) return wrapper diff --git a/coverage/control.py b/coverage/control.py index 32e6fad3..dab9eca4 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -14,7 +14,8 @@ class coverage: """Programmatic access to Coverage. """ - def __init__(self, data_file=None, data_suffix=False, cover_pylib=False): + def __init__(self, data_file=None, data_suffix=False, cover_pylib=False, + auto_data=False): """Create a new coverage measurement context. `data_file` is the base name of the data file to use, defaulting to @@ -26,11 +27,17 @@ class coverage: with the Python interpreter is measured. This includes the Python standard library and any packages installed with the interpreter. + If `auto_data` is true, then any existing data file will be read when + coverage measurement starts, and data will be saved automatically when + measurement stops. + """ from coverage.collector import Collector from coverage import __version__ self.cover_pylib = cover_pylib + self.auto_data = auto_data + self.exclude_re = "" self.exclude_list = [] @@ -105,6 +112,11 @@ class coverage: def start(self): """Start measuring code coverage.""" + if self.auto_data: + self.load() + # Save coverage data when Python exits. + import atexit + atexit.register(self.save) self.collector.start() def stop(self): diff --git a/test/test_coverage.py b/test/test_coverage.py index 8a10dce4..2c0aed4a 100644 --- a/test/test_coverage.py +++ b/test/test_coverage.py @@ -1552,8 +1552,7 @@ if sys.hexversion >= 0x020500f0: class ModuleTest(CoverageTest): def testNotSingleton(self): - """ You *can* create another coverage object. - """ + # You *can* create another coverage object. coverage.coverage() coverage.coverage() -- cgit v1.2.1