annotate Tools/scripts/trace.py @ 25135:0f0016632be4 legacy-trunk

_formatparam(), set_param(): RFC 2231 encoding support by Oleg Broytmann in SF patch #600096. Specifically, the former function now encodes the triplets, while the latter adds optional charset and language arguments.
author Barry Warsaw <barry@python.org>
date Fri, 06 Sep 2002 03:55:04 +0000
parents cf5928fab108
children 8a7a2f1f88f0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1 #!/usr/bin/env python
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
3 # portions copyright 2001, Autonomous Zones Industries, Inc., all rights...
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
4 # err... reserved and offered to the public under the terms of the
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
5 # Python 2.2 license.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
6 # Author: Zooko O'Whielacronx
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
7 # http://zooko.com/
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
8 # mailto:zooko@zooko.com
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
9 #
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
10 # Copyright 2000, Mojam Media, Inc., all rights reserved.
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
11 # Author: Skip Montanaro
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
12 #
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
13 # Copyright 1999, Bioreason, Inc., all rights reserved.
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
14 # Author: Andrew Dalke
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
15 #
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
16 # Copyright 1995-1997, Automatrix, Inc., all rights reserved.
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
17 # Author: Skip Montanaro
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
18 #
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
19 # Copyright 1991-1995, Stichting Mathematisch Centrum, all rights reserved.
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
20 #
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
21 #
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
22 # Permission to use, copy, modify, and distribute this Python software and
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
23 # its associated documentation for any purpose without fee is hereby
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
24 # granted, provided that the above copyright notice appears in all copies,
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
25 # and that both that copyright notice and this permission notice appear in
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
26 # supporting documentation, and that the name of neither Automatrix,
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
27 # Bioreason or Mojam Media be used in advertising or publicity pertaining to
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
28 # distribution of the software without specific, written prior permission.
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
29 #
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
30 #
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
31 # Cleaned up the usage message --GvR 11/28/01
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
32 #
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
33 # Summary of even more recent changes, --Zooko 2001-10-14
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
34 # Used new `inspect' module for better (?) determination of file<->module
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
35 # mappings, line numbers, and source code.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
36 # Used new local trace function for faster (and better?) operation.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
37 # Removed "speed hack", which, as far as I can tell, meant that it would
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
38 # ignore all files ??? (When I tried it, it would ignore only *most* of my
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
39 # files. In any case with the speed hack removed in favor of actually
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
40 # calling `Ignore.names()', it ignores only those files that I told it to
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
41 # ignore, so I am happy.)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
42 # Rolled the `Coverage' class into `Trace', which now does either tracing or
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
43 # counting or both according to constructor flags.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
44 # Moved the construction of the `Ignore' object inside the constructor of
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
45 # `Trace', simplifying usage.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
46 # Changed function `create_results_log()' into method
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
47 # `CoverageResults.write_results()'.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
48 # Add new mode "countfuncs" which is faster and which just reports which
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
49 # functions were invoked.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
50
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
51 # Made `write_results' create `coverdir' if it doesn't already exist.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
52 # Moved the `run' funcs into `Trace' for simpler usage.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
53 # Use pickle instead of marshal for persistence.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
54 #
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
55 # Summary of recent changes:
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
56 # Support for files with the same basename (submodules in packages)
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
57 # Expanded the idea of how to ignore files or modules
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
58 # Split tracing and counting into different classes
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
59 # Extracted count information and reporting from the count class
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
60 # Added some ability to detect which missing lines could be executed
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
61 # Added pseudo-pragma to prohibit complaining about unexecuted lines
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
62 # Rewrote the main program
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
63
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
64 # Summary of older changes:
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
65 # Added run-time display of statements being executed
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
66 # Incorporated portability and performance fixes from Greg Stein
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
67 # Incorporated main program from Michael Scharf
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
68
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
69 """
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
70 program/module to trace Python program or function execution
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
71
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
72 Sample use, command line:
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
73 trace.py -c -f counts --ignore-dir '$prefix' spam.py eggs
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
74 trace.py -t --ignore-dir '$prefix' spam.py eggs
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
75
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
76 Sample use, programmatically
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
77 # create a Trace object, telling it what to ignore, and whether to do tracing
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
78 # or line-counting or both.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
79 trace = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], trace=0, count=1)
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
80 # run the new command using the given trace
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
81 trace.run(coverage.globaltrace, 'main()')
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
82 # make a report, telling it where you want output
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
83 trace.print_results(show_missing=1)
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
84 """
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
85
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
86 import sys, os, string, tempfile, types, copy, operator, inspect, exceptions, marshal
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
87 try:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
88 import cPickle
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
89 pickle = cPickle
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
90 except ImportError:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
91 import pickle
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
92
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
93 true = 1
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
94 false = None
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
95
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
96 # DEBUG_MODE=1 # make this true to get printouts which help you understand what's going on
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
97
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
98 def usage(outfile):
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
99 outfile.write("""Usage: %s [OPTIONS] <file> [ARGS]
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
100
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
101 Meta-options:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
102 --help Display this help then exit.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
103 --version Output version information then exit.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
104
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
105 Otherwise, exactly one of the following three options must be given:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
106 -t, --trace Print each line to sys.stdout before it is executed.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
107 -c, --count Count the number of times each line is executed
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
108 and write the counts to <module>.cover for each
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
109 module executed, in the module's directory.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
110 See also `--coverdir', `--file', `--no-report' below.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
111 -r, --report Generate a report from a counts file; do not execute
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
112 any code. `--file' must specify the results file to
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
113 read, which must have been created in a previous run
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
114 with `--count --file=FILE'.
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
115
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
116 Modifiers:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
117 -f, --file=<file> File to accumulate counts over several runs.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
118 -R, --no-report Do not generate the coverage report files.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
119 Useful if you want to accumulate over several runs.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
120 -C, --coverdir=<dir> Directory where the report files. The coverage
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
121 report for <package>.<module> is written to file
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
122 <dir>/<package>/<module>.cover.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
123 -m, --missing Annotate executable lines that were not executed
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
124 with '>>>>>> '.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
125 -s, --summary Write a brief summary on stdout for each file.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
126 (Can only be used with --count or --report.)
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
127
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
128 Filters, may be repeated multiple times:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
129 --ignore-module=<mod> Ignore the given module and its submodules
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
130 (if it is a package).
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
131 --ignore-dir=<dir> Ignore files in the given directory (multiple
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
132 directories can be joined by os.pathsep).
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
133 """ % sys.argv[0])
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
134
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
135 class Ignore:
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
136 def __init__(self, modules = None, dirs = None):
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
137 self._mods = modules or []
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
138 self._dirs = dirs or []
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
139
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
140 self._dirs = map(os.path.normpath, self._dirs)
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
141 self._ignore = { '<string>': 1 }
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
142
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
143 def names(self, filename, modulename):
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
144 if self._ignore.has_key(modulename):
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
145 return self._ignore[modulename]
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
146
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
147 # haven't seen this one before, so see if the module name is
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
148 # on the ignore list. Need to take some care since ignoring
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
149 # "cmp" musn't mean ignoring "cmpcache" but ignoring
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
150 # "Spam" must also mean ignoring "Spam.Eggs".
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
151 for mod in self._mods:
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
152 if mod == modulename: # Identical names, so ignore
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
153 self._ignore[modulename] = 1
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
154 return 1
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
155 # check if the module is a proper submodule of something on
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
156 # the ignore list
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
157 n = len(mod)
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
158 # (will not overflow since if the first n characters are the
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
159 # same and the name has not already occured, then the size
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
160 # of "name" is greater than that of "mod")
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
161 if mod == modulename[:n] and modulename[n] == '.':
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
162 self._ignore[modulename] = 1
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
163 return 1
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
164
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
165 # Now check that __file__ isn't in one of the directories
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
166 if filename is None:
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
167 # must be a built-in, so we must ignore
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
168 self._ignore[modulename] = 1
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
169 return 1
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
170
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
171 # Ignore a file when it contains one of the ignorable paths
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
172 for d in self._dirs:
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
173 # The '+ os.sep' is to ensure that d is a parent directory,
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
174 # as compared to cases like:
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
175 # d = "/usr/local"
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
176 # filename = "/usr/local.py"
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
177 # or
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
178 # d = "/usr/local.py"
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
179 # filename = "/usr/local.py"
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
180 if string.find(filename, d + os.sep) == 0:
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
181 self._ignore[modulename] = 1
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
182 return 1
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
183
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
184 # Tried the different ways, so we don't ignore this module
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
185 self._ignore[modulename] = 0
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
186 return 0
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
187
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
188 class CoverageResults:
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
189 def __init__(self, counts=None, calledfuncs=None, infile=None, outfile=None):
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
190 self.counts = counts
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
191 if self.counts is None:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
192 self.counts = {}
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
193 self.counter = self.counts.copy() # map (filename, lineno) to count
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
194 self.calledfuncs = calledfuncs
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
195 if self.calledfuncs is None:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
196 self.calledfuncs = {}
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
197 self.calledfuncs = self.calledfuncs.copy()
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
198 self.infile = infile
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
199 self.outfile = outfile
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
200 if self.infile:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
201 # try and merge existing counts file
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
202 try:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
203 thingie = pickle.load(open(self.infile, 'r'))
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
204 if type(thingie) is types.DictType:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
205 # backwards compatibility for old trace.py after Zooko touched it but before calledfuncs --Zooko 2001-10-24
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
206 self.update(self.__class__(thingie))
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
207 elif type(thingie) is types.TupleType and len(thingie) == 2:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
208 (counts, calledfuncs,) = thingie
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
209 self.update(self.__class__(counts, calledfuncs))
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
210 except (IOError, EOFError,):
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
211 pass
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
212 except pickle.UnpicklingError:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
213 # backwards compatibility for old trace.py before Zooko touched it --Zooko 2001-10-24
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
214 self.update(self.__class__(marshal.load(open(self.infile))))
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
215
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
216 def update(self, other):
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
217 """Merge in the data from another CoverageResults"""
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
218 counts = self.counts
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
219 calledfuncs = self.calledfuncs
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
220 other_counts = other.counts
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
221 other_calledfuncs = other.calledfuncs
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
222
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
223 for key in other_counts.keys():
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
224 if key != 'calledfuncs': # backwards compatibility for abortive attempt to stuff calledfuncs into self.counts, by Zooko --Zooko 2001-10-24
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
225 counts[key] = counts.get(key, 0) + other_counts[key]
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
226
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
227 for key in other_calledfuncs.keys():
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
228 calledfuncs[key] = 1
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
229
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
230 def write_results(self, show_missing = 1, summary = 0, coverdir = None):
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
231 """
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
232 @param coverdir
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
233 """
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
234 for (filename, modulename, funcname,) in self.calledfuncs.keys():
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
235 print "filename: %s, modulename: %s, funcname: %s" % (filename, modulename, funcname,)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
236
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
237 import re
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
238 # turn the counts data ("(filename, lineno) = count") into something
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
239 # accessible on a per-file basis
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
240 per_file = {}
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
241 for thingie in self.counts.keys():
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
242 if thingie != "calledfuncs": # backwards compatibility for abortive attempt to stuff calledfuncs into self.counts, by Zooko --Zooko 2001-10-24
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
243 (filename, lineno,) = thingie
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
244 lines_hit = per_file[filename] = per_file.get(filename, {})
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
245 lines_hit[lineno] = self.counts[(filename, lineno)]
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
246
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
247 # there are many places where this is insufficient, like a blank
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
248 # line embedded in a multiline string.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
249 blank = re.compile(r'^\s*(#.*)?$')
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
250
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
251 # accumulate summary info, if needed
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
252 sums = {}
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
253
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
254 # generate file paths for the coverage files we are going to write...
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
255 fnlist = []
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
256 tfdir = tempfile.gettempdir()
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
257 for key in per_file.keys():
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
258 filename = key
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
259
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
260 # skip some "files" we don't care about...
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
261 if filename == "<string>":
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
262 continue
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
263 # are these caused by code compiled using exec or something?
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
264 if filename.startswith(tfdir):
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
265 continue
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
266
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
267 modulename = inspect.getmodulename(filename)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
268
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
269 if filename.endswith(".pyc") or filename.endswith(".pyo"):
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
270 filename = filename[:-1]
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
271
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
272 if coverdir:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
273 thiscoverdir = coverdir
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
274 else:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
275 thiscoverdir = os.path.dirname(os.path.abspath(filename))
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
276
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
277 # the code from here to "<<<" is the contents of the `fileutil.make_dirs()' function in the Mojo Nation project. --Zooko 2001-10-14
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
278 # http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/mojonation/evil/common/fileutil.py?rev=HEAD&content-type=text/vnd.viewcvs-markup
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
279 tx = None
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
280 try:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
281 os.makedirs(thiscoverdir)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
282 except OSError, x:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
283 tx = x
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
284
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
285 if not os.path.isdir(thiscoverdir):
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
286 if tx:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
287 raise tx
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
288 raise exceptions.IOError, "unknown error prevented creation of directory: %s" % thiscoverdir # careful not to construct an IOError with a 2-tuple, as that has a special meaning...
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
289 # <<<
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
290
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
291 # build list file name by appending a ".cover" to the module name
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
292 # and sticking it into the specified directory
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
293 if "." in modulename:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
294 # A module in a package
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
295 finalname = modulename.split(".")[-1]
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
296 listfilename = os.path.join(thiscoverdir, finalname + ".cover")
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
297 else:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
298 listfilename = os.path.join(thiscoverdir, modulename + ".cover")
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
299
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
300 # Get the original lines from the .py file
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
301 try:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
302 lines = open(filename, 'r').readlines()
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
303 except IOError, err:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
304 sys.stderr.write("trace: Could not open %s for reading because: %s - skipping\n" % (`filename`, err))
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
305 continue
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
306
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
307 try:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
308 outfile = open(listfilename, 'w')
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
309 except IOError, err:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
310 sys.stderr.write(
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
311 '%s: Could not open %s for writing because: %s" \
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
312 "- skipping\n' % ("trace", `listfilename`, err))
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
313 continue
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
314
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
315 # If desired, get a list of the line numbers which represent
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
316 # executable content (returned as a dict for better lookup speed)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
317 if show_missing:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
318 executable_linenos = find_executable_linenos(filename)
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
319 else:
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
320 executable_linenos = {}
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
321
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
322 n_lines = 0
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
323 n_hits = 0
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
324 lines_hit = per_file[key]
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
325 for i in range(len(lines)):
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
326 line = lines[i]
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
327
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
328 # do the blank/comment match to try to mark more lines
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
329 # (help the reader find stuff that hasn't been covered)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
330 if lines_hit.has_key(i+1):
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
331 # count precedes the lines that we captured
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
332 outfile.write('%5d: ' % lines_hit[i+1])
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
333 n_hits = n_hits + 1
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
334 n_lines = n_lines + 1
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
335 elif blank.match(line):
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
336 # blank lines and comments are preceded by dots
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
337 outfile.write(' . ')
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
338 else:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
339 # lines preceded by no marks weren't hit
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
340 # Highlight them if so indicated, unless the line contains
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
341 # '#pragma: NO COVER' (it is possible to embed this into
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
342 # the text as a non-comment; no easy fix)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
343 if executable_linenos.has_key(i+1) and \
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
344 string.find(lines[i],
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
345 string.join(['#pragma', 'NO COVER'])) == -1:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
346 outfile.write('>>>>>> ')
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
347 else:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
348 outfile.write(' '*7)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
349 n_lines = n_lines + 1
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
350 outfile.write(string.expandtabs(lines[i], 8))
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
351
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
352 outfile.close()
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
353
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
354 if summary and n_lines:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
355 percent = int(100 * n_hits / n_lines)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
356 sums[modulename] = n_lines, percent, modulename, filename
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
357
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
358 if summary and sums:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
359 mods = sums.keys()
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
360 mods.sort()
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
361 print "lines cov% module (path)"
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
362 for m in mods:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
363 n_lines, percent, modulename, filename = sums[m]
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
364 print "%5d %3d%% %s (%s)" % sums[m]
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
365
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
366 if self.outfile:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
367 # try and store counts and module info into self.outfile
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
368 try:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
369 pickle.dump((self.counts, self.calledfuncs,), open(self.outfile, 'w'), 1)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
370 except IOError, err:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
371 sys.stderr.write("cannot save counts files because %s" % err)
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
372
24844
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24377
diff changeset
373 def _find_LINENO_from_code(code):
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24377
diff changeset
374 """return the numbers of the lines containing the source code that
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24377
diff changeset
375 was compiled into code"""
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
376 linenos = {}
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
377
24844
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24377
diff changeset
378 line_increments = [ord(c) for c in code.co_lnotab[1::2]]
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24377
diff changeset
379 table_length = len(line_increments)
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24377
diff changeset
380
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24377
diff changeset
381 lineno = code.co_first_lineno
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24377
diff changeset
382
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24377
diff changeset
383 for li in line_increments:
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24377
diff changeset
384 linenos[lineno] = 1
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24377
diff changeset
385 lineno += li
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24377
diff changeset
386 linenos[lineno] = 1
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24377
diff changeset
387
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
388 return linenos
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
389
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
390 def _find_LINENO(code):
24844
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24377
diff changeset
391 """return all of the lineno information from a code object"""
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
392 import types
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
393
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
394 # get all of the lineno information from the code of this scope level
24844
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24377
diff changeset
395 linenos = _find_LINENO_from_code(code)
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
396
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
397 # and check the constants for references to other code objects
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
398 for c in code.co_consts:
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
399 if type(c) == types.CodeType:
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
400 # find another code object, so recurse into it
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
401 linenos.update(_find_LINENO(c))
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
402 return linenos
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
403
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
404 def find_executable_linenos(filename):
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
405 """return a dict of the line numbers from executable statements in a file
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
406
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
407 """
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
408 import parser
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
409
17703
f3b8785bd022 Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15930
diff changeset
410 assert filename.endswith('.py')
f3b8785bd022 Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15930
diff changeset
411
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
412 prog = open(filename).read()
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
413 ast = parser.suite(prog)
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
414 code = parser.compileast(ast, filename)
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
415
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
416 return _find_LINENO(code)
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
417
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
418 ### XXX because os.path.commonprefix seems broken by my way of thinking...
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
419 def commonprefix(dirs):
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
420 "Given a list of pathnames, returns the longest common leading component"
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
421 if not dirs: return ''
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
422 n = copy.copy(dirs)
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
423 for i in range(len(n)):
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
424 n[i] = n[i].split(os.sep)
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
425 prefix = n[0]
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
426 for item in n:
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
427 for i in range(len(prefix)):
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
428 if prefix[:i+1] <> item[:i+1]:
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
429 prefix = prefix[:i]
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
430 if i == 0: return ''
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
431 break
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
432 return os.sep.join(prefix)
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14657
diff changeset
433
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
434 class Trace:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
435 def __init__(self, count=1, trace=1, countfuncs=0, ignoremods=(), ignoredirs=(), infile=None, outfile=None):
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
436 """
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
437 @param count true iff it should count number of times each line is executed
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
438 @param trace true iff it should print out each line that is being counted
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
439 @param countfuncs true iff it should just output a list of (filename, modulename, funcname,) for functions that were called at least once; This overrides `count' and `trace'
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
440 @param ignoremods a list of the names of modules to ignore
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
441 @param ignoredirs a list of the names of directories to ignore all of the (recursive) contents of
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
442 @param infile file from which to read stored counts to be added into the results
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
443 @param outfile file in which to write the results
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
444 """
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
445 self.infile = infile
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
446 self.outfile = outfile
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
447 self.ignore = Ignore(ignoremods, ignoredirs)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
448 self.counts = {} # keys are (filename, linenumber)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
449 self.blabbed = {} # for debugging
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
450 self.pathtobasename = {} # for memoizing os.path.basename
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
451 self.donothing = 0
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
452 self.trace = trace
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
453 self._calledfuncs = {}
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
454 if countfuncs:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
455 self.globaltrace = self.globaltrace_countfuncs
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
456 elif trace and count:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
457 self.globaltrace = self.globaltrace_lt
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
458 self.localtrace = self.localtrace_trace_and_count
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
459 elif trace:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
460 self.globaltrace = self.globaltrace_lt
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
461 self.localtrace = self.localtrace_trace
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
462 elif count:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
463 self.globaltrace = self.globaltrace_lt
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
464 self.localtrace = self.localtrace_count
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
465 else:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
466 # Ahem -- do nothing? Okay.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
467 self.donothing = 1
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
468
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
469 def run(self, cmd):
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
470 import __main__
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
471 dict = __main__.__dict__
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
472 if not self.donothing:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
473 sys.settrace(self.globaltrace)
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
474 try:
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
475 exec cmd in dict, dict
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
476 finally:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
477 if not self.donothing:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
478 sys.settrace(None)
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
479
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
480 def runctx(self, cmd, globals=None, locals=None):
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
481 if globals is None: globals = {}
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
482 if locals is None: locals = {}
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
483 if not self.donothing:
24377
ec2c854bd196 * runctx - fix a couple typos
Skip Montanaro <skip@pobox.com>
parents: 21071
diff changeset
484 sys.settrace(self.globaltrace)
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
485 try:
24377
ec2c854bd196 * runctx - fix a couple typos
Skip Montanaro <skip@pobox.com>
parents: 21071
diff changeset
486 exec cmd in globals, locals
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
487 finally:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
488 if not self.donothing:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
489 sys.settrace(None)
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
490
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
491 def runfunc(self, func, *args, **kw):
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
492 result = None
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
493 if not self.donothing:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
494 sys.settrace(self.globaltrace)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
495 try:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
496 result = apply(func, args, kw)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
497 finally:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
498 if not self.donothing:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
499 sys.settrace(None)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
500 return result
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
501
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
502 def globaltrace_countfuncs(self, frame, why, arg):
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
503 """
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
504 Handles `call' events (why == 'call') and adds the (filename, modulename, funcname,) to the self._calledfuncs dict.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
505 """
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
506 if why == 'call':
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
507 (filename, lineno, funcname, context, lineindex,) = inspect.getframeinfo(frame, 0)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
508 if filename:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
509 modulename = inspect.getmodulename(filename)
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
510 else:
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
511 modulename = None
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
512 self._calledfuncs[(filename, modulename, funcname,)] = 1
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
513
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
514 def globaltrace_lt(self, frame, why, arg):
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
515 """
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
516 Handles `call' events (why == 'call') and if the code block being entered is to be ignored then it returns `None', else it returns `self.localtrace'.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
517 """
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
518 if why == 'call':
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
519 (filename, lineno, funcname, context, lineindex,) = inspect.getframeinfo(frame, 0)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
520 # if DEBUG_MODE and not filename:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
521 # print "%s.globaltrace(frame: %s, why: %s, arg: %s): filename: %s, lineno: %s, funcname: %s, context: %s, lineindex: %s\n" % (self, frame, why, arg, filename, lineno, funcname, context, lineindex,)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
522 if filename:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
523 modulename = inspect.getmodulename(filename)
24377
ec2c854bd196 * runctx - fix a couple typos
Skip Montanaro <skip@pobox.com>
parents: 21071
diff changeset
524 if modulename is not None:
ec2c854bd196 * runctx - fix a couple typos
Skip Montanaro <skip@pobox.com>
parents: 21071
diff changeset
525 ignore_it = self.ignore.names(filename, modulename)
ec2c854bd196 * runctx - fix a couple typos
Skip Montanaro <skip@pobox.com>
parents: 21071
diff changeset
526 # if DEBUG_MODE and not self.blabbed.has_key((filename, modulename,)):
ec2c854bd196 * runctx - fix a couple typos
Skip Montanaro <skip@pobox.com>
parents: 21071
diff changeset
527 # self.blabbed[(filename, modulename,)] = None
ec2c854bd196 * runctx - fix a couple typos
Skip Montanaro <skip@pobox.com>
parents: 21071
diff changeset
528 # print "%s.globaltrace(frame: %s, why: %s, arg: %s, filename: %s, modulename: %s, ignore_it: %s\n" % (self, frame, why, arg, filename, modulename, ignore_it,)
ec2c854bd196 * runctx - fix a couple typos
Skip Montanaro <skip@pobox.com>
parents: 21071
diff changeset
529 if not ignore_it:
ec2c854bd196 * runctx - fix a couple typos
Skip Montanaro <skip@pobox.com>
parents: 21071
diff changeset
530 if self.trace:
ec2c854bd196 * runctx - fix a couple typos
Skip Montanaro <skip@pobox.com>
parents: 21071
diff changeset
531 print " --- modulename: %s, funcname: %s" % (modulename, funcname,)
ec2c854bd196 * runctx - fix a couple typos
Skip Montanaro <skip@pobox.com>
parents: 21071
diff changeset
532 # if DEBUG_MODE:
ec2c854bd196 * runctx - fix a couple typos
Skip Montanaro <skip@pobox.com>
parents: 21071
diff changeset
533 # print "%s.globaltrace(frame: %s, why: %s, arg: %s, filename: %s, modulename: %s, ignore_it: %s -- about to localtrace\n" % (self, frame, why, arg, filename, modulename, ignore_it,)
ec2c854bd196 * runctx - fix a couple typos
Skip Montanaro <skip@pobox.com>
parents: 21071
diff changeset
534 return self.localtrace
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
535 else:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
536 # XXX why no filename?
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
537 return None
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
538
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
539 def localtrace_trace_and_count(self, frame, why, arg):
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
540 if why == 'line':
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
541 # record the file name and line number of every trace
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
542 # XXX I wish inspect offered me an optimized `getfilename(frame)' to use in place of the presumably heavier `getframeinfo()'. --Zooko 2001-10-14
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
543 (filename, lineno, funcname, context, lineindex,) = inspect.getframeinfo(frame, 1)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
544 key = (filename, lineno,)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
545 self.counts[key] = self.counts.get(key, 0) + 1
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
546 # XXX not convinced that this memoizing is a performance win -- I don't know enough about Python guts to tell. --Zooko 2001-10-14
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
547 bname = self.pathtobasename.get(filename)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
548 if bname is None:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
549 # Using setdefault faster than two separate lines? --Zooko 2001-10-14
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
550 bname = self.pathtobasename.setdefault(filename, os.path.basename(filename))
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
551 try:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
552 print "%s(%d): %s" % (bname, lineno, context[lineindex],),
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
553 except IndexError:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
554 # Uh.. sometimes getframeinfo gives me a context of length 1 and a lineindex of -2. Oh well.
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
555 pass
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
556 return self.localtrace
17703
f3b8785bd022 Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15930
diff changeset
557
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
558 def localtrace_trace(self, frame, why, arg):
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
559 if why == 'line':
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
560 # XXX shouldn't do the count increment when arg is exception? But be careful to return self.localtrace when arg is exception! ? --Zooko 2001-10-14
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
561 # record the file name and line number of every trace
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
562 # XXX I wish inspect offered me an optimized `getfilename(frame)' to use in place of the presumably heavier `getframeinfo()'. --Zooko 2001-10-14
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
563 (filename, lineno, funcname, context, lineindex,) = inspect.getframeinfo(frame)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
564 # if DEBUG_MODE:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
565 # print "%s.localtrace_trace(frame: %s, why: %s, arg: %s); filename: %s, lineno: %s, funcname: %s, context: %s, lineindex: %s\n" % (self, frame, why, arg, filename, lineno, funcname, context, lineindex,)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
566 # XXX not convinced that this memoizing is a performance win -- I don't know enough about Python guts to tell. --Zooko 2001-10-14
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
567 bname = self.pathtobasename.get(filename)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
568 if bname is None:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
569 # Using setdefault faster than two separate lines? --Zooko 2001-10-14
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
570 bname = self.pathtobasename.setdefault(filename, os.path.basename(filename))
24377
ec2c854bd196 * runctx - fix a couple typos
Skip Montanaro <skip@pobox.com>
parents: 21071
diff changeset
571 if context is not None:
ec2c854bd196 * runctx - fix a couple typos
Skip Montanaro <skip@pobox.com>
parents: 21071
diff changeset
572 try:
ec2c854bd196 * runctx - fix a couple typos
Skip Montanaro <skip@pobox.com>
parents: 21071
diff changeset
573 print "%s(%d): %s" % (bname, lineno, context[lineindex],),
ec2c854bd196 * runctx - fix a couple typos
Skip Montanaro <skip@pobox.com>
parents: 21071
diff changeset
574 except IndexError:
ec2c854bd196 * runctx - fix a couple typos
Skip Montanaro <skip@pobox.com>
parents: 21071
diff changeset
575 # Uh.. sometimes getframeinfo gives me a context of length 1 and a lineindex of -2. Oh well.
ec2c854bd196 * runctx - fix a couple typos
Skip Montanaro <skip@pobox.com>
parents: 21071
diff changeset
576 pass
ec2c854bd196 * runctx - fix a couple typos
Skip Montanaro <skip@pobox.com>
parents: 21071
diff changeset
577 else:
ec2c854bd196 * runctx - fix a couple typos
Skip Montanaro <skip@pobox.com>
parents: 21071
diff changeset
578 print "%s(???): ???" % bname
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
579 return self.localtrace
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
580
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
581 def localtrace_count(self, frame, why, arg):
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
582 if why == 'line':
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
583 # XXX shouldn't do the count increment when arg is exception? But be careful to return self.localtrace when arg is exception! ? --Zooko 2001-10-14
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
584 # record the file name and line number of every trace
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
585 # XXX I wish inspect offered me an optimized `getfilename(frame)' to use in place of the presumably heavier `getframeinfo()'. --Zooko 2001-10-14
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
586 (filename, lineno, funcname, context, lineindex,) = inspect.getframeinfo(frame)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
587 key = (filename, lineno,)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
588 self.counts[key] = self.counts.get(key, 0) + 1
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
589 return self.localtrace
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
590
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
591 def results(self):
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
592 return CoverageResults(self.counts, infile=self.infile, outfile=self.outfile, calledfuncs=self._calledfuncs)
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
593
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
594 def _err_exit(msg):
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
595 sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
596 sys.exit(1)
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
597
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
598 def main(argv=None):
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
599 import getopt
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
600
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
601 if argv is None:
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
602 argv = sys.argv
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
603 try:
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
604 opts, prog_argv = getopt.getopt(argv[1:], "tcrRf:d:msC:l",
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
605 ["help", "version", "trace", "count",
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
606 "report", "no-report",
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
607 "file=", "missing",
17703
f3b8785bd022 Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15930
diff changeset
608 "ignore-module=", "ignore-dir=",
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
609 "coverdir=", "listfuncs",])
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
610
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
611 except getopt.error, msg:
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
612 sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
613 sys.stderr.write("Try `%s --help' for more information\n" % sys.argv[0])
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
614 sys.exit(1)
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
615
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
616 trace = 0
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
617 count = 0
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
618 report = 0
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
619 no_report = 0
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
620 counts_file = None
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
621 missing = 0
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
622 ignore_modules = []
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
623 ignore_dirs = []
17703
f3b8785bd022 Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15930
diff changeset
624 coverdir = None
f3b8785bd022 Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15930
diff changeset
625 summary = 0
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
626 listfuncs = false
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
627
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
628 for opt, val in opts:
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
629 if opt == "--help":
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
630 usage(sys.stdout)
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
631 sys.exit(0)
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
632
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
633 if opt == "--version":
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
634 sys.stdout.write("trace 2.0\n")
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
635 sys.exit(0)
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
636
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
637 if opt == "-l" or opt == "--listfuncs":
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
638 listfuncs = true
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
639 continue
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
640
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
641 if opt == "-t" or opt == "--trace":
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
642 trace = 1
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
643 continue
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
644
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
645 if opt == "-c" or opt == "--count":
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
646 count = 1
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
647 continue
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
648
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
649 if opt == "-r" or opt == "--report":
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
650 report = 1
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
651 continue
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
652
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
653 if opt == "-R" or opt == "--no-report":
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
654 no_report = 1
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
655 continue
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
656
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
657 if opt == "-f" or opt == "--file":
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
658 counts_file = val
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
659 continue
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
660
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
661 if opt == "-m" or opt == "--missing":
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
662 missing = 1
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
663 continue
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
664
17703
f3b8785bd022 Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15930
diff changeset
665 if opt == "-C" or opt == "--coverdir":
f3b8785bd022 Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15930
diff changeset
666 coverdir = val
f3b8785bd022 Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15930
diff changeset
667 continue
f3b8785bd022 Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15930
diff changeset
668
f3b8785bd022 Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15930
diff changeset
669 if opt == "-s" or opt == "--summary":
f3b8785bd022 Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15930
diff changeset
670 summary = 1
f3b8785bd022 Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15930
diff changeset
671 continue
f3b8785bd022 Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15930
diff changeset
672
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
673 if opt == "--ignore-module":
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
674 ignore_modules.append(val)
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
675 continue
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
676
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
677 if opt == "--ignore-dir":
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
678 for s in string.split(val, os.pathsep):
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
679 s = os.path.expandvars(s)
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
680 # should I also call expanduser? (after all, could use $HOME)
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
681
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
682 s = string.replace(s, "$prefix",
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
683 os.path.join(sys.prefix, "lib",
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
684 "python" + sys.version[:3]))
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
685 s = string.replace(s, "$exec_prefix",
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
686 os.path.join(sys.exec_prefix, "lib",
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
687 "python" + sys.version[:3]))
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
688 s = os.path.normpath(s)
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
689 ignore_dirs.append(s)
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
690 continue
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
691
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
692 assert 0, "Should never get here"
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
693
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
694 if listfuncs and (count or trace):
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
695 _err_exit("cannot specify both --listfuncs and (--trace or --count)")
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
696
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
697 if not count and not trace and not report and not listfuncs:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
698 _err_exit("must specify one of --trace, --count, --report or --listfuncs")
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
699
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
700 if report and no_report:
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
701 _err_exit("cannot specify both --report and --no-report")
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
702
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
703 if report and not counts_file:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
704 _err_exit("--report requires a --file")
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
705
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
706 if no_report and len(prog_argv) == 0:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
707 _err_exit("missing name of file to run")
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
708
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
709 # everything is ready
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
710 if report:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
711 results = CoverageResults(infile=counts_file, outfile=counts_file)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
712 results.write_results(missing, summary=summary, coverdir=coverdir)
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
713 else:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
714 sys.argv = prog_argv
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
715 progname = prog_argv[0]
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
716 if eval(sys.version[:3])>1.3:
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
717 sys.path[0] = os.path.split(progname)[0] # ???
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
718
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
719 t = Trace(count, trace, countfuncs=listfuncs, ignoremods=ignore_modules, ignoredirs=ignore_dirs, infile=counts_file, outfile=counts_file)
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
720 try:
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
721 t.run('execfile(' + `progname` + ')')
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
722 except IOError, err:
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
723 _err_exit("Cannot run file %s because: %s" % (`sys.argv[0]`, err))
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
724 except SystemExit:
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
725 pass
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
726
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
727 results = t.results()
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
728
14657
946709e82be0 satisfy the tabnanny
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13838
diff changeset
729 if not no_report:
21071
6392f03222e0 Checking in Zooko's version per SF patch #476866, plus my changes to
Guido van Rossum <guido@python.org>
parents: 20322
diff changeset
730 results.write_results(missing, summary=summary, coverdir=coverdir)
13838
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
731
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
732 if __name__=='__main__':
91d95a7b7858 Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
733 main()