1 | \section{\module{trace} ---
|
---|
2 | Trace or track Python statement execution}
|
---|
3 |
|
---|
4 | \declaremodule{standard}{trace}
|
---|
5 | \modulesynopsis{Trace or track Python statement execution.}
|
---|
6 |
|
---|
7 | The \module{trace} module allows you to trace program execution, generate
|
---|
8 | annotated statement coverage listings, print caller/callee relationships and
|
---|
9 | list functions executed during a program run. It can be used in another
|
---|
10 | program or from the command line.
|
---|
11 |
|
---|
12 | \subsection{Command Line Usage\label{trace-cli}}
|
---|
13 |
|
---|
14 | The \module{trace} module can be invoked from the command line. It can be
|
---|
15 | as simple as
|
---|
16 |
|
---|
17 | \begin{verbatim}
|
---|
18 | python -m trace --count somefile.py ...
|
---|
19 | \end{verbatim}
|
---|
20 |
|
---|
21 | The above will generate annotated listings of all Python modules imported
|
---|
22 | during the execution of \file{somefile.py}.
|
---|
23 |
|
---|
24 | The following command-line arguments are supported:
|
---|
25 |
|
---|
26 | \begin{description}
|
---|
27 | \item[\longprogramopt{trace}, \programopt{-t}]
|
---|
28 | Display lines as they are executed.
|
---|
29 |
|
---|
30 | \item[\longprogramopt{count}, \programopt{-c}]
|
---|
31 | Produce a set of annotated listing files upon program
|
---|
32 | completion that shows how many times each statement was executed.
|
---|
33 |
|
---|
34 | \item[\longprogramopt{report}, \programopt{-r}]
|
---|
35 | Produce an annotated list from an earlier program run that
|
---|
36 | used the \longprogramopt{count} and \longprogramopt{file} arguments.
|
---|
37 |
|
---|
38 | \item[\longprogramopt{no-report}, \programopt{-R}]
|
---|
39 | Do not generate annotated listings. This is useful if you intend to make
|
---|
40 | several runs with \longprogramopt{count} then produce a single set
|
---|
41 | of annotated listings at the end.
|
---|
42 |
|
---|
43 | \item[\longprogramopt{listfuncs}, \programopt{-l}]
|
---|
44 | List the functions executed by running the program.
|
---|
45 |
|
---|
46 | \item[\longprogramopt{trackcalls}, \programopt{-T}]
|
---|
47 | Generate calling relationships exposed by running the program.
|
---|
48 |
|
---|
49 | \item[\longprogramopt{file}, \programopt{-f}]
|
---|
50 | Name a file containing (or to contain) counts.
|
---|
51 |
|
---|
52 | \item[\longprogramopt{coverdir}, \programopt{-C}]
|
---|
53 | Name a directory in which to save annotated listing files.
|
---|
54 |
|
---|
55 | \item[\longprogramopt{missing}, \programopt{-m}]
|
---|
56 | When generating annotated listings, mark lines which
|
---|
57 | were not executed with `\code{>>>>>>}'.
|
---|
58 |
|
---|
59 | \item[\longprogramopt{summary}, \programopt{-s}]
|
---|
60 | When using \longprogramopt{count} or \longprogramopt{report}, write a
|
---|
61 | brief summary to stdout for each file processed.
|
---|
62 |
|
---|
63 | \item[\longprogramopt{ignore-module}]
|
---|
64 | Ignore the named module and its submodules (if it is
|
---|
65 | a package). May be given multiple times.
|
---|
66 |
|
---|
67 | \item[\longprogramopt{ignore-dir}]
|
---|
68 | Ignore all modules and packages in the named directory
|
---|
69 | and subdirectories. May be given multiple times.
|
---|
70 | \end{description}
|
---|
71 |
|
---|
72 | \subsection{Programming Interface\label{trace-api}}
|
---|
73 |
|
---|
74 | \begin{classdesc}{Trace}{\optional{count=1\optional{, trace=1\optional{,
|
---|
75 | countfuncs=0\optional{, countcallers=0\optional{,
|
---|
76 | ignoremods=()\optional{, ignoredirs=()\optional{,
|
---|
77 | infile=None\optional{, outfile=None}}}}}}}}}
|
---|
78 | Create an object to trace execution of a single statement or expression.
|
---|
79 | All parameters are optional. \var{count} enables counting of line numbers.
|
---|
80 | \var{trace} enables line execution tracing. \var{countfuncs} enables
|
---|
81 | listing of the functions called during the run. \var{countcallers} enables
|
---|
82 | call relationship tracking. \var{ignoremods} is a list of modules or
|
---|
83 | packages to ignore. \var{ignoredirs} is a list of directories whose modules
|
---|
84 | or packages should be ignored. \var{infile} is the file from which to read
|
---|
85 | stored count information. \var{outfile} is a file in which to write updated
|
---|
86 | count information.
|
---|
87 | \end{classdesc}
|
---|
88 |
|
---|
89 | \begin{methoddesc}[Trace]{run}{cmd}
|
---|
90 | Run \var{cmd} under control of the Trace object with the current tracing
|
---|
91 | parameters.
|
---|
92 | \end{methoddesc}
|
---|
93 |
|
---|
94 | \begin{methoddesc}[Trace]{runctx}{cmd\optional{, globals=None\optional{,
|
---|
95 | locals=None}}}
|
---|
96 | Run \var{cmd} under control of the Trace object with the current tracing
|
---|
97 | parameters in the defined global and local environments. If not defined,
|
---|
98 | \var{globals} and \var{locals} default to empty dictionaries.
|
---|
99 | \end{methoddesc}
|
---|
100 |
|
---|
101 | \begin{methoddesc}[Trace]{runfunc}{func, *args, **kwds}
|
---|
102 | Call \var{func} with the given arguments under control of the
|
---|
103 | \class{Trace} object with the current tracing parameters.
|
---|
104 | \end{methoddesc}
|
---|
105 |
|
---|
106 | This is a simple example showing the use of this module:
|
---|
107 |
|
---|
108 | \begin{verbatim}
|
---|
109 | import sys
|
---|
110 | import trace
|
---|
111 |
|
---|
112 | # create a Trace object, telling it what to ignore, and whether to
|
---|
113 | # do tracing or line-counting or both.
|
---|
114 | tracer = trace.Trace(
|
---|
115 | ignoredirs=[sys.prefix, sys.exec_prefix],
|
---|
116 | trace=0,
|
---|
117 | count=1)
|
---|
118 |
|
---|
119 | # run the new command using the given tracer
|
---|
120 | tracer.run('main()')
|
---|
121 |
|
---|
122 | # make a report, placing output in /tmp
|
---|
123 | r = tracer.results()
|
---|
124 | r.write_results(show_missing=True, coverdir="/tmp")
|
---|
125 | \end{verbatim}
|
---|