blob: c01935f2460adcbde86f1dd83f79696576d15d81 [file] [log] [blame]
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +00001#!/usr/bin/env python
2
3# portions copyright 2001, Autonomous Zones Industries, Inc., all rights...
4# err... reserved and offered to the public under the terms of the
5# Python 2.2 license.
6# Author: Zooko O'Whielacronx
7# http://zooko.com/
8# mailto:zooko@zooko.com
9#
10# Copyright 2000, Mojam Media, Inc., all rights reserved.
11# Author: Skip Montanaro
12#
13# Copyright 1999, Bioreason, Inc., all rights reserved.
14# Author: Andrew Dalke
15#
16# Copyright 1995-1997, Automatrix, Inc., all rights reserved.
17# Author: Skip Montanaro
18#
19# Copyright 1991-1995, Stichting Mathematisch Centrum, all rights reserved.
20#
21#
22# Permission to use, copy, modify, and distribute this Python software and
23# its associated documentation for any purpose without fee is hereby
24# granted, provided that the above copyright notice appears in all copies,
25# and that both that copyright notice and this permission notice appear in
26# supporting documentation, and that the name of neither Automatrix,
27# Bioreason or Mojam Media be used in advertising or publicity pertaining to
28# distribution of the software without specific, written prior permission.
29#
30"""program/module to trace Python program or function execution
31
32Sample 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 Montanaro5bfd9842004-04-10 16:29:58 +000035 trace.py --trackcalls spam.py eggs
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +000036
37Sample use, programmatically
Thomas Wouters477c8d52006-05-27 19:21:47 +000038 import sys
39
40 # create a Trace object, telling it what to ignore, and whether to
41 # do tracing or line-counting or both.
42 tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], trace=0,
43 count=1)
44 # run the new command using the given tracer
45 tracer.run('main()')
46 # make a report, placing output in /tmp
47 r = tracer.results()
48 r.write_results(show_missing=True, coverdir="/tmp")
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +000049"""
50
Georg Brandl33c28812009-04-01 23:07:29 +000051import io
Jeremy Hylton38732e12003-04-21 22:04:46 +000052import linecache
Jeremy Hylton38732e12003-04-21 22:04:46 +000053import os
54import re
55import sys
Christian Heimes380f7f22008-02-28 11:19:05 +000056import time
Jeremy Hylton38732e12003-04-21 22:04:46 +000057import token
58import tokenize
Alexander Belopolsky96656372010-09-13 18:38:54 +000059import inspect
Skip Montanaro5bfd9842004-04-10 16:29:58 +000060import gc
Alexander Belopolsky402392b2010-09-24 18:08:24 +000061import dis
Guido van Rossum99603b02007-07-20 00:22:32 +000062import pickle
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +000063
Alexander Belopolsky997980f2010-11-06 01:35:01 +000064try:
65 import threading
66except ImportError:
67 _settrace = sys.settrace
68
69 def _unsettrace():
70 sys.settrace(None)
71else:
72 def _settrace(func):
73 threading.settrace(func)
74 sys.settrace(func)
75
76 def _unsettrace():
77 sys.settrace(None)
78 threading.settrace(None)
79
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +000080def usage(outfile):
81 outfile.write("""Usage: %s [OPTIONS] <file> [ARGS]
82
83Meta-options:
84--help Display this help then exit.
85--version Output version information then exit.
86
87Otherwise, exactly one of the following three options must be given:
88-t, --trace Print each line to sys.stdout before it is executed.
89-c, --count Count the number of times each line is executed
90 and write the counts to <module>.cover for each
91 module executed, in the module's directory.
92 See also `--coverdir', `--file', `--no-report' below.
Skip Montanaroa7b8ac62003-06-27 19:09:33 +000093-l, --listfuncs Keep track of which functions are executed at least
Fred Drake01c623b2003-06-27 19:22:11 +000094 once and write the results to sys.stdout after the
Skip Montanaroa7b8ac62003-06-27 19:09:33 +000095 program exits.
Skip Montanarocafc8112004-04-07 15:46:05 +000096-T, --trackcalls Keep track of caller/called pairs and write the
97 results to sys.stdout after the program exits.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +000098-r, --report Generate a report from a counts file; do not execute
99 any code. `--file' must specify the results file to
100 read, which must have been created in a previous run
101 with `--count --file=FILE'.
102
103Modifiers:
104-f, --file=<file> File to accumulate counts over several runs.
105-R, --no-report Do not generate the coverage report files.
106 Useful if you want to accumulate over several runs.
107-C, --coverdir=<dir> Directory where the report files. The coverage
108 report for <package>.<module> is written to file
109 <dir>/<package>/<module>.cover.
110-m, --missing Annotate executable lines that were not executed
111 with '>>>>>> '.
112-s, --summary Write a brief summary on stdout for each file.
113 (Can only be used with --count or --report.)
Christian Heimes380f7f22008-02-28 11:19:05 +0000114-g, --timing Prefix each line with the time since the program started.
115 Only used while tracing.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000116
117Filters, may be repeated multiple times:
Georg Brandlfceab5a2008-01-19 20:08:23 +0000118--ignore-module=<mod> Ignore the given module(s) and its submodules
119 (if it is a package). Accepts comma separated
120 list of module names
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000121--ignore-dir=<dir> Ignore files in the given directory (multiple
122 directories can be joined by os.pathsep).
123""" % sys.argv[0])
124
Jeremy Hylton38732e12003-04-21 22:04:46 +0000125PRAGMA_NOCOVER = "#pragma NO COVER"
126
127# Simple rx to find lines with no code.
128rx_blank = re.compile(r'^\s*(#.*)?$')
129
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000130class Ignore:
131 def __init__(self, modules = None, dirs = None):
132 self._mods = modules or []
133 self._dirs = dirs or []
134
Andrew M. Kuchling54e14f22010-02-22 22:21:05 +0000135 self._dirs = list(map(os.path.normpath, self._dirs))
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000136 self._ignore = { '<string>': 1 }
137
138 def names(self, filename, modulename):
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000139 if modulename in self._ignore:
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000140 return self._ignore[modulename]
141
142 # haven't seen this one before, so see if the module name is
143 # on the ignore list. Need to take some care since ignoring
144 # "cmp" musn't mean ignoring "cmpcache" but ignoring
145 # "Spam" must also mean ignoring "Spam.Eggs".
146 for mod in self._mods:
147 if mod == modulename: # Identical names, so ignore
148 self._ignore[modulename] = 1
149 return 1
150 # check if the module is a proper submodule of something on
151 # the ignore list
152 n = len(mod)
153 # (will not overflow since if the first n characters are the
Fred Drakedb390c12005-10-28 14:39:47 +0000154 # same and the name has not already occurred, then the size
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000155 # of "name" is greater than that of "mod")
156 if mod == modulename[:n] and modulename[n] == '.':
157 self._ignore[modulename] = 1
158 return 1
159
160 # Now check that __file__ isn't in one of the directories
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
Jeremy Hylton38732e12003-04-21 22:04:46 +0000183def modname(path):
184 """Return a plausible module name for the patch."""
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000185
Jeremy Hylton38732e12003-04-21 22:04:46 +0000186 base = os.path.basename(path)
187 filename, ext = os.path.splitext(base)
188 return filename
189
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000190def fullmodname(path):
Jeremy Hyltonc8c8b942003-04-22 15:35:51 +0000191 """Return a plausible module name for the path."""
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000192
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 Wouters00ee7ba2006-08-21 19:07:27 +0000198 comparepath = os.path.normcase(path)
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000199 longest = ""
200 for dir in sys.path:
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000201 dir = os.path.normcase(dir)
202 if comparepath.startswith(dir) and comparepath[len(dir)] == os.sep:
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000203 if len(dir) > len(longest):
204 longest = dir
205
Guido van Rossumb427c002003-10-10 23:02:01 +0000206 if longest:
207 base = path[len(longest) + 1:]
208 else:
209 base = path
Georg Brandlcea7e552010-08-01 18:56:30 +0000210 # the drive letter is never part of the module name
211 drive, base = os.path.splitdrive(base)
Guido van Rossumbbca8da2004-02-19 19:16:50 +0000212 base = base.replace(os.sep, ".")
213 if os.altsep:
214 base = base.replace(os.altsep, ".")
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000215 filename, ext = os.path.splitext(base)
Georg Brandlcea7e552010-08-01 18:56:30 +0000216 return filename.lstrip(".")
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000217
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000218class CoverageResults:
219 def __init__(self, counts=None, calledfuncs=None, infile=None,
Skip Montanarocafc8112004-04-07 15:46:05 +0000220 callers=None, outfile=None):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000221 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 Montanarocafc8112004-04-07 15:46:05 +0000229 self.callers = callers
230 if self.callers is None:
231 self.callers = {}
232 self.callers = self.callers.copy()
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000233 self.infile = infile
234 self.outfile = outfile
235 if self.infile:
Jeremy Hyltond7ce86d2003-07-07 16:08:47 +0000236 # Try to merge existing counts file.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000237 try:
Skip Montanarocafc8112004-04-07 15:46:05 +0000238 counts, calledfuncs, callers = \
239 pickle.load(open(self.infile, 'rb'))
240 self.update(self.__class__(counts, calledfuncs, callers))
Guido van Rossumb940e112007-01-10 16:19:56 +0000241 except (IOError, EOFError, ValueError) as err:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000242 print(("Skipping counts file %r: %s"
243 % (self.infile, err)), file=sys.stderr)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000244
Georg Brandl33c28812009-04-01 23:07:29 +0000245 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 """
249 return (filename == "<string>" or
250 filename.startswith("<doctest "))
251
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000252 def update(self, other):
253 """Merge in the data from another CoverageResults"""
254 counts = self.counts
255 calledfuncs = self.calledfuncs
Skip Montanarocafc8112004-04-07 15:46:05 +0000256 callers = self.callers
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000257 other_counts = other.counts
258 other_calledfuncs = other.calledfuncs
Skip Montanarocafc8112004-04-07 15:46:05 +0000259 other_callers = other.callers
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000260
261 for key in other_counts.keys():
Jeremy Hylton38732e12003-04-21 22:04:46 +0000262 counts[key] = counts.get(key, 0) + other_counts[key]
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000263
264 for key in other_calledfuncs.keys():
265 calledfuncs[key] = 1
266
Skip Montanarocafc8112004-04-07 15:46:05 +0000267 for key in other_callers.keys():
268 callers[key] = 1
269
Jeremy Hylton38732e12003-04-21 22:04:46 +0000270 def write_results(self, show_missing=True, summary=False, coverdir=None):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000271 """
272 @param coverdir
273 """
Skip Montanarocafc8112004-04-07 15:46:05 +0000274 if self.calledfuncs:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000275 print()
276 print("functions called:")
Alexander Belopolsky13aeb362010-07-20 20:13:45 +0000277 calls = self.calledfuncs.keys()
278 for filename, modulename, funcname in sorted(calls):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000279 print(("filename: %s, modulename: %s, funcname: %s"
280 % (filename, modulename, funcname)))
Skip Montanarocafc8112004-04-07 15:46:05 +0000281
282 if self.callers:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000283 print()
284 print("calling relationships:")
Skip Montanarocafc8112004-04-07 15:46:05 +0000285 lastfile = lastcfile = ""
Georg Brandl33c28812009-04-01 23:07:29 +0000286 for ((pfile, pmod, pfunc), (cfile, cmod, cfunc)) \
287 in sorted(self.callers.keys()):
Skip Montanarocafc8112004-04-07 15:46:05 +0000288 if pfile != lastfile:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000289 print()
290 print("***", pfile, "***")
Skip Montanarocafc8112004-04-07 15:46:05 +0000291 lastfile = pfile
292 lastcfile = ""
293 if cfile != pfile and lastcfile != cfile:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000294 print(" -->", cfile)
Skip Montanarocafc8112004-04-07 15:46:05 +0000295 lastcfile = cfile
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000296 print(" %s.%s -> %s.%s" % (pmod, pfunc, cmod, cfunc))
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000297
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000298 # turn the counts data ("(filename, lineno) = count") into something
299 # accessible on a per-file basis
300 per_file = {}
Jeremy Hylton38732e12003-04-21 22:04:46 +0000301 for filename, lineno in self.counts.keys():
302 lines_hit = per_file[filename] = per_file.get(filename, {})
303 lines_hit[lineno] = self.counts[(filename, lineno)]
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000304
305 # accumulate summary info, if needed
306 sums = {}
307
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000308 for filename, count in per_file.items():
Georg Brandl33c28812009-04-01 23:07:29 +0000309 if self.is_ignored_filename(filename):
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000310 continue
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000311
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000312 if filename.endswith((".pyc", ".pyo")):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000313 filename = filename[:-1]
314
Jeremy Hylton38732e12003-04-21 22:04:46 +0000315 if coverdir is None:
316 dir = os.path.dirname(os.path.abspath(filename))
Jeremy Hyltonc8c8b942003-04-22 15:35:51 +0000317 modulename = modname(filename)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000318 else:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000319 dir = coverdir
320 if not os.path.exists(dir):
321 os.makedirs(dir)
Jeremy Hyltonc8c8b942003-04-22 15:35:51 +0000322 modulename = fullmodname(filename)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000323
324 # If desired, get a list of the line numbers which represent
325 # executable content (returned as a dict for better lookup speed)
326 if show_missing:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000327 lnotab = find_executable_linenos(filename)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000328 else:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000329 lnotab = {}
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000330
Jeremy Hylton38732e12003-04-21 22:04:46 +0000331 source = linecache.getlines(filename)
332 coverpath = os.path.join(dir, modulename + ".cover")
333 n_hits, n_lines = self.write_results_file(coverpath, source,
334 lnotab, count)
Tim Peters0eadaac2003-04-24 16:02:54 +0000335
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000336 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 Rossumbe19ed72007-02-09 05:37:30 +0000341 print("lines cov% module (path)")
Martin v. Löwis8efc62c2008-04-10 19:02:25 +0000342 for m in sorted(sums.keys()):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000343 n_lines, percent, modulename, filename = sums[m]
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000344 print("%5d %3d%% %s (%s)" % sums[m])
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000345
346 if self.outfile:
347 # try and store counts and module info into self.outfile
348 try:
Skip Montanarocafc8112004-04-07 15:46:05 +0000349 pickle.dump((self.counts, self.calledfuncs, self.callers),
Jeremy Hyltond0e27052003-10-14 20:12:06 +0000350 open(self.outfile, 'wb'), 1)
Guido van Rossumb940e112007-01-10 16:19:56 +0000351 except IOError as err:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000352 print("Can't save counts files because %s" % err, file=sys.stderr)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000353
Jeremy Hylton38732e12003-04-21 22:04:46 +0000354 def write_results_file(self, path, lines, lnotab, lines_hit):
355 """Return a coverage results file in path."""
356
357 try:
358 outfile = open(path, "w")
Guido van Rossumb940e112007-01-10 16:19:56 +0000359 except IOError as err:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000360 print(("trace: Could not open %r for writing: %s"
361 "- skipping" % (path, err)), file=sys.stderr)
Guido van Rossumbbca8da2004-02-19 19:16:50 +0000362 return 0, 0
Jeremy Hylton38732e12003-04-21 22:04:46 +0000363
364 n_lines = 0
365 n_hits = 0
366 for i, line in enumerate(lines):
367 lineno = i + 1
368 # do the blank/comment match to try to mark more lines
369 # (help the reader find stuff that hasn't been covered)
370 if lineno in lines_hit:
371 outfile.write("%5d: " % lines_hit[lineno])
372 n_hits += 1
373 n_lines += 1
374 elif rx_blank.match(line):
Walter Dörwaldc1711722003-07-15 10:34:02 +0000375 outfile.write(" ")
Jeremy Hylton38732e12003-04-21 22:04:46 +0000376 else:
377 # lines preceded by no marks weren't hit
378 # Highlight them if so indicated, unless the line contains
379 # #pragma: NO COVER
380 if lineno in lnotab and not PRAGMA_NOCOVER in lines[i]:
381 outfile.write(">>>>>> ")
Jeremy Hylton546e34b2003-06-26 14:56:17 +0000382 n_lines += 1
Jeremy Hylton38732e12003-04-21 22:04:46 +0000383 else:
384 outfile.write(" ")
Jeremy Hylton38732e12003-04-21 22:04:46 +0000385 outfile.write(lines[i].expandtabs(8))
386 outfile.close()
387
388 return n_hits, n_lines
389
390def find_lines_from_code(code, strs):
391 """Return dict where keys are lines in the line number table."""
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000392 linenos = {}
393
Alexander Belopolsky402392b2010-09-24 18:08:24 +0000394 for _, lineno in dis.findlinestarts(code):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000395 if lineno not in strs:
396 linenos[lineno] = 1
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000397
398 return linenos
399
Jeremy Hylton38732e12003-04-21 22:04:46 +0000400def find_lines(code, strs):
401 """Return lineno dict for all code objects reachable from code."""
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000402 # get all of the lineno information from the code of this scope level
Jeremy Hylton38732e12003-04-21 22:04:46 +0000403 linenos = find_lines_from_code(code, strs)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000404
405 # and check the constants for references to other code objects
406 for c in code.co_consts:
Alexander Belopolsky96656372010-09-13 18:38:54 +0000407 if inspect.iscode(c):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000408 # find another code object, so recurse into it
Jeremy Hylton38732e12003-04-21 22:04:46 +0000409 linenos.update(find_lines(c, strs))
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000410 return linenos
411
Georg Brandl33c28812009-04-01 23:07:29 +0000412def find_strings(filename, encoding=None):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000413 """Return a dict of possible docstring positions.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000414
Jeremy Hylton38732e12003-04-21 22:04:46 +0000415 The dict maps line numbers to strings. There is an entry for
416 line that contains only a string or a part of a triple-quoted
417 string.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000418 """
Jeremy Hylton38732e12003-04-21 22:04:46 +0000419 d = {}
420 # If the first token is a string, then it's the module docstring.
421 # Add this special case so that the test in the loop passes.
422 prev_ttype = token.INDENT
Georg Brandl33c28812009-04-01 23:07:29 +0000423 f = open(filename, encoding=encoding)
Jeremy Hylton38732e12003-04-21 22:04:46 +0000424 for ttype, tstr, start, end, line in tokenize.generate_tokens(f.readline):
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
432 f.close()
433 return d
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000434
Jeremy Hylton38732e12003-04-21 22:04:46 +0000435def find_executable_linenos(filename):
436 """Return dict where keys are line numbers in the line number table."""
Jeremy Hylton38732e12003-04-21 22:04:46 +0000437 try:
Georg Brandl33c28812009-04-01 23:07:29 +0000438 with io.FileIO(filename, 'r') as file:
439 encoding, lines = tokenize.detect_encoding(file.readline)
440 prog = open(filename, "r", encoding=encoding).read()
Guido van Rossumb940e112007-01-10 16:19:56 +0000441 except IOError as err:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000442 print(("Not printing coverage data for %r: %s"
443 % (filename, err)), file=sys.stderr)
Jeremy Hylton38732e12003-04-21 22:04:46 +0000444 return {}
445 code = compile(prog, filename, "exec")
Georg Brandl33c28812009-04-01 23:07:29 +0000446 strs = find_strings(filename, encoding)
Jeremy Hylton38732e12003-04-21 22:04:46 +0000447 return find_lines(code, strs)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000448
449class Trace:
Skip Montanarocafc8112004-04-07 15:46:05 +0000450 def __init__(self, count=1, trace=1, countfuncs=0, countcallers=0,
Christian Heimes380f7f22008-02-28 11:19:05 +0000451 ignoremods=(), ignoredirs=(), infile=None, outfile=None,
452 timing=False):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000453 """
454 @param count true iff it should count number of times each
Tim Petersf2715e02003-02-19 02:35:07 +0000455 line is executed
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000456 @param trace true iff it should print out each line that is
Tim Petersf2715e02003-02-19 02:35:07 +0000457 being counted
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000458 @param countfuncs true iff it should just output a list of
459 (filename, modulename, funcname,) for functions
460 that were called at least once; This overrides
Tim Petersf2715e02003-02-19 02:35:07 +0000461 `count' and `trace'
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000462 @param ignoremods a list of the names of modules to ignore
463 @param ignoredirs a list of the names of directories to ignore
Tim Petersf2715e02003-02-19 02:35:07 +0000464 all of the (recursive) contents of
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000465 @param infile file from which to read stored counts to be
Tim Petersf2715e02003-02-19 02:35:07 +0000466 added into the results
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000467 @param outfile file in which to write the results
Christian Heimes380f7f22008-02-28 11:19:05 +0000468 @param timing true iff timing information be displayed
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000469 """
470 self.infile = infile
471 self.outfile = outfile
472 self.ignore = Ignore(ignoremods, ignoredirs)
473 self.counts = {} # keys are (filename, linenumber)
474 self.blabbed = {} # for debugging
475 self.pathtobasename = {} # for memoizing os.path.basename
476 self.donothing = 0
477 self.trace = trace
478 self._calledfuncs = {}
Skip Montanarocafc8112004-04-07 15:46:05 +0000479 self._callers = {}
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000480 self._caller_cache = {}
Christian Heimes380f7f22008-02-28 11:19:05 +0000481 self.start_time = None
482 if timing:
483 self.start_time = time.time()
Skip Montanarocafc8112004-04-07 15:46:05 +0000484 if countcallers:
485 self.globaltrace = self.globaltrace_trackcallers
486 elif countfuncs:
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000487 self.globaltrace = self.globaltrace_countfuncs
488 elif trace and count:
489 self.globaltrace = self.globaltrace_lt
490 self.localtrace = self.localtrace_trace_and_count
491 elif trace:
492 self.globaltrace = self.globaltrace_lt
493 self.localtrace = self.localtrace_trace
494 elif count:
495 self.globaltrace = self.globaltrace_lt
496 self.localtrace = self.localtrace_count
497 else:
498 # Ahem -- do nothing? Okay.
499 self.donothing = 1
500
501 def run(self, cmd):
502 import __main__
503 dict = __main__.__dict__
504 if not self.donothing:
Jeremy Hylton546e34b2003-06-26 14:56:17 +0000505 threading.settrace(self.globaltrace)
Georg Brandl4009c9e2010-10-06 08:26:09 +0000506 sys.settrace(self.globaltrace)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000507 try:
Georg Brandl7cae87c2006-09-06 06:51:57 +0000508 exec(cmd, dict, dict)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000509 finally:
510 if not self.donothing:
511 sys.settrace(None)
Jeremy Hylton546e34b2003-06-26 14:56:17 +0000512 threading.settrace(None)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000513
514 def runctx(self, cmd, globals=None, locals=None):
515 if globals is None: globals = {}
516 if locals is None: locals = {}
517 if not self.donothing:
Alexander Belopolsky997980f2010-11-06 01:35:01 +0000518 _settrace(self.globaltrace)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000519 try:
Georg Brandl7cae87c2006-09-06 06:51:57 +0000520 exec(cmd, globals, locals)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000521 finally:
522 if not self.donothing:
Alexander Belopolsky997980f2010-11-06 01:35:01 +0000523 _unsettrace()
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000524
525 def runfunc(self, func, *args, **kw):
526 result = None
527 if not self.donothing:
528 sys.settrace(self.globaltrace)
529 try:
Guido van Rossum68468eb2003-02-27 20:14:51 +0000530 result = func(*args, **kw)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000531 finally:
532 if not self.donothing:
533 sys.settrace(None)
534 return result
535
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000536 def file_module_function_of(self, frame):
537 code = frame.f_code
538 filename = code.co_filename
539 if filename:
540 modulename = modname(filename)
541 else:
542 modulename = None
543
544 funcname = code.co_name
545 clsname = None
546 if code in self._caller_cache:
547 if self._caller_cache[code] is not None:
548 clsname = self._caller_cache[code]
549 else:
550 self._caller_cache[code] = None
551 ## use of gc.get_referrers() was suggested by Michael Hudson
552 # all functions which refer to this code object
553 funcs = [f for f in gc.get_referrers(code)
Alexander Belopolsky96656372010-09-13 18:38:54 +0000554 if inspect.isfunction(f)]
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000555 # require len(func) == 1 to avoid ambiguity caused by calls to
556 # new.function(): "In the face of ambiguity, refuse the
557 # temptation to guess."
558 if len(funcs) == 1:
559 dicts = [d for d in gc.get_referrers(funcs[0])
560 if isinstance(d, dict)]
561 if len(dicts) == 1:
562 classes = [c for c in gc.get_referrers(dicts[0])
563 if hasattr(c, "__bases__")]
564 if len(classes) == 1:
565 # ditto for new.classobj()
Alexander Belopolsky96656372010-09-13 18:38:54 +0000566 clsname = classes[0].__name__
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000567 # cache the result - assumption is that new.* is
568 # not called later to disturb this relationship
569 # _caller_cache could be flushed if functions in
570 # the new module get called.
571 self._caller_cache[code] = clsname
572 if clsname is not None:
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000573 funcname = "%s.%s" % (clsname, funcname)
574
575 return filename, modulename, funcname
576
Skip Montanarocafc8112004-04-07 15:46:05 +0000577 def globaltrace_trackcallers(self, frame, why, arg):
578 """Handler for call events.
579
580 Adds information about who called who to the self._callers dict.
581 """
582 if why == 'call':
583 # XXX Should do a better job of identifying methods
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000584 this_func = self.file_module_function_of(frame)
585 parent_func = self.file_module_function_of(frame.f_back)
Skip Montanarocafc8112004-04-07 15:46:05 +0000586 self._callers[(parent_func, this_func)] = 1
587
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000588 def globaltrace_countfuncs(self, frame, why, arg):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000589 """Handler for call events.
Tim Peters0eadaac2003-04-24 16:02:54 +0000590
Jeremy Hylton38732e12003-04-21 22:04:46 +0000591 Adds (filename, modulename, funcname) to the self._calledfuncs dict.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000592 """
593 if why == 'call':
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000594 this_func = self.file_module_function_of(frame)
595 self._calledfuncs[this_func] = 1
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000596
597 def globaltrace_lt(self, frame, why, arg):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000598 """Handler for call events.
599
600 If the code block being entered is to be ignored, returns `None',
601 else returns self.localtrace.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000602 """
603 if why == 'call':
Jeremy Hylton38732e12003-04-21 22:04:46 +0000604 code = frame.f_code
Thomas Wouterscf297e42007-02-23 15:07:44 +0000605 filename = frame.f_globals.get('__file__', None)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000606 if filename:
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000607 # XXX modname() doesn't work right for packages, so
608 # the ignore support won't work right for packages
Jeremy Hylton38732e12003-04-21 22:04:46 +0000609 modulename = modname(filename)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000610 if modulename is not None:
611 ignore_it = self.ignore.names(filename, modulename)
612 if not ignore_it:
613 if self.trace:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000614 print((" --- modulename: %s, funcname: %s"
615 % (modulename, code.co_name)))
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000616 return self.localtrace
617 else:
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000618 return None
619
620 def localtrace_trace_and_count(self, frame, why, arg):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000621 if why == "line":
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000622 # record the file name and line number of every trace
Jeremy Hylton38732e12003-04-21 22:04:46 +0000623 filename = frame.f_code.co_filename
624 lineno = frame.f_lineno
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000625 key = filename, lineno
626 self.counts[key] = self.counts.get(key, 0) + 1
Tim Petersf2715e02003-02-19 02:35:07 +0000627
Christian Heimes380f7f22008-02-28 11:19:05 +0000628 if self.start_time:
629 print('%.2f' % (time.time() - self.start_time), end=' ')
Jeremy Hylton38732e12003-04-21 22:04:46 +0000630 bname = os.path.basename(filename)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000631 print("%s(%d): %s" % (bname, lineno,
Georg Brandl4009c9e2010-10-06 08:26:09 +0000632 linecache.getline(filename, lineno)), end='')
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000633 return self.localtrace
634
635 def localtrace_trace(self, frame, why, arg):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000636 if why == "line":
637 # record the file name and line number of every trace
638 filename = frame.f_code.co_filename
639 lineno = frame.f_lineno
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000640
Christian Heimes380f7f22008-02-28 11:19:05 +0000641 if self.start_time:
642 print('%.2f' % (time.time() - self.start_time), end=' ')
Jeremy Hylton38732e12003-04-21 22:04:46 +0000643 bname = os.path.basename(filename)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000644 print("%s(%d): %s" % (bname, lineno,
Georg Brandl4009c9e2010-10-06 08:26:09 +0000645 linecache.getline(filename, lineno)), end='')
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000646 return self.localtrace
647
648 def localtrace_count(self, frame, why, arg):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000649 if why == "line":
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000650 filename = frame.f_code.co_filename
651 lineno = frame.f_lineno
652 key = filename, lineno
653 self.counts[key] = self.counts.get(key, 0) + 1
654 return self.localtrace
655
656 def results(self):
657 return CoverageResults(self.counts, infile=self.infile,
658 outfile=self.outfile,
Skip Montanarocafc8112004-04-07 15:46:05 +0000659 calledfuncs=self._calledfuncs,
660 callers=self._callers)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000661
662def _err_exit(msg):
663 sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
664 sys.exit(1)
665
666def main(argv=None):
667 import getopt
668
669 if argv is None:
670 argv = sys.argv
671 try:
Christian Heimes380f7f22008-02-28 11:19:05 +0000672 opts, prog_argv = getopt.getopt(argv[1:], "tcrRf:d:msC:lTg",
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000673 ["help", "version", "trace", "count",
674 "report", "no-report", "summary",
675 "file=", "missing",
676 "ignore-module=", "ignore-dir=",
Skip Montanarocafc8112004-04-07 15:46:05 +0000677 "coverdir=", "listfuncs",
Christian Heimes380f7f22008-02-28 11:19:05 +0000678 "trackcalls", "timing"])
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000679
Guido van Rossumb940e112007-01-10 16:19:56 +0000680 except getopt.error as msg:
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000681 sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
682 sys.stderr.write("Try `%s --help' for more information\n"
683 % sys.argv[0])
684 sys.exit(1)
685
686 trace = 0
687 count = 0
688 report = 0
689 no_report = 0
690 counts_file = None
691 missing = 0
692 ignore_modules = []
693 ignore_dirs = []
694 coverdir = None
695 summary = 0
696 listfuncs = False
Skip Montanarocafc8112004-04-07 15:46:05 +0000697 countcallers = False
Christian Heimes380f7f22008-02-28 11:19:05 +0000698 timing = False
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000699
700 for opt, val in opts:
701 if opt == "--help":
702 usage(sys.stdout)
703 sys.exit(0)
704
705 if opt == "--version":
706 sys.stdout.write("trace 2.0\n")
707 sys.exit(0)
708
Skip Montanarocafc8112004-04-07 15:46:05 +0000709 if opt == "-T" or opt == "--trackcalls":
710 countcallers = True
711 continue
712
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000713 if opt == "-l" or opt == "--listfuncs":
714 listfuncs = True
715 continue
716
Christian Heimes380f7f22008-02-28 11:19:05 +0000717 if opt == "-g" or opt == "--timing":
718 timing = True
719 continue
720
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000721 if opt == "-t" or opt == "--trace":
722 trace = 1
723 continue
724
725 if opt == "-c" or opt == "--count":
726 count = 1
727 continue
728
729 if opt == "-r" or opt == "--report":
730 report = 1
731 continue
732
733 if opt == "-R" or opt == "--no-report":
734 no_report = 1
735 continue
736
737 if opt == "-f" or opt == "--file":
738 counts_file = val
739 continue
740
741 if opt == "-m" or opt == "--missing":
742 missing = 1
743 continue
744
745 if opt == "-C" or opt == "--coverdir":
746 coverdir = val
747 continue
748
749 if opt == "-s" or opt == "--summary":
750 summary = 1
751 continue
752
753 if opt == "--ignore-module":
Georg Brandlfceab5a2008-01-19 20:08:23 +0000754 for mod in val.split(","):
755 ignore_modules.append(mod.strip())
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000756 continue
757
758 if opt == "--ignore-dir":
759 for s in val.split(os.pathsep):
760 s = os.path.expandvars(s)
761 # should I also call expanduser? (after all, could use $HOME)
762
763 s = s.replace("$prefix",
764 os.path.join(sys.prefix, "lib",
765 "python" + sys.version[:3]))
766 s = s.replace("$exec_prefix",
767 os.path.join(sys.exec_prefix, "lib",
768 "python" + sys.version[:3]))
769 s = os.path.normpath(s)
770 ignore_dirs.append(s)
771 continue
772
773 assert 0, "Should never get here"
774
775 if listfuncs and (count or trace):
776 _err_exit("cannot specify both --listfuncs and (--trace or --count)")
777
Skip Montanarocafc8112004-04-07 15:46:05 +0000778 if not (count or trace or report or listfuncs or countcallers):
779 _err_exit("must specify one of --trace, --count, --report, "
780 "--listfuncs, or --trackcalls")
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000781
782 if report and no_report:
783 _err_exit("cannot specify both --report and --no-report")
784
785 if report and not counts_file:
786 _err_exit("--report requires a --file")
787
788 if no_report and len(prog_argv) == 0:
789 _err_exit("missing name of file to run")
790
791 # everything is ready
792 if report:
793 results = CoverageResults(infile=counts_file, outfile=counts_file)
794 results.write_results(missing, summary=summary, coverdir=coverdir)
795 else:
796 sys.argv = prog_argv
797 progname = prog_argv[0]
798 sys.path[0] = os.path.split(progname)[0]
799
800 t = Trace(count, trace, countfuncs=listfuncs,
Skip Montanarocafc8112004-04-07 15:46:05 +0000801 countcallers=countcallers, ignoremods=ignore_modules,
802 ignoredirs=ignore_dirs, infile=counts_file,
Christian Heimes380f7f22008-02-28 11:19:05 +0000803 outfile=counts_file, timing=timing)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000804 try:
Alexander Belopolskyeeec92f2010-07-21 17:50:34 +0000805 with open(progname) as fp:
806 code = compile(fp.read(), progname, 'exec')
Georg Brandlcea7e552010-08-01 18:56:30 +0000807 # try to emulate __main__ namespace as much as possible
808 globs = {
809 '__file__': progname,
810 '__name__': '__main__',
811 '__package__': None,
812 '__cached__': None,
813 }
814 t.runctx(code, globs, globs)
Guido van Rossumb940e112007-01-10 16:19:56 +0000815 except IOError as err:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000816 _err_exit("Cannot run file %r because: %s" % (sys.argv[0], err))
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000817 except SystemExit:
818 pass
819
820 results = t.results()
821
822 if not no_report:
823 results.write_results(missing, summary=summary, coverdir=coverdir)
824
825if __name__=='__main__':
826 main()