Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d00862a

Browse files
committed
Improve trace documentation (#9264). Patch by Eli Bendersky.
1 parent b8eb9cb commit d00862a

1 file changed

Lines changed: 119 additions & 50 deletions

File tree

Doc/library/trace.rst

Lines changed: 119 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,71 +17,119 @@ or from the command line.
1717

1818
.. _trace-cli:
1919

20-
Command Line Usage
20+
Command-Line Usage
2121
------------------
2222

2323
The :mod:`trace` module can be invoked from the command line. It can be as
2424
simple as ::
2525

26-
python -m trace --count somefile.py ...
26+
python -m trace --count -C . somefile.py ...
2727

28-
The above will generate annotated listings of all Python modules imported during
29-
the execution of :file:`somefile.py`.
28+
The above will execute :file:`somefile.py` and generate annotated listings of all
29+
Python modules imported during the execution into the current directory.
3030

31-
The following command-line arguments are supported:
31+
.. program:: trace
32+
33+
.. cmdoption:: --help
34+
35+
Display usage and exit.
36+
37+
.. cmdoption:: --version
38+
39+
Display the version of the module and exit.
40+
41+
Main options
42+
^^^^^^^^^^^^
43+
44+
At least one of the following options must be specified when invoking :mod:`trace`.
45+
The :option:`--listfuncs <-l>` option is mutually exclusive with the
46+
:option:`--trace <-t>` and :option:`--counts <-c>` options . When :option:`--listfuncs <-l>`
47+
is provided, neither :option:`--counts <-c>` nor :option:`--trace <-t>` are accepted,
48+
and vice versa.
49+
50+
.. program:: trace
51+
52+
.. cmdoption:: -c, --count
53+
54+
Produce a set of annotated listing files upon program completion that shows
55+
how many times each statement was executed.
56+
See also :option:`--coverdir <-C>`, :option:`--file <-f>`,
57+
:option:`--no-report <-R>` below.
58+
59+
.. cmdoption:: -t, --trace
3260

33-
:option:`--trace`, :option:`-t`
3461
Display lines as they are executed.
3562

36-
:option:`--count`, :option:`-c`
37-
Produce a set of annotated listing files upon program completion that shows how
38-
many times each statement was executed.
63+
.. cmdoption:: -l, --listfuncs
64+
65+
Display the functions executed by running the program.
66+
67+
.. cmdoption:: -r, --report
3968

40-
:option:`--report`, :option:`-r`
4169
Produce an annotated list from an earlier program run that used the
42-
:option:`--count` and :option:`--file` arguments.
70+
:option:`--count <-c>` and :option:`--file <-f>` option. Do not execute any code.
4371

44-
:option:`--no-report`, :option:`-R`
45-
Do not generate annotated listings. This is useful if you intend to make
46-
several runs with :option:`--count` then produce a single set of annotated
47-
listings at the end.
72+
.. cmdoption:: -T, --trackcalls
73+
74+
Display the calling relationships exposed by running the program.
75+
76+
Modifiers
77+
^^^^^^^^^
4878

49-
:option:`--listfuncs`, :option:`-l`
50-
List the functions executed by running the program.
79+
.. program:: trace
5180

52-
:option:`--trackcalls`, :option:`-T`
53-
Generate calling relationships exposed by running the program.
81+
.. cmdoption:: -f, --file=<file>
5482

55-
:option:`--file`, :option:`-f`
56-
Name a file containing (or to contain) counts.
83+
Name of a file to accumulate counts over several tracing runs. Should be used
84+
with the :option:`--count <-c>` option.
5785

58-
:option:`--coverdir`, :option:`-C`
59-
Name a directory in which to save annotated listing files.
86+
.. cmdoption:: -C, --coverdir=<dir>
87+
88+
Directory where the report files go. The coverage report for
89+
``package.module`` is written to file :file:`{dir}/{package}/{module}.cover`.
90+
91+
.. cmdoption:: -m, --missing
6092

61-
:option:`--missing`, :option:`-m`
6293
When generating annotated listings, mark lines which were not executed with
63-
'``>>>>>>``'.
94+
``>>>>>>``.
6495

65-
:option:`--summary`, :option:`-s`
66-
When using :option:`--count` or :option:`--report`, write a brief summary to
67-
stdout for each file processed.
96+
.. cmdoption:: -s, --summary
6897

69-
:option:`--ignore-module`
70-
Accepts comma separated list of module names. Ignore each of the named
71-
module and its submodules (if it is a package). May be given
72-
multiple times.
98+
When using :option:`--count <-c>` or :option:`--report <-r>`, write a brief
99+
summary to stdout for each file processed.
73100

74-
:option:`--ignore-dir`
75-
Ignore all modules and packages in the named directory and subdirectories
76-
(multiple directories can be joined by os.pathsep). May be given multiple
77-
times.
101+
.. cmdoption:: -R, --no-report
78102

103+
Do not generate annotated listings. This is useful if you intend to make
104+
several runs with :option:`--count <-c>`, and then produce a single set of
105+
annotated listings at the end.
79106

80-
.. _trace-api:
107+
.. cmdoption:: -g, --timing
108+
109+
Prefix each line with the time since the program started. Only used while
110+
tracing.
81111

82-
Programming Interface
83-
---------------------
112+
Filters
113+
^^^^^^^
84114

115+
These options may be repeated multiple times.
116+
117+
.. program:: trace
118+
119+
.. cmdoption:: --ignore-module=<mod>
120+
121+
Ignore each of the given module names and its submodules (if it is a package).
122+
The argument can be a list of names separated by a comma.
123+
124+
.. cmdoption:: --ignore-dir=<dir>
125+
126+
Ignore all modules and packages in the named directory and subdirectories.
127+
The argument can be a list of directories separated by :data:`os.pathsep`.
128+
129+
.. _trace-api:
130+
131+
Programmatic Interface
132+
----------------------
85133

86134
.. class:: Trace(count=1, trace=1, countfuncs=0, countcallers=0, ignoremods=(), ignoredirs=(), infile=None, outfile=None, timing=False)
87135

@@ -91,29 +139,50 @@ Programming Interface
91139
called during the run. *countcallers* enables call relationship tracking.
92140
*ignoremods* is a list of modules or packages to ignore. *ignoredirs* is a list
93141
of directories whose modules or packages should be ignored. *infile* is the
94-
file from which to read stored count information. *outfile* is a file in which
95-
to write updated count information. *timing* enables a timestamp relative
96-
to when tracing was started to be displayed.
97-
142+
name of the file from which to read stored count information. *outfile* is
143+
the name of the file in which to write updated count information. *timing*
144+
enables a timestamp relative to when tracing was started to be displayed.
98145

99146
.. method:: Trace.run(cmd)
100147

101-
Run *cmd* under control of the Trace object with the current tracing parameters.
102-
148+
Execute the command and gather statistics from the execution with
149+
the current tracing parameters.
150+
*cmd* must be a string or code object, suitable for passing into :func:`exec`.
103151

104152
.. method:: Trace.runctx(cmd, globals=None, locals=None)
105153

106-
Run *cmd* under control of the Trace object with the current tracing parameters
107-
in the defined global and local environments. If not defined, *globals* and
108-
*locals* default to empty dictionaries.
109-
154+
Execute the command and gather statistics from the execution with
155+
the current tracing parameters, in the defined global and local environments.
156+
If not defined, *globals* and *locals* default to empty dictionaries.
110157

111158
.. method:: Trace.runfunc(func, *args, **kwds)
112159

113160
Call *func* with the given arguments under control of the :class:`Trace` object
114161
with the current tracing parameters.
115162

116-
This is a simple example showing the use of this module::
163+
.. method:: Trace.results()
164+
165+
Return a :class:`CoverageResults` object that contains the cumulative results
166+
of all previous calls to ``run``, ``runctx`` and ``runfunc`` for the given
167+
:class:`Trace` instance. Does not reset the accumulated trace results.
168+
169+
.. class:: CoverageResults
170+
171+
A container for coverage results, created by :meth:`Trace.results`. Should not
172+
be created directly by the user.
173+
174+
.. method:: CoverageResults.update(other)
175+
176+
Merge in data from another :class:`CoverageResults` object.
177+
178+
.. method:: CoverageResults.write_results(show_missing=True, summary=False, coverdir=None)
179+
180+
Write coverage results. Set *show_missing* to show lines that had no hits.
181+
Set *summary* to include in the output the coverage summary per module. *coverdir*
182+
specifies the directory into which the coverage result files will be output.
183+
If ``None``, the results for each source file are placed in its directory.
184+
185+
A simple example demonstrating the use of the programmatic interface::
117186

118187
import sys
119188
import trace

0 commit comments

Comments
 (0)