Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # portions copyright 2001, Autonomous Zones Industries, Inc., all rights... |
| 4 | # err... reserved and offered to the public under the terms of the |
| 5 | # Python 2.2 license. |
| 6 | # Author: Zooko O'Whielacronx |
| 7 | # http://zooko.com/ |
| 8 | # mailto:zooko@zooko.com |
| 9 | # |
| 10 | # Copyright 2000, Mojam Media, Inc., all rights reserved. |
| 11 | # Author: Skip Montanaro |
| 12 | # |
| 13 | # Copyright 1999, Bioreason, Inc., all rights reserved. |
| 14 | # Author: Andrew Dalke |
| 15 | # |
| 16 | # Copyright 1995-1997, Automatrix, Inc., all rights reserved. |
| 17 | # Author: Skip Montanaro |
| 18 | # |
| 19 | # Copyright 1991-1995, Stichting Mathematisch Centrum, all rights reserved. |
| 20 | # |
| 21 | # |
| 22 | # Permission to use, copy, modify, and distribute this Python software and |
| 23 | # its associated documentation for any purpose without fee is hereby |
| 24 | # granted, provided that the above copyright notice appears in all copies, |
| 25 | # and that both that copyright notice and this permission notice appear in |
| 26 | # supporting documentation, and that the name of neither Automatrix, |
| 27 | # Bioreason or Mojam Media be used in advertising or publicity pertaining to |
| 28 | # distribution of the software without specific, written prior permission. |
| 29 | # |
| 30 | """program/module to trace Python program or function execution |
| 31 | |
| 32 | Sample use, command line: |
| 33 | trace.py -c -f counts --ignore-dir '$prefix' spam.py eggs |
| 34 | trace.py -t --ignore-dir '$prefix' spam.py eggs |
Skip Montanaro | 5bfd984 | 2004-04-10 16:29:58 +0000 | [diff] [blame] | 35 | trace.py --trackcalls spam.py eggs |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 36 | |
| 37 | Sample use, programmatically |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 38 | import sys |
| 39 | |
| 40 | # create a Trace object, telling it what to ignore, and whether to |
| 41 | # do tracing or line-counting or both. |
| 42 | tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], trace=0, |
| 43 | count=1) |
| 44 | # run the new command using the given tracer |
| 45 | tracer.run('main()') |
| 46 | # make a report, placing output in /tmp |
| 47 | r = tracer.results() |
| 48 | r.write_results(show_missing=True, coverdir="/tmp") |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 49 | """ |
| 50 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 51 | import linecache |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 52 | import os |
| 53 | import re |
| 54 | import sys |
Jeremy Hylton | 546e34b | 2003-06-26 14:56:17 +0000 | [diff] [blame] | 55 | import threading |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame^] | 56 | import time |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 57 | import token |
| 58 | import tokenize |
| 59 | import types |
Skip Montanaro | 5bfd984 | 2004-04-10 16:29:58 +0000 | [diff] [blame] | 60 | import gc |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 99603b0 | 2007-07-20 00:22:32 +0000 | [diff] [blame] | 62 | import pickle |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 63 | |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 64 | def usage(outfile): |
| 65 | outfile.write("""Usage: %s [OPTIONS] <file> [ARGS] |
| 66 | |
| 67 | Meta-options: |
| 68 | --help Display this help then exit. |
| 69 | --version Output version information then exit. |
| 70 | |
| 71 | Otherwise, exactly one of the following three options must be given: |
| 72 | -t, --trace Print each line to sys.stdout before it is executed. |
| 73 | -c, --count Count the number of times each line is executed |
| 74 | and write the counts to <module>.cover for each |
| 75 | module executed, in the module's directory. |
| 76 | See also `--coverdir', `--file', `--no-report' below. |
Skip Montanaro | a7b8ac6 | 2003-06-27 19:09:33 +0000 | [diff] [blame] | 77 | -l, --listfuncs Keep track of which functions are executed at least |
Fred Drake | 01c623b | 2003-06-27 19:22:11 +0000 | [diff] [blame] | 78 | once and write the results to sys.stdout after the |
Skip Montanaro | a7b8ac6 | 2003-06-27 19:09:33 +0000 | [diff] [blame] | 79 | program exits. |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 80 | -T, --trackcalls Keep track of caller/called pairs and write the |
| 81 | results to sys.stdout after the program exits. |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 82 | -r, --report Generate a report from a counts file; do not execute |
| 83 | any code. `--file' must specify the results file to |
| 84 | read, which must have been created in a previous run |
| 85 | with `--count --file=FILE'. |
| 86 | |
| 87 | Modifiers: |
| 88 | -f, --file=<file> File to accumulate counts over several runs. |
| 89 | -R, --no-report Do not generate the coverage report files. |
| 90 | Useful if you want to accumulate over several runs. |
| 91 | -C, --coverdir=<dir> Directory where the report files. The coverage |
| 92 | report for <package>.<module> is written to file |
| 93 | <dir>/<package>/<module>.cover. |
| 94 | -m, --missing Annotate executable lines that were not executed |
| 95 | with '>>>>>> '. |
| 96 | -s, --summary Write a brief summary on stdout for each file. |
| 97 | (Can only be used with --count or --report.) |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame^] | 98 | -g, --timing Prefix each line with the time since the program started. |
| 99 | Only used while tracing. |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 100 | |
| 101 | Filters, may be repeated multiple times: |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 102 | --ignore-module=<mod> Ignore the given module(s) and its submodules |
| 103 | (if it is a package). Accepts comma separated |
| 104 | list of module names |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 105 | --ignore-dir=<dir> Ignore files in the given directory (multiple |
| 106 | directories can be joined by os.pathsep). |
| 107 | """ % sys.argv[0]) |
| 108 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 109 | PRAGMA_NOCOVER = "#pragma NO COVER" |
| 110 | |
| 111 | # Simple rx to find lines with no code. |
| 112 | rx_blank = re.compile(r'^\s*(#.*)?$') |
| 113 | |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 114 | class Ignore: |
| 115 | def __init__(self, modules = None, dirs = None): |
| 116 | self._mods = modules or [] |
| 117 | self._dirs = dirs or [] |
| 118 | |
| 119 | self._dirs = map(os.path.normpath, self._dirs) |
| 120 | self._ignore = { '<string>': 1 } |
| 121 | |
| 122 | def names(self, filename, modulename): |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 123 | if modulename in self._ignore: |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 124 | return self._ignore[modulename] |
| 125 | |
| 126 | # haven't seen this one before, so see if the module name is |
| 127 | # on the ignore list. Need to take some care since ignoring |
| 128 | # "cmp" musn't mean ignoring "cmpcache" but ignoring |
| 129 | # "Spam" must also mean ignoring "Spam.Eggs". |
| 130 | for mod in self._mods: |
| 131 | if mod == modulename: # Identical names, so ignore |
| 132 | self._ignore[modulename] = 1 |
| 133 | return 1 |
| 134 | # check if the module is a proper submodule of something on |
| 135 | # the ignore list |
| 136 | n = len(mod) |
| 137 | # (will not overflow since if the first n characters are the |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 138 | # same and the name has not already occurred, then the size |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 139 | # of "name" is greater than that of "mod") |
| 140 | if mod == modulename[:n] and modulename[n] == '.': |
| 141 | self._ignore[modulename] = 1 |
| 142 | return 1 |
| 143 | |
| 144 | # Now check that __file__ isn't in one of the directories |
| 145 | if filename is None: |
| 146 | # must be a built-in, so we must ignore |
| 147 | self._ignore[modulename] = 1 |
| 148 | return 1 |
| 149 | |
| 150 | # Ignore a file when it contains one of the ignorable paths |
| 151 | for d in self._dirs: |
| 152 | # The '+ os.sep' is to ensure that d is a parent directory, |
| 153 | # as compared to cases like: |
| 154 | # d = "/usr/local" |
| 155 | # filename = "/usr/local.py" |
| 156 | # or |
| 157 | # d = "/usr/local.py" |
| 158 | # filename = "/usr/local.py" |
| 159 | if filename.startswith(d + os.sep): |
| 160 | self._ignore[modulename] = 1 |
| 161 | return 1 |
| 162 | |
| 163 | # Tried the different ways, so we don't ignore this module |
| 164 | self._ignore[modulename] = 0 |
| 165 | return 0 |
| 166 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 167 | def modname(path): |
| 168 | """Return a plausible module name for the patch.""" |
Jeremy Hylton | dfbfe73 | 2003-04-21 22:49:17 +0000 | [diff] [blame] | 169 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 170 | base = os.path.basename(path) |
| 171 | filename, ext = os.path.splitext(base) |
| 172 | return filename |
| 173 | |
Jeremy Hylton | dfbfe73 | 2003-04-21 22:49:17 +0000 | [diff] [blame] | 174 | def fullmodname(path): |
Jeremy Hylton | c8c8b94 | 2003-04-22 15:35:51 +0000 | [diff] [blame] | 175 | """Return a plausible module name for the path.""" |
Jeremy Hylton | dfbfe73 | 2003-04-21 22:49:17 +0000 | [diff] [blame] | 176 | |
| 177 | # If the file 'path' is part of a package, then the filename isn't |
| 178 | # enough to uniquely identify it. Try to do the right thing by |
| 179 | # looking in sys.path for the longest matching prefix. We'll |
| 180 | # assume that the rest is the package name. |
| 181 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 182 | comparepath = os.path.normcase(path) |
Jeremy Hylton | dfbfe73 | 2003-04-21 22:49:17 +0000 | [diff] [blame] | 183 | longest = "" |
| 184 | for dir in sys.path: |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 185 | dir = os.path.normcase(dir) |
| 186 | if comparepath.startswith(dir) and comparepath[len(dir)] == os.sep: |
Jeremy Hylton | dfbfe73 | 2003-04-21 22:49:17 +0000 | [diff] [blame] | 187 | if len(dir) > len(longest): |
| 188 | longest = dir |
| 189 | |
Guido van Rossum | b427c00 | 2003-10-10 23:02:01 +0000 | [diff] [blame] | 190 | if longest: |
| 191 | base = path[len(longest) + 1:] |
| 192 | else: |
| 193 | base = path |
Guido van Rossum | bbca8da | 2004-02-19 19:16:50 +0000 | [diff] [blame] | 194 | base = base.replace(os.sep, ".") |
| 195 | if os.altsep: |
| 196 | base = base.replace(os.altsep, ".") |
Jeremy Hylton | dfbfe73 | 2003-04-21 22:49:17 +0000 | [diff] [blame] | 197 | filename, ext = os.path.splitext(base) |
| 198 | return filename |
| 199 | |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 200 | class CoverageResults: |
| 201 | def __init__(self, counts=None, calledfuncs=None, infile=None, |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 202 | callers=None, outfile=None): |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 203 | self.counts = counts |
| 204 | if self.counts is None: |
| 205 | self.counts = {} |
| 206 | self.counter = self.counts.copy() # map (filename, lineno) to count |
| 207 | self.calledfuncs = calledfuncs |
| 208 | if self.calledfuncs is None: |
| 209 | self.calledfuncs = {} |
| 210 | self.calledfuncs = self.calledfuncs.copy() |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 211 | self.callers = callers |
| 212 | if self.callers is None: |
| 213 | self.callers = {} |
| 214 | self.callers = self.callers.copy() |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 215 | self.infile = infile |
| 216 | self.outfile = outfile |
| 217 | if self.infile: |
Jeremy Hylton | d7ce86d | 2003-07-07 16:08:47 +0000 | [diff] [blame] | 218 | # Try to merge existing counts file. |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 219 | try: |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 220 | counts, calledfuncs, callers = \ |
| 221 | pickle.load(open(self.infile, 'rb')) |
| 222 | self.update(self.__class__(counts, calledfuncs, callers)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 223 | except (IOError, EOFError, ValueError) as err: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 224 | print(("Skipping counts file %r: %s" |
| 225 | % (self.infile, err)), file=sys.stderr) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 226 | |
| 227 | def update(self, other): |
| 228 | """Merge in the data from another CoverageResults""" |
| 229 | counts = self.counts |
| 230 | calledfuncs = self.calledfuncs |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 231 | callers = self.callers |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 232 | other_counts = other.counts |
| 233 | other_calledfuncs = other.calledfuncs |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 234 | other_callers = other.callers |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 235 | |
| 236 | for key in other_counts.keys(): |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 237 | counts[key] = counts.get(key, 0) + other_counts[key] |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 238 | |
| 239 | for key in other_calledfuncs.keys(): |
| 240 | calledfuncs[key] = 1 |
| 241 | |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 242 | for key in other_callers.keys(): |
| 243 | callers[key] = 1 |
| 244 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 245 | def write_results(self, show_missing=True, summary=False, coverdir=None): |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 246 | """ |
| 247 | @param coverdir |
| 248 | """ |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 249 | if self.calledfuncs: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 250 | print() |
| 251 | print("functions called:") |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 252 | calls = self.calledfuncs.keys() |
| 253 | calls.sort() |
| 254 | for filename, modulename, funcname in calls: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 255 | print(("filename: %s, modulename: %s, funcname: %s" |
| 256 | % (filename, modulename, funcname))) |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 257 | |
| 258 | if self.callers: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 259 | print() |
| 260 | print("calling relationships:") |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 261 | calls = self.callers.keys() |
| 262 | calls.sort() |
| 263 | lastfile = lastcfile = "" |
| 264 | for ((pfile, pmod, pfunc), (cfile, cmod, cfunc)) in calls: |
| 265 | if pfile != lastfile: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 266 | print() |
| 267 | print("***", pfile, "***") |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 268 | lastfile = pfile |
| 269 | lastcfile = "" |
| 270 | if cfile != pfile and lastcfile != cfile: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 271 | print(" -->", cfile) |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 272 | lastcfile = cfile |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 273 | print(" %s.%s -> %s.%s" % (pmod, pfunc, cmod, cfunc)) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 274 | |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 275 | # turn the counts data ("(filename, lineno) = count") into something |
| 276 | # accessible on a per-file basis |
| 277 | per_file = {} |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 278 | for filename, lineno in self.counts.keys(): |
| 279 | lines_hit = per_file[filename] = per_file.get(filename, {}) |
| 280 | lines_hit[lineno] = self.counts[(filename, lineno)] |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 281 | |
| 282 | # accumulate summary info, if needed |
| 283 | sums = {} |
| 284 | |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 285 | for filename, count in per_file.items(): |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 286 | # skip some "files" we don't care about... |
| 287 | if filename == "<string>": |
| 288 | continue |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 289 | if filename.startswith("<doctest "): |
| 290 | continue |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 291 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 292 | if filename.endswith((".pyc", ".pyo")): |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 293 | filename = filename[:-1] |
| 294 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 295 | if coverdir is None: |
| 296 | dir = os.path.dirname(os.path.abspath(filename)) |
Jeremy Hylton | c8c8b94 | 2003-04-22 15:35:51 +0000 | [diff] [blame] | 297 | modulename = modname(filename) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 298 | else: |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 299 | dir = coverdir |
| 300 | if not os.path.exists(dir): |
| 301 | os.makedirs(dir) |
Jeremy Hylton | c8c8b94 | 2003-04-22 15:35:51 +0000 | [diff] [blame] | 302 | modulename = fullmodname(filename) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 303 | |
| 304 | # If desired, get a list of the line numbers which represent |
| 305 | # executable content (returned as a dict for better lookup speed) |
| 306 | if show_missing: |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 307 | lnotab = find_executable_linenos(filename) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 308 | else: |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 309 | lnotab = {} |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 310 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 311 | source = linecache.getlines(filename) |
| 312 | coverpath = os.path.join(dir, modulename + ".cover") |
| 313 | n_hits, n_lines = self.write_results_file(coverpath, source, |
| 314 | lnotab, count) |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 315 | |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 316 | if summary and n_lines: |
| 317 | percent = int(100 * n_hits / n_lines) |
| 318 | sums[modulename] = n_lines, percent, modulename, filename |
| 319 | |
| 320 | if summary and sums: |
| 321 | mods = sums.keys() |
| 322 | mods.sort() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 323 | print("lines cov% module (path)") |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 324 | for m in mods: |
| 325 | n_lines, percent, modulename, filename = sums[m] |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 326 | print("%5d %3d%% %s (%s)" % sums[m]) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 327 | |
| 328 | if self.outfile: |
| 329 | # try and store counts and module info into self.outfile |
| 330 | try: |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 331 | pickle.dump((self.counts, self.calledfuncs, self.callers), |
Jeremy Hylton | d0e2705 | 2003-10-14 20:12:06 +0000 | [diff] [blame] | 332 | open(self.outfile, 'wb'), 1) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 333 | except IOError as err: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 334 | print("Can't save counts files because %s" % err, file=sys.stderr) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 335 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 336 | def write_results_file(self, path, lines, lnotab, lines_hit): |
| 337 | """Return a coverage results file in path.""" |
| 338 | |
| 339 | try: |
| 340 | outfile = open(path, "w") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 341 | except IOError as err: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 342 | print(("trace: Could not open %r for writing: %s" |
| 343 | "- skipping" % (path, err)), file=sys.stderr) |
Guido van Rossum | bbca8da | 2004-02-19 19:16:50 +0000 | [diff] [blame] | 344 | return 0, 0 |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 345 | |
| 346 | n_lines = 0 |
| 347 | n_hits = 0 |
| 348 | for i, line in enumerate(lines): |
| 349 | lineno = i + 1 |
| 350 | # do the blank/comment match to try to mark more lines |
| 351 | # (help the reader find stuff that hasn't been covered) |
| 352 | if lineno in lines_hit: |
| 353 | outfile.write("%5d: " % lines_hit[lineno]) |
| 354 | n_hits += 1 |
| 355 | n_lines += 1 |
| 356 | elif rx_blank.match(line): |
Walter Dörwald | c171172 | 2003-07-15 10:34:02 +0000 | [diff] [blame] | 357 | outfile.write(" ") |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 358 | else: |
| 359 | # lines preceded by no marks weren't hit |
| 360 | # Highlight them if so indicated, unless the line contains |
| 361 | # #pragma: NO COVER |
| 362 | if lineno in lnotab and not PRAGMA_NOCOVER in lines[i]: |
| 363 | outfile.write(">>>>>> ") |
Jeremy Hylton | 546e34b | 2003-06-26 14:56:17 +0000 | [diff] [blame] | 364 | n_lines += 1 |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 365 | else: |
| 366 | outfile.write(" ") |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 367 | outfile.write(lines[i].expandtabs(8)) |
| 368 | outfile.close() |
| 369 | |
| 370 | return n_hits, n_lines |
| 371 | |
| 372 | def find_lines_from_code(code, strs): |
| 373 | """Return dict where keys are lines in the line number table.""" |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 374 | linenos = {} |
| 375 | |
| 376 | line_increments = [ord(c) for c in code.co_lnotab[1::2]] |
| 377 | table_length = len(line_increments) |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 378 | docstring = False |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 379 | |
| 380 | lineno = code.co_firstlineno |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 381 | for li in line_increments: |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 382 | lineno += li |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 383 | if lineno not in strs: |
| 384 | linenos[lineno] = 1 |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 385 | |
| 386 | return linenos |
| 387 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 388 | def find_lines(code, strs): |
| 389 | """Return lineno dict for all code objects reachable from code.""" |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 390 | # get all of the lineno information from the code of this scope level |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 391 | linenos = find_lines_from_code(code, strs) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 392 | |
| 393 | # and check the constants for references to other code objects |
| 394 | for c in code.co_consts: |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 395 | if isinstance(c, types.CodeType): |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 396 | # find another code object, so recurse into it |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 397 | linenos.update(find_lines(c, strs)) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 398 | return linenos |
| 399 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 400 | def find_strings(filename): |
| 401 | """Return a dict of possible docstring positions. |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 402 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 403 | The dict maps line numbers to strings. There is an entry for |
| 404 | line that contains only a string or a part of a triple-quoted |
| 405 | string. |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 406 | """ |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 407 | d = {} |
| 408 | # If the first token is a string, then it's the module docstring. |
| 409 | # Add this special case so that the test in the loop passes. |
| 410 | prev_ttype = token.INDENT |
| 411 | f = open(filename) |
| 412 | for ttype, tstr, start, end, line in tokenize.generate_tokens(f.readline): |
| 413 | if ttype == token.STRING: |
| 414 | if prev_ttype == token.INDENT: |
| 415 | sline, scol = start |
| 416 | eline, ecol = end |
| 417 | for i in range(sline, eline + 1): |
| 418 | d[i] = 1 |
| 419 | prev_ttype = ttype |
| 420 | f.close() |
| 421 | return d |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 422 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 423 | def find_executable_linenos(filename): |
| 424 | """Return dict where keys are line numbers in the line number table.""" |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 425 | try: |
Skip Montanaro | c00fc84 | 2004-04-16 03:28:19 +0000 | [diff] [blame] | 426 | prog = open(filename, "rU").read() |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 427 | except IOError as err: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 428 | print(("Not printing coverage data for %r: %s" |
| 429 | % (filename, err)), file=sys.stderr) |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 430 | return {} |
| 431 | code = compile(prog, filename, "exec") |
| 432 | strs = find_strings(filename) |
| 433 | return find_lines(code, strs) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 434 | |
| 435 | class Trace: |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 436 | def __init__(self, count=1, trace=1, countfuncs=0, countcallers=0, |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame^] | 437 | ignoremods=(), ignoredirs=(), infile=None, outfile=None, |
| 438 | timing=False): |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 439 | """ |
| 440 | @param count true iff it should count number of times each |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 441 | line is executed |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 442 | @param trace true iff it should print out each line that is |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 443 | being counted |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 444 | @param countfuncs true iff it should just output a list of |
| 445 | (filename, modulename, funcname,) for functions |
| 446 | that were called at least once; This overrides |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 447 | `count' and `trace' |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 448 | @param ignoremods a list of the names of modules to ignore |
| 449 | @param ignoredirs a list of the names of directories to ignore |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 450 | all of the (recursive) contents of |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 451 | @param infile file from which to read stored counts to be |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 452 | added into the results |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 453 | @param outfile file in which to write the results |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame^] | 454 | @param timing true iff timing information be displayed |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 455 | """ |
| 456 | self.infile = infile |
| 457 | self.outfile = outfile |
| 458 | self.ignore = Ignore(ignoremods, ignoredirs) |
| 459 | self.counts = {} # keys are (filename, linenumber) |
| 460 | self.blabbed = {} # for debugging |
| 461 | self.pathtobasename = {} # for memoizing os.path.basename |
| 462 | self.donothing = 0 |
| 463 | self.trace = trace |
| 464 | self._calledfuncs = {} |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 465 | self._callers = {} |
Skip Montanaro | 5bfd984 | 2004-04-10 16:29:58 +0000 | [diff] [blame] | 466 | self._caller_cache = {} |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame^] | 467 | self.start_time = None |
| 468 | if timing: |
| 469 | self.start_time = time.time() |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 470 | if countcallers: |
| 471 | self.globaltrace = self.globaltrace_trackcallers |
| 472 | elif countfuncs: |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 473 | self.globaltrace = self.globaltrace_countfuncs |
| 474 | elif trace and count: |
| 475 | self.globaltrace = self.globaltrace_lt |
| 476 | self.localtrace = self.localtrace_trace_and_count |
| 477 | elif trace: |
| 478 | self.globaltrace = self.globaltrace_lt |
| 479 | self.localtrace = self.localtrace_trace |
| 480 | elif count: |
| 481 | self.globaltrace = self.globaltrace_lt |
| 482 | self.localtrace = self.localtrace_count |
| 483 | else: |
| 484 | # Ahem -- do nothing? Okay. |
| 485 | self.donothing = 1 |
| 486 | |
| 487 | def run(self, cmd): |
| 488 | import __main__ |
| 489 | dict = __main__.__dict__ |
| 490 | if not self.donothing: |
| 491 | sys.settrace(self.globaltrace) |
Jeremy Hylton | 546e34b | 2003-06-26 14:56:17 +0000 | [diff] [blame] | 492 | threading.settrace(self.globaltrace) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 493 | try: |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 494 | exec(cmd, dict, dict) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 495 | finally: |
| 496 | if not self.donothing: |
| 497 | sys.settrace(None) |
Jeremy Hylton | 546e34b | 2003-06-26 14:56:17 +0000 | [diff] [blame] | 498 | threading.settrace(None) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 499 | |
| 500 | def runctx(self, cmd, globals=None, locals=None): |
| 501 | if globals is None: globals = {} |
| 502 | if locals is None: locals = {} |
| 503 | if not self.donothing: |
| 504 | sys.settrace(self.globaltrace) |
Jeremy Hylton | 546e34b | 2003-06-26 14:56:17 +0000 | [diff] [blame] | 505 | threading.settrace(self.globaltrace) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 506 | try: |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 507 | exec(cmd, globals, locals) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 508 | finally: |
| 509 | if not self.donothing: |
| 510 | sys.settrace(None) |
Jeremy Hylton | 546e34b | 2003-06-26 14:56:17 +0000 | [diff] [blame] | 511 | threading.settrace(None) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 512 | |
| 513 | def runfunc(self, func, *args, **kw): |
| 514 | result = None |
| 515 | if not self.donothing: |
| 516 | sys.settrace(self.globaltrace) |
| 517 | try: |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 518 | result = func(*args, **kw) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 519 | finally: |
| 520 | if not self.donothing: |
| 521 | sys.settrace(None) |
| 522 | return result |
| 523 | |
Skip Montanaro | 5bfd984 | 2004-04-10 16:29:58 +0000 | [diff] [blame] | 524 | def file_module_function_of(self, frame): |
| 525 | code = frame.f_code |
| 526 | filename = code.co_filename |
| 527 | if filename: |
| 528 | modulename = modname(filename) |
| 529 | else: |
| 530 | modulename = None |
| 531 | |
| 532 | funcname = code.co_name |
| 533 | clsname = None |
| 534 | if code in self._caller_cache: |
| 535 | if self._caller_cache[code] is not None: |
| 536 | clsname = self._caller_cache[code] |
| 537 | else: |
| 538 | self._caller_cache[code] = None |
| 539 | ## use of gc.get_referrers() was suggested by Michael Hudson |
| 540 | # all functions which refer to this code object |
| 541 | funcs = [f for f in gc.get_referrers(code) |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 542 | if hasattr(f, "__doc__")] |
Skip Montanaro | 5bfd984 | 2004-04-10 16:29:58 +0000 | [diff] [blame] | 543 | # require len(func) == 1 to avoid ambiguity caused by calls to |
| 544 | # new.function(): "In the face of ambiguity, refuse the |
| 545 | # temptation to guess." |
| 546 | if len(funcs) == 1: |
| 547 | dicts = [d for d in gc.get_referrers(funcs[0]) |
| 548 | if isinstance(d, dict)] |
| 549 | if len(dicts) == 1: |
| 550 | classes = [c for c in gc.get_referrers(dicts[0]) |
| 551 | if hasattr(c, "__bases__")] |
| 552 | if len(classes) == 1: |
| 553 | # ditto for new.classobj() |
| 554 | clsname = str(classes[0]) |
| 555 | # cache the result - assumption is that new.* is |
| 556 | # not called later to disturb this relationship |
| 557 | # _caller_cache could be flushed if functions in |
| 558 | # the new module get called. |
| 559 | self._caller_cache[code] = clsname |
| 560 | if clsname is not None: |
| 561 | # final hack - module name shows up in str(cls), but we've already |
| 562 | # computed module name, so remove it |
| 563 | clsname = clsname.split(".")[1:] |
| 564 | clsname = ".".join(clsname) |
| 565 | funcname = "%s.%s" % (clsname, funcname) |
| 566 | |
| 567 | return filename, modulename, funcname |
| 568 | |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 569 | def globaltrace_trackcallers(self, frame, why, arg): |
| 570 | """Handler for call events. |
| 571 | |
| 572 | Adds information about who called who to the self._callers dict. |
| 573 | """ |
| 574 | if why == 'call': |
| 575 | # XXX Should do a better job of identifying methods |
Skip Montanaro | 5bfd984 | 2004-04-10 16:29:58 +0000 | [diff] [blame] | 576 | this_func = self.file_module_function_of(frame) |
| 577 | parent_func = self.file_module_function_of(frame.f_back) |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 578 | self._callers[(parent_func, this_func)] = 1 |
| 579 | |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 580 | def globaltrace_countfuncs(self, frame, why, arg): |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 581 | """Handler for call events. |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 582 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 583 | Adds (filename, modulename, funcname) to the self._calledfuncs dict. |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 584 | """ |
| 585 | if why == 'call': |
Skip Montanaro | 5bfd984 | 2004-04-10 16:29:58 +0000 | [diff] [blame] | 586 | this_func = self.file_module_function_of(frame) |
| 587 | self._calledfuncs[this_func] = 1 |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 588 | |
| 589 | def globaltrace_lt(self, frame, why, arg): |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 590 | """Handler for call events. |
| 591 | |
| 592 | If the code block being entered is to be ignored, returns `None', |
| 593 | else returns self.localtrace. |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 594 | """ |
| 595 | if why == 'call': |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 596 | code = frame.f_code |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 597 | filename = frame.f_globals.get('__file__', None) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 598 | if filename: |
Jeremy Hylton | dfbfe73 | 2003-04-21 22:49:17 +0000 | [diff] [blame] | 599 | # XXX modname() doesn't work right for packages, so |
| 600 | # the ignore support won't work right for packages |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 601 | modulename = modname(filename) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 602 | if modulename is not None: |
| 603 | ignore_it = self.ignore.names(filename, modulename) |
| 604 | if not ignore_it: |
| 605 | if self.trace: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 606 | print((" --- modulename: %s, funcname: %s" |
| 607 | % (modulename, code.co_name))) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 608 | return self.localtrace |
| 609 | else: |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 610 | return None |
| 611 | |
| 612 | def localtrace_trace_and_count(self, frame, why, arg): |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 613 | if why == "line": |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 614 | # record the file name and line number of every trace |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 615 | filename = frame.f_code.co_filename |
| 616 | lineno = frame.f_lineno |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 617 | key = filename, lineno |
| 618 | self.counts[key] = self.counts.get(key, 0) + 1 |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 619 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame^] | 620 | if self.start_time: |
| 621 | print('%.2f' % (time.time() - self.start_time), end=' ') |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 622 | bname = os.path.basename(filename) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 623 | print("%s(%d): %s" % (bname, lineno, |
| 624 | linecache.getline(filename, lineno)), end=' ') |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 625 | return self.localtrace |
| 626 | |
| 627 | def localtrace_trace(self, frame, why, arg): |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 628 | if why == "line": |
| 629 | # record the file name and line number of every trace |
| 630 | filename = frame.f_code.co_filename |
| 631 | lineno = frame.f_lineno |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 632 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame^] | 633 | if self.start_time: |
| 634 | print('%.2f' % (time.time() - self.start_time), end=' ') |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 635 | bname = os.path.basename(filename) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 636 | print("%s(%d): %s" % (bname, lineno, |
| 637 | linecache.getline(filename, lineno)), end=' ') |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 638 | return self.localtrace |
| 639 | |
| 640 | def localtrace_count(self, frame, why, arg): |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 641 | if why == "line": |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 642 | filename = frame.f_code.co_filename |
| 643 | lineno = frame.f_lineno |
| 644 | key = filename, lineno |
| 645 | self.counts[key] = self.counts.get(key, 0) + 1 |
| 646 | return self.localtrace |
| 647 | |
| 648 | def results(self): |
| 649 | return CoverageResults(self.counts, infile=self.infile, |
| 650 | outfile=self.outfile, |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 651 | calledfuncs=self._calledfuncs, |
| 652 | callers=self._callers) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 653 | |
| 654 | def _err_exit(msg): |
| 655 | sys.stderr.write("%s: %s\n" % (sys.argv[0], msg)) |
| 656 | sys.exit(1) |
| 657 | |
| 658 | def main(argv=None): |
| 659 | import getopt |
| 660 | |
| 661 | if argv is None: |
| 662 | argv = sys.argv |
| 663 | try: |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame^] | 664 | opts, prog_argv = getopt.getopt(argv[1:], "tcrRf:d:msC:lTg", |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 665 | ["help", "version", "trace", "count", |
| 666 | "report", "no-report", "summary", |
| 667 | "file=", "missing", |
| 668 | "ignore-module=", "ignore-dir=", |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 669 | "coverdir=", "listfuncs", |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame^] | 670 | "trackcalls", "timing"]) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 671 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 672 | except getopt.error as msg: |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 673 | sys.stderr.write("%s: %s\n" % (sys.argv[0], msg)) |
| 674 | sys.stderr.write("Try `%s --help' for more information\n" |
| 675 | % sys.argv[0]) |
| 676 | sys.exit(1) |
| 677 | |
| 678 | trace = 0 |
| 679 | count = 0 |
| 680 | report = 0 |
| 681 | no_report = 0 |
| 682 | counts_file = None |
| 683 | missing = 0 |
| 684 | ignore_modules = [] |
| 685 | ignore_dirs = [] |
| 686 | coverdir = None |
| 687 | summary = 0 |
| 688 | listfuncs = False |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 689 | countcallers = False |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame^] | 690 | timing = False |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 691 | |
| 692 | for opt, val in opts: |
| 693 | if opt == "--help": |
| 694 | usage(sys.stdout) |
| 695 | sys.exit(0) |
| 696 | |
| 697 | if opt == "--version": |
| 698 | sys.stdout.write("trace 2.0\n") |
| 699 | sys.exit(0) |
| 700 | |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 701 | if opt == "-T" or opt == "--trackcalls": |
| 702 | countcallers = True |
| 703 | continue |
| 704 | |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 705 | if opt == "-l" or opt == "--listfuncs": |
| 706 | listfuncs = True |
| 707 | continue |
| 708 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame^] | 709 | if opt == "-g" or opt == "--timing": |
| 710 | timing = True |
| 711 | continue |
| 712 | |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 713 | if opt == "-t" or opt == "--trace": |
| 714 | trace = 1 |
| 715 | continue |
| 716 | |
| 717 | if opt == "-c" or opt == "--count": |
| 718 | count = 1 |
| 719 | continue |
| 720 | |
| 721 | if opt == "-r" or opt == "--report": |
| 722 | report = 1 |
| 723 | continue |
| 724 | |
| 725 | if opt == "-R" or opt == "--no-report": |
| 726 | no_report = 1 |
| 727 | continue |
| 728 | |
| 729 | if opt == "-f" or opt == "--file": |
| 730 | counts_file = val |
| 731 | continue |
| 732 | |
| 733 | if opt == "-m" or opt == "--missing": |
| 734 | missing = 1 |
| 735 | continue |
| 736 | |
| 737 | if opt == "-C" or opt == "--coverdir": |
| 738 | coverdir = val |
| 739 | continue |
| 740 | |
| 741 | if opt == "-s" or opt == "--summary": |
| 742 | summary = 1 |
| 743 | continue |
| 744 | |
| 745 | if opt == "--ignore-module": |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 746 | for mod in val.split(","): |
| 747 | ignore_modules.append(mod.strip()) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 748 | continue |
| 749 | |
| 750 | if opt == "--ignore-dir": |
| 751 | for s in val.split(os.pathsep): |
| 752 | s = os.path.expandvars(s) |
| 753 | # should I also call expanduser? (after all, could use $HOME) |
| 754 | |
| 755 | s = s.replace("$prefix", |
| 756 | os.path.join(sys.prefix, "lib", |
| 757 | "python" + sys.version[:3])) |
| 758 | s = s.replace("$exec_prefix", |
| 759 | os.path.join(sys.exec_prefix, "lib", |
| 760 | "python" + sys.version[:3])) |
| 761 | s = os.path.normpath(s) |
| 762 | ignore_dirs.append(s) |
| 763 | continue |
| 764 | |
| 765 | assert 0, "Should never get here" |
| 766 | |
| 767 | if listfuncs and (count or trace): |
| 768 | _err_exit("cannot specify both --listfuncs and (--trace or --count)") |
| 769 | |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 770 | if not (count or trace or report or listfuncs or countcallers): |
| 771 | _err_exit("must specify one of --trace, --count, --report, " |
| 772 | "--listfuncs, or --trackcalls") |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 773 | |
| 774 | if report and no_report: |
| 775 | _err_exit("cannot specify both --report and --no-report") |
| 776 | |
| 777 | if report and not counts_file: |
| 778 | _err_exit("--report requires a --file") |
| 779 | |
| 780 | if no_report and len(prog_argv) == 0: |
| 781 | _err_exit("missing name of file to run") |
| 782 | |
| 783 | # everything is ready |
| 784 | if report: |
| 785 | results = CoverageResults(infile=counts_file, outfile=counts_file) |
| 786 | results.write_results(missing, summary=summary, coverdir=coverdir) |
| 787 | else: |
| 788 | sys.argv = prog_argv |
| 789 | progname = prog_argv[0] |
| 790 | sys.path[0] = os.path.split(progname)[0] |
| 791 | |
| 792 | t = Trace(count, trace, countfuncs=listfuncs, |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 793 | countcallers=countcallers, ignoremods=ignore_modules, |
| 794 | ignoredirs=ignore_dirs, infile=counts_file, |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame^] | 795 | outfile=counts_file, timing=timing) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 796 | try: |
Neal Norwitz | 0168802 | 2007-08-12 00:43:29 +0000 | [diff] [blame] | 797 | fp = open(progname) |
| 798 | try: |
| 799 | script = fp.read() |
| 800 | finally: |
| 801 | fp.close() |
| 802 | t.run('exec(%r)' % (script,)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 803 | except IOError as err: |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 804 | _err_exit("Cannot run file %r because: %s" % (sys.argv[0], err)) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 805 | except SystemExit: |
| 806 | pass |
| 807 | |
| 808 | results = t.results() |
| 809 | |
| 810 | if not no_report: |
| 811 | results.write_results(missing, summary=summary, coverdir=coverdir) |
| 812 | |
| 813 | if __name__=='__main__': |
| 814 | main() |