annotate Doc/lib/libtraceback.tex @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents dbdada966858
children cf5928fab108
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8918
acfffe14628e New section header style.
Fred Drake <fdrake@acm.org>
parents: 8716
diff changeset
1 \section{\module{traceback} ---
11078
d1b48ee14601 Improve description of the example.
Fred Drake <fdrake@acm.org>
parents: 10313
diff changeset
2 Print or retrieve a stack traceback}
d1b48ee14601 Improve description of the example.
Fred Drake <fdrake@acm.org>
parents: 10313
diff changeset
3
8716
760dcf197048 Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents: 8519
diff changeset
4 \declaremodule{standard}{traceback}
760dcf197048 Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents: 8519
diff changeset
5 \modulesynopsis{Print or retrieve a stack traceback.}
760dcf197048 Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents: 8519
diff changeset
6
2460
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
8518
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
8 This module provides a standard interface to extract, format and print
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
9 stack traces of Python programs. It exactly mimics the behavior of
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
10 the Python interpreter when it prints a stack trace. This is useful
24385
dbdada966858 Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
11 when you want to print stack traces under program control, such as in a
2460
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 ``wrapper'' around the interpreter.
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
24385
dbdada966858 Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
14 The module uses traceback objects --- this is the object type that is
dbdada966858 Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
15 stored in the variables \code{sys.exc_traceback} (deprecated) and
8518
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
16 \code{sys.last_traceback} and returned as the third item from
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
17 \function{sys.exc_info()}.
7604
a63d553eb658 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
18 \obindex{traceback}
2460
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 The module defines the following functions:
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
8518
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
22 \begin{funcdesc}{print_tb}{traceback\optional{, limit\optional{, file}}}
2460
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 Print up to \var{limit} stack trace entries from \var{traceback}. If
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 \var{limit} is omitted or \code{None}, all entries are printed.
8518
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
25 If \var{file} is omitted or \code{None}, the output goes to
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
26 \code{sys.stderr}; otherwise it should be an open file or file-like
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
27 object to receive the output.
2460
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 \end{funcdesc}
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
10313
fa18b0fc0c18 extract_tb(): Only describe this function once. Problem reported by
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
30 \begin{funcdesc}{print_exception}{type, value, traceback\optional{,
fa18b0fc0c18 extract_tb(): Only describe this function once. Problem reported by
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
31 limit\optional{, file}}}
2460
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 Print exception information and up to \var{limit} stack trace entries
8518
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
33 from \var{traceback} to \var{file}.
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
34 This differs from \function{print_tb()} in the
2460
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 following ways: (1) if \var{traceback} is not \code{None}, it prints a
16590
f813e987e65c Reflect change in traceback format:
Fred Drake <fdrake@acm.org>
parents: 13625
diff changeset
36 header \samp{Traceback (most recent call last):}; (2) it prints the
2460
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 exception \var{type} and \var{value} after the stack trace; (3) if
24385
dbdada966858 Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
38 \var{type} is \exception{SyntaxError} and \var{value} has the
dbdada966858 Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
39 appropriate format, it prints the line where the syntax error occurred
dbdada966858 Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
40 with a caret indicating the approximate position of the error.
2460
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 \end{funcdesc}
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
8518
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
43 \begin{funcdesc}{print_exc}{\optional{limit\optional{, file}}}
24385
dbdada966858 Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
44 This is a shorthand for \code{print_exception(sys.exc_type,
dbdada966858 Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
45 sys.exc_value, sys.exc_traceback, \var{limit}, \var{file})}. (In
dbdada966858 Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
46 fact, it uses \function{sys.exc_info()} to retrieve the same
dbdada966858 Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
47 information in a thread-safe way instead of using the deprecated
dbdada966858 Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
48 variables.)
8518
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
49 \end{funcdesc}
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
50
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
51 \begin{funcdesc}{print_last}{\optional{limit\optional{, file}}}
24385
dbdada966858 Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
52 This is a shorthand for \code{print_exception(sys.last_type,
dbdada966858 Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
53 sys.last_value, sys.last_traceback, \var{limit}, \var{file})}.
8518
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
54 \end{funcdesc}
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
55
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
56 \begin{funcdesc}{print_stack}{\optional{f\optional{, limit\optional{, file}}}}
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
57 This function prints a stack trace from its invocation point. The
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
58 optional \var{f} argument can be used to specify an alternate stack
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
59 frame to start. The optional \var{limit} and \var{file} arguments have the
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
60 same meaning as for \function{print_exception()}.
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
61 \end{funcdesc}
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
62
10313
fa18b0fc0c18 extract_tb(): Only describe this function once. Problem reported by
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
63 \begin{funcdesc}{extract_tb}{traceback\optional{, limit}}
fa18b0fc0c18 extract_tb(): Only describe this function once. Problem reported by
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
64 Return a list of up to \var{limit} ``pre-processed'' stack trace
fa18b0fc0c18 extract_tb(): Only describe this function once. Problem reported by
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
65 entries extracted from the traceback object \var{traceback}. It is
fa18b0fc0c18 extract_tb(): Only describe this function once. Problem reported by
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
66 useful for alternate formatting of stack traces. If \var{limit} is
fa18b0fc0c18 extract_tb(): Only describe this function once. Problem reported by
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
67 omitted or \code{None}, all entries are extracted. A
fa18b0fc0c18 extract_tb(): Only describe this function once. Problem reported by
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
68 ``pre-processed'' stack trace entry is a quadruple (\var{filename},
fa18b0fc0c18 extract_tb(): Only describe this function once. Problem reported by
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
69 \var{line number}, \var{function name}, \var{text}) representing
fa18b0fc0c18 extract_tb(): Only describe this function once. Problem reported by
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
70 the information that is usually printed for a stack trace. The
fa18b0fc0c18 extract_tb(): Only describe this function once. Problem reported by
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
71 \var{text} is a string with leading and trailing whitespace
fa18b0fc0c18 extract_tb(): Only describe this function once. Problem reported by
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
72 stripped; if the source is not available it is \code{None}.
8518
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
73 \end{funcdesc}
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
74
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
75 \begin{funcdesc}{extract_stack}{\optional{f\optional{, limit}}}
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
76 Extract the raw traceback from the current stack frame. The return
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
77 value has the same format as for \function{extract_tb()}. The
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
78 optional \var{f} and \var{limit} arguments have the same meaning as
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
79 for \function{print_stack()}.
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
80 \end{funcdesc}
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
81
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
82 \begin{funcdesc}{format_list}{list}
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
83 Given a list of tuples as returned by \function{extract_tb()} or
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
84 \function{extract_stack()}, return a list of strings ready for
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
85 printing. Each string in the resulting list corresponds to the item
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
86 with the same index in the argument list. Each string ends in a
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
87 newline; the strings may contain internal newlines as well, for those
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
88 items whose source text line is not \code{None}.
2460
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 \end{funcdesc}
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
8518
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
91 \begin{funcdesc}{format_exception_only}{type, value}
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
92 Format the exception part of a traceback. The arguments are the
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
93 exception type and value such as given by \code{sys.last_type} and
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
94 \code{sys.last_value}. The return value is a list of strings, each
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
95 ending in a newline. Normally, the list contains a single string;
24385
dbdada966858 Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
96 however, for \exception{SyntaxError} exceptions, it contains several
dbdada966858 Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
97 lines that (when printed) display detailed information about where the
8518
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
98 syntax error occurred. The message indicating which exception
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
99 occurred is the always last string in the list.
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
100 \end{funcdesc}
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
101
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
102 \begin{funcdesc}{format_exception}{type, value, tb\optional{, limit}}
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
103 Format a stack trace and the exception information. The arguments
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
104 have the same meaning as the corresponding arguments to
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
105 \function{print_exception()}. The return value is a list of strings,
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
106 each ending in a newline and some containing internal newlines. When
13625
fdfd1049f36e Rob W. W. Hooft's spelling fixes for the Library Reference. I hope
Thomas Wouters <thomas@python.org>
parents: 11078
diff changeset
107 these lines are concatenated and printed, exactly the same text is
8518
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
108 printed as does \function{print_exception()}.
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
109 \end{funcdesc}
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
110
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
111 \begin{funcdesc}{format_tb}{tb\optional{, limit}}
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
112 A shorthand for \code{format_list(extract_tb(\var{tb}, \var{limit}))}.
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
113 \end{funcdesc}
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
114
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
115 \begin{funcdesc}{format_stack}{\optional{f\optional{, limit}}}
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
116 A shorthand for \code{format_list(extract_stack(\var{f}, \var{limit}))}.
2460
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 \end{funcdesc}
8518
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
118
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
119 \begin{funcdesc}{tb_lineno}{tb}
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
120 This function returns the current line number set in the traceback
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
121 object. This is normally the same as the \code{\var{tb}.tb_lineno}
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
122 field of the object, but when optimization is used (the -O flag) this
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
123 field is not updated correctly; this function calculates the correct
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
124 value.
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
125 \end{funcdesc}
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
126
11078
d1b48ee14601 Improve description of the example.
Fred Drake <fdrake@acm.org>
parents: 10313
diff changeset
127
d1b48ee14601 Improve description of the example.
Fred Drake <fdrake@acm.org>
parents: 10313
diff changeset
128 \subsection{Traceback Example \label{traceback-example}}
d1b48ee14601 Improve description of the example.
Fred Drake <fdrake@acm.org>
parents: 10313
diff changeset
129
d1b48ee14601 Improve description of the example.
Fred Drake <fdrake@acm.org>
parents: 10313
diff changeset
130 This simple example implements a basic read-eval-print loop, similar
d1b48ee14601 Improve description of the example.
Fred Drake <fdrake@acm.org>
parents: 10313
diff changeset
131 to (but less useful than) the standard Python interactive interpreter
d1b48ee14601 Improve description of the example.
Fred Drake <fdrake@acm.org>
parents: 10313
diff changeset
132 loop. For a more complete implementation of the interpreter loop,
d1b48ee14601 Improve description of the example.
Fred Drake <fdrake@acm.org>
parents: 10313
diff changeset
133 refer to the \refmodule{code} module.
8518
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
134
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
135 \begin{verbatim}
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
136 import sys, traceback
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
137
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
138 def run_user_code(envdir):
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
139 source = raw_input(">>> ")
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
140 try:
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
141 exec source in envdir
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
142 except:
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
143 print "Exception in user code:"
8519
61a9c529455a # Untabified the example :-(
Guido van Rossum <guido@python.org>
parents: 8518
diff changeset
144 print '-'*60
8518
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
145 traceback.print_exc(file=sys.stdout)
8519
61a9c529455a # Untabified the example :-(
Guido van Rossum <guido@python.org>
parents: 8518
diff changeset
146 print '-'*60
8518
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
147
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
148 envdir = {}
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
149 while 1:
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
150 run_user_code(envdir)
b56981c0d61e Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
151 \end{verbatim}