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