/Doc/library/trace.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 131 lines · 87 code · 44 blank · 0 comment · 0 complexity · 7739ed44aa45a49db28655233a0f19ca MD5 · raw file

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