blob: ec45d812ac6ac5c3a55e56d06eb331229b6255d2 [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#!/usr/bin/env python3
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +00002
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
Jeremy Hylton546e34b2003-06-26 14:56:17 +000056import threading
Christian Heimes380f7f22008-02-28 11:19:05 +000057import time
Jeremy Hylton38732e12003-04-21 22:04:46 +000058import token
59import tokenize
Alexander Belopolsky4d770172010-09-13 18:14:34 +000060import inspect
Skip Montanaro5bfd9842004-04-10 16:29:58 +000061import gc
Alexander Belopolskyff09ce22010-09-24 18:03:12 +000062import dis
Guido van Rossum99603b02007-07-20 00:22:32 +000063import pickle
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +000064
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +000065def usage(outfile):
66 outfile.write("""Usage: %s [OPTIONS] <file> [ARGS]
67
68Meta-options:
69--help Display this help then exit.
70--version Output version information then exit.
71
72Otherwise, exactly one of the following three options must be given:
73-t, --trace Print each line to sys.stdout before it is executed.
74-c, --count Count the number of times each line is executed
75 and write the counts to <module>.cover for each
76 module executed, in the module's directory.
77 See also `--coverdir', `--file', `--no-report' below.
Skip Montanaroa7b8ac62003-06-27 19:09:33 +000078-l, --listfuncs Keep track of which functions are executed at least
Fred Drake01c623b2003-06-27 19:22:11 +000079 once and write the results to sys.stdout after the
Skip Montanaroa7b8ac62003-06-27 19:09:33 +000080 program exits.
Skip Montanarocafc8112004-04-07 15:46:05 +000081-T, --trackcalls Keep track of caller/called pairs and write the
82 results to sys.stdout after the program exits.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +000083-r, --report Generate a report from a counts file; do not execute
84 any code. `--file' must specify the results file to
85 read, which must have been created in a previous run
86 with `--count --file=FILE'.
87
88Modifiers:
89-f, --file=<file> File to accumulate counts over several runs.
90-R, --no-report Do not generate the coverage report files.
91 Useful if you want to accumulate over several runs.
92-C, --coverdir=<dir> Directory where the report files. The coverage
93 report for <package>.<module> is written to file
94 <dir>/<package>/<module>.cover.
95-m, --missing Annotate executable lines that were not executed
96 with '>>>>>> '.
97-s, --summary Write a brief summary on stdout for each file.
98 (Can only be used with --count or --report.)
Christian Heimes380f7f22008-02-28 11:19:05 +000099-g, --timing Prefix each line with the time since the program started.
100 Only used while tracing.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000101
102Filters, may be repeated multiple times:
Georg Brandlfceab5a2008-01-19 20:08:23 +0000103--ignore-module=<mod> Ignore the given module(s) and its submodules
104 (if it is a package). Accepts comma separated
105 list of module names
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000106--ignore-dir=<dir> Ignore files in the given directory (multiple
107 directories can be joined by os.pathsep).
108""" % sys.argv[0])
109
Jeremy Hylton38732e12003-04-21 22:04:46 +0000110PRAGMA_NOCOVER = "#pragma NO COVER"
111
112# Simple rx to find lines with no code.
113rx_blank = re.compile(r'^\s*(#.*)?$')
114
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000115class Ignore:
116 def __init__(self, modules = None, dirs = None):
117 self._mods = modules or []
118 self._dirs = dirs or []
119
Andrew M. Kuchlinge1f9beb2010-02-22 22:16:58 +0000120 self._dirs = list(map(os.path.normpath, self._dirs))
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000121 self._ignore = { '<string>': 1 }
122
123 def names(self, filename, modulename):
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000124 if modulename in self._ignore:
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000125 return self._ignore[modulename]
126
127 # haven't seen this one before, so see if the module name is
128 # on the ignore list. Need to take some care since ignoring
129 # "cmp" musn't mean ignoring "cmpcache" but ignoring
130 # "Spam" must also mean ignoring "Spam.Eggs".
131 for mod in self._mods:
132 if mod == modulename: # Identical names, so ignore
133 self._ignore[modulename] = 1
134 return 1
135 # check if the module is a proper submodule of something on
136 # the ignore list
137 n = len(mod)
138 # (will not overflow since if the first n characters are the
Fred Drakedb390c12005-10-28 14:39:47 +0000139 # same and the name has not already occurred, then the size
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000140 # of "name" is greater than that of "mod")
141 if mod == modulename[:n] and modulename[n] == '.':
142 self._ignore[modulename] = 1
143 return 1
144
145 # Now check that __file__ isn't in one of the directories
146 if filename is None:
147 # must be a built-in, so we must ignore
148 self._ignore[modulename] = 1
149 return 1
150
151 # Ignore a file when it contains one of the ignorable paths
152 for d in self._dirs:
153 # The '+ os.sep' is to ensure that d is a parent directory,
154 # as compared to cases like:
155 # d = "/usr/local"
156 # filename = "/usr/local.py"
157 # or
158 # d = "/usr/local.py"
159 # filename = "/usr/local.py"
160 if filename.startswith(d + os.sep):
161 self._ignore[modulename] = 1
162 return 1
163
164 # Tried the different ways, so we don't ignore this module
165 self._ignore[modulename] = 0
166 return 0
167
Jeremy Hylton38732e12003-04-21 22:04:46 +0000168def modname(path):
169 """Return a plausible module name for the patch."""
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000170
Jeremy Hylton38732e12003-04-21 22:04:46 +0000171 base = os.path.basename(path)
172 filename, ext = os.path.splitext(base)
173 return filename
174
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000175def fullmodname(path):
Jeremy Hyltonc8c8b942003-04-22 15:35:51 +0000176 """Return a plausible module name for the path."""
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000177
178 # If the file 'path' is part of a package, then the filename isn't
179 # enough to uniquely identify it. Try to do the right thing by
180 # looking in sys.path for the longest matching prefix. We'll
181 # assume that the rest is the package name.
182
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000183 comparepath = os.path.normcase(path)
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000184 longest = ""
185 for dir in sys.path:
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000186 dir = os.path.normcase(dir)
187 if comparepath.startswith(dir) and comparepath[len(dir)] == os.sep:
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000188 if len(dir) > len(longest):
189 longest = dir
190
Guido van Rossumb427c002003-10-10 23:02:01 +0000191 if longest:
192 base = path[len(longest) + 1:]
193 else:
194 base = path
Georg Brandl120d6332010-08-01 14:38:17 +0000195 # the drive letter is never part of the module name
196 drive, base = os.path.splitdrive(base)
Guido van Rossumbbca8da2004-02-19 19:16:50 +0000197 base = base.replace(os.sep, ".")
198 if os.altsep:
199 base = base.replace(os.altsep, ".")
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000200 filename, ext = os.path.splitext(base)
Georg Brandl120d6332010-08-01 14:38:17 +0000201 return filename.lstrip(".")
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000202
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000203class CoverageResults:
204 def __init__(self, counts=None, calledfuncs=None, infile=None,
Skip Montanarocafc8112004-04-07 15:46:05 +0000205 callers=None, outfile=None):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000206 self.counts = counts
207 if self.counts is None:
208 self.counts = {}
209 self.counter = self.counts.copy() # map (filename, lineno) to count
210 self.calledfuncs = calledfuncs
211 if self.calledfuncs is None:
212 self.calledfuncs = {}
213 self.calledfuncs = self.calledfuncs.copy()
Skip Montanarocafc8112004-04-07 15:46:05 +0000214 self.callers = callers
215 if self.callers is None:
216 self.callers = {}
217 self.callers = self.callers.copy()
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000218 self.infile = infile
219 self.outfile = outfile
220 if self.infile:
Jeremy Hyltond7ce86d2003-07-07 16:08:47 +0000221 # Try to merge existing counts file.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000222 try:
Skip Montanarocafc8112004-04-07 15:46:05 +0000223 counts, calledfuncs, callers = \
224 pickle.load(open(self.infile, 'rb'))
225 self.update(self.__class__(counts, calledfuncs, callers))
Guido van Rossumb940e112007-01-10 16:19:56 +0000226 except (IOError, EOFError, ValueError) as err:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000227 print(("Skipping counts file %r: %s"
228 % (self.infile, err)), file=sys.stderr)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000229
Georg Brandl33c28812009-04-01 23:07:29 +0000230 def is_ignored_filename(self, filename):
231 """Return True if the filename does not refer to a file
232 we want to have reported.
233 """
234 return (filename == "<string>" or
235 filename.startswith("<doctest "))
236
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000237 def update(self, other):
238 """Merge in the data from another CoverageResults"""
239 counts = self.counts
240 calledfuncs = self.calledfuncs
Skip Montanarocafc8112004-04-07 15:46:05 +0000241 callers = self.callers
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000242 other_counts = other.counts
243 other_calledfuncs = other.calledfuncs
Skip Montanarocafc8112004-04-07 15:46:05 +0000244 other_callers = other.callers
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000245
Alexander Belopolsky0ae33612010-09-27 15:49:20 +0000246 for key in other_counts:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000247 counts[key] = counts.get(key, 0) + other_counts[key]
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000248
Alexander Belopolsky0ae33612010-09-27 15:49:20 +0000249 for key in other_calledfuncs:
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000250 calledfuncs[key] = 1
251
Alexander Belopolsky0ae33612010-09-27 15:49:20 +0000252 for key in other_callers:
Skip Montanarocafc8112004-04-07 15:46:05 +0000253 callers[key] = 1
254
Jeremy Hylton38732e12003-04-21 22:04:46 +0000255 def write_results(self, show_missing=True, summary=False, coverdir=None):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000256 """
257 @param coverdir
258 """
Skip Montanarocafc8112004-04-07 15:46:05 +0000259 if self.calledfuncs:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000260 print()
261 print("functions called:")
Alexander Belopolsky0ae33612010-09-27 15:49:20 +0000262 calls = self.calledfuncs
Alexander Belopolsky533a1672010-07-20 19:55:18 +0000263 for filename, modulename, funcname in sorted(calls):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000264 print(("filename: %s, modulename: %s, funcname: %s"
265 % (filename, modulename, funcname)))
Skip Montanarocafc8112004-04-07 15:46:05 +0000266
267 if self.callers:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000268 print()
269 print("calling relationships:")
Skip Montanarocafc8112004-04-07 15:46:05 +0000270 lastfile = lastcfile = ""
Georg Brandl33c28812009-04-01 23:07:29 +0000271 for ((pfile, pmod, pfunc), (cfile, cmod, cfunc)) \
Alexander Belopolsky0ae33612010-09-27 15:49:20 +0000272 in sorted(self.callers):
Skip Montanarocafc8112004-04-07 15:46:05 +0000273 if pfile != lastfile:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000274 print()
275 print("***", pfile, "***")
Skip Montanarocafc8112004-04-07 15:46:05 +0000276 lastfile = pfile
277 lastcfile = ""
278 if cfile != pfile and lastcfile != cfile:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000279 print(" -->", cfile)
Skip Montanarocafc8112004-04-07 15:46:05 +0000280 lastcfile = cfile
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000281 print(" %s.%s -> %s.%s" % (pmod, pfunc, cmod, cfunc))
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000282
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000283 # turn the counts data ("(filename, lineno) = count") into something
284 # accessible on a per-file basis
285 per_file = {}
Alexander Belopolsky0ae33612010-09-27 15:49:20 +0000286 for filename, lineno in self.counts:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000287 lines_hit = per_file[filename] = per_file.get(filename, {})
288 lines_hit[lineno] = self.counts[(filename, lineno)]
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000289
290 # accumulate summary info, if needed
291 sums = {}
292
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000293 for filename, count in per_file.items():
Georg Brandl33c28812009-04-01 23:07:29 +0000294 if self.is_ignored_filename(filename):
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000295 continue
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000296
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000297 if filename.endswith((".pyc", ".pyo")):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000298 filename = filename[:-1]
299
Jeremy Hylton38732e12003-04-21 22:04:46 +0000300 if coverdir is None:
301 dir = os.path.dirname(os.path.abspath(filename))
Jeremy Hyltonc8c8b942003-04-22 15:35:51 +0000302 modulename = modname(filename)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000303 else:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000304 dir = coverdir
305 if not os.path.exists(dir):
306 os.makedirs(dir)
Jeremy Hyltonc8c8b942003-04-22 15:35:51 +0000307 modulename = fullmodname(filename)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000308
309 # If desired, get a list of the line numbers which represent
310 # executable content (returned as a dict for better lookup speed)
311 if show_missing:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000312 lnotab = find_executable_linenos(filename)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000313 else:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000314 lnotab = {}
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000315
Jeremy Hylton38732e12003-04-21 22:04:46 +0000316 source = linecache.getlines(filename)
317 coverpath = os.path.join(dir, modulename + ".cover")
318 n_hits, n_lines = self.write_results_file(coverpath, source,
319 lnotab, count)
Tim Peters0eadaac2003-04-24 16:02:54 +0000320
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000321 if summary and n_lines:
322 percent = int(100 * n_hits / n_lines)
323 sums[modulename] = n_lines, percent, modulename, filename
324
325 if summary and sums:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000326 print("lines cov% module (path)")
Alexander Belopolsky0ae33612010-09-27 15:49:20 +0000327 for m in sorted(sums):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000328 n_lines, percent, modulename, filename = sums[m]
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000329 print("%5d %3d%% %s (%s)" % sums[m])
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000330
331 if self.outfile:
332 # try and store counts and module info into self.outfile
333 try:
Skip Montanarocafc8112004-04-07 15:46:05 +0000334 pickle.dump((self.counts, self.calledfuncs, self.callers),
Jeremy Hyltond0e27052003-10-14 20:12:06 +0000335 open(self.outfile, 'wb'), 1)
Guido van Rossumb940e112007-01-10 16:19:56 +0000336 except IOError as err:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000337 print("Can't save counts files because %s" % err, file=sys.stderr)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000338
Jeremy Hylton38732e12003-04-21 22:04:46 +0000339 def write_results_file(self, path, lines, lnotab, lines_hit):
340 """Return a coverage results file in path."""
341
342 try:
343 outfile = open(path, "w")
Guido van Rossumb940e112007-01-10 16:19:56 +0000344 except IOError as err:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000345 print(("trace: Could not open %r for writing: %s"
346 "- skipping" % (path, err)), file=sys.stderr)
Guido van Rossumbbca8da2004-02-19 19:16:50 +0000347 return 0, 0
Jeremy Hylton38732e12003-04-21 22:04:46 +0000348
349 n_lines = 0
350 n_hits = 0
Alexander Belopolsky0ae33612010-09-27 15:49:20 +0000351 for lineno, line in enumerate(lines, 1):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000352 # do the blank/comment match to try to mark more lines
353 # (help the reader find stuff that hasn't been covered)
354 if lineno in lines_hit:
355 outfile.write("%5d: " % lines_hit[lineno])
356 n_hits += 1
357 n_lines += 1
358 elif rx_blank.match(line):
Walter Dörwaldc1711722003-07-15 10:34:02 +0000359 outfile.write(" ")
Jeremy Hylton38732e12003-04-21 22:04:46 +0000360 else:
361 # lines preceded by no marks weren't hit
362 # Highlight them if so indicated, unless the line contains
363 # #pragma: NO COVER
Alexander Belopolsky0ae33612010-09-27 15:49:20 +0000364 if lineno in lnotab and not PRAGMA_NOCOVER in line:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000365 outfile.write(">>>>>> ")
Jeremy Hylton546e34b2003-06-26 14:56:17 +0000366 n_lines += 1
Jeremy Hylton38732e12003-04-21 22:04:46 +0000367 else:
368 outfile.write(" ")
Alexander Belopolsky0ae33612010-09-27 15:49:20 +0000369 outfile.write(line.expandtabs(8))
Jeremy Hylton38732e12003-04-21 22:04:46 +0000370 outfile.close()
371
372 return n_hits, n_lines
373
374def find_lines_from_code(code, strs):
375 """Return dict where keys are lines in the line number table."""
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000376 linenos = {}
377
Alexander Belopolskyff09ce22010-09-24 18:03:12 +0000378 for _, lineno in dis.findlinestarts(code):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000379 if lineno not in strs:
380 linenos[lineno] = 1
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000381
382 return linenos
383
Jeremy Hylton38732e12003-04-21 22:04:46 +0000384def find_lines(code, strs):
385 """Return lineno dict for all code objects reachable from code."""
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000386 # get all of the lineno information from the code of this scope level
Jeremy Hylton38732e12003-04-21 22:04:46 +0000387 linenos = find_lines_from_code(code, strs)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000388
389 # and check the constants for references to other code objects
390 for c in code.co_consts:
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000391 if inspect.iscode(c):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000392 # find another code object, so recurse into it
Jeremy Hylton38732e12003-04-21 22:04:46 +0000393 linenos.update(find_lines(c, strs))
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000394 return linenos
395
Georg Brandl33c28812009-04-01 23:07:29 +0000396def find_strings(filename, encoding=None):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000397 """Return a dict of possible docstring positions.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000398
Jeremy Hylton38732e12003-04-21 22:04:46 +0000399 The dict maps line numbers to strings. There is an entry for
400 line that contains only a string or a part of a triple-quoted
401 string.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000402 """
Jeremy Hylton38732e12003-04-21 22:04:46 +0000403 d = {}
404 # If the first token is a string, then it's the module docstring.
405 # Add this special case so that the test in the loop passes.
406 prev_ttype = token.INDENT
Benjamin Petersonb2fda232010-10-30 23:51:34 +0000407 with open(filename, encoding=encoding) as f:
408 tok = tokenize.generate_tokens(f.readline)
409 for ttype, tstr, start, end, line in tok:
410 if ttype == token.STRING:
411 if prev_ttype == token.INDENT:
412 sline, scol = start
413 eline, ecol = end
414 for i in range(sline, eline + 1):
415 d[i] = 1
416 prev_ttype = ttype
Jeremy Hylton38732e12003-04-21 22:04:46 +0000417 return d
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000418
Jeremy Hylton38732e12003-04-21 22:04:46 +0000419def find_executable_linenos(filename):
420 """Return dict where keys are line numbers in the line number table."""
Jeremy Hylton38732e12003-04-21 22:04:46 +0000421 try:
Georg Brandl33c28812009-04-01 23:07:29 +0000422 with io.FileIO(filename, 'r') as file:
423 encoding, lines = tokenize.detect_encoding(file.readline)
Benjamin Petersonb2fda232010-10-30 23:51:34 +0000424 with open(filename, "r", encoding=encoding) as f:
425 prog = f.read()
Guido van Rossumb940e112007-01-10 16:19:56 +0000426 except IOError as err:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000427 print(("Not printing coverage data for %r: %s"
428 % (filename, err)), file=sys.stderr)
Jeremy Hylton38732e12003-04-21 22:04:46 +0000429 return {}
430 code = compile(prog, filename, "exec")
Georg Brandl33c28812009-04-01 23:07:29 +0000431 strs = find_strings(filename, encoding)
Jeremy Hylton38732e12003-04-21 22:04:46 +0000432 return find_lines(code, strs)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000433
434class Trace:
Skip Montanarocafc8112004-04-07 15:46:05 +0000435 def __init__(self, count=1, trace=1, countfuncs=0, countcallers=0,
Christian Heimes380f7f22008-02-28 11:19:05 +0000436 ignoremods=(), ignoredirs=(), infile=None, outfile=None,
437 timing=False):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000438 """
439 @param count true iff it should count number of times each
Tim Petersf2715e02003-02-19 02:35:07 +0000440 line is executed
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000441 @param trace true iff it should print out each line that is
Tim Petersf2715e02003-02-19 02:35:07 +0000442 being counted
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000443 @param countfuncs true iff it should just output a list of
444 (filename, modulename, funcname,) for functions
445 that were called at least once; This overrides
Tim Petersf2715e02003-02-19 02:35:07 +0000446 `count' and `trace'
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000447 @param ignoremods a list of the names of modules to ignore
448 @param ignoredirs a list of the names of directories to ignore
Tim Petersf2715e02003-02-19 02:35:07 +0000449 all of the (recursive) contents of
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000450 @param infile file from which to read stored counts to be
Tim Petersf2715e02003-02-19 02:35:07 +0000451 added into the results
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000452 @param outfile file in which to write the results
Christian Heimes380f7f22008-02-28 11:19:05 +0000453 @param timing true iff timing information be displayed
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000454 """
455 self.infile = infile
456 self.outfile = outfile
457 self.ignore = Ignore(ignoremods, ignoredirs)
458 self.counts = {} # keys are (filename, linenumber)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000459 self.pathtobasename = {} # for memoizing os.path.basename
460 self.donothing = 0
461 self.trace = trace
462 self._calledfuncs = {}
Skip Montanarocafc8112004-04-07 15:46:05 +0000463 self._callers = {}
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000464 self._caller_cache = {}
Christian Heimes380f7f22008-02-28 11:19:05 +0000465 self.start_time = None
466 if timing:
467 self.start_time = time.time()
Skip Montanarocafc8112004-04-07 15:46:05 +0000468 if countcallers:
469 self.globaltrace = self.globaltrace_trackcallers
470 elif countfuncs:
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000471 self.globaltrace = self.globaltrace_countfuncs
472 elif trace and count:
473 self.globaltrace = self.globaltrace_lt
474 self.localtrace = self.localtrace_trace_and_count
475 elif trace:
476 self.globaltrace = self.globaltrace_lt
477 self.localtrace = self.localtrace_trace
478 elif count:
479 self.globaltrace = self.globaltrace_lt
480 self.localtrace = self.localtrace_count
481 else:
482 # Ahem -- do nothing? Okay.
483 self.donothing = 1
484
485 def run(self, cmd):
486 import __main__
487 dict = __main__.__dict__
Alexander Belopolsky0ae33612010-09-27 15:49:20 +0000488 self.runctx(cmd, dict, dict)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000489
490 def runctx(self, cmd, globals=None, locals=None):
491 if globals is None: globals = {}
492 if locals is None: locals = {}
493 if not self.donothing:
Jeremy Hylton546e34b2003-06-26 14:56:17 +0000494 threading.settrace(self.globaltrace)
Georg Brandl24085d72010-08-02 12:36:24 +0000495 sys.settrace(self.globaltrace)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000496 try:
Georg Brandl7cae87c2006-09-06 06:51:57 +0000497 exec(cmd, globals, locals)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000498 finally:
499 if not self.donothing:
500 sys.settrace(None)
Jeremy Hylton546e34b2003-06-26 14:56:17 +0000501 threading.settrace(None)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000502
503 def runfunc(self, func, *args, **kw):
504 result = None
505 if not self.donothing:
506 sys.settrace(self.globaltrace)
507 try:
Guido van Rossum68468eb2003-02-27 20:14:51 +0000508 result = func(*args, **kw)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000509 finally:
510 if not self.donothing:
511 sys.settrace(None)
512 return result
513
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000514 def file_module_function_of(self, frame):
515 code = frame.f_code
516 filename = code.co_filename
517 if filename:
518 modulename = modname(filename)
519 else:
520 modulename = None
521
522 funcname = code.co_name
523 clsname = None
524 if code in self._caller_cache:
525 if self._caller_cache[code] is not None:
526 clsname = self._caller_cache[code]
527 else:
528 self._caller_cache[code] = None
529 ## use of gc.get_referrers() was suggested by Michael Hudson
530 # all functions which refer to this code object
531 funcs = [f for f in gc.get_referrers(code)
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000532 if inspect.isfunction(f)]
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000533 # require len(func) == 1 to avoid ambiguity caused by calls to
534 # new.function(): "In the face of ambiguity, refuse the
535 # temptation to guess."
536 if len(funcs) == 1:
537 dicts = [d for d in gc.get_referrers(funcs[0])
538 if isinstance(d, dict)]
539 if len(dicts) == 1:
540 classes = [c for c in gc.get_referrers(dicts[0])
541 if hasattr(c, "__bases__")]
542 if len(classes) == 1:
543 # ditto for new.classobj()
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000544 clsname = classes[0].__name__
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000545 # cache the result - assumption is that new.* is
546 # not called later to disturb this relationship
547 # _caller_cache could be flushed if functions in
548 # the new module get called.
549 self._caller_cache[code] = clsname
550 if clsname is not None:
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000551 funcname = "%s.%s" % (clsname, funcname)
552
553 return filename, modulename, funcname
554
Skip Montanarocafc8112004-04-07 15:46:05 +0000555 def globaltrace_trackcallers(self, frame, why, arg):
556 """Handler for call events.
557
558 Adds information about who called who to the self._callers dict.
559 """
560 if why == 'call':
561 # XXX Should do a better job of identifying methods
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000562 this_func = self.file_module_function_of(frame)
563 parent_func = self.file_module_function_of(frame.f_back)
Skip Montanarocafc8112004-04-07 15:46:05 +0000564 self._callers[(parent_func, this_func)] = 1
565
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000566 def globaltrace_countfuncs(self, frame, why, arg):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000567 """Handler for call events.
Tim Peters0eadaac2003-04-24 16:02:54 +0000568
Jeremy Hylton38732e12003-04-21 22:04:46 +0000569 Adds (filename, modulename, funcname) to the self._calledfuncs dict.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000570 """
571 if why == 'call':
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000572 this_func = self.file_module_function_of(frame)
573 self._calledfuncs[this_func] = 1
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000574
575 def globaltrace_lt(self, frame, why, arg):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000576 """Handler for call events.
577
578 If the code block being entered is to be ignored, returns `None',
579 else returns self.localtrace.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000580 """
581 if why == 'call':
Jeremy Hylton38732e12003-04-21 22:04:46 +0000582 code = frame.f_code
Thomas Wouterscf297e42007-02-23 15:07:44 +0000583 filename = frame.f_globals.get('__file__', None)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000584 if filename:
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000585 # XXX modname() doesn't work right for packages, so
586 # the ignore support won't work right for packages
Jeremy Hylton38732e12003-04-21 22:04:46 +0000587 modulename = modname(filename)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000588 if modulename is not None:
589 ignore_it = self.ignore.names(filename, modulename)
590 if not ignore_it:
591 if self.trace:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000592 print((" --- modulename: %s, funcname: %s"
593 % (modulename, code.co_name)))
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000594 return self.localtrace
595 else:
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000596 return None
597
598 def localtrace_trace_and_count(self, frame, why, arg):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000599 if why == "line":
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000600 # record the file name and line number of every trace
Jeremy Hylton38732e12003-04-21 22:04:46 +0000601 filename = frame.f_code.co_filename
602 lineno = frame.f_lineno
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000603 key = filename, lineno
604 self.counts[key] = self.counts.get(key, 0) + 1
Tim Petersf2715e02003-02-19 02:35:07 +0000605
Christian Heimes380f7f22008-02-28 11:19:05 +0000606 if self.start_time:
607 print('%.2f' % (time.time() - self.start_time), end=' ')
Jeremy Hylton38732e12003-04-21 22:04:46 +0000608 bname = os.path.basename(filename)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000609 print("%s(%d): %s" % (bname, lineno,
Georg Brandldc50c692010-08-02 12:40:22 +0000610 linecache.getline(filename, lineno)), end='')
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000611 return self.localtrace
612
613 def localtrace_trace(self, frame, why, arg):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000614 if why == "line":
615 # record the file name and line number of every trace
616 filename = frame.f_code.co_filename
617 lineno = frame.f_lineno
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000618
Christian Heimes380f7f22008-02-28 11:19:05 +0000619 if self.start_time:
620 print('%.2f' % (time.time() - self.start_time), end=' ')
Jeremy Hylton38732e12003-04-21 22:04:46 +0000621 bname = os.path.basename(filename)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000622 print("%s(%d): %s" % (bname, lineno,
Georg Brandldc50c692010-08-02 12:40:22 +0000623 linecache.getline(filename, lineno)), end='')
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000624 return self.localtrace
625
626 def localtrace_count(self, frame, why, arg):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000627 if why == "line":
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000628 filename = frame.f_code.co_filename
629 lineno = frame.f_lineno
630 key = filename, lineno
631 self.counts[key] = self.counts.get(key, 0) + 1
632 return self.localtrace
633
634 def results(self):
635 return CoverageResults(self.counts, infile=self.infile,
636 outfile=self.outfile,
Skip Montanarocafc8112004-04-07 15:46:05 +0000637 calledfuncs=self._calledfuncs,
638 callers=self._callers)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000639
640def _err_exit(msg):
641 sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
642 sys.exit(1)
643
644def main(argv=None):
645 import getopt
646
647 if argv is None:
648 argv = sys.argv
649 try:
Christian Heimes380f7f22008-02-28 11:19:05 +0000650 opts, prog_argv = getopt.getopt(argv[1:], "tcrRf:d:msC:lTg",
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000651 ["help", "version", "trace", "count",
652 "report", "no-report", "summary",
653 "file=", "missing",
654 "ignore-module=", "ignore-dir=",
Skip Montanarocafc8112004-04-07 15:46:05 +0000655 "coverdir=", "listfuncs",
Christian Heimes380f7f22008-02-28 11:19:05 +0000656 "trackcalls", "timing"])
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000657
Guido van Rossumb940e112007-01-10 16:19:56 +0000658 except getopt.error as msg:
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000659 sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
660 sys.stderr.write("Try `%s --help' for more information\n"
661 % sys.argv[0])
662 sys.exit(1)
663
664 trace = 0
665 count = 0
666 report = 0
667 no_report = 0
668 counts_file = None
669 missing = 0
670 ignore_modules = []
671 ignore_dirs = []
672 coverdir = None
673 summary = 0
674 listfuncs = False
Skip Montanarocafc8112004-04-07 15:46:05 +0000675 countcallers = False
Christian Heimes380f7f22008-02-28 11:19:05 +0000676 timing = False
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000677
678 for opt, val in opts:
679 if opt == "--help":
680 usage(sys.stdout)
681 sys.exit(0)
682
683 if opt == "--version":
684 sys.stdout.write("trace 2.0\n")
685 sys.exit(0)
686
Skip Montanarocafc8112004-04-07 15:46:05 +0000687 if opt == "-T" or opt == "--trackcalls":
688 countcallers = True
689 continue
690
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000691 if opt == "-l" or opt == "--listfuncs":
692 listfuncs = True
693 continue
694
Christian Heimes380f7f22008-02-28 11:19:05 +0000695 if opt == "-g" or opt == "--timing":
696 timing = True
697 continue
698
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000699 if opt == "-t" or opt == "--trace":
700 trace = 1
701 continue
702
703 if opt == "-c" or opt == "--count":
704 count = 1
705 continue
706
707 if opt == "-r" or opt == "--report":
708 report = 1
709 continue
710
711 if opt == "-R" or opt == "--no-report":
712 no_report = 1
713 continue
714
715 if opt == "-f" or opt == "--file":
716 counts_file = val
717 continue
718
719 if opt == "-m" or opt == "--missing":
720 missing = 1
721 continue
722
723 if opt == "-C" or opt == "--coverdir":
724 coverdir = val
725 continue
726
727 if opt == "-s" or opt == "--summary":
728 summary = 1
729 continue
730
731 if opt == "--ignore-module":
Georg Brandlfceab5a2008-01-19 20:08:23 +0000732 for mod in val.split(","):
733 ignore_modules.append(mod.strip())
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000734 continue
735
736 if opt == "--ignore-dir":
737 for s in val.split(os.pathsep):
738 s = os.path.expandvars(s)
739 # should I also call expanduser? (after all, could use $HOME)
740
741 s = s.replace("$prefix",
742 os.path.join(sys.prefix, "lib",
743 "python" + sys.version[:3]))
744 s = s.replace("$exec_prefix",
745 os.path.join(sys.exec_prefix, "lib",
746 "python" + sys.version[:3]))
747 s = os.path.normpath(s)
748 ignore_dirs.append(s)
749 continue
750
751 assert 0, "Should never get here"
752
753 if listfuncs and (count or trace):
754 _err_exit("cannot specify both --listfuncs and (--trace or --count)")
755
Skip Montanarocafc8112004-04-07 15:46:05 +0000756 if not (count or trace or report or listfuncs or countcallers):
757 _err_exit("must specify one of --trace, --count, --report, "
758 "--listfuncs, or --trackcalls")
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000759
760 if report and no_report:
761 _err_exit("cannot specify both --report and --no-report")
762
763 if report and not counts_file:
764 _err_exit("--report requires a --file")
765
766 if no_report and len(prog_argv) == 0:
767 _err_exit("missing name of file to run")
768
769 # everything is ready
770 if report:
771 results = CoverageResults(infile=counts_file, outfile=counts_file)
772 results.write_results(missing, summary=summary, coverdir=coverdir)
773 else:
774 sys.argv = prog_argv
775 progname = prog_argv[0]
776 sys.path[0] = os.path.split(progname)[0]
777
778 t = Trace(count, trace, countfuncs=listfuncs,
Skip Montanarocafc8112004-04-07 15:46:05 +0000779 countcallers=countcallers, ignoremods=ignore_modules,
780 ignoredirs=ignore_dirs, infile=counts_file,
Christian Heimes380f7f22008-02-28 11:19:05 +0000781 outfile=counts_file, timing=timing)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000782 try:
Alexander Belopolsky3f8ecab2010-07-21 17:43:42 +0000783 with open(progname) as fp:
784 code = compile(fp.read(), progname, 'exec')
Georg Brandl8f9f4662010-08-01 08:35:29 +0000785 # try to emulate __main__ namespace as much as possible
786 globs = {
787 '__file__': progname,
788 '__name__': '__main__',
789 '__package__': None,
790 '__cached__': None,
791 }
792 t.runctx(code, globs, globs)
Guido van Rossumb940e112007-01-10 16:19:56 +0000793 except IOError as err:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000794 _err_exit("Cannot run file %r because: %s" % (sys.argv[0], err))
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000795 except SystemExit:
796 pass
797
798 results = t.results()
799
800 if not no_report:
801 results.write_results(missing, summary=summary, coverdir=coverdir)
802
803if __name__=='__main__':
804 main()