1 | n/a | #!/usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | # portions copyright 2001, Autonomous Zones Industries, Inc., all rights... |
---|
4 | n/a | # err... reserved and offered to the public under the terms of the |
---|
5 | n/a | # Python 2.2 license. |
---|
6 | n/a | # Author: Zooko O'Whielacronx |
---|
7 | n/a | # http://zooko.com/ |
---|
8 | n/a | # mailto:zooko@zooko.com |
---|
9 | n/a | # |
---|
10 | n/a | # Copyright 2000, Mojam Media, Inc., all rights reserved. |
---|
11 | n/a | # Author: Skip Montanaro |
---|
12 | n/a | # |
---|
13 | n/a | # Copyright 1999, Bioreason, Inc., all rights reserved. |
---|
14 | n/a | # Author: Andrew Dalke |
---|
15 | n/a | # |
---|
16 | n/a | # Copyright 1995-1997, Automatrix, Inc., all rights reserved. |
---|
17 | n/a | # Author: Skip Montanaro |
---|
18 | n/a | # |
---|
19 | n/a | # Copyright 1991-1995, Stichting Mathematisch Centrum, all rights reserved. |
---|
20 | n/a | # |
---|
21 | n/a | # |
---|
22 | n/a | # Permission to use, copy, modify, and distribute this Python software and |
---|
23 | n/a | # its associated documentation for any purpose without fee is hereby |
---|
24 | n/a | # granted, provided that the above copyright notice appears in all copies, |
---|
25 | n/a | # and that both that copyright notice and this permission notice appear in |
---|
26 | n/a | # supporting documentation, and that the name of neither Automatrix, |
---|
27 | n/a | # Bioreason or Mojam Media be used in advertising or publicity pertaining to |
---|
28 | n/a | # distribution of the software without specific, written prior permission. |
---|
29 | n/a | # |
---|
30 | n/a | """program/module to trace Python program or function execution |
---|
31 | n/a | |
---|
32 | n/a | Sample use, command line: |
---|
33 | n/a | trace.py -c -f counts --ignore-dir '$prefix' spam.py eggs |
---|
34 | n/a | trace.py -t --ignore-dir '$prefix' spam.py eggs |
---|
35 | n/a | trace.py --trackcalls spam.py eggs |
---|
36 | n/a | |
---|
37 | n/a | Sample use, programmatically |
---|
38 | n/a | import sys |
---|
39 | n/a | |
---|
40 | n/a | # create a Trace object, telling it what to ignore, and whether to |
---|
41 | n/a | # do tracing or line-counting or both. |
---|
42 | n/a | tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,], |
---|
43 | n/a | trace=0, count=1) |
---|
44 | n/a | # run the new command using the given tracer |
---|
45 | n/a | tracer.run('main()') |
---|
46 | n/a | # make a report, placing output in /tmp |
---|
47 | n/a | r = tracer.results() |
---|
48 | n/a | r.write_results(show_missing=True, coverdir="/tmp") |
---|
49 | n/a | """ |
---|
50 | n/a | __all__ = ['Trace', 'CoverageResults'] |
---|
51 | n/a | import argparse |
---|
52 | n/a | import linecache |
---|
53 | n/a | import os |
---|
54 | n/a | import re |
---|
55 | n/a | import sys |
---|
56 | n/a | import token |
---|
57 | n/a | import tokenize |
---|
58 | n/a | import inspect |
---|
59 | n/a | import gc |
---|
60 | n/a | import dis |
---|
61 | n/a | import pickle |
---|
62 | n/a | from time import monotonic as _time |
---|
63 | n/a | |
---|
64 | n/a | try: |
---|
65 | n/a | import threading |
---|
66 | n/a | except ImportError: |
---|
67 | n/a | _settrace = sys.settrace |
---|
68 | n/a | |
---|
69 | n/a | def _unsettrace(): |
---|
70 | n/a | sys.settrace(None) |
---|
71 | n/a | else: |
---|
72 | n/a | def _settrace(func): |
---|
73 | n/a | threading.settrace(func) |
---|
74 | n/a | sys.settrace(func) |
---|
75 | n/a | |
---|
76 | n/a | def _unsettrace(): |
---|
77 | n/a | sys.settrace(None) |
---|
78 | n/a | threading.settrace(None) |
---|
79 | n/a | |
---|
80 | n/a | PRAGMA_NOCOVER = "#pragma NO COVER" |
---|
81 | n/a | |
---|
82 | n/a | # Simple rx to find lines with no code. |
---|
83 | n/a | rx_blank = re.compile(r'^\s*(#.*)?$') |
---|
84 | n/a | |
---|
85 | n/a | class _Ignore: |
---|
86 | n/a | def __init__(self, modules=None, dirs=None): |
---|
87 | n/a | self._mods = set() if not modules else set(modules) |
---|
88 | n/a | self._dirs = [] if not dirs else [os.path.normpath(d) |
---|
89 | n/a | for d in dirs] |
---|
90 | n/a | self._ignore = { '<string>': 1 } |
---|
91 | n/a | |
---|
92 | n/a | def names(self, filename, modulename): |
---|
93 | n/a | if modulename in self._ignore: |
---|
94 | n/a | return self._ignore[modulename] |
---|
95 | n/a | |
---|
96 | n/a | # haven't seen this one before, so see if the module name is |
---|
97 | n/a | # on the ignore list. |
---|
98 | n/a | if modulename in self._mods: # Identical names, so ignore |
---|
99 | n/a | self._ignore[modulename] = 1 |
---|
100 | n/a | return 1 |
---|
101 | n/a | |
---|
102 | n/a | # check if the module is a proper submodule of something on |
---|
103 | n/a | # the ignore list |
---|
104 | n/a | for mod in self._mods: |
---|
105 | n/a | # Need to take some care since ignoring |
---|
106 | n/a | # "cmp" mustn't mean ignoring "cmpcache" but ignoring |
---|
107 | n/a | # "Spam" must also mean ignoring "Spam.Eggs". |
---|
108 | n/a | if modulename.startswith(mod + '.'): |
---|
109 | n/a | self._ignore[modulename] = 1 |
---|
110 | n/a | return 1 |
---|
111 | n/a | |
---|
112 | n/a | # Now check that filename isn't in one of the directories |
---|
113 | n/a | if filename is None: |
---|
114 | n/a | # must be a built-in, so we must ignore |
---|
115 | n/a | self._ignore[modulename] = 1 |
---|
116 | n/a | return 1 |
---|
117 | n/a | |
---|
118 | n/a | # Ignore a file when it contains one of the ignorable paths |
---|
119 | n/a | for d in self._dirs: |
---|
120 | n/a | # The '+ os.sep' is to ensure that d is a parent directory, |
---|
121 | n/a | # as compared to cases like: |
---|
122 | n/a | # d = "/usr/local" |
---|
123 | n/a | # filename = "/usr/local.py" |
---|
124 | n/a | # or |
---|
125 | n/a | # d = "/usr/local.py" |
---|
126 | n/a | # filename = "/usr/local.py" |
---|
127 | n/a | if filename.startswith(d + os.sep): |
---|
128 | n/a | self._ignore[modulename] = 1 |
---|
129 | n/a | return 1 |
---|
130 | n/a | |
---|
131 | n/a | # Tried the different ways, so we don't ignore this module |
---|
132 | n/a | self._ignore[modulename] = 0 |
---|
133 | n/a | return 0 |
---|
134 | n/a | |
---|
135 | n/a | def _modname(path): |
---|
136 | n/a | """Return a plausible module name for the patch.""" |
---|
137 | n/a | |
---|
138 | n/a | base = os.path.basename(path) |
---|
139 | n/a | filename, ext = os.path.splitext(base) |
---|
140 | n/a | return filename |
---|
141 | n/a | |
---|
142 | n/a | def _fullmodname(path): |
---|
143 | n/a | """Return a plausible module name for the path.""" |
---|
144 | n/a | |
---|
145 | n/a | # If the file 'path' is part of a package, then the filename isn't |
---|
146 | n/a | # enough to uniquely identify it. Try to do the right thing by |
---|
147 | n/a | # looking in sys.path for the longest matching prefix. We'll |
---|
148 | n/a | # assume that the rest is the package name. |
---|
149 | n/a | |
---|
150 | n/a | comparepath = os.path.normcase(path) |
---|
151 | n/a | longest = "" |
---|
152 | n/a | for dir in sys.path: |
---|
153 | n/a | dir = os.path.normcase(dir) |
---|
154 | n/a | if comparepath.startswith(dir) and comparepath[len(dir)] == os.sep: |
---|
155 | n/a | if len(dir) > len(longest): |
---|
156 | n/a | longest = dir |
---|
157 | n/a | |
---|
158 | n/a | if longest: |
---|
159 | n/a | base = path[len(longest) + 1:] |
---|
160 | n/a | else: |
---|
161 | n/a | base = path |
---|
162 | n/a | # the drive letter is never part of the module name |
---|
163 | n/a | drive, base = os.path.splitdrive(base) |
---|
164 | n/a | base = base.replace(os.sep, ".") |
---|
165 | n/a | if os.altsep: |
---|
166 | n/a | base = base.replace(os.altsep, ".") |
---|
167 | n/a | filename, ext = os.path.splitext(base) |
---|
168 | n/a | return filename.lstrip(".") |
---|
169 | n/a | |
---|
170 | n/a | class CoverageResults: |
---|
171 | n/a | def __init__(self, counts=None, calledfuncs=None, infile=None, |
---|
172 | n/a | callers=None, outfile=None): |
---|
173 | n/a | self.counts = counts |
---|
174 | n/a | if self.counts is None: |
---|
175 | n/a | self.counts = {} |
---|
176 | n/a | self.counter = self.counts.copy() # map (filename, lineno) to count |
---|
177 | n/a | self.calledfuncs = calledfuncs |
---|
178 | n/a | if self.calledfuncs is None: |
---|
179 | n/a | self.calledfuncs = {} |
---|
180 | n/a | self.calledfuncs = self.calledfuncs.copy() |
---|
181 | n/a | self.callers = callers |
---|
182 | n/a | if self.callers is None: |
---|
183 | n/a | self.callers = {} |
---|
184 | n/a | self.callers = self.callers.copy() |
---|
185 | n/a | self.infile = infile |
---|
186 | n/a | self.outfile = outfile |
---|
187 | n/a | if self.infile: |
---|
188 | n/a | # Try to merge existing counts file. |
---|
189 | n/a | try: |
---|
190 | n/a | with open(self.infile, 'rb') as f: |
---|
191 | n/a | counts, calledfuncs, callers = pickle.load(f) |
---|
192 | n/a | self.update(self.__class__(counts, calledfuncs, callers)) |
---|
193 | n/a | except (OSError, EOFError, ValueError) as err: |
---|
194 | n/a | print(("Skipping counts file %r: %s" |
---|
195 | n/a | % (self.infile, err)), file=sys.stderr) |
---|
196 | n/a | |
---|
197 | n/a | def is_ignored_filename(self, filename): |
---|
198 | n/a | """Return True if the filename does not refer to a file |
---|
199 | n/a | we want to have reported. |
---|
200 | n/a | """ |
---|
201 | n/a | return filename.startswith('<') and filename.endswith('>') |
---|
202 | n/a | |
---|
203 | n/a | def update(self, other): |
---|
204 | n/a | """Merge in the data from another CoverageResults""" |
---|
205 | n/a | counts = self.counts |
---|
206 | n/a | calledfuncs = self.calledfuncs |
---|
207 | n/a | callers = self.callers |
---|
208 | n/a | other_counts = other.counts |
---|
209 | n/a | other_calledfuncs = other.calledfuncs |
---|
210 | n/a | other_callers = other.callers |
---|
211 | n/a | |
---|
212 | n/a | for key in other_counts: |
---|
213 | n/a | counts[key] = counts.get(key, 0) + other_counts[key] |
---|
214 | n/a | |
---|
215 | n/a | for key in other_calledfuncs: |
---|
216 | n/a | calledfuncs[key] = 1 |
---|
217 | n/a | |
---|
218 | n/a | for key in other_callers: |
---|
219 | n/a | callers[key] = 1 |
---|
220 | n/a | |
---|
221 | n/a | def write_results(self, show_missing=True, summary=False, coverdir=None): |
---|
222 | n/a | """ |
---|
223 | n/a | Write the coverage results. |
---|
224 | n/a | |
---|
225 | n/a | :param show_missing: Show lines that had no hits. |
---|
226 | n/a | :param summary: Include coverage summary per module. |
---|
227 | n/a | :param coverdir: If None, the results of each module are placed in its |
---|
228 | n/a | directory, otherwise it is included in the directory |
---|
229 | n/a | specified. |
---|
230 | n/a | """ |
---|
231 | n/a | if self.calledfuncs: |
---|
232 | n/a | print() |
---|
233 | n/a | print("functions called:") |
---|
234 | n/a | calls = self.calledfuncs |
---|
235 | n/a | for filename, modulename, funcname in sorted(calls): |
---|
236 | n/a | print(("filename: %s, modulename: %s, funcname: %s" |
---|
237 | n/a | % (filename, modulename, funcname))) |
---|
238 | n/a | |
---|
239 | n/a | if self.callers: |
---|
240 | n/a | print() |
---|
241 | n/a | print("calling relationships:") |
---|
242 | n/a | lastfile = lastcfile = "" |
---|
243 | n/a | for ((pfile, pmod, pfunc), (cfile, cmod, cfunc)) \ |
---|
244 | n/a | in sorted(self.callers): |
---|
245 | n/a | if pfile != lastfile: |
---|
246 | n/a | print() |
---|
247 | n/a | print("***", pfile, "***") |
---|
248 | n/a | lastfile = pfile |
---|
249 | n/a | lastcfile = "" |
---|
250 | n/a | if cfile != pfile and lastcfile != cfile: |
---|
251 | n/a | print(" -->", cfile) |
---|
252 | n/a | lastcfile = cfile |
---|
253 | n/a | print(" %s.%s -> %s.%s" % (pmod, pfunc, cmod, cfunc)) |
---|
254 | n/a | |
---|
255 | n/a | # turn the counts data ("(filename, lineno) = count") into something |
---|
256 | n/a | # accessible on a per-file basis |
---|
257 | n/a | per_file = {} |
---|
258 | n/a | for filename, lineno in self.counts: |
---|
259 | n/a | lines_hit = per_file[filename] = per_file.get(filename, {}) |
---|
260 | n/a | lines_hit[lineno] = self.counts[(filename, lineno)] |
---|
261 | n/a | |
---|
262 | n/a | # accumulate summary info, if needed |
---|
263 | n/a | sums = {} |
---|
264 | n/a | |
---|
265 | n/a | for filename, count in per_file.items(): |
---|
266 | n/a | if self.is_ignored_filename(filename): |
---|
267 | n/a | continue |
---|
268 | n/a | |
---|
269 | n/a | if filename.endswith(".pyc"): |
---|
270 | n/a | filename = filename[:-1] |
---|
271 | n/a | |
---|
272 | n/a | if coverdir is None: |
---|
273 | n/a | dir = os.path.dirname(os.path.abspath(filename)) |
---|
274 | n/a | modulename = _modname(filename) |
---|
275 | n/a | else: |
---|
276 | n/a | dir = coverdir |
---|
277 | n/a | if not os.path.exists(dir): |
---|
278 | n/a | os.makedirs(dir) |
---|
279 | n/a | modulename = _fullmodname(filename) |
---|
280 | n/a | |
---|
281 | n/a | # If desired, get a list of the line numbers which represent |
---|
282 | n/a | # executable content (returned as a dict for better lookup speed) |
---|
283 | n/a | if show_missing: |
---|
284 | n/a | lnotab = _find_executable_linenos(filename) |
---|
285 | n/a | else: |
---|
286 | n/a | lnotab = {} |
---|
287 | n/a | if lnotab: |
---|
288 | n/a | source = linecache.getlines(filename) |
---|
289 | n/a | coverpath = os.path.join(dir, modulename + ".cover") |
---|
290 | n/a | with open(filename, 'rb') as fp: |
---|
291 | n/a | encoding, _ = tokenize.detect_encoding(fp.readline) |
---|
292 | n/a | n_hits, n_lines = self.write_results_file(coverpath, source, |
---|
293 | n/a | lnotab, count, encoding) |
---|
294 | n/a | if summary and n_lines: |
---|
295 | n/a | percent = int(100 * n_hits / n_lines) |
---|
296 | n/a | sums[modulename] = n_lines, percent, modulename, filename |
---|
297 | n/a | |
---|
298 | n/a | |
---|
299 | n/a | if summary and sums: |
---|
300 | n/a | print("lines cov% module (path)") |
---|
301 | n/a | for m in sorted(sums): |
---|
302 | n/a | n_lines, percent, modulename, filename = sums[m] |
---|
303 | n/a | print("%5d %3d%% %s (%s)" % sums[m]) |
---|
304 | n/a | |
---|
305 | n/a | if self.outfile: |
---|
306 | n/a | # try and store counts and module info into self.outfile |
---|
307 | n/a | try: |
---|
308 | n/a | pickle.dump((self.counts, self.calledfuncs, self.callers), |
---|
309 | n/a | open(self.outfile, 'wb'), 1) |
---|
310 | n/a | except OSError as err: |
---|
311 | n/a | print("Can't save counts files because %s" % err, file=sys.stderr) |
---|
312 | n/a | |
---|
313 | n/a | def write_results_file(self, path, lines, lnotab, lines_hit, encoding=None): |
---|
314 | n/a | """Return a coverage results file in path.""" |
---|
315 | n/a | |
---|
316 | n/a | try: |
---|
317 | n/a | outfile = open(path, "w", encoding=encoding) |
---|
318 | n/a | except OSError as err: |
---|
319 | n/a | print(("trace: Could not open %r for writing: %s" |
---|
320 | n/a | "- skipping" % (path, err)), file=sys.stderr) |
---|
321 | n/a | return 0, 0 |
---|
322 | n/a | |
---|
323 | n/a | n_lines = 0 |
---|
324 | n/a | n_hits = 0 |
---|
325 | n/a | with outfile: |
---|
326 | n/a | for lineno, line in enumerate(lines, 1): |
---|
327 | n/a | # do the blank/comment match to try to mark more lines |
---|
328 | n/a | # (help the reader find stuff that hasn't been covered) |
---|
329 | n/a | if lineno in lines_hit: |
---|
330 | n/a | outfile.write("%5d: " % lines_hit[lineno]) |
---|
331 | n/a | n_hits += 1 |
---|
332 | n/a | n_lines += 1 |
---|
333 | n/a | elif rx_blank.match(line): |
---|
334 | n/a | outfile.write(" ") |
---|
335 | n/a | else: |
---|
336 | n/a | # lines preceded by no marks weren't hit |
---|
337 | n/a | # Highlight them if so indicated, unless the line contains |
---|
338 | n/a | # #pragma: NO COVER |
---|
339 | n/a | if lineno in lnotab and not PRAGMA_NOCOVER in line: |
---|
340 | n/a | outfile.write(">>>>>> ") |
---|
341 | n/a | n_lines += 1 |
---|
342 | n/a | else: |
---|
343 | n/a | outfile.write(" ") |
---|
344 | n/a | outfile.write(line.expandtabs(8)) |
---|
345 | n/a | |
---|
346 | n/a | return n_hits, n_lines |
---|
347 | n/a | |
---|
348 | n/a | def _find_lines_from_code(code, strs): |
---|
349 | n/a | """Return dict where keys are lines in the line number table.""" |
---|
350 | n/a | linenos = {} |
---|
351 | n/a | |
---|
352 | n/a | for _, lineno in dis.findlinestarts(code): |
---|
353 | n/a | if lineno not in strs: |
---|
354 | n/a | linenos[lineno] = 1 |
---|
355 | n/a | |
---|
356 | n/a | return linenos |
---|
357 | n/a | |
---|
358 | n/a | def _find_lines(code, strs): |
---|
359 | n/a | """Return lineno dict for all code objects reachable from code.""" |
---|
360 | n/a | # get all of the lineno information from the code of this scope level |
---|
361 | n/a | linenos = _find_lines_from_code(code, strs) |
---|
362 | n/a | |
---|
363 | n/a | # and check the constants for references to other code objects |
---|
364 | n/a | for c in code.co_consts: |
---|
365 | n/a | if inspect.iscode(c): |
---|
366 | n/a | # find another code object, so recurse into it |
---|
367 | n/a | linenos.update(_find_lines(c, strs)) |
---|
368 | n/a | return linenos |
---|
369 | n/a | |
---|
370 | n/a | def _find_strings(filename, encoding=None): |
---|
371 | n/a | """Return a dict of possible docstring positions. |
---|
372 | n/a | |
---|
373 | n/a | The dict maps line numbers to strings. There is an entry for |
---|
374 | n/a | line that contains only a string or a part of a triple-quoted |
---|
375 | n/a | string. |
---|
376 | n/a | """ |
---|
377 | n/a | d = {} |
---|
378 | n/a | # If the first token is a string, then it's the module docstring. |
---|
379 | n/a | # Add this special case so that the test in the loop passes. |
---|
380 | n/a | prev_ttype = token.INDENT |
---|
381 | n/a | with open(filename, encoding=encoding) as f: |
---|
382 | n/a | tok = tokenize.generate_tokens(f.readline) |
---|
383 | n/a | for ttype, tstr, start, end, line in tok: |
---|
384 | n/a | if ttype == token.STRING: |
---|
385 | n/a | if prev_ttype == token.INDENT: |
---|
386 | n/a | sline, scol = start |
---|
387 | n/a | eline, ecol = end |
---|
388 | n/a | for i in range(sline, eline + 1): |
---|
389 | n/a | d[i] = 1 |
---|
390 | n/a | prev_ttype = ttype |
---|
391 | n/a | return d |
---|
392 | n/a | |
---|
393 | n/a | def _find_executable_linenos(filename): |
---|
394 | n/a | """Return dict where keys are line numbers in the line number table.""" |
---|
395 | n/a | try: |
---|
396 | n/a | with tokenize.open(filename) as f: |
---|
397 | n/a | prog = f.read() |
---|
398 | n/a | encoding = f.encoding |
---|
399 | n/a | except OSError as err: |
---|
400 | n/a | print(("Not printing coverage data for %r: %s" |
---|
401 | n/a | % (filename, err)), file=sys.stderr) |
---|
402 | n/a | return {} |
---|
403 | n/a | code = compile(prog, filename, "exec") |
---|
404 | n/a | strs = _find_strings(filename, encoding) |
---|
405 | n/a | return _find_lines(code, strs) |
---|
406 | n/a | |
---|
407 | n/a | class Trace: |
---|
408 | n/a | def __init__(self, count=1, trace=1, countfuncs=0, countcallers=0, |
---|
409 | n/a | ignoremods=(), ignoredirs=(), infile=None, outfile=None, |
---|
410 | n/a | timing=False): |
---|
411 | n/a | """ |
---|
412 | n/a | @param count true iff it should count number of times each |
---|
413 | n/a | line is executed |
---|
414 | n/a | @param trace true iff it should print out each line that is |
---|
415 | n/a | being counted |
---|
416 | n/a | @param countfuncs true iff it should just output a list of |
---|
417 | n/a | (filename, modulename, funcname,) for functions |
---|
418 | n/a | that were called at least once; This overrides |
---|
419 | n/a | `count' and `trace' |
---|
420 | n/a | @param ignoremods a list of the names of modules to ignore |
---|
421 | n/a | @param ignoredirs a list of the names of directories to ignore |
---|
422 | n/a | all of the (recursive) contents of |
---|
423 | n/a | @param infile file from which to read stored counts to be |
---|
424 | n/a | added into the results |
---|
425 | n/a | @param outfile file in which to write the results |
---|
426 | n/a | @param timing true iff timing information be displayed |
---|
427 | n/a | """ |
---|
428 | n/a | self.infile = infile |
---|
429 | n/a | self.outfile = outfile |
---|
430 | n/a | self.ignore = _Ignore(ignoremods, ignoredirs) |
---|
431 | n/a | self.counts = {} # keys are (filename, linenumber) |
---|
432 | n/a | self.pathtobasename = {} # for memoizing os.path.basename |
---|
433 | n/a | self.donothing = 0 |
---|
434 | n/a | self.trace = trace |
---|
435 | n/a | self._calledfuncs = {} |
---|
436 | n/a | self._callers = {} |
---|
437 | n/a | self._caller_cache = {} |
---|
438 | n/a | self.start_time = None |
---|
439 | n/a | if timing: |
---|
440 | n/a | self.start_time = _time() |
---|
441 | n/a | if countcallers: |
---|
442 | n/a | self.globaltrace = self.globaltrace_trackcallers |
---|
443 | n/a | elif countfuncs: |
---|
444 | n/a | self.globaltrace = self.globaltrace_countfuncs |
---|
445 | n/a | elif trace and count: |
---|
446 | n/a | self.globaltrace = self.globaltrace_lt |
---|
447 | n/a | self.localtrace = self.localtrace_trace_and_count |
---|
448 | n/a | elif trace: |
---|
449 | n/a | self.globaltrace = self.globaltrace_lt |
---|
450 | n/a | self.localtrace = self.localtrace_trace |
---|
451 | n/a | elif count: |
---|
452 | n/a | self.globaltrace = self.globaltrace_lt |
---|
453 | n/a | self.localtrace = self.localtrace_count |
---|
454 | n/a | else: |
---|
455 | n/a | # Ahem -- do nothing? Okay. |
---|
456 | n/a | self.donothing = 1 |
---|
457 | n/a | |
---|
458 | n/a | def run(self, cmd): |
---|
459 | n/a | import __main__ |
---|
460 | n/a | dict = __main__.__dict__ |
---|
461 | n/a | self.runctx(cmd, dict, dict) |
---|
462 | n/a | |
---|
463 | n/a | def runctx(self, cmd, globals=None, locals=None): |
---|
464 | n/a | if globals is None: globals = {} |
---|
465 | n/a | if locals is None: locals = {} |
---|
466 | n/a | if not self.donothing: |
---|
467 | n/a | _settrace(self.globaltrace) |
---|
468 | n/a | try: |
---|
469 | n/a | exec(cmd, globals, locals) |
---|
470 | n/a | finally: |
---|
471 | n/a | if not self.donothing: |
---|
472 | n/a | _unsettrace() |
---|
473 | n/a | |
---|
474 | n/a | def runfunc(self, func, *args, **kw): |
---|
475 | n/a | result = None |
---|
476 | n/a | if not self.donothing: |
---|
477 | n/a | sys.settrace(self.globaltrace) |
---|
478 | n/a | try: |
---|
479 | n/a | result = func(*args, **kw) |
---|
480 | n/a | finally: |
---|
481 | n/a | if not self.donothing: |
---|
482 | n/a | sys.settrace(None) |
---|
483 | n/a | return result |
---|
484 | n/a | |
---|
485 | n/a | def file_module_function_of(self, frame): |
---|
486 | n/a | code = frame.f_code |
---|
487 | n/a | filename = code.co_filename |
---|
488 | n/a | if filename: |
---|
489 | n/a | modulename = _modname(filename) |
---|
490 | n/a | else: |
---|
491 | n/a | modulename = None |
---|
492 | n/a | |
---|
493 | n/a | funcname = code.co_name |
---|
494 | n/a | clsname = None |
---|
495 | n/a | if code in self._caller_cache: |
---|
496 | n/a | if self._caller_cache[code] is not None: |
---|
497 | n/a | clsname = self._caller_cache[code] |
---|
498 | n/a | else: |
---|
499 | n/a | self._caller_cache[code] = None |
---|
500 | n/a | ## use of gc.get_referrers() was suggested by Michael Hudson |
---|
501 | n/a | # all functions which refer to this code object |
---|
502 | n/a | funcs = [f for f in gc.get_referrers(code) |
---|
503 | n/a | if inspect.isfunction(f)] |
---|
504 | n/a | # require len(func) == 1 to avoid ambiguity caused by calls to |
---|
505 | n/a | # new.function(): "In the face of ambiguity, refuse the |
---|
506 | n/a | # temptation to guess." |
---|
507 | n/a | if len(funcs) == 1: |
---|
508 | n/a | dicts = [d for d in gc.get_referrers(funcs[0]) |
---|
509 | n/a | if isinstance(d, dict)] |
---|
510 | n/a | if len(dicts) == 1: |
---|
511 | n/a | classes = [c for c in gc.get_referrers(dicts[0]) |
---|
512 | n/a | if hasattr(c, "__bases__")] |
---|
513 | n/a | if len(classes) == 1: |
---|
514 | n/a | # ditto for new.classobj() |
---|
515 | n/a | clsname = classes[0].__name__ |
---|
516 | n/a | # cache the result - assumption is that new.* is |
---|
517 | n/a | # not called later to disturb this relationship |
---|
518 | n/a | # _caller_cache could be flushed if functions in |
---|
519 | n/a | # the new module get called. |
---|
520 | n/a | self._caller_cache[code] = clsname |
---|
521 | n/a | if clsname is not None: |
---|
522 | n/a | funcname = "%s.%s" % (clsname, funcname) |
---|
523 | n/a | |
---|
524 | n/a | return filename, modulename, funcname |
---|
525 | n/a | |
---|
526 | n/a | def globaltrace_trackcallers(self, frame, why, arg): |
---|
527 | n/a | """Handler for call events. |
---|
528 | n/a | |
---|
529 | n/a | Adds information about who called who to the self._callers dict. |
---|
530 | n/a | """ |
---|
531 | n/a | if why == 'call': |
---|
532 | n/a | # XXX Should do a better job of identifying methods |
---|
533 | n/a | this_func = self.file_module_function_of(frame) |
---|
534 | n/a | parent_func = self.file_module_function_of(frame.f_back) |
---|
535 | n/a | self._callers[(parent_func, this_func)] = 1 |
---|
536 | n/a | |
---|
537 | n/a | def globaltrace_countfuncs(self, frame, why, arg): |
---|
538 | n/a | """Handler for call events. |
---|
539 | n/a | |
---|
540 | n/a | Adds (filename, modulename, funcname) to the self._calledfuncs dict. |
---|
541 | n/a | """ |
---|
542 | n/a | if why == 'call': |
---|
543 | n/a | this_func = self.file_module_function_of(frame) |
---|
544 | n/a | self._calledfuncs[this_func] = 1 |
---|
545 | n/a | |
---|
546 | n/a | def globaltrace_lt(self, frame, why, arg): |
---|
547 | n/a | """Handler for call events. |
---|
548 | n/a | |
---|
549 | n/a | If the code block being entered is to be ignored, returns `None', |
---|
550 | n/a | else returns self.localtrace. |
---|
551 | n/a | """ |
---|
552 | n/a | if why == 'call': |
---|
553 | n/a | code = frame.f_code |
---|
554 | n/a | filename = frame.f_globals.get('__file__', None) |
---|
555 | n/a | if filename: |
---|
556 | n/a | # XXX _modname() doesn't work right for packages, so |
---|
557 | n/a | # the ignore support won't work right for packages |
---|
558 | n/a | modulename = _modname(filename) |
---|
559 | n/a | if modulename is not None: |
---|
560 | n/a | ignore_it = self.ignore.names(filename, modulename) |
---|
561 | n/a | if not ignore_it: |
---|
562 | n/a | if self.trace: |
---|
563 | n/a | print((" --- modulename: %s, funcname: %s" |
---|
564 | n/a | % (modulename, code.co_name))) |
---|
565 | n/a | return self.localtrace |
---|
566 | n/a | else: |
---|
567 | n/a | return None |
---|
568 | n/a | |
---|
569 | n/a | def localtrace_trace_and_count(self, frame, why, arg): |
---|
570 | n/a | if why == "line": |
---|
571 | n/a | # record the file name and line number of every trace |
---|
572 | n/a | filename = frame.f_code.co_filename |
---|
573 | n/a | lineno = frame.f_lineno |
---|
574 | n/a | key = filename, lineno |
---|
575 | n/a | self.counts[key] = self.counts.get(key, 0) + 1 |
---|
576 | n/a | |
---|
577 | n/a | if self.start_time: |
---|
578 | n/a | print('%.2f' % (_time() - self.start_time), end=' ') |
---|
579 | n/a | bname = os.path.basename(filename) |
---|
580 | n/a | print("%s(%d): %s" % (bname, lineno, |
---|
581 | n/a | linecache.getline(filename, lineno)), end='') |
---|
582 | n/a | return self.localtrace |
---|
583 | n/a | |
---|
584 | n/a | def localtrace_trace(self, frame, why, arg): |
---|
585 | n/a | if why == "line": |
---|
586 | n/a | # record the file name and line number of every trace |
---|
587 | n/a | filename = frame.f_code.co_filename |
---|
588 | n/a | lineno = frame.f_lineno |
---|
589 | n/a | |
---|
590 | n/a | if self.start_time: |
---|
591 | n/a | print('%.2f' % (_time() - self.start_time), end=' ') |
---|
592 | n/a | bname = os.path.basename(filename) |
---|
593 | n/a | print("%s(%d): %s" % (bname, lineno, |
---|
594 | n/a | linecache.getline(filename, lineno)), end='') |
---|
595 | n/a | return self.localtrace |
---|
596 | n/a | |
---|
597 | n/a | def localtrace_count(self, frame, why, arg): |
---|
598 | n/a | if why == "line": |
---|
599 | n/a | filename = frame.f_code.co_filename |
---|
600 | n/a | lineno = frame.f_lineno |
---|
601 | n/a | key = filename, lineno |
---|
602 | n/a | self.counts[key] = self.counts.get(key, 0) + 1 |
---|
603 | n/a | return self.localtrace |
---|
604 | n/a | |
---|
605 | n/a | def results(self): |
---|
606 | n/a | return CoverageResults(self.counts, infile=self.infile, |
---|
607 | n/a | outfile=self.outfile, |
---|
608 | n/a | calledfuncs=self._calledfuncs, |
---|
609 | n/a | callers=self._callers) |
---|
610 | n/a | |
---|
611 | n/a | def main(): |
---|
612 | n/a | |
---|
613 | n/a | parser = argparse.ArgumentParser() |
---|
614 | n/a | parser.add_argument('--version', action='version', version='trace 2.0') |
---|
615 | n/a | |
---|
616 | n/a | grp = parser.add_argument_group('Main options', |
---|
617 | n/a | 'One of these (or --report) must be given') |
---|
618 | n/a | |
---|
619 | n/a | grp.add_argument('-c', '--count', action='store_true', |
---|
620 | n/a | help='Count the number of times each line is executed and write ' |
---|
621 | n/a | 'the counts to <module>.cover for each module executed, in ' |
---|
622 | n/a | 'the module\'s directory. See also --coverdir, --file, ' |
---|
623 | n/a | '--no-report below.') |
---|
624 | n/a | grp.add_argument('-t', '--trace', action='store_true', |
---|
625 | n/a | help='Print each line to sys.stdout before it is executed') |
---|
626 | n/a | grp.add_argument('-l', '--listfuncs', action='store_true', |
---|
627 | n/a | help='Keep track of which functions are executed at least once ' |
---|
628 | n/a | 'and write the results to sys.stdout after the program exits. ' |
---|
629 | n/a | 'Cannot be specified alongside --trace or --count.') |
---|
630 | n/a | grp.add_argument('-T', '--trackcalls', action='store_true', |
---|
631 | n/a | help='Keep track of caller/called pairs and write the results to ' |
---|
632 | n/a | 'sys.stdout after the program exits.') |
---|
633 | n/a | |
---|
634 | n/a | grp = parser.add_argument_group('Modifiers') |
---|
635 | n/a | |
---|
636 | n/a | _grp = grp.add_mutually_exclusive_group() |
---|
637 | n/a | _grp.add_argument('-r', '--report', action='store_true', |
---|
638 | n/a | help='Generate a report from a counts file; does not execute any ' |
---|
639 | n/a | 'code. --file must specify the results file to read, which ' |
---|
640 | n/a | 'must have been created in a previous run with --count ' |
---|
641 | n/a | '--file=FILE') |
---|
642 | n/a | _grp.add_argument('-R', '--no-report', action='store_true', |
---|
643 | n/a | help='Do not generate the coverage report files. ' |
---|
644 | n/a | 'Useful if you want to accumulate over several runs.') |
---|
645 | n/a | |
---|
646 | n/a | grp.add_argument('-f', '--file', |
---|
647 | n/a | help='File to accumulate counts over several runs') |
---|
648 | n/a | grp.add_argument('-C', '--coverdir', |
---|
649 | n/a | help='Directory where the report files go. The coverage report ' |
---|
650 | n/a | 'for <package>.<module> will be written to file ' |
---|
651 | n/a | '<dir>/<package>/<module>.cover') |
---|
652 | n/a | grp.add_argument('-m', '--missing', action='store_true', |
---|
653 | n/a | help='Annotate executable lines that were not executed with ' |
---|
654 | n/a | '">>>>>> "') |
---|
655 | n/a | grp.add_argument('-s', '--summary', action='store_true', |
---|
656 | n/a | help='Write a brief summary for each file to sys.stdout. ' |
---|
657 | n/a | 'Can only be used with --count or --report') |
---|
658 | n/a | grp.add_argument('-g', '--timing', action='store_true', |
---|
659 | n/a | help='Prefix each line with the time since the program started. ' |
---|
660 | n/a | 'Only used while tracing') |
---|
661 | n/a | |
---|
662 | n/a | grp = parser.add_argument_group('Filters', |
---|
663 | n/a | 'Can be specified multiple times') |
---|
664 | n/a | grp.add_argument('--ignore-module', action='append', default=[], |
---|
665 | n/a | help='Ignore the given module(s) and its submodules' |
---|
666 | n/a | '(if it is a package). Accepts comma separated list of ' |
---|
667 | n/a | 'module names.') |
---|
668 | n/a | grp.add_argument('--ignore-dir', action='append', default=[], |
---|
669 | n/a | help='Ignore files in the given directory ' |
---|
670 | n/a | '(multiple directories can be joined by os.pathsep).') |
---|
671 | n/a | |
---|
672 | n/a | parser.add_argument('filename', nargs='?', |
---|
673 | n/a | help='file to run as main program') |
---|
674 | n/a | parser.add_argument('arguments', nargs=argparse.REMAINDER, |
---|
675 | n/a | help='arguments to the program') |
---|
676 | n/a | |
---|
677 | n/a | opts = parser.parse_args() |
---|
678 | n/a | |
---|
679 | n/a | if opts.ignore_dir: |
---|
680 | n/a | rel_path = 'lib', 'python{0.major}.{0.minor}'.format(sys.version_info) |
---|
681 | n/a | _prefix = os.path.join(sys.base_prefix, *rel_path) |
---|
682 | n/a | _exec_prefix = os.path.join(sys.base_exec_prefix, *rel_path) |
---|
683 | n/a | |
---|
684 | n/a | def parse_ignore_dir(s): |
---|
685 | n/a | s = os.path.expanduser(os.path.expandvars(s)) |
---|
686 | n/a | s = s.replace('$prefix', _prefix).replace('$exec_prefix', _exec_prefix) |
---|
687 | n/a | return os.path.normpath(s) |
---|
688 | n/a | |
---|
689 | n/a | opts.ignore_module = [mod.strip() |
---|
690 | n/a | for i in opts.ignore_module for mod in i.split(',')] |
---|
691 | n/a | opts.ignore_dir = [parse_ignore_dir(s) |
---|
692 | n/a | for i in opts.ignore_dir for s in i.split(os.pathsep)] |
---|
693 | n/a | |
---|
694 | n/a | if opts.report: |
---|
695 | n/a | if not opts.file: |
---|
696 | n/a | parser.error('-r/--report requires -f/--file') |
---|
697 | n/a | results = CoverageResults(infile=opts.file, outfile=opts.file) |
---|
698 | n/a | return results.write_results(opts.missing, opts.summary, opts.coverdir) |
---|
699 | n/a | |
---|
700 | n/a | if not any([opts.trace, opts.count, opts.listfuncs, opts.trackcalls]): |
---|
701 | n/a | parser.error('must specify one of --trace, --count, --report, ' |
---|
702 | n/a | '--listfuncs, or --trackcalls') |
---|
703 | n/a | |
---|
704 | n/a | if opts.listfuncs and (opts.count or opts.trace): |
---|
705 | n/a | parser.error('cannot specify both --listfuncs and (--trace or --count)') |
---|
706 | n/a | |
---|
707 | n/a | if opts.summary and not opts.count: |
---|
708 | n/a | parser.error('--summary can only be used with --count or --report') |
---|
709 | n/a | |
---|
710 | n/a | if opts.filename is None: |
---|
711 | n/a | parser.error('filename is missing: required with the main options') |
---|
712 | n/a | |
---|
713 | n/a | sys.argv = opts.filename, *opts.arguments |
---|
714 | n/a | sys.path[0] = os.path.dirname(opts.filename) |
---|
715 | n/a | |
---|
716 | n/a | t = Trace(opts.count, opts.trace, countfuncs=opts.listfuncs, |
---|
717 | n/a | countcallers=opts.trackcalls, ignoremods=opts.ignore_module, |
---|
718 | n/a | ignoredirs=opts.ignore_dir, infile=opts.file, |
---|
719 | n/a | outfile=opts.file, timing=opts.timing) |
---|
720 | n/a | try: |
---|
721 | n/a | with open(opts.filename) as fp: |
---|
722 | n/a | code = compile(fp.read(), opts.filename, 'exec') |
---|
723 | n/a | # try to emulate __main__ namespace as much as possible |
---|
724 | n/a | globs = { |
---|
725 | n/a | '__file__': opts.filename, |
---|
726 | n/a | '__name__': '__main__', |
---|
727 | n/a | '__package__': None, |
---|
728 | n/a | '__cached__': None, |
---|
729 | n/a | } |
---|
730 | n/a | t.runctx(code, globs, globs) |
---|
731 | n/a | except OSError as err: |
---|
732 | n/a | sys.exit("Cannot run file %r because: %s" % (sys.argv[0], err)) |
---|
733 | n/a | except SystemExit: |
---|
734 | n/a | pass |
---|
735 | n/a | |
---|
736 | n/a | results = t.results() |
---|
737 | n/a | |
---|
738 | n/a | if not opts.no_report: |
---|
739 | n/a | results.write_results(opts.missing, opts.summary, opts.coverdir) |
---|
740 | n/a | |
---|
741 | n/a | if __name__=='__main__': |
---|
742 | n/a | main() |
---|