summaryrefslogtreecommitdiff
path: root/coverage
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2009-12-03 08:54:27 -0500
committerNed Batchelder <ned@nedbatchelder.com>2009-12-03 08:54:27 -0500
commit5f7375fb90aaf728ebf6c9da9a61b5302dcbd71b (patch)
tree60f4c9265e686d2685c1a10d4a4acdff04ef7f79 /coverage
parente0b2014fad18f284256c94422d940ceb84e8be24 (diff)
parent99a3e92e89c3e9d4a0dd73ae71b0b849d7fddbee (diff)
downloadpython-coveragepy-5f7375fb90aaf728ebf6c9da9a61b5302dcbd71b.tar.gz
Merged default onto config.
Diffstat (limited to 'coverage')
-rw-r--r--coverage/__init__.py4
-rw-r--r--coverage/annotate.py18
-rw-r--r--coverage/bytecode.py22
-rw-r--r--coverage/cmdline.py102
-rw-r--r--coverage/codeunit.py35
-rw-r--r--coverage/collector.py48
-rw-r--r--coverage/control.py81
-rw-r--r--coverage/data.py46
-rw-r--r--coverage/execfile.py6
-rw-r--r--coverage/files.py12
-rw-r--r--coverage/html.py36
-rw-r--r--coverage/htmlfiles/pyfile.html2
-rw-r--r--coverage/misc.py12
-rw-r--r--coverage/parser.py164
-rw-r--r--coverage/phystokens.py12
-rw-r--r--coverage/report.py16
-rw-r--r--coverage/results.py24
-rw-r--r--coverage/summary.py6
-rw-r--r--coverage/templite.py38
-rw-r--r--coverage/tracer.c42
-rw-r--r--coverage/xmlreport.py20
21 files changed, 388 insertions, 358 deletions
diff --git a/coverage/__init__.py b/coverage/__init__.py
index 9dea800..9b355ad 100644
--- a/coverage/__init__.py
+++ b/coverage/__init__.py
@@ -26,10 +26,10 @@ _the_coverage = None
def _singleton_method(name):
"""Return a function to the `name` method on a singleton `coverage` object.
-
+
The singleton object is created the first time one of these functions is
called.
-
+
"""
def wrapper(*args, **kwargs):
"""Singleton wrapper around a coverage method."""
diff --git a/coverage/annotate.py b/coverage/annotate.py
index 2fa9d5c..2117b65 100644
--- a/coverage/annotate.py
+++ b/coverage/annotate.py
@@ -6,11 +6,11 @@ from coverage.report import Reporter
class AnnotateReporter(Reporter):
"""Generate annotated source files showing line coverage.
-
+
This reporter creates annotated copies of the measured source files. Each
.py file is copied as a .py,cover file, with a left-hand margin annotating
each line::
-
+
> def h(x):
- if 0: #pragma: no cover
- pass
@@ -18,30 +18,30 @@ class AnnotateReporter(Reporter):
! a = 1
> else:
> a = 2
-
+
> h(2)
Executed lines use '>', lines not executed use '!', lines excluded from
consideration use '-'.
-
+
"""
def __init__(self, coverage, ignore_errors=False):
super(AnnotateReporter, self).__init__(coverage, ignore_errors)
self.directory = None
-
+
blank_re = re.compile(r"\s*(#|$)")
else_re = re.compile(r"\s*else\s*:\s*(#|$)")
def report(self, morfs, directory=None, omit_prefixes=None):
"""Run the report."""
self.report_files(self.annotate_file, morfs, directory, omit_prefixes)
-
+
def annotate_file(self, cu, analysis):
"""Annotate a single file.
-
+
`cu` is the CodeUnit for the file to annotate.
-
+
"""
if not cu.relative:
return
@@ -77,7 +77,7 @@ class AnnotateReporter(Reporter):
if self.blank_re.match(line):
dest.write(' ')
elif self.else_re.match(line):
- # Special logic for lines containing only 'else:'.
+ # Special logic for lines containing only 'else:'.
if i >= len(statements) and j >= len(missing):
dest.write('! ')
elif i >= len(statements) or j >= len(missing):
diff --git a/coverage/bytecode.py b/coverage/bytecode.py
index 62a19ba..ac28034 100644
--- a/coverage/bytecode.py
+++ b/coverage/bytecode.py
@@ -14,14 +14,14 @@ class ByteCode(object):
class ByteCodes(object):
"""Iterator over byte codes in `code`.
-
+
Returns `ByteCode` objects.
-
+
"""
def __init__(self, code):
self.code = code
self.offset = 0
-
+
if sys.hexversion > 0x03000000:
def __getitem__(self, i):
return self.code[i]
@@ -31,30 +31,30 @@ class ByteCodes(object):
def __iter__(self):
return self
-
+
def __next__(self):
if self.offset >= len(self.code):
raise StopIteration
-
+
bc = ByteCode()
bc.op = self[self.offset]
bc.offset = self.offset
-
+
next_offset = self.offset+1
if bc.op >= opcode.HAVE_ARGUMENT:
bc.arg = self[self.offset+1] + 256*self[self.offset+2]
next_offset += 2
-
+
label = -1
if bc.op in opcode.hasjrel:
label = next_offset + bc.arg
elif bc.op in opcode.hasjabs:
label = bc.arg
bc.jump_to = label
-
+
bc.next_offset = self.offset = next_offset
return bc
-
+
next = __next__ # Py2k uses an old-style non-dunder name.
@@ -62,10 +62,10 @@ class CodeObjects(object):
"""Iterate over all the code objects in `code`."""
def __init__(self, code):
self.stack = [code]
-
+
def __iter__(self):
return self
-
+
def __next__(self):
if self.stack:
# We're going to return the code object on the stack, but first
diff --git a/coverage/cmdline.py b/coverage/cmdline.py
index 938099e..fe8da54 100644
--- a/coverage/cmdline.py
+++ b/coverage/cmdline.py
@@ -1,6 +1,6 @@
"""Command-line support for Coverage."""
-import optparse, sys
+import optparse, re, sys
from coverage.execfile import run_python_file
from coverage.misc import CoverageException
@@ -8,10 +8,15 @@ from coverage.misc import CoverageException
class Opts(object):
"""A namespace class for individual options we'll build parsers from."""
-
+
+ append = optparse.Option(
+ '-a', '--append', action='store_false', dest="erase_first",
+ help="Append coverage data to .coverage, otherwise it is started "
+ "clean with each run."
+ )
branch = optparse.Option(
'', '--branch', action='store_true',
- help="Measure branch execution. HIGHLY EXPERIMENTAL!"
+ help="Measure branch coverage in addition to statement coverage."
)
directory = optparse.Option(
'-d', '--directory', action='store',
@@ -63,18 +68,18 @@ class Opts(object):
help="Use a simpler but slower trace method. Try this if you get "
"seemingly impossible results!"
)
- append = optparse.Option(
- '-a', '--append', action='store_false', dest="erase_first",
- help="Append coverage data to .coverage, otherwise it is started "
- "clean with each run."
+ version = optparse.Option(
+ '', '--version', action='store_true',
+ help="Display version information and exit."
)
-
+
+
class CoverageOptionParser(optparse.OptionParser, object):
"""Base OptionParser for coverage.
-
+
Problems don't exit the program.
Defaults are initialized for all options.
-
+
"""
def __init__(self, *args, **kwargs):
@@ -93,6 +98,7 @@ class CoverageOptionParser(optparse.OptionParser, object):
show_missing=None,
timid=None,
erase_first=None,
+ version=None,
)
self.disable_interspersed_args()
@@ -101,12 +107,12 @@ class CoverageOptionParser(optparse.OptionParser, object):
class OptionParserError(Exception):
"""Used to stop the optparse error handler ending the process."""
pass
-
+
def parse_args(self, args=None, options=None):
"""Call optparse.parse_args, but return a triple:
-
+
(ok, options, args)
-
+
"""
try:
options, args = \
@@ -114,7 +120,7 @@ class CoverageOptionParser(optparse.OptionParser, object):
except self.OptionParserError:
return False, None, None
return True, options, args
-
+
def error(self, msg):
"""Override optparse.error so sys.exit doesn't get called."""
self.help_fn(msg)
@@ -126,7 +132,7 @@ class ClassicOptionParser(CoverageOptionParser):
def __init__(self):
super(ClassicOptionParser, self).__init__()
-
+
self.add_action('-a', '--annotate', 'annotate')
self.add_action('-b', '--html', 'html')
self.add_action('-c', '--combine', 'combine')
@@ -143,6 +149,7 @@ class ClassicOptionParser(CoverageOptionParser):
Opts.old_omit,
Opts.parallel_mode,
Opts.timid,
+ Opts.version,
])
def add_action(self, dash, dashdash, action_code):
@@ -151,7 +158,7 @@ class ClassicOptionParser(CoverageOptionParser):
callback=self._append_action
)
option.action_code = action_code
-
+
def _append_action(self, option, opt_unused, value_unused, parser):
"""Callback for an option that adds to the `actions` list."""
parser.values.actions.append(option.action_code)
@@ -159,19 +166,19 @@ class ClassicOptionParser(CoverageOptionParser):
class CmdOptionParser(CoverageOptionParser):
"""Parse one of the new-style commands for coverage.py."""
-
+
def __init__(self, action, options=None, defaults=None, usage=None,
cmd=None, description=None
):
"""Create an OptionParser for a coverage command.
-
+
`action` is the slug to put into `options.actions`.
`options` is a list of Option's for the command.
`defaults` is a dict of default value for options.
`usage` is the usage string to display in help.
`cmd` is the command name, if different than `action`.
`description` is the description of the command, for the help text.
-
+
"""
if usage:
usage = "%prog " + usage
@@ -222,20 +229,20 @@ CMDS = {
"Each file gets its own page, with the source decorated to show "
"executed, excluded, and missed lines."
),
-
+
'combine': CmdOptionParser("combine", [Opts.help],
usage = " ",
description = "Combine data from multiple coverage files collected "
- "with 'run -p'. The combined results are stored into a single "
- "file representing the union of the coverage."
+ "with 'run -p'. The combined results are written to a single "
+ "file representing the union of the data."
),
'debug': CmdOptionParser("debug", [Opts.help],
usage = "<topic>",
- description = "Display information the internals of coverage.py, "
+ description = "Display information on the internals of coverage.py, "
"for diagnosing problems. "
- "Topics are 'data' to show a summary of the data collected in "
- ".coverage, or 'sys' to show installation information."
+ "Topics are 'data' to show a summary of the collected data, "
+ "or 'sys' to show installation information."
),
'erase': CmdOptionParser("erase", [Opts.help],
@@ -251,7 +258,7 @@ CMDS = {
Opts.help,
],
usage = "[options] [modules]",
- description = "Report coverage stats on modules."
+ description = "Report coverage statistics on modules."
),
'run': CmdOptionParser("execute",
@@ -263,12 +270,12 @@ CMDS = {
Opts.timid,
Opts.help,
],
- defaults = {'erase_first':True},
+ defaults = {'erase_first': True},
cmd = "run",
usage = "[options] <pyfile> [program options]",
- description = "Run a python program, measuring code execution."
+ description = "Run a Python program, measuring code execution."
),
-
+
'xml': CmdOptionParser("xml",
[
Opts.ignore_errors,
@@ -286,9 +293,10 @@ CMDS = {
OK, ERR = 0, 1
+
class CoverageScript(object):
"""The command-line interface to Coverage."""
-
+
def __init__(self, _covpkg=None, _run_python_file=None, _help_fn=None):
# _covpkg is for dependency injection, so we can test this code.
if _covpkg:
@@ -296,13 +304,13 @@ class CoverageScript(object):
else:
import coverage
self.covpkg = coverage
-
+
# _run_python_file is for dependency injection also.
self.run_python_file = _run_python_file or run_python_file
-
+
# _help_fn is for dependency injection.
self.help_fn = _help_fn or self.help
-
+
self.coverage = None
def help(self, error=None, topic=None, parser=None):
@@ -315,7 +323,6 @@ class CoverageScript(object):
print(parser.format_help().strip())
else:
# Parse out the topic we want from HELP_TOPICS
- import re
topic_list = re.split("(?m)^=+ (\w+) =+$", HELP_TOPICS)
topics = dict(zip(topic_list[1::2], topic_list[2::2]))
help_msg = topics.get(topic, '').strip()
@@ -326,14 +333,14 @@ class CoverageScript(object):
def command_line(self, argv):
"""The bulk of the command line interface to Coverage.
-
+
`argv` is the argument list to process.
Returns 0 if all is well, 1 if something went wrong.
"""
# Collect the command-line options.
-
+
if not argv:
self.help_fn(topic='minimum_help')
return OK
@@ -363,6 +370,10 @@ class CoverageScript(object):
self.help_fn(parser=parser)
return OK
+ if options.version:
+ self.help_fn(topic='version')
+ return OK
+
if "help" in options.actions:
if args:
for a in args:
@@ -399,11 +410,11 @@ class CoverageScript(object):
if not args_allowed and args:
self.help_fn("Unexpected arguments: %s" % " ".join(args))
return ERR
-
+
if 'execute' in options.actions and not args:
self.help_fn("Nothing to do.")
return ERR
-
+
# Do something.
self.coverage = self.covpkg.coverage(
data_suffix = bool(options.parallel_mode),
@@ -472,7 +483,7 @@ class CoverageScript(object):
if options.omit:
omit = options.omit.split(',')
report_args['omit_prefixes'] = omit
-
+
if 'report' in options.actions:
self.coverage.report(
show_missing=options.show_missing, **report_args)
@@ -494,7 +505,7 @@ class CoverageScript(object):
HELP_TOPICS = r"""
== classic ====================================================================
-Coverage version %(__version__)s
+Coverage.py version %(__version__)s
Measure, collect, and report on code coverage in Python programs.
Usage:
@@ -540,14 +551,14 @@ Coverage data is saved in the file .coverage by default. Set the
COVERAGE_FILE environment variable to save it somewhere else.
== help =======================================================================
-Coverage version %(__version__)s
+Coverage.py, version %(__version__)s
Measure, collect, and report on code coverage in Python programs.
usage: coverage <command> [options] [args]
Commands:
annotate Annotate source files with execution information.
- combine Combine a number of data files.
+ combine Combine a number of data files.
erase Erase previously collected coverage data.
help Get help on using coverage.py.
html Create an HTML report.
@@ -562,14 +573,17 @@ For more information, see %(__url__)s
== minimum_help ===============================================================
Code coverage for Python. Use 'coverage help' for help.
+== version ====================================================================
+Coverage.py, version %(__version__)s. %(__url__)s
+
"""
def main():
"""The main entrypoint to Coverage.
-
+
This is installed as the script entrypoint.
-
+
"""
try:
status = CoverageScript().command_line(sys.argv[1:])
diff --git a/coverage/codeunit.py b/coverage/codeunit.py
index e310705..73baaa0 100644
--- a/coverage/codeunit.py
+++ b/coverage/codeunit.py
@@ -8,20 +8,20 @@ from coverage.misc import CoverageException
def code_unit_factory(morfs, file_locator, omit_prefixes=None):
"""Construct a list of CodeUnits from polymorphic inputs.
-
+
`morfs` is a module or a filename, or a list of same.
`file_locator` is a FileLocator that can help resolve filenames.
`omit_prefixes` is a list of prefixes. CodeUnits that match those prefixes
will be omitted from the list.
-
+
Returns a list of CodeUnit objects.
-
+
"""
# Be sure we have a list.
if not isinstance(morfs, (list, tuple)):
morfs = [morfs]
-
+
# On Windows, the shell doesn't expand wildcards. Do it here.
globbed = []
for morf in morfs:
@@ -32,8 +32,9 @@ def code_unit_factory(morfs, file_locator, omit_prefixes=None):
morfs = globbed
code_units = [CodeUnit(morf, file_locator) for morf in morfs]
-
+
if omit_prefixes:
+ assert not isinstance(omit_prefixes, string_class) # common mistake
prefixes = [file_locator.abs_file(p) for p in omit_prefixes]
filtered = []
for cu in code_units:
@@ -42,7 +43,7 @@ def code_unit_factory(morfs, file_locator, omit_prefixes=None):
break
else:
filtered.append(cu)
-
+
code_units = filtered
return code_units
@@ -50,13 +51,13 @@ def code_unit_factory(morfs, file_locator, omit_prefixes=None):
class CodeUnit(object):
"""Code unit: a filename or module.
-
+
Instance attributes:
-
+
`name` is a human-readable name for this code unit.
`filename` is the os path from which we can read the source.
`relative` is a boolean.
-
+
"""
def __init__(self, morf, file_locator):
@@ -91,34 +92,34 @@ class CodeUnit(object):
# Annoying comparison operators. Py3k wants __lt__ etc, and Py2k needs all
# of them defined.
-
+
def __lt__(self, other):
return self.name < other.name
-
+
def __le__(self, other):
return self.name <= other.name
def __eq__(self, other):
return self.name == other.name
-
+
def __ne__(self, other):
return self.name != other.name
def __gt__(self, other):
return self.name > other.name
-
+
def __ge__(self, other):
return self.name >= other.name
def flat_rootname(self):
"""A base for a flat filename to correspond to this code unit.
-
+
Useful for writing files about the code where you want all the files in
the same directory, but need to differentiate same-named files from
different directories.
-
+
For example, the file a/b/c.py might return 'a_b_c'
-
+
"""
if self.modname:
return self.modname.replace('.', '_')
@@ -136,7 +137,7 @@ class CodeUnit(object):
source = self.file_locator.get_zip_data(self.filename)
if source is not None:
return StringIO(source)
-
+
# Couldn't find source.
raise CoverageException(
"No source for code %r." % self.filename
diff --git a/coverage/collector.py b/coverage/collector.py
index 5bbd02e..29dddf6 100644
--- a/coverage/collector.py
+++ b/coverage/collector.py
@@ -12,7 +12,7 @@ except ImportError:
class PyTracer(object):
"""Python implementation of the raw data tracer."""
-
+
# Because of poor implementations of trace-function-manipulating tools,
# the Python trace function must be kept very simple. In particular, there
# must be only one function ever set as the trace function, both through
@@ -41,10 +41,10 @@ class PyTracer(object):
def _trace(self, frame, event, arg_unused):
"""The trace function passed to sys.settrace."""
-
+
#print "trace event: %s %r @%d" % (
# event, frame.f_code.co_filename, frame.f_lineno)
-
+
if self.last_exc_back:
if frame == self.last_exc_back:
# Someone forgot a return event.
@@ -52,7 +52,7 @@ class PyTracer(object):
self.cur_file_data[(self.last_line, -1)] = None
self.cur_file_data, self.last_line = self.data_stack.pop()
self.last_exc_back = None
-
+
if event == 'call':
# Entering a new function context. Decide if we should trace
# in this file.
@@ -85,7 +85,7 @@ class PyTracer(object):
#print "exc", self.last_line, frame.f_lineno
self.last_exc_back = frame.f_back
return self._trace
-
+
def start(self):
"""Start this Tracer."""
sys.settrace(self._trace)
@@ -102,18 +102,19 @@ class PyTracer(object):
class Collector(object):
"""Collects trace data.
- Creates a Tracer object for each thread, since they track stack information.
- Each Tracer points to the same shared data, contributing traced data points.
-
+ Creates a Tracer object for each thread, since they track stack
+ information. Each Tracer points to the same shared data, contributing
+ traced data points.
+
When the Collector is started, it creates a Tracer for the current thread,
and installs a function to create Tracers for each new thread started.
When the Collector is stopped, all active Tracers are stopped.
-
+
Threads started while the Collector is stopped will never have Tracers
associated with them.
-
+
"""
-
+
# The stack of active Collectors. Collectors are added here when started,
# and popped when stopped. Collectors on the stack are paused when not
# the top, and resumed when they become the top again.
@@ -121,20 +122,20 @@ class Collector(object):
def __init__(self, should_trace, timid, branch):
"""Create a collector.
-
+
`should_trace` is a function, taking a filename, and returning a
canonicalized filename, or False depending on whether the file should
be traced or not.
-
+
If `timid` is true, then a slower simpler trace function will be
used. This is important for some environments where manipulation of
tracing functions make the faster more sophisticated trace function not
operate properly.
-
+
If `branch` is true, then branches will be measured. This involves
collecting data on which statements followed each other (arcs). Use
`get_arc_data` to get the arc data.
-
+
"""
self.should_trace = should_trace
self.branch = branch
@@ -148,6 +149,9 @@ class Collector(object):
# trace function.
self._trace_class = Tracer or PyTracer
+ def __repr__(self):
+ return "<Collector at 0x%x>" % id(self)
+
def tracer_name(self):
"""Return the class name of the tracer we're using."""
return self._trace_class.__name__
@@ -157,7 +161,7 @@ class Collector(object):
# A dictionary mapping filenames to dicts with linenumber keys,
# or mapping filenames to dicts with linenumber pairs as keys.
self.data = {}
-
+
# A cache of the results from should_trace, the decision about whether
# to trace execution in a file. A dict of filename to (filename or
# False).
@@ -196,6 +200,7 @@ class Collector(object):
if self._collectors:
self._collectors[-1].pause()
self._collectors.append(self)
+ #print >>sys.stderr, "Started: %r" % self._collectors
# Install the tracer on this thread.
self._start_tracer()
# Install our installation tracer in threading, to jump start other
@@ -204,12 +209,13 @@ class Collector(object):
def stop(self):
"""Stop collecting trace information."""
+ #print >>sys.stderr, "Stopping: %r" % self._collectors
assert self._collectors
assert self._collectors[-1] is self
self.pause()
self.tracers = []
-
+
# Remove this Collector from the stack, and resume the one underneath
# (if any).
self._collectors.pop()
@@ -226,7 +232,7 @@ class Collector(object):
for k in sorted(stats.keys()):
print("%16s: %s" % (k, stats[k]))
threading.settrace(None)
-
+
def resume(self):
"""Resume tracing after a `pause`."""
for tracer in self.tracers:
@@ -235,9 +241,9 @@ class Collector(object):
def get_line_data(self):
"""Return the line data collected.
-
+
Data is { filename: { lineno: None, ...}, ...}
-
+
"""
if self.branch:
# If we were measuring branches, then we have to re-build the dict
@@ -254,7 +260,7 @@ class Collector(object):
def get_arc_data(self):
"""Return the arc data collected.
-
+
Data is { filename: { (l1, l2): None, ...}, ...}
Note that no data is collected or returned if the Collector wasn't
diff --git a/coverage/control.py b/coverage/control.py
index eb13445..39e3dfb 100644
--- a/coverage/control.py
+++ b/coverage/control.py
@@ -1,6 +1,6 @@
"""Core control stuff for Coverage."""
-import os, socket
+import atexit, os, socket
from coverage.annotate import AnnotateReporter
from coverage.backward import string_class
@@ -18,9 +18,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 ..
@@ -31,24 +31,24 @@ class coverage(object):
def __init__(self, data_file=None, data_suffix=False, cover_pylib=None,
auto_data=False, timid=None, branch=None, config_file=True):
- """
+ """
`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.
@@ -63,7 +63,7 @@ class coverage(object):
# Build our configuration from a number of sources:
# 1: defaults:
self.config = CoverageConfig()
-
+
# 2: from the coveragerc file:
if config_file:
if config_file is True:
@@ -83,12 +83,13 @@ class coverage(object):
)
self.auto_data = auto_data
-
+ self.atexit_registered = False
+
self.exclude_re = ""
self._compile_exclude()
self.file_locator = FileLocator()
-
+
self.collector = Collector(
self._should_trace, timid=self.config.timid,
branch=self.config.branch
@@ -121,13 +122,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
@@ -173,9 +174,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)
@@ -183,16 +184,17 @@ 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:
self.load()
# Save coverage data when Python exits.
- import atexit
- atexit.register(self.save)
+ if not self.atexit_registered:
+ atexit.register(self.save)
+ self.atexit_registered = True
self.collector.start()
-
+
def stop(self):
"""Stop measuring code coverage."""
self.collector.stop()
@@ -200,10 +202,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()
@@ -215,12 +217,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.config.exclude_list.append(regex)
self._compile_exclude()
@@ -240,11 +242,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()
@@ -262,14 +264,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
@@ -284,22 +287,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)
@@ -307,12 +310,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(
@@ -321,7 +324,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(
@@ -330,9 +333,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")
@@ -345,7 +348,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
diff --git a/coverage/data.py b/coverage/data.py
index 55ed7a3..bd14775 100644
--- a/coverage/data.py
+++ b/coverage/data.py
@@ -7,20 +7,20 @@ from coverage.backward import pickle, sorted # pylint: disable-msg=W0622
class CoverageData(object):
"""Manages collected coverage data, including file storage.
-
+
The data file format is a pickled dict, with these keys:
-
+
* collector: a string identifying the collecting software
* lines: a dict mapping filenames to sorted lists of line numbers
executed:
{ 'file1': [17,23,45], 'file2': [1,2,3], ... }
-
+
* arcs: a dict mapping filenames to sorted lists of line number pairs:
{ 'file1': [(17,23), (17,25), (25,26)], ... }
"""
-
+
# Name of the data file (unless environment variable is set).
filename_default = ".coverage"
@@ -29,9 +29,9 @@ class CoverageData(object):
def __init__(self, basename=None, suffix=None, collector=None):
"""Create a CoverageData.
-
+
`basename` is the name of the file to use for storing data.
-
+
`suffix` is a suffix to append to the base file name. This can be used
for multiple or parallel execution, so that many coverage data files
can exist simultaneously.
@@ -40,7 +40,7 @@ class CoverageData(object):
"""
self.collector = collector or 'unknown'
-
+
self.use_file = True
# Construct the filename that will be used for data file storage, if we
@@ -60,14 +60,14 @@ class CoverageData(object):
# }
#
self.lines = {}
-
+
# A map from canonical Python source file name to a dictionary with an
# entry for each pair of line numbers forming an arc:
#
# { filename: { (l1,l2): None, ... }, ...}
#
self.arcs = {}
-
+
def usefile(self, use_file=True):
"""Set whether or not to use a disk file for data."""
self.use_file = use_file
@@ -91,7 +91,7 @@ class CoverageData(object):
os.remove(self.filename)
self.lines = {}
self.arcs = {}
-
+
def line_data(self):
"""Return the map from filenames to lists of line numbers executed."""
return dict(
@@ -103,11 +103,11 @@ class CoverageData(object):
return dict(
[(f, sorted(amap.keys())) for f, amap in self.arcs.items()]
)
-
+
def write_file(self, filename):
"""Write the coverage data to `filename`."""
- # Create the file data.
+ # Create the file data.
data = {}
data['lines'] = self.line_data()
@@ -140,10 +140,10 @@ class CoverageData(object):
def _read_file(self, filename):
"""Return the stored coverage data from the given file.
-
+
Returns two values, suitable for assigning to `self.lines` and
`self.arcs`.
-
+
"""
lines = {}
arcs = {}
@@ -166,10 +166,10 @@ class CoverageData(object):
def combine_parallel_data(self):
"""Combine a number of data files together.
-
+
Treat `self.filename` as a file prefix, and combine the data from all
of the data files starting with that prefix.
-
+
"""
data_dir, local = os.path.split(self.filename)
for f in os.listdir(data_dir or '.'):
@@ -183,18 +183,18 @@ class CoverageData(object):
def add_line_data(self, line_data):
"""Add executed line data.
-
+
`line_data` is { filename: { lineno: None, ... }, ...}
-
+
"""
for filename, linenos in line_data.items():
self.lines.setdefault(filename, {}).update(linenos)
def add_arc_data(self, arc_data):
"""Add measured arc data.
-
+
`arc_data` is { filename: { (l1,l2): None, ... }, ...}
-
+
"""
for filename, arcs in arc_data.items():
self.arcs.setdefault(filename, {}).update(arcs)
@@ -205,7 +205,7 @@ class CoverageData(object):
def executed_lines(self, filename):
"""A map containing all the line numbers executed in `filename`.
-
+
If `filename` hasn't been collected at all (because it wasn't executed)
then return an empty map.
@@ -218,11 +218,11 @@ class CoverageData(object):
def summary(self, fullpath=False):
"""Return a dict summarizing the coverage data.
-
+
Keys are based on the filenames, and values are the number of executed
lines. If `fullpath` is true, then the keys are the full pathnames of
the files, otherwise they are the basenames of the files.
-
+
"""
summ = {}
if fullpath:
diff --git a/coverage/execfile.py b/coverage/execfile.py
index ddcfa14..15f0a5f 100644
--- a/coverage/execfile.py
+++ b/coverage/execfile.py
@@ -16,11 +16,11 @@ except KeyError:
def run_python_file(filename, args):
"""Run a python file as if it were the main program on the command line.
-
+
`filename` is the path to the file to execute, it need not be a .py file.
`args` is the argument array to present as sys.argv, including the first
element representing the file being executed.
-
+
"""
# Create a module to serve as __main__
old_main_mod = sys.modules['__main__']
@@ -44,7 +44,7 @@ def run_python_file(filename, args):
finally:
# Restore the old __main__
sys.modules['__main__'] = old_main_mod
-
+
# Restore the old argv and path
sys.argv = old_argv
sys.path[0] = old_path0
diff --git a/coverage/files.py b/coverage/files.py
index 400646c..ba228c2 100644
--- a/coverage/files.py
+++ b/coverage/files.py
@@ -18,18 +18,18 @@ class FileLocator(object):
def relative_filename(self, filename):
"""Return the relative form of `filename`.
-
+
The filename will be relative to the current directory when the
FileLocator was constructed.
-
+
"""
return filename.replace(self.relative_dir, "")
def canonical_filename(self, filename):
"""Return a canonical filename for `filename`.
-
+
An absolute path with no redundant components and normalized case.
-
+
"""
if filename not in self.canonical_filename_cache:
f = filename
@@ -48,11 +48,11 @@ class FileLocator(object):
def get_zip_data(self, filename):
"""Get data from `filename` if it is a zip file path.
-
+
Returns the string data read from the zip file, or None if no zip file
could be found or `filename` isn't in it. The data returned will be
an empty string if the file is empty.
-
+
"""
import zipimport
markers = ['.zip'+os.sep, '.egg'+os.sep]
diff --git a/coverage/html.py b/coverage/html.py
index 6df84a5..4d51eb3 100644
--- a/coverage/html.py
+++ b/coverage/html.py
@@ -18,29 +18,29 @@ def data_filename(fname):
def data(fname):
"""Return the contents of a data file of ours."""
return open(data_filename(fname)).read()
-
+
class HtmlReporter(Reporter):
"""HTML reporting."""
-
+
def __init__(self, coverage, ignore_errors=False):
super(HtmlReporter, self).__init__(coverage, ignore_errors)
self.directory = None
self.source_tmpl = Templite(data("htmlfiles/pyfile.html"), globals())
-
+
self.files = []
self.arcs = coverage.data.has_arcs()
def report(self, morfs, directory, omit_prefixes=None):
"""Generate an HTML report for `morfs`.
-
+
`morfs` is a list of modules or filenames. `directory` is where to put
the HTML files. `omit_prefixes` is a list of strings, prefixes of
modules to omit from the report.
-
+
"""
assert directory, "must provide a directory for html reporting"
-
+
# Process all the files.
self.report_files(self.html_file, morfs, directory, omit_prefixes)
@@ -59,10 +59,10 @@ class HtmlReporter(Reporter):
def html_file(self, cu, analysis):
"""Generate an HTML file for one source file."""
-
+
source = cu.source_file().read()
- nums = analysis.numbers
+ nums = analysis.numbers
missing_branch_arcs = analysis.missing_branch_arcs()
n_par = 0 # accumulated below.
@@ -75,12 +75,13 @@ class HtmlReporter(Reporter):
c_par = " par" + c_run
lines = []
-
+
for lineno, line in enumerate(source_token_lines(source)):
lineno += 1 # 1-based line numbers.
# Figure out how to mark this line.
line_class = ""
- annotate = ""
+ annotate_html = ""
+ annotate_title = ""
if lineno in analysis.statements:
line_class += " stm"
if lineno in analysis.excluded:
@@ -96,10 +97,14 @@ class HtmlReporter(Reporter):
annlines.append("exit")
else:
annlines.append(str(b))
- annotate = " ".join(annlines)
+ annotate_html = "&nbsp;&nbsp; ".join(annlines)
+ if len(annlines) > 1:
+ annotate_title = "no jumps to these line numbers"
+ elif len(annlines) == 1:
+ annotate_title = "no jump to this line number"
elif lineno in analysis.statements:
line_class += c_run
-
+
# Build the HTML for the line
html = ""
for tok_type, tok_text in line:
@@ -113,7 +118,8 @@ class HtmlReporter(Reporter):
'html': html,
'number': lineno,
'class': line_class.strip() or "pln",
- 'annotate': annotate,
+ 'annotate': annotate_html,
+ 'annotate_title': annotate_title,
})
# Write the HTML page for this file.
@@ -167,10 +173,10 @@ def format_pct(p):
def spaceless(html):
"""Squeeze out some annoying extra space from an HTML string.
-
+
Nicely-formatted templates mean lots of extra space in the result. Get
rid of some.
-
+
"""
html = re.sub(">\s+<p ", ">\n<p ", html)
return html
diff --git a/coverage/htmlfiles/pyfile.html b/coverage/htmlfiles/pyfile.html
index 566244f..62518ba 100644
--- a/coverage/htmlfiles/pyfile.html
+++ b/coverage/htmlfiles/pyfile.html
@@ -48,7 +48,7 @@ function toggle_lines(btn, cls) {
</td>
<td class='text' valign='top'>
{% for line in lines %}
- <p class='{{line.class}}'>{% if line.annotate %}<span class='annotate'>{{line.annotate}}</span>{% endif %}{{line.html}}<span class='strut'>&nbsp;</span></p>
+ <p class='{{line.class}}'>{% if line.annotate %}<span class='annotate' title='{{line.annotate_title}}'>{{line.annotate}}</span>{% endif %}{{line.html}}<span class='strut'>&nbsp;</span></p>
{% endfor %}
</td>
</tr>
diff --git a/coverage/misc.py b/coverage/misc.py
index aa61fc9..0e6bcf9 100644
--- a/coverage/misc.py
+++ b/coverage/misc.py
@@ -2,10 +2,10 @@
def nice_pair(pair):
"""Make a nice string representation of a pair of numbers.
-
+
If the numbers are equal, just return the number, otherwise return the pair
with a dash between them, indicating the range.
-
+
"""
start, end = pair
if start == end:
@@ -20,10 +20,10 @@ def format_lines(statements, lines):
Format a list of line numbers for printing by coalescing groups of lines as
long as the lines represent consecutive statements. This will coalesce
even if there are gaps between statements.
-
+
For example, if `statements` is [1,2,3,4,5,10,11,12,13,14] and
`lines` is [1,2,5,10,11,13,14] then the result will be "1-2, 5-11, 13-14".
-
+
"""
pairs = []
i = 0
@@ -47,9 +47,9 @@ def format_lines(statements, lines):
def expensive(fn):
"""A decorator to cache the result of an expensive operation.
-
+
Only applies to methods with no arguments.
-
+
"""
attr = "_cache_" + fn.__name__
def _wrapped(self):
diff --git a/coverage/parser.py b/coverage/parser.py
index 505ce1c..43f691f 100644
--- a/coverage/parser.py
+++ b/coverage/parser.py
@@ -9,13 +9,13 @@ from coverage.misc import nice_pair, CoverageException, NoSource, expensive
class CodeParser(object):
"""Parse code to find executable lines, excluded lines, etc."""
-
+
def __init__(self, text=None, filename=None, exclude=None):
"""
Source can be provided as `text`, the text itself, or `filename`, from
- which text will be read. Excluded lines are those that match `exclude`,
- a regex.
-
+ which text will be read. Excluded lines are those that match
+ `exclude`, a regex.
+
"""
assert text or filename, "CodeParser needs either text or filename"
self.filename = filename or "<code>"
@@ -33,7 +33,7 @@ class CodeParser(object):
self.text = self.text.replace('\r\n', '\n')
self.exclude = exclude
-
+
self.show_tokens = False
# The text lines of the parsed code.
@@ -41,22 +41,22 @@ class CodeParser(object):
# The line numbers of excluded lines of code.
self.excluded = set()
-
+
# The line numbers of docstring lines.
self.docstrings = set()
-
+
# The line numbers of class definitions.
self.classdefs = set()
# A dict mapping line numbers to (lo,hi) for multi-line statements.
self.multiline = {}
-
+
# The line numbers that start statements.
self.statement_starts = set()
# Lazily-created ByteParser
self._byte_parser = None
-
+
def _get_byte_parser(self):
"""Create a ByteParser on demand."""
if not self._byte_parser:
@@ -67,9 +67,9 @@ class CodeParser(object):
def _raw_parse(self):
"""Parse the source to find the interesting facts about its lines.
-
+
A handful of member fields are updated.
-
+
"""
# Find lines which match an exclusion pattern.
if self.exclude:
@@ -77,7 +77,7 @@ class CodeParser(object):
for i, ltext in enumerate(self.lines):
if re_exclude.search(ltext):
self.excluded.add(i+1)
-
+
# Tokenize, to find excluded suites, to find docstrings, and to find
# multi-line statements.
indent = 0
@@ -88,7 +88,7 @@ class CodeParser(object):
tokgen = tokenize.generate_tokens(StringIO(self.text).readline)
for toktype, ttext, (slineno, _), (elineno, _), ltext in tokgen:
- if self.show_tokens:
+ if self.show_tokens: # pragma: no cover
print("%10s %5s %-20r %r" % (
tokenize.tok_name.get(toktype, toktype),
nice_pair((slineno, elineno)), ttext, ltext
@@ -125,7 +125,7 @@ class CodeParser(object):
for l in range(first_line, elineno+1):
self.multiline[l] = rng
first_line = None
-
+
if ttext.strip() and toktype != tokenize.COMMENT:
# A non-whitespace token.
if first_line is None:
@@ -137,7 +137,7 @@ class CodeParser(object):
excluding = False
if excluding:
self.excluded.add(elineno)
-
+
prev_toktype = toktype
# Find the starts of the executable statements.
@@ -155,11 +155,11 @@ class CodeParser(object):
def first_lines(self, lines, ignore=None):
"""Map the line numbers in `lines` to the correct first line of the
statement.
-
+
Skip any line mentioned in `ignore`.
-
+
Returns a sorted list of the first lines.
-
+
"""
ignore = ignore or []
lset = set()
@@ -170,31 +170,31 @@ class CodeParser(object):
if new_l not in ignore:
lset.add(new_l)
return sorted(lset)
-
+
def parse_source(self):
"""Parse source text to find executable lines, excluded lines, etc.
Return values are 1) a sorted list of executable line numbers, and
2) a sorted list of excluded line numbers.
-
+
Reported line numbers are normalized to the first line of multi-line
statements.
-
+
"""
self._raw_parse()
-
+
excluded_lines = self.first_lines(self.excluded)
ignore = excluded_lines + list(self.docstrings)
lines = self.first_lines(self.statement_starts, ignore)
-
+
return lines, excluded_lines
def arcs(self):
"""Get information about the arcs available in the code.
-
+
Returns a sorted list of line number pairs. Line numbers have been
normalized to the first line of multiline statements.
-
+
"""
all_arcs = []
for l1, l2 in self.byte_parser._all_arcs():
@@ -207,9 +207,9 @@ class CodeParser(object):
def exit_counts(self):
"""Get a mapping from line numbers to count of exits from that line.
-
+
Excluded lines are excluded.
-
+
"""
excluded_lines = self.first_lines(self.excluded)
exit_counts = {}
@@ -232,7 +232,7 @@ class CodeParser(object):
# Ensure key is there: classdefs can include excluded lines.
if l in exit_counts:
exit_counts[l] -= 1
-
+
return exit_counts
exit_counts = expensive(exit_counts)
@@ -303,13 +303,13 @@ class ByteParser(object):
def child_parsers(self):
"""Iterate over all the code objects nested within this one.
-
+
The iteration includes `self` as its first value.
-
+
"""
return map(lambda c: ByteParser(code=c), CodeObjects(self.code))
- # Getting numbers from the lnotab value changed in Py3.0.
+ # Getting numbers from the lnotab value changed in Py3.0.
if sys.hexversion >= 0x03000000:
def _lnotab_increments(self, lnotab):
"""Return a list of ints from the lnotab bytes in 3.x"""
@@ -321,15 +321,15 @@ class ByteParser(object):
def _bytes_lines(self):
"""Map byte offsets to line numbers in `code`.
-
+
Uses co_lnotab described in Python/compile.c to map byte offsets to
line numbers. Returns a list: [(b0, l0), (b1, l1), ...]
-
+
"""
# Adapted from dis.py in the standard library.
byte_increments = self._lnotab_increments(self.code.co_lnotab[0::2])
line_increments = self._lnotab_increments(self.code.co_lnotab[1::2])
-
+
bytes_lines = []
last_line_num = None
line_num = self.code.co_firstlineno
@@ -344,13 +344,13 @@ class ByteParser(object):
if line_num != last_line_num:
bytes_lines.append((byte_num, line_num))
return bytes_lines
-
+
def _find_statements(self):
"""Find the statements in `self.code`.
-
+
Return a set of line numbers that start statements. Recurses into all
code objects reachable from `self.code`.
-
+
"""
stmts = set()
for bp in self.child_parsers():
@@ -358,12 +358,12 @@ class ByteParser(object):
for _, l in bp._bytes_lines():
stmts.add(l)
return stmts
-
- def _disassemble(self):
+
+ def _disassemble(self): # pragma: no cover
"""Disassemble code, for ad-hoc experimenting."""
-
+
import dis
-
+
for bp in self.child_parsers():
print("\n%s: " % bp.code)
dis.dis(bp.code)
@@ -373,24 +373,24 @@ class ByteParser(object):
def _split_into_chunks(self):
"""Split the code object into a list of `Chunk` objects.
-
+
Each chunk is only entered at its first instruction, though there can
be many exits from a chunk.
-
+
Returns a list of `Chunk` objects.
-
+
"""
# The list of chunks so far, and the one we're working on.
chunks = []
chunk = None
bytes_lines_map = dict(self._bytes_lines())
-
+
# The block stack: loops and try blocks get pushed here for the
# implicit jumps that can occur.
# Each entry is a tuple: (block type, destination)
block_stack = []
-
+
# Some op codes are followed by branches that should be ignored. This
# is a count of how many ignores are left.
ignore_branch = 0
@@ -405,12 +405,12 @@ class ByteParser(object):
chunk.exits.add(bc.offset)
chunk = Chunk(bc.offset, bytes_lines_map[bc.offset])
chunks.append(chunk)
-
+
if not chunk:
chunk = Chunk(bc.offset)
chunks.append(chunk)
- # Look at the opcode
+ # Look at the opcode
if bc.jump_to >= 0 and bc.op not in OPS_NO_JUMP:
if ignore_branch:
# Someone earlier wanted us to ignore this branch.
@@ -418,7 +418,7 @@ class ByteParser(object):
else:
# The opcode has a jump, it's an exit for this chunk.
chunk.exits.add(bc.jump_to)
-
+
if bc.op in OPS_CODE_END:
# The opcode can exit the code object.
chunk.exits.add(-1)
@@ -454,7 +454,7 @@ class ByteParser(object):
penult = ult
ult = bc
-
+
if chunks:
# The last two bytecodes could be a dummy "return None" that
# shouldn't be counted as real code. Every Python code object seems
@@ -483,35 +483,35 @@ class ByteParser(object):
def _arcs(self):
"""Find the executable arcs in the code.
-
+
Returns a set of pairs, (from,to). From and to are integer line
numbers. If from is -1, then the arc is an entrance into the code
object. If to is -1, the arc is an exit from the code object.
-
+
"""
chunks = self._split_into_chunks()
-
+
# A map from byte offsets to chunks jumped into.
byte_chunks = dict([(c.byte, c) for c in chunks])
# Build a map from byte offsets to actual lines reached.
byte_lines = {-1:[-1]}
bytes_to_add = set([c.byte for c in chunks])
-
+
while bytes_to_add:
byte_to_add = bytes_to_add.pop()
if byte_to_add in byte_lines or byte_to_add == -1:
continue
-
+
# Which lines does this chunk lead to?
bytes_considered = set()
bytes_to_consider = [byte_to_add]
lines = set()
-
+
while bytes_to_consider:
byte = bytes_to_consider.pop()
bytes_considered.add(byte)
-
+
# Find chunk for byte
try:
ch = byte_chunks[byte]
@@ -523,7 +523,7 @@ class ByteParser(object):
# No chunk for this byte!
raise Exception("Couldn't find chunk @ %d" % byte)
byte_chunks[byte] = ch
-
+
if ch.line:
lines.add(ch.line)
else:
@@ -536,7 +536,7 @@ class ByteParser(object):
bytes_to_add.update(ch.exits)
byte_lines[byte_to_add] = lines
-
+
# Figure out for each chunk where the exits go.
arcs = set()
for chunk in chunks:
@@ -547,65 +547,65 @@ class ByteParser(object):
arcs.add((chunk.line, exit_line))
for line in byte_lines[0]:
arcs.add((-1, line))
-
+
return arcs
-
+
def _all_chunks(self):
"""Returns a list of `Chunk` objects for this code and its children.
-
+
See `_split_into_chunks` for details.
-
+
"""
chunks = []
for bp in self.child_parsers():
chunks.extend(bp._split_into_chunks())
-
+
return chunks
def _all_arcs(self):
"""Get the set of all arcs in this code object and its children.
-
+
See `_arcs` for details.
-
+
"""
arcs = set()
for bp in self.child_parsers():
arcs.update(bp._arcs())
-
+
return arcs
class Chunk(object):
"""A sequence of bytecodes with a single entrance.
-
+
To analyze byte code, we have to divide it into chunks, sequences of byte
codes such that each basic block has only one entrance, the first
- instruction in the block.
-
+ instruction in the block.
+
This is almost the CS concept of `basic block`_, except that we're willing
to have many exits from a chunk, and "basic block" is a more cumbersome
term.
-
+
.. _basic block: http://en.wikipedia.org/wiki/Basic_block
-
+
An exit of -1 means the chunk can leave the code (return).
-
+
"""
def __init__(self, byte, line=0):
self.byte = byte
self.line = line
self.length = 0
self.exits = set()
-
+
def __repr__(self):
return "<%d+%d @%d %r>" % (
self.byte, self.length, self.line, list(self.exits)
)
-class AdHocMain(object):
+class AdHocMain(object): # pragma: no cover
"""An ad-hoc main for code parsing experiments."""
-
+
def main(self, args):
"""A main function for trying the code from the command line."""
@@ -632,7 +632,7 @@ class AdHocMain(object):
"-t", action="store_true", dest="tokens",
help="Show tokens"
)
-
+
options, args = parser.parse_args()
if options.recursive:
if args:
@@ -647,12 +647,12 @@ class AdHocMain(object):
def adhoc_one_file(self, options, filename):
"""Process just one file."""
-
+
if options.dis or options.chunks:
try:
bp = ByteParser(filename=filename)
except CoverageException:
- _, err, _ = sys.exc_info()
+ _, err, _ = sys.exc_info()
print("%s" % (err,))
return
@@ -679,7 +679,7 @@ class AdHocMain(object):
arc_width, arc_chars = self.arc_ascii_art(arcs)
else:
arc_width, arc_chars = 0, {}
-
+
exit_counts = cp.exit_counts()
for i, ltext in enumerate(cp.lines):
@@ -703,10 +703,10 @@ class AdHocMain(object):
def arc_ascii_art(self, arcs):
"""Draw arcs as ascii art.
-
+
Returns a width of characters needed to draw all the arcs, and a
dictionary mapping line numbers to ascii strings to draw for that line.
-
+
"""
arc_chars = {}
for lfrom, lto in sorted(arcs):
diff --git a/coverage/phystokens.py b/coverage/phystokens.py
index 131b362..5824b9b 100644
--- a/coverage/phystokens.py
+++ b/coverage/phystokens.py
@@ -5,13 +5,13 @@ from coverage.backward import StringIO # pylint: disable-msg=W0622
def phys_tokens(toks):
"""Return all physical tokens, even line continuations.
-
+
tokenize.generate_tokens() doesn't return a token for the backslash that
continues lines. This wrapper provides those tokens so that we can
re-create a faithful representation of the original source.
-
+
Returns the same values as generate_tokens()
-
+
"""
last_line = None
last_lineno = -1
@@ -61,13 +61,13 @@ def phys_tokens(toks):
def source_token_lines(source):
"""Generate a series of lines, one for each line in `source`.
-
+
Each line is a list of pairs, each pair is a token::
-
+
[('key', 'def'), ('ws', ' '), ('nam', 'hello'), ('op', '('), ... ]
Each pair has a token class, and the token text.
-
+
If you concatenate all the token texts, and then join them with newlines,
you should have your original `source` back, with two differences:
trailing whitespace is not preserved, and a final line with no newline
diff --git a/coverage/report.py b/coverage/report.py
index c221552..5b66f99 100644
--- a/coverage/report.py
+++ b/coverage/report.py
@@ -6,30 +6,30 @@ from coverage.misc import CoverageException, NoSource
class Reporter(object):
"""A base class for all reporters."""
-
+
def __init__(self, coverage, ignore_errors=False):
"""Create a reporter.
-
+
`coverage` is the coverage instance. `ignore_errors` controls how
skittish the reporter will be during file processing.
"""
self.coverage = coverage
self.ignore_errors = ignore_errors
-
+
# The code units to report on. Set by find_code_units.
self.code_units = []
-
+
# The directory into which to place the report, used by some derived
# classes.
self.directory = None
def find_code_units(self, morfs, omit_prefixes):
"""Find the code units we'll report on.
-
+
`morfs` is a list of modules or filenames. `omit_prefixes` is a list
of prefixes to leave out of the list.
-
+
"""
morfs = morfs or self.coverage.data.executed_files()
self.code_units = code_unit_factory(
@@ -39,9 +39,9 @@ class Reporter(object):
def report_files(self, report_fn, morfs, directory=None,
omit_prefixes=None):
"""Run a reporting function on a number of morfs.
-
+
`report_fn` is called for each relative morf in `morfs`.
-
+
"""
self.find_code_units(morfs, omit_prefixes)
diff --git a/coverage/results.py b/coverage/results.py
index 77c461a..e80ec0a 100644
--- a/coverage/results.py
+++ b/coverage/results.py
@@ -9,11 +9,11 @@ from coverage.parser import CodeParser
class Analysis(object):
"""The results of analyzing a code unit."""
-
+
def __init__(self, cov, code_unit):
self.coverage = cov
self.code_unit = code_unit
-
+
self.filename = self.code_unit.filename
ext = os.path.splitext(self.filename)[1]
source = None
@@ -40,10 +40,10 @@ class Analysis(object):
n_missing_branches = sum([len(v) for v in mba.values()])
else:
n_branches = n_missing_branches = 0
-
+
self.numbers = Numbers(
n_files=1,
- n_statements=len(self.statements),
+ n_statements=len(self.statements),
n_excluded=len(self.excluded),
n_missing=len(self.missing),
n_branches=n_branches,
@@ -52,9 +52,9 @@ class Analysis(object):
def missing_formatted(self):
"""The missing line numbers, formatted nicely.
-
+
Returns a string like "1-2, 5-11, 13-14".
-
+
"""
return format_lines(self.statements, self.missing)
@@ -102,12 +102,12 @@ class Analysis(object):
"""How many total branches are there?"""
exit_counts = self.parser.exit_counts()
return sum([count for count in exit_counts.values() if count > 1])
-
+
def missing_branch_arcs(self):
"""Return arcs that weren't executed from branch lines.
-
+
Returns {l1:[l2a,l2b,...], ...}
-
+
"""
missing = self.arcs_missing()
branch_lines = set(self.branch_lines())
@@ -122,7 +122,7 @@ class Analysis(object):
class Numbers(object):
"""The numerical results of measuring coverage.
-
+
This holds the basic statistics from `Analysis`, and is used to roll
up statistics across files.
@@ -141,12 +141,12 @@ class Numbers(object):
"""Returns the number of executed statements."""
return self.n_statements - self.n_missing
n_executed = property(_get_n_executed)
-
+
def _get_n_executed_branches(self):
"""Returns the number of executed branches."""
return self.n_branches - self.n_missing_branches
n_executed_branches = property(_get_n_executed_branches)
-
+
def _get_pc_covered(self):
"""Returns a single percentage value for coverage."""
if self.n_statements > 0:
diff --git a/coverage/summary.py b/coverage/summary.py
index e0e9eba..f4d3c2c 100644
--- a/coverage/summary.py
+++ b/coverage/summary.py
@@ -8,7 +8,7 @@ from coverage.results import Numbers
class SummaryReporter(Reporter):
"""A reporter for writing the summary report."""
-
+
def __init__(self, coverage, show_missing=True, ignore_errors=False):
super(SummaryReporter, self).__init__(coverage, ignore_errors)
self.show_missing = show_missing
@@ -16,7 +16,7 @@ class SummaryReporter(Reporter):
def report(self, morfs, omit_prefixes=None, outfile=None):
"""Writes a report summarizing coverage statistics per module."""
-
+
self.find_code_units(morfs, omit_prefixes)
# Prepare the formatting strings
@@ -45,7 +45,7 @@ class SummaryReporter(Reporter):
outfile.write(rule)
total = Numbers()
-
+
for cu in self.code_units:
try:
analysis = self.coverage._analyze(cu)
diff --git a/coverage/templite.py b/coverage/templite.py
index 0654f29..d3c673c 100644
--- a/coverage/templite.py
+++ b/coverage/templite.py
@@ -8,40 +8,40 @@ class Templite(object):
"""A simple template renderer, for a nano-subset of Django syntax.
Supported constructs are extended variable access::
-
+
{{var.modifer.modifier|filter|filter}}
-
+
loops::
-
+
{% for var in list %}...{% endfor %}
-
+
and ifs::
-
+
{% if var %}...{% endif %}
Comments are within curly-hash markers::
-
+
{# This will be ignored #}
Construct a Templite with the template text, then use `render` against a
dictionary context to create a finished string.
-
+
"""
def __init__(self, text, *contexts):
"""Construct a Templite with the given `text`.
-
+
`contexts` are dictionaries of values to use for future renderings.
These are good for filters and global values.
-
+
"""
self.text = text
self.context = {}
for context in contexts:
self.context.update(context)
-
+
# Split the text to form a list of tokens.
toks = re.split(r"(?s)({{.*?}}|{%.*?%}|{#.*?#})", text)
-
+
# Parse the tokens into a nested list of operations. Each item in the
# list is a tuple with an opcode, and arguments. They'll be
# interpreted by TempliteEngine.
@@ -83,21 +83,21 @@ class Templite(object):
raise SyntaxError("Don't understand tag %r" % words)
else:
ops.append(('lit', tok))
-
+
assert not ops_stack, "Unmatched action tag: %r" % ops_stack[-1][0]
self.ops = ops
def render(self, context=None):
"""Render this template by applying it to `context`.
-
+
`context` is a dictionary of values to use in this rendering.
-
+
"""
# Make the complete context we'll use.
ctx = dict(self.context)
if context:
ctx.update(context)
-
+
# Run it through an engine, and return the result.
engine = _TempliteEngine(ctx)
engine.execute(self.ops)
@@ -112,9 +112,9 @@ class _TempliteEngine(object):
def execute(self, ops):
"""Execute `ops` in the engine.
-
+
Called recursively for the bodies of if's and loops.
-
+
"""
for op, args in ops:
if op == 'lit':
@@ -142,9 +142,9 @@ class _TempliteEngine(object):
def evaluate(self, expr):
"""Evaluate an expression.
-
+
`expr` can have pipes and dots to indicate data access and filtering.
-
+
"""
if "|" in expr:
pipes = expr.split("|")
diff --git a/coverage/tracer.c b/coverage/tracer.c
index 304190e..8193c34 100644
--- a/coverage/tracer.c
+++ b/coverage/tracer.c
@@ -60,7 +60,7 @@ typedef struct {
PyObject * data;
PyObject * should_trace_cache;
PyObject * arcs;
-
+
/* Has the tracer been started? */
int started;
/* Are we tracing arcs, or just lines? */
@@ -71,7 +71,7 @@ typedef struct {
data for a single source file. The data stack parallels the call stack:
each call pushes the new frame's file data onto the data stack, and each
return pops file data off.
-
+
The file data is a dictionary whose form depends on the tracing options.
If tracing arcs, the keys are line number pairs. If not tracing arcs,
the keys are line numbers. In both cases, the value is irrelevant
@@ -130,7 +130,7 @@ Tracer_init(Tracer *self, PyObject *args, PyObject *kwds)
self->data = NULL;
self->should_trace_cache = NULL;
self->arcs = NULL;
-
+
self->started = 0;
self->tracing_arcs = 0;
@@ -143,7 +143,7 @@ Tracer_init(Tracer *self, PyObject *args, PyObject *kwds)
}
self->data_stack_alloc = STACK_DELTA;
- self->cur_file_data = NULL;
+ self->cur_file_data = NULL;
self->last_line = -1;
self->last_exc_back = NULL;
@@ -171,7 +171,7 @@ Tracer_dealloc(Tracer *self)
static const char *
indent(int n)
{
- static const char * spaces =
+ static const char * spaces =
" "
" "
" "
@@ -220,7 +220,7 @@ static int
Tracer_record_pair(Tracer *self, int l1, int l2)
{
int ret = 0;
-
+
PyObject * t = PyTuple_New(2);
if (t != NULL) {
PyTuple_SET_ITEM(t, 0, MyInt_FromLong(l1));
@@ -248,11 +248,11 @@ Tracer_trace(Tracer *self, PyFrameObject *frame, int what, PyObject *arg)
PyObject * filename = NULL;
PyObject * tracename = NULL;
- #if WHAT_LOG
+ #if WHAT_LOG
if (what <= sizeof(what_sym)/sizeof(const char *)) {
printf("trace: %s @ %s %d\n", what_sym[what], MyText_AS_STRING(frame->f_code->co_filename), frame->f_lineno);
}
- #endif
+ #endif
#if TRACE_LOG
if (strstr(MyText_AS_STRING(frame->f_code->co_filename), start_file) && frame->f_lineno == start_line) {
@@ -269,7 +269,7 @@ Tracer_trace(Tracer *self, PyFrameObject *frame, int what, PyObject *arg)
that frame is gone. Our handling for RETURN doesn't need the
actual frame, but we do log it, so that will look a little off if
you're looking at the detailed log.
-
+
If someday we need to examine the frame when doing RETURN, then
we'll need to keep more of the missed frame's state.
*/
@@ -288,7 +288,7 @@ Tracer_trace(Tracer *self, PyFrameObject *frame, int what, PyObject *arg)
}
self->last_exc_back = NULL;
}
-
+
switch (what) {
case PyTrace_CALL: /* 0 */
@@ -361,12 +361,12 @@ Tracer_trace(Tracer *self, PyFrameObject *frame, int what, PyObject *arg)
self->cur_file_data = NULL;
SHOWLOG(self->depth, frame->f_lineno, filename, "skipped");
}
-
+
Py_DECREF(tracename);
self->last_line = -1;
break;
-
+
case PyTrace_RETURN: /* 3 */
STATS( self->stats.returns++; )
/* A near-copy of this code is above in the missing-return handler. */
@@ -383,7 +383,7 @@ Tracer_trace(Tracer *self, PyFrameObject *frame, int what, PyObject *arg)
self->depth--;
}
break;
-
+
case PyTrace_LINE: /* 2 */
STATS( self->stats.lines++; )
if (self->depth >= 0) {
@@ -414,29 +414,29 @@ Tracer_trace(Tracer *self, PyFrameObject *frame, int what, PyObject *arg)
self->last_line = frame->f_lineno;
}
break;
-
+
case PyTrace_EXCEPTION:
/* Some code (Python 2.3, and pyexpat anywhere) fires an exception event
without a return event. To detect that, we'll keep a copy of the
parent frame for an exception event. If the next event is in that
frame, then we must have returned without a return event. We can
synthesize the missing event then.
-
+
Python itself fixed this problem in 2.4. Pyexpat still has the bug.
I've reported the problem with pyexpat as http://bugs.python.org/issue6359 .
If it gets fixed, this code should still work properly. Maybe some day
the bug will be fixed everywhere coverage.py is supported, and we can
remove this missing-return detection.
-
+
More about this fix: http://nedbatchelder.com/blog/200907/a_nasty_little_bug.html
*/
STATS( self->stats.exceptions++; )
self->last_exc_back = frame->f_back;
break;
-
+
default:
STATS( self->stats.others++; )
- break;
+ break;
}
return 0;
@@ -586,7 +586,7 @@ PyInit_tracer(void)
if (mod == NULL) {
return NULL;
}
-
+
TracerType.tp_new = PyType_GenericNew;
if (PyType_Ready(&TracerType) < 0) {
Py_DECREF(mod);
@@ -595,8 +595,8 @@ PyInit_tracer(void)
Py_INCREF(&TracerType);
PyModule_AddObject(mod, "Tracer", (PyObject *)&TracerType);
-
- return mod;
+
+ return mod;
}
#else
diff --git a/coverage/xmlreport.py b/coverage/xmlreport.py
index 9cc6567..ab44025 100644
--- a/coverage/xmlreport.py
+++ b/coverage/xmlreport.py
@@ -14,20 +14,20 @@ def rate(hit, num):
class XmlReporter(Reporter):
"""A reporter for writing Cobertura-style XML coverage results."""
-
+
def __init__(self, coverage, ignore_errors=False):
super(XmlReporter, self).__init__(coverage, ignore_errors)
-
+
self.packages = None
self.xml_out = None
self.arcs = coverage.data.has_arcs()
def report(self, morfs, omit_prefixes=None, outfile=None):
"""Generate a Cobertura-compatible XML report for `morfs`.
-
+
`morfs` is a list of modules or filenames. `omit_prefixes` is a list
of strings, prefixes of modules to omit from the report.
-
+
"""
# Initial setup.
outfile = outfile or sys.stdout
@@ -39,7 +39,7 @@ class XmlReporter(Reporter):
"http://cobertura.sourceforge.net/xml/coverage-03.dtd"
)
self.xml_out = impl.createDocument(None, "coverage", docType)
-
+
# Write header stuff.
xcoverage = self.xml_out.documentElement
xcoverage.setAttribute("version", __version__)
@@ -56,7 +56,7 @@ class XmlReporter(Reporter):
lnum_tot, lhits_tot = 0, 0
bnum_tot, bhits_tot = 0, 0
-
+
# Populate the XML DOM with the package info.
for pkg_name, pkg_data in self.packages.items():
class_elts, lhits, lnum, bhits, bnum = pkg_data
@@ -75,16 +75,16 @@ class XmlReporter(Reporter):
lhits_tot += lhits
bnum_tot += bnum
bhits_tot += bhits
-
+
xcoverage.setAttribute("line-rate", str(rate(lhits_tot, lnum_tot)))
xcoverage.setAttribute("branch-rate", str(rate(bhits_tot, bnum_tot)))
-
+
# Use the DOM to write the output file.
outfile.write(self.xml_out.toprettyxml())
def xml_file(self, cu, analysis):
"""Add to the XML report for a single file."""
-
+
# Create the 'lines' and 'package' XML elements, which
# are populated later. Note that a package == a directory.
dirname, fname = os.path.split(cu.name)
@@ -133,7 +133,7 @@ class XmlReporter(Reporter):
else:
class_branches = 0.0
class_branch_hits = 0.0
-
+
# Finalize the statistics that are collected in the XML DOM.
line_rate = rate(class_hits, class_lines)
branch_rate = rate(class_branch_hits, class_branches)