blob: 38a13e2a9f0fa92ddcfec3c92022c355a3a4a943 [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
Skip Montanaro7b1559a2006-04-23 19:32:14 +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,
Tim Petersbe635cd2006-04-24 22:45:13 +000043 count=1)
Skip Montanaro7b1559a2006-04-23 19:32:14 +000044 # 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
Jeremy Hylton38732e12003-04-21 22:04:46 +000051import linecache
Jeremy Hylton38732e12003-04-21 22:04:46 +000052import os
53import re
54import sys
Neal Norwitzca376612008-02-26 08:21:28 +000055import time
Jeremy Hylton38732e12003-04-21 22:04:46 +000056import token
57import tokenize
Alexander Belopolsky9d17da32010-09-13 16:45:02 +000058import inspect
Skip Montanaro5bfd9842004-04-10 16:29:58 +000059import gc
Alexander Belopolsky517185e2010-09-24 18:14:18 +000060import dis
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +000061try:
62 import cPickle
63 pickle = cPickle
64except ImportError:
65 import pickle
66
Alexander Belopolsky860c05d2010-11-06 01:38:48 +000067try:
68 import threading
69except ImportError:
70 _settrace = sys.settrace
71
72 def _unsettrace():
73 sys.settrace(None)
74else:
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
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +000083def usage(outfile):
84 outfile.write("""Usage: %s [OPTIONS] <file> [ARGS]
85
86Meta-options:
87--help Display this help then exit.
88--version Output version information then exit.
89
90Otherwise, 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 Montanaroa7b8ac62003-06-27 19:09:33 +000096-l, --listfuncs Keep track of which functions are executed at least
Fred Drake01c623b2003-06-27 19:22:11 +000097 once and write the results to sys.stdout after the
Skip Montanaroa7b8ac62003-06-27 19:09:33 +000098 program exits.
Skip Montanarocafc8112004-04-07 15:46:05 +000099-T, --trackcalls Keep track of caller/called pairs and write the
100 results to sys.stdout after the program exits.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000101-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
106Modifiers:
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.)
Neal Norwitzca376612008-02-26 08:21:28 +0000117-g, --timing Prefix each line with the time since the program started.
118 Only used while tracing.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000119
120Filters, may be repeated multiple times:
Facundo Batista873c9852008-01-19 18:38:19 +0000121--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 Hylton4edaa0d2003-02-18 15:06:17 +0000124--ignore-dir=<dir> Ignore files in the given directory (multiple
125 directories can be joined by os.pathsep).
126""" % sys.argv[0])
127
Jeremy Hylton38732e12003-04-21 22:04:46 +0000128PRAGMA_NOCOVER = "#pragma NO COVER"
129
130# Simple rx to find lines with no code.
131rx_blank = re.compile(r'^\s*(#.*)?$')
132
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000133class Ignore:
134 def __init__(self, modules = None, dirs = None):
135 self._mods = modules or []
136 self._dirs = dirs or []
137
138 self._dirs = map(os.path.normpath, self._dirs)
139 self._ignore = { '<string>': 1 }
140
141 def names(self, filename, modulename):
Benjamin Peterson6e3dbbd2009-10-09 22:15:50 +0000142 if modulename in self._ignore:
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000143 return self._ignore[modulename]
144
145 # haven't seen this one before, so see if the module name is
146 # on the ignore list. Need to take some care since ignoring
147 # "cmp" musn't mean ignoring "cmpcache" but ignoring
148 # "Spam" must also mean ignoring "Spam.Eggs".
149 for mod in self._mods:
150 if mod == modulename: # Identical names, so ignore
151 self._ignore[modulename] = 1
152 return 1
153 # check if the module is a proper submodule of something on
154 # the ignore list
155 n = len(mod)
156 # (will not overflow since if the first n characters are the
Fred Drakedb390c12005-10-28 14:39:47 +0000157 # same and the name has not already occurred, then the size
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000158 # of "name" is greater than that of "mod")
159 if mod == modulename[:n] and modulename[n] == '.':
160 self._ignore[modulename] = 1
161 return 1
162
163 # Now check that __file__ isn't in one of the directories
164 if filename is None:
165 # must be a built-in, so we must ignore
166 self._ignore[modulename] = 1
167 return 1
168
169 # Ignore a file when it contains one of the ignorable paths
170 for d in self._dirs:
171 # The '+ os.sep' is to ensure that d is a parent directory,
172 # as compared to cases like:
173 # d = "/usr/local"
174 # filename = "/usr/local.py"
175 # or
176 # d = "/usr/local.py"
177 # filename = "/usr/local.py"
178 if filename.startswith(d + os.sep):
179 self._ignore[modulename] = 1
180 return 1
181
182 # Tried the different ways, so we don't ignore this module
183 self._ignore[modulename] = 0
184 return 0
185
Jeremy Hylton38732e12003-04-21 22:04:46 +0000186def modname(path):
187 """Return a plausible module name for the patch."""
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000188
Jeremy Hylton38732e12003-04-21 22:04:46 +0000189 base = os.path.basename(path)
190 filename, ext = os.path.splitext(base)
191 return filename
192
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000193def fullmodname(path):
Jeremy Hyltonc8c8b942003-04-22 15:35:51 +0000194 """Return a plausible module name for the path."""
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000195
196 # If the file 'path' is part of a package, then the filename isn't
197 # enough to uniquely identify it. Try to do the right thing by
198 # looking in sys.path for the longest matching prefix. We'll
199 # assume that the rest is the package name.
200
Georg Brandl7a1af772006-08-14 21:55:28 +0000201 comparepath = os.path.normcase(path)
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000202 longest = ""
203 for dir in sys.path:
Georg Brandl7a1af772006-08-14 21:55:28 +0000204 dir = os.path.normcase(dir)
205 if comparepath.startswith(dir) and comparepath[len(dir)] == os.sep:
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000206 if len(dir) > len(longest):
207 longest = dir
208
Guido van Rossumb427c002003-10-10 23:02:01 +0000209 if longest:
210 base = path[len(longest) + 1:]
211 else:
212 base = path
Georg Brandl78e69572010-08-01 18:52:52 +0000213 # the drive letter is never part of the module name
214 drive, base = os.path.splitdrive(base)
Guido van Rossumbbca8da2004-02-19 19:16:50 +0000215 base = base.replace(os.sep, ".")
216 if os.altsep:
217 base = base.replace(os.altsep, ".")
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000218 filename, ext = os.path.splitext(base)
Georg Brandl78e69572010-08-01 18:52:52 +0000219 return filename.lstrip(".")
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000220
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000221class CoverageResults:
222 def __init__(self, counts=None, calledfuncs=None, infile=None,
Skip Montanarocafc8112004-04-07 15:46:05 +0000223 callers=None, outfile=None):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000224 self.counts = counts
225 if self.counts is None:
226 self.counts = {}
227 self.counter = self.counts.copy() # map (filename, lineno) to count
228 self.calledfuncs = calledfuncs
229 if self.calledfuncs is None:
230 self.calledfuncs = {}
231 self.calledfuncs = self.calledfuncs.copy()
Skip Montanarocafc8112004-04-07 15:46:05 +0000232 self.callers = callers
233 if self.callers is None:
234 self.callers = {}
235 self.callers = self.callers.copy()
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000236 self.infile = infile
237 self.outfile = outfile
238 if self.infile:
Jeremy Hyltond7ce86d2003-07-07 16:08:47 +0000239 # Try to merge existing counts file.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000240 try:
Skip Montanarocafc8112004-04-07 15:46:05 +0000241 counts, calledfuncs, callers = \
242 pickle.load(open(self.infile, 'rb'))
243 self.update(self.__class__(counts, calledfuncs, callers))
Jeremy Hyltond7ce86d2003-07-07 16:08:47 +0000244 except (IOError, EOFError, ValueError), err:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000245 print >> sys.stderr, ("Skipping counts file %r: %s"
246 % (self.infile, err))
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000247
248 def update(self, other):
249 """Merge in the data from another CoverageResults"""
250 counts = self.counts
251 calledfuncs = self.calledfuncs
Skip Montanarocafc8112004-04-07 15:46:05 +0000252 callers = self.callers
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000253 other_counts = other.counts
254 other_calledfuncs = other.calledfuncs
Skip Montanarocafc8112004-04-07 15:46:05 +0000255 other_callers = other.callers
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000256
257 for key in other_counts.keys():
Jeremy Hylton38732e12003-04-21 22:04:46 +0000258 counts[key] = counts.get(key, 0) + other_counts[key]
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000259
260 for key in other_calledfuncs.keys():
261 calledfuncs[key] = 1
262
Skip Montanarocafc8112004-04-07 15:46:05 +0000263 for key in other_callers.keys():
264 callers[key] = 1
265
Jeremy Hylton38732e12003-04-21 22:04:46 +0000266 def write_results(self, show_missing=True, summary=False, coverdir=None):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000267 """
268 @param coverdir
269 """
Skip Montanarocafc8112004-04-07 15:46:05 +0000270 if self.calledfuncs:
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000271 print
Skip Montanarocafc8112004-04-07 15:46:05 +0000272 print "functions called:"
273 calls = self.calledfuncs.keys()
274 calls.sort()
275 for filename, modulename, funcname in calls:
276 print ("filename: %s, modulename: %s, funcname: %s"
277 % (filename, modulename, funcname))
278
279 if self.callers:
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000280 print
Skip Montanarocafc8112004-04-07 15:46:05 +0000281 print "calling relationships:"
282 calls = self.callers.keys()
283 calls.sort()
284 lastfile = lastcfile = ""
285 for ((pfile, pmod, pfunc), (cfile, cmod, cfunc)) in calls:
286 if pfile != lastfile:
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000287 print
Skip Montanarocafc8112004-04-07 15:46:05 +0000288 print "***", pfile, "***"
289 lastfile = pfile
290 lastcfile = ""
291 if cfile != pfile and lastcfile != cfile:
292 print " -->", cfile
293 lastcfile = cfile
294 print " %s.%s -> %s.%s" % (pmod, pfunc, cmod, cfunc)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000295
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000296 # turn the counts data ("(filename, lineno) = count") into something
297 # accessible on a per-file basis
298 per_file = {}
Jeremy Hylton38732e12003-04-21 22:04:46 +0000299 for filename, lineno in self.counts.keys():
300 lines_hit = per_file[filename] = per_file.get(filename, {})
301 lines_hit[lineno] = self.counts[(filename, lineno)]
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000302
303 # accumulate summary info, if needed
304 sums = {}
305
Jeremy Hylton38732e12003-04-21 22:04:46 +0000306 for filename, count in per_file.iteritems():
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000307 # skip some "files" we don't care about...
308 if filename == "<string>":
309 continue
Skip Montanaro58a6f442007-11-24 14:30:47 +0000310 if filename.startswith("<doctest "):
311 continue
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000312
Georg Brandlb2afe852006-06-09 20:43:48 +0000313 if filename.endswith((".pyc", ".pyo")):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000314 filename = filename[:-1]
315
Jeremy Hylton38732e12003-04-21 22:04:46 +0000316 if coverdir is None:
317 dir = os.path.dirname(os.path.abspath(filename))
Jeremy Hyltonc8c8b942003-04-22 15:35:51 +0000318 modulename = modname(filename)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000319 else:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000320 dir = coverdir
321 if not os.path.exists(dir):
322 os.makedirs(dir)
Jeremy Hyltonc8c8b942003-04-22 15:35:51 +0000323 modulename = fullmodname(filename)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000324
325 # If desired, get a list of the line numbers which represent
326 # executable content (returned as a dict for better lookup speed)
327 if show_missing:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000328 lnotab = find_executable_linenos(filename)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000329 else:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000330 lnotab = {}
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000331
Jeremy Hylton38732e12003-04-21 22:04:46 +0000332 source = linecache.getlines(filename)
333 coverpath = os.path.join(dir, modulename + ".cover")
334 n_hits, n_lines = self.write_results_file(coverpath, source,
335 lnotab, count)
Tim Peters0eadaac2003-04-24 16:02:54 +0000336
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000337 if summary and n_lines:
Raymond Hettinger9d2ad442011-04-11 17:45:01 -0700338 percent = 100 * n_hits // n_lines
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000339 sums[modulename] = n_lines, percent, modulename, filename
340
341 if summary and sums:
342 mods = sums.keys()
343 mods.sort()
344 print "lines cov% module (path)"
345 for m in mods:
346 n_lines, percent, modulename, filename = sums[m]
347 print "%5d %3d%% %s (%s)" % sums[m]
348
349 if self.outfile:
350 # try and store counts and module info into self.outfile
351 try:
Skip Montanarocafc8112004-04-07 15:46:05 +0000352 pickle.dump((self.counts, self.calledfuncs, self.callers),
Jeremy Hyltond0e27052003-10-14 20:12:06 +0000353 open(self.outfile, 'wb'), 1)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000354 except IOError, err:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000355 print >> sys.stderr, "Can't save counts files because %s" % err
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000356
Jeremy Hylton38732e12003-04-21 22:04:46 +0000357 def write_results_file(self, path, lines, lnotab, lines_hit):
358 """Return a coverage results file in path."""
359
360 try:
361 outfile = open(path, "w")
362 except IOError, err:
363 print >> sys.stderr, ("trace: Could not open %r for writing: %s"
364 "- skipping" % (path, err))
Guido van Rossumbbca8da2004-02-19 19:16:50 +0000365 return 0, 0
Jeremy Hylton38732e12003-04-21 22:04:46 +0000366
367 n_lines = 0
368 n_hits = 0
369 for i, line in enumerate(lines):
370 lineno = i + 1
371 # do the blank/comment match to try to mark more lines
372 # (help the reader find stuff that hasn't been covered)
373 if lineno in lines_hit:
374 outfile.write("%5d: " % lines_hit[lineno])
375 n_hits += 1
376 n_lines += 1
377 elif rx_blank.match(line):
Walter Dörwaldc1711722003-07-15 10:34:02 +0000378 outfile.write(" ")
Jeremy Hylton38732e12003-04-21 22:04:46 +0000379 else:
380 # lines preceded by no marks weren't hit
381 # Highlight them if so indicated, unless the line contains
382 # #pragma: NO COVER
383 if lineno in lnotab and not PRAGMA_NOCOVER in lines[i]:
384 outfile.write(">>>>>> ")
Jeremy Hylton546e34b2003-06-26 14:56:17 +0000385 n_lines += 1
Jeremy Hylton38732e12003-04-21 22:04:46 +0000386 else:
387 outfile.write(" ")
Jeremy Hylton38732e12003-04-21 22:04:46 +0000388 outfile.write(lines[i].expandtabs(8))
389 outfile.close()
390
391 return n_hits, n_lines
392
393def find_lines_from_code(code, strs):
394 """Return dict where keys are lines in the line number table."""
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000395 linenos = {}
396
Alexander Belopolsky517185e2010-09-24 18:14:18 +0000397 for _, lineno in dis.findlinestarts(code):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000398 if lineno not in strs:
399 linenos[lineno] = 1
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000400
401 return linenos
402
Jeremy Hylton38732e12003-04-21 22:04:46 +0000403def find_lines(code, strs):
404 """Return lineno dict for all code objects reachable from code."""
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000405 # get all of the lineno information from the code of this scope level
Jeremy Hylton38732e12003-04-21 22:04:46 +0000406 linenos = find_lines_from_code(code, strs)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000407
408 # and check the constants for references to other code objects
409 for c in code.co_consts:
Alexander Belopolsky9d17da32010-09-13 16:45:02 +0000410 if inspect.iscode(c):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000411 # find another code object, so recurse into it
Jeremy Hylton38732e12003-04-21 22:04:46 +0000412 linenos.update(find_lines(c, strs))
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000413 return linenos
414
Jeremy Hylton38732e12003-04-21 22:04:46 +0000415def find_strings(filename):
416 """Return a dict of possible docstring positions.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000417
Jeremy Hylton38732e12003-04-21 22:04:46 +0000418 The dict maps line numbers to strings. There is an entry for
419 line that contains only a string or a part of a triple-quoted
420 string.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000421 """
Jeremy Hylton38732e12003-04-21 22:04:46 +0000422 d = {}
423 # If the first token is a string, then it's the module docstring.
424 # Add this special case so that the test in the loop passes.
425 prev_ttype = token.INDENT
426 f = open(filename)
427 for ttype, tstr, start, end, line in tokenize.generate_tokens(f.readline):
428 if ttype == token.STRING:
429 if prev_ttype == token.INDENT:
430 sline, scol = start
431 eline, ecol = end
432 for i in range(sline, eline + 1):
433 d[i] = 1
434 prev_ttype = ttype
435 f.close()
436 return d
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000437
Jeremy Hylton38732e12003-04-21 22:04:46 +0000438def find_executable_linenos(filename):
439 """Return dict where keys are line numbers in the line number table."""
Jeremy Hylton38732e12003-04-21 22:04:46 +0000440 try:
Skip Montanaroc00fc842004-04-16 03:28:19 +0000441 prog = open(filename, "rU").read()
Jeremy Hylton38732e12003-04-21 22:04:46 +0000442 except IOError, err:
443 print >> sys.stderr, ("Not printing coverage data for %r: %s"
444 % (filename, err))
445 return {}
446 code = compile(prog, filename, "exec")
447 strs = find_strings(filename)
448 return find_lines(code, strs)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000449
450class Trace:
Skip Montanarocafc8112004-04-07 15:46:05 +0000451 def __init__(self, count=1, trace=1, countfuncs=0, countcallers=0,
Neal Norwitzca376612008-02-26 08:21:28 +0000452 ignoremods=(), ignoredirs=(), infile=None, outfile=None,
453 timing=False):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000454 """
455 @param count true iff it should count number of times each
Tim Petersf2715e02003-02-19 02:35:07 +0000456 line is executed
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000457 @param trace true iff it should print out each line that is
Tim Petersf2715e02003-02-19 02:35:07 +0000458 being counted
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000459 @param countfuncs true iff it should just output a list of
460 (filename, modulename, funcname,) for functions
461 that were called at least once; This overrides
Tim Petersf2715e02003-02-19 02:35:07 +0000462 `count' and `trace'
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000463 @param ignoremods a list of the names of modules to ignore
464 @param ignoredirs a list of the names of directories to ignore
Tim Petersf2715e02003-02-19 02:35:07 +0000465 all of the (recursive) contents of
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000466 @param infile file from which to read stored counts to be
Tim Petersf2715e02003-02-19 02:35:07 +0000467 added into the results
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000468 @param outfile file in which to write the results
Neal Norwitzca376612008-02-26 08:21:28 +0000469 @param timing true iff timing information be displayed
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000470 """
471 self.infile = infile
472 self.outfile = outfile
473 self.ignore = Ignore(ignoremods, ignoredirs)
474 self.counts = {} # keys are (filename, linenumber)
475 self.blabbed = {} # for debugging
476 self.pathtobasename = {} # for memoizing os.path.basename
477 self.donothing = 0
478 self.trace = trace
479 self._calledfuncs = {}
Skip Montanarocafc8112004-04-07 15:46:05 +0000480 self._callers = {}
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000481 self._caller_cache = {}
Neal Norwitzca376612008-02-26 08:21:28 +0000482 self.start_time = None
483 if timing:
484 self.start_time = time.time()
Skip Montanarocafc8112004-04-07 15:46:05 +0000485 if countcallers:
486 self.globaltrace = self.globaltrace_trackcallers
487 elif countfuncs:
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000488 self.globaltrace = self.globaltrace_countfuncs
489 elif trace and count:
490 self.globaltrace = self.globaltrace_lt
491 self.localtrace = self.localtrace_trace_and_count
492 elif trace:
493 self.globaltrace = self.globaltrace_lt
494 self.localtrace = self.localtrace_trace
495 elif count:
496 self.globaltrace = self.globaltrace_lt
497 self.localtrace = self.localtrace_count
498 else:
499 # Ahem -- do nothing? Okay.
500 self.donothing = 1
501
502 def run(self, cmd):
503 import __main__
504 dict = __main__.__dict__
Nadeem Vawda66e53c72011-08-29 09:37:07 +0200505 self.runctx(cmd, dict, dict)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000506
507 def runctx(self, cmd, globals=None, locals=None):
508 if globals is None: globals = {}
509 if locals is None: locals = {}
510 if not self.donothing:
Alexander Belopolsky860c05d2010-11-06 01:38:48 +0000511 _settrace(self.globaltrace)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000512 try:
513 exec cmd in globals, locals
514 finally:
515 if not self.donothing:
Alexander Belopolsky860c05d2010-11-06 01:38:48 +0000516 _unsettrace()
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000517
518 def runfunc(self, func, *args, **kw):
519 result = None
520 if not self.donothing:
521 sys.settrace(self.globaltrace)
522 try:
Guido van Rossum68468eb2003-02-27 20:14:51 +0000523 result = func(*args, **kw)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000524 finally:
525 if not self.donothing:
526 sys.settrace(None)
527 return result
528
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000529 def file_module_function_of(self, frame):
530 code = frame.f_code
531 filename = code.co_filename
532 if filename:
533 modulename = modname(filename)
534 else:
535 modulename = None
536
537 funcname = code.co_name
538 clsname = None
539 if code in self._caller_cache:
540 if self._caller_cache[code] is not None:
541 clsname = self._caller_cache[code]
542 else:
543 self._caller_cache[code] = None
544 ## use of gc.get_referrers() was suggested by Michael Hudson
545 # all functions which refer to this code object
546 funcs = [f for f in gc.get_referrers(code)
Alexander Belopolsky9d17da32010-09-13 16:45:02 +0000547 if inspect.isfunction(f)]
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000548 # require len(func) == 1 to avoid ambiguity caused by calls to
549 # new.function(): "In the face of ambiguity, refuse the
550 # temptation to guess."
551 if len(funcs) == 1:
552 dicts = [d for d in gc.get_referrers(funcs[0])
553 if isinstance(d, dict)]
554 if len(dicts) == 1:
555 classes = [c for c in gc.get_referrers(dicts[0])
556 if hasattr(c, "__bases__")]
557 if len(classes) == 1:
558 # ditto for new.classobj()
Alexander Belopolsky9d17da32010-09-13 16:45:02 +0000559 clsname = classes[0].__name__
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000560 # cache the result - assumption is that new.* is
561 # not called later to disturb this relationship
562 # _caller_cache could be flushed if functions in
563 # the new module get called.
564 self._caller_cache[code] = clsname
565 if clsname is not None:
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000566 funcname = "%s.%s" % (clsname, funcname)
567
568 return filename, modulename, funcname
569
Skip Montanarocafc8112004-04-07 15:46:05 +0000570 def globaltrace_trackcallers(self, frame, why, arg):
571 """Handler for call events.
572
573 Adds information about who called who to the self._callers dict.
574 """
575 if why == 'call':
576 # XXX Should do a better job of identifying methods
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000577 this_func = self.file_module_function_of(frame)
578 parent_func = self.file_module_function_of(frame.f_back)
Skip Montanarocafc8112004-04-07 15:46:05 +0000579 self._callers[(parent_func, this_func)] = 1
580
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000581 def globaltrace_countfuncs(self, frame, why, arg):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000582 """Handler for call events.
Tim Peters0eadaac2003-04-24 16:02:54 +0000583
Jeremy Hylton38732e12003-04-21 22:04:46 +0000584 Adds (filename, modulename, funcname) to the self._calledfuncs dict.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000585 """
586 if why == 'call':
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000587 this_func = self.file_module_function_of(frame)
588 self._calledfuncs[this_func] = 1
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000589
590 def globaltrace_lt(self, frame, why, arg):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000591 """Handler for call events.
592
593 If the code block being entered is to be ignored, returns `None',
594 else returns self.localtrace.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000595 """
596 if why == 'call':
Jeremy Hylton38732e12003-04-21 22:04:46 +0000597 code = frame.f_code
Skip Montanaro691acf22007-02-11 18:24:37 +0000598 filename = frame.f_globals.get('__file__', None)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000599 if filename:
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000600 # XXX modname() doesn't work right for packages, so
601 # the ignore support won't work right for packages
Jeremy Hylton38732e12003-04-21 22:04:46 +0000602 modulename = modname(filename)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000603 if modulename is not None:
604 ignore_it = self.ignore.names(filename, modulename)
605 if not ignore_it:
606 if self.trace:
607 print (" --- modulename: %s, funcname: %s"
Jeremy Hylton38732e12003-04-21 22:04:46 +0000608 % (modulename, code.co_name))
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000609 return self.localtrace
610 else:
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000611 return None
612
613 def localtrace_trace_and_count(self, frame, why, arg):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000614 if why == "line":
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000615 # record the file name and line number of every trace
Jeremy Hylton38732e12003-04-21 22:04:46 +0000616 filename = frame.f_code.co_filename
617 lineno = frame.f_lineno
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000618 key = filename, lineno
619 self.counts[key] = self.counts.get(key, 0) + 1
Tim Petersf2715e02003-02-19 02:35:07 +0000620
Neal Norwitzca376612008-02-26 08:21:28 +0000621 if self.start_time:
622 print '%.2f' % (time.time() - self.start_time),
Jeremy Hylton38732e12003-04-21 22:04:46 +0000623 bname = os.path.basename(filename)
624 print "%s(%d): %s" % (bname, lineno,
625 linecache.getline(filename, lineno)),
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000626 return self.localtrace
627
628 def localtrace_trace(self, frame, why, arg):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000629 if why == "line":
630 # record the file name and line number of every trace
631 filename = frame.f_code.co_filename
632 lineno = frame.f_lineno
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000633
Neal Norwitzca376612008-02-26 08:21:28 +0000634 if self.start_time:
635 print '%.2f' % (time.time() - self.start_time),
Jeremy Hylton38732e12003-04-21 22:04:46 +0000636 bname = os.path.basename(filename)
637 print "%s(%d): %s" % (bname, lineno,
638 linecache.getline(filename, lineno)),
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000639 return self.localtrace
640
641 def localtrace_count(self, frame, why, arg):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000642 if why == "line":
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000643 filename = frame.f_code.co_filename
644 lineno = frame.f_lineno
645 key = filename, lineno
646 self.counts[key] = self.counts.get(key, 0) + 1
647 return self.localtrace
648
649 def results(self):
650 return CoverageResults(self.counts, infile=self.infile,
651 outfile=self.outfile,
Skip Montanarocafc8112004-04-07 15:46:05 +0000652 calledfuncs=self._calledfuncs,
653 callers=self._callers)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000654
655def _err_exit(msg):
656 sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
657 sys.exit(1)
658
659def main(argv=None):
660 import getopt
661
662 if argv is None:
663 argv = sys.argv
664 try:
Neal Norwitzca376612008-02-26 08:21:28 +0000665 opts, prog_argv = getopt.getopt(argv[1:], "tcrRf:d:msC:lTg",
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000666 ["help", "version", "trace", "count",
667 "report", "no-report", "summary",
668 "file=", "missing",
669 "ignore-module=", "ignore-dir=",
Skip Montanarocafc8112004-04-07 15:46:05 +0000670 "coverdir=", "listfuncs",
Neal Norwitzca376612008-02-26 08:21:28 +0000671 "trackcalls", "timing"])
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000672
673 except getopt.error, msg:
674 sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
675 sys.stderr.write("Try `%s --help' for more information\n"
676 % sys.argv[0])
677 sys.exit(1)
678
679 trace = 0
680 count = 0
681 report = 0
682 no_report = 0
683 counts_file = None
684 missing = 0
685 ignore_modules = []
686 ignore_dirs = []
687 coverdir = None
688 summary = 0
689 listfuncs = False
Skip Montanarocafc8112004-04-07 15:46:05 +0000690 countcallers = False
Neal Norwitzca376612008-02-26 08:21:28 +0000691 timing = False
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000692
693 for opt, val in opts:
694 if opt == "--help":
695 usage(sys.stdout)
696 sys.exit(0)
697
698 if opt == "--version":
699 sys.stdout.write("trace 2.0\n")
700 sys.exit(0)
701
Skip Montanarocafc8112004-04-07 15:46:05 +0000702 if opt == "-T" or opt == "--trackcalls":
703 countcallers = True
704 continue
705
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000706 if opt == "-l" or opt == "--listfuncs":
707 listfuncs = True
708 continue
709
Neal Norwitzca376612008-02-26 08:21:28 +0000710 if opt == "-g" or opt == "--timing":
711 timing = True
712 continue
713
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000714 if opt == "-t" or opt == "--trace":
715 trace = 1
716 continue
717
718 if opt == "-c" or opt == "--count":
719 count = 1
720 continue
721
722 if opt == "-r" or opt == "--report":
723 report = 1
724 continue
725
726 if opt == "-R" or opt == "--no-report":
727 no_report = 1
728 continue
729
730 if opt == "-f" or opt == "--file":
731 counts_file = val
732 continue
733
734 if opt == "-m" or opt == "--missing":
735 missing = 1
736 continue
737
738 if opt == "-C" or opt == "--coverdir":
739 coverdir = val
740 continue
741
742 if opt == "-s" or opt == "--summary":
743 summary = 1
744 continue
745
746 if opt == "--ignore-module":
Facundo Batista873c9852008-01-19 18:38:19 +0000747 for mod in val.split(","):
748 ignore_modules.append(mod.strip())
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000749 continue
750
751 if opt == "--ignore-dir":
752 for s in val.split(os.pathsep):
753 s = os.path.expandvars(s)
754 # should I also call expanduser? (after all, could use $HOME)
755
756 s = s.replace("$prefix",
757 os.path.join(sys.prefix, "lib",
758 "python" + sys.version[:3]))
759 s = s.replace("$exec_prefix",
760 os.path.join(sys.exec_prefix, "lib",
761 "python" + sys.version[:3]))
762 s = os.path.normpath(s)
763 ignore_dirs.append(s)
764 continue
765
766 assert 0, "Should never get here"
767
768 if listfuncs and (count or trace):
769 _err_exit("cannot specify both --listfuncs and (--trace or --count)")
770
Skip Montanarocafc8112004-04-07 15:46:05 +0000771 if not (count or trace or report or listfuncs or countcallers):
772 _err_exit("must specify one of --trace, --count, --report, "
773 "--listfuncs, or --trackcalls")
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000774
775 if report and no_report:
776 _err_exit("cannot specify both --report and --no-report")
777
778 if report and not counts_file:
779 _err_exit("--report requires a --file")
780
781 if no_report and len(prog_argv) == 0:
782 _err_exit("missing name of file to run")
783
784 # everything is ready
785 if report:
786 results = CoverageResults(infile=counts_file, outfile=counts_file)
787 results.write_results(missing, summary=summary, coverdir=coverdir)
788 else:
789 sys.argv = prog_argv
790 progname = prog_argv[0]
791 sys.path[0] = os.path.split(progname)[0]
792
793 t = Trace(count, trace, countfuncs=listfuncs,
Skip Montanarocafc8112004-04-07 15:46:05 +0000794 countcallers=countcallers, ignoremods=ignore_modules,
795 ignoredirs=ignore_dirs, infile=counts_file,
Neal Norwitzca376612008-02-26 08:21:28 +0000796 outfile=counts_file, timing=timing)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000797 try:
Georg Brandl78e69572010-08-01 18:52:52 +0000798 with open(progname) as fp:
799 code = compile(fp.read(), progname, 'exec')
800 # try to emulate __main__ namespace as much as possible
801 globs = {
802 '__file__': progname,
803 '__name__': '__main__',
804 '__package__': None,
805 '__cached__': None,
806 }
807 t.runctx(code, globs, globs)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000808 except IOError, err:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000809 _err_exit("Cannot run file %r because: %s" % (sys.argv[0], err))
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000810 except SystemExit:
811 pass
812
813 results = t.results()
814
815 if not no_report:
816 results.write_results(missing, summary=summary, coverdir=coverdir)
817
818if __name__=='__main__':
819 main()