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