summaryrefslogtreecommitdiff
path: root/coverage/control.py
diff options
context:
space:
mode:
Diffstat (limited to 'coverage/control.py')
-rw-r--r--coverage/control.py73
1 files changed, 37 insertions, 36 deletions
diff --git a/coverage/control.py b/coverage/control.py
index 7efc3492..15bbe982 100644
--- a/coverage/control.py
+++ b/coverage/control.py
@@ -17,9 +17,9 @@ class coverage(object):
"""Programmatic access to Coverage.
To use::
-
+
from coverage import coverage
-
+
cov = coverage()
cov.start()
#.. blah blah (run your code) blah blah ..
@@ -30,39 +30,39 @@ class coverage(object):
def __init__(self, data_file=None, data_suffix=False, cover_pylib=False,
auto_data=False, timid=False, branch=False):
- """
+ """
`data_file` is the base name of the data file to use, defaulting to
".coverage". `data_suffix` is appended to `data_file` to create the
final file name. If `data_suffix` is simply True, then a suffix is
created with the machine and process identity included.
-
+
`cover_pylib` is a boolean determining whether Python code installed
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.
-
+
If `timid` is true, then a slower and simpler trace function will be
used. This is important for some environments where manipulation of
tracing functions breaks the faster trace function.
-
+
If `branch` is true, then branch coverage will be measured in addition
to the usual statement coverage.
"""
from coverage import __version__
-
+
self.cover_pylib = cover_pylib
self.auto_data = auto_data
self.atexit_registered = False
self.exclude_re = ""
self.exclude_list = []
-
+
self.file_locator = FileLocator()
-
+
# Timidity: for nose users, read an environment variable. This is a
# cheap hack, since the rest of the command line arguments aren't
# recognized, but it solves some users' problems.
@@ -101,13 +101,13 @@ class coverage(object):
def _should_trace(self, filename, frame):
"""Decide whether to trace execution in `filename`
-
+
This function is called from the trace function. As each new file name
is encountered, this function determines whether it is traced or not.
-
+
Returns a canonicalized filename if it should be traced, False if it
should not.
-
+
"""
if filename == '<string>':
# There's no point in ever tracing string executions, we can't do
@@ -153,9 +153,9 @@ class coverage(object):
def use_cache(self, usecache):
"""Control the use of a data file (incorrectly called a cache).
-
+
`usecache` is true or false, whether to read and write data on disk.
-
+
"""
self.data.usefile(usecache)
@@ -163,7 +163,7 @@ class coverage(object):
"""Load previously-collected coverage data from the data file."""
self.collector.reset()
self.data.read()
-
+
def start(self):
"""Start measuring code coverage."""
if self.auto_data:
@@ -173,7 +173,7 @@ class coverage(object):
atexit.register(self.save)
self.atexit_registered = True
self.collector.start()
-
+
def stop(self):
"""Stop measuring code coverage."""
self.collector.stop()
@@ -181,10 +181,10 @@ class coverage(object):
def erase(self):
"""Erase previously-collected coverage data.
-
+
This removes the in-memory data collected in this session as well as
discarding the data file.
-
+
"""
self.collector.reset()
self.data.erase()
@@ -196,12 +196,12 @@ class coverage(object):
def exclude(self, regex):
"""Exclude source lines from execution consideration.
-
+
`regex` is a regular expression. Lines matching this expression are
not considered executable when reporting code coverage. A list of
regexes is maintained; this function adds a new regex to the list.
Matching any of the regexes excludes a source line.
-
+
"""
self.exclude_list.append(regex)
self.exclude_re = "(" + ")|(".join(self.exclude_list) + ")"
@@ -217,11 +217,11 @@ class coverage(object):
def combine(self):
"""Combine together a number of similarly-named coverage data files.
-
+
All coverage data files whose name starts with `data_file` (from the
coverage() constructor) will be read, and combined together into the
current measurements.
-
+
"""
self.data.combine_parallel_data()
@@ -239,14 +239,15 @@ class coverage(object):
def analysis2(self, morf):
"""Analyze a module.
-
+
`morf` is a module or a filename. It will be analyzed to determine
its coverage statistics. The return value is a 5-tuple:
-
+
* The filename for the module.
* A list of line numbers of executable statements.
* A list of line numbers of excluded statements.
- * A list of line numbers of statements not run (missing from execution).
+ * A list of line numbers of statements not run (missing from
+ execution).
* A readable formatted string of the missing line numbers.
The analysis uses the source file itself and the current measured
@@ -261,22 +262,22 @@ class coverage(object):
def _analyze(self, it):
"""Analyze a single morf or code unit.
-
+
Returns an `Analysis` object.
"""
if not isinstance(it, CodeUnit):
it = code_unit_factory(it, self.file_locator)[0]
-
+
return Analysis(self, it)
def report(self, morfs=None, show_missing=True, ignore_errors=False,
file=None, omit_prefixes=None): # pylint: disable-msg=W0622
"""Write a summary report to `file`.
-
+
Each module in `morfs` is listed, with counts of statements, executed
statements, missing statements, and a list of lines missed.
-
+
"""
reporter = SummaryReporter(self, show_missing, ignore_errors)
reporter.report(morfs, outfile=file, omit_prefixes=omit_prefixes)
@@ -284,12 +285,12 @@ class coverage(object):
def annotate(self, morfs=None, directory=None, ignore_errors=False,
omit_prefixes=None):
"""Annotate a list of modules.
-
+
Each module in `morfs` is annotated. The source is written to a new
file, named with a ",cover" suffix, with each line prefixed with a
marker to indicate the coverage of the line. Covered lines have ">",
excluded lines have "-", and missing lines have "!".
-
+
"""
reporter = AnnotateReporter(self, ignore_errors)
reporter.report(
@@ -298,7 +299,7 @@ class coverage(object):
def html_report(self, morfs=None, directory=None, ignore_errors=False,
omit_prefixes=None):
"""Generate an HTML report.
-
+
"""
reporter = HtmlReporter(self, ignore_errors)
reporter.report(
@@ -307,9 +308,9 @@ class coverage(object):
def xml_report(self, morfs=None, outfile=None, ignore_errors=False,
omit_prefixes=None):
"""Generate an XML report of coverage results.
-
+
The report is compatible with Cobertura reports.
-
+
"""
if outfile:
outfile = open(outfile, "w")
@@ -322,7 +323,7 @@ class coverage(object):
def sysinfo(self):
"""Return a list of key,value pairs showing internal information."""
-
+
import coverage as covmod
import platform, re, sys