annotate Doc/lib/libtraceback.tex @ 37850:ebe59cd18273

Use explicit relative import for an, ehm, relative import.
author Thomas Wouters <thomas@python.org>
date Tue, 18 Apr 2006 21:41:36 +0000
parents 7ada16a14968
children c94f37054f19
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
24384
6673171fc37e 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
24384
6673171fc37e 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
37042
7ada16a14968 Remove sys.exc_type, sys.exc_value, sys.exc_traceback
Neal Norwitz <nnorwitz@gmail.com>
parents: 36043
diff changeset
15 stored in the \code{sys.last_traceback} variable and returned
7ada16a14968 Remove sys.exc_type, sys.exc_value, sys.exc_traceback
Neal Norwitz <nnorwitz@gmail.com>
parents: 36043
diff changeset
16 as the third item from \function{sys.exc_info()}.
7604
a63d553eb658 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
17 \obindex{traceback}
2460
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 The module defines the following functions:
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
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
21 \begin{funcdesc}{print_tb}{traceback\optional{, limit\optional{, file}}}
2460
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 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
23 \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
24 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
25 \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
26 object to receive the output.
2460
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 \end{funcdesc}
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
10313
fa18b0fc0c18 extract_tb(): Only describe this function once. Problem reported by
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
29 \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
30 limit\optional{, file}}}
2460
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 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
32 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
33 This differs from \function{print_tb()} in the
2460
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 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
35 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
36 exception \var{type} and \var{value} after the stack trace; (3) if
24384
6673171fc37e Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
37 \var{type} is \exception{SyntaxError} and \var{value} has the
6673171fc37e Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
38 appropriate format, it prints the line where the syntax error occurred
6673171fc37e Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
39 with a caret indicating the approximate position of the error.
2460
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 \end{funcdesc}
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
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
42 \begin{funcdesc}{print_exc}{\optional{limit\optional{, file}}}
37042
7ada16a14968 Remove sys.exc_type, sys.exc_value, sys.exc_traceback
Neal Norwitz <nnorwitz@gmail.com>
parents: 36043
diff changeset
43 This is a shorthand for \code{print_exception(*\function{sys.exc_info()}}.
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
44 \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
45
36043
98653d9f9cc3 Patch #1376914: traceback.format_exc() has no "file" argument
Georg Brandl <georg@python.org>
parents: 31183
diff changeset
46 \begin{funcdesc}{format_exc}{\optional{limit}}
30862
d45f8981fdea Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 27539
diff changeset
47 This is like \code{print_exc(\var{limit})} but returns a string
d45f8981fdea Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 27539
diff changeset
48 instead of printing to a file.
31183
10c4cc352d13 Add a versionadded tag for traceback.format_exc()
Neal Norwitz <nnorwitz@gmail.com>
parents: 30862
diff changeset
49 \versionadded{2.4}
30862
d45f8981fdea Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 27539
diff changeset
50 \end{funcdesc}
d45f8981fdea Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 27539
diff changeset
51
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
52 \begin{funcdesc}{print_last}{\optional{limit\optional{, file}}}
24384
6673171fc37e Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
53 This is a shorthand for \code{print_exception(sys.last_type,
6673171fc37e Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
54 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
55 \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
56
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 \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
58 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
59 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
60 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
61 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
62 \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
63
10313
fa18b0fc0c18 extract_tb(): Only describe this function once. Problem reported by
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
64 \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
65 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
66 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
67 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
68 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
69 ``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
70 \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
71 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
72 \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
73 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
74 \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
75
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 \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
77 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
78 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
79 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
80 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
81 \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
82
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 \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
84 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
85 \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
86 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
87 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
88 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
89 items whose source text line is not \code{None}.
2460
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 \end{funcdesc}
1c29f6bdd898 en weer twee!
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
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
92 \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
93 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
94 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
95 \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
96 ending in a newline. Normally, the list contains a single string;
24384
6673171fc37e Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
97 however, for \exception{SyntaxError} exceptions, it contains several
6673171fc37e Small clarifications when referring to the sys.exc_* variables so that
Fred Drake <fdrake@acm.org>
parents: 16590
diff changeset
98 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
99 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
100 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
101 \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
102
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 \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
104 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
105 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
106 \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
107 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
108 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
109 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
110 \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
111
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 \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
113 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
114 \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
115
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 \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
117 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
118 \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
119
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 \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
121 This function returns the current line number set in the traceback
24844
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24384
diff changeset
122 object. This function was necessary because in versions of Python
27539
97b28170634c Fix markup error.
Fred Drake <fdrake@acm.org>
parents: 24844
diff changeset
123 prior to 2.3 when the \programopt{-O} flag was passed to Python the
24844
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24384
diff changeset
124 \code{\var{tb}.tb_lineno} was not updated correctly. This function
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24384
diff changeset
125 has no use in versions past 2.3.
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
126 \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
127
11078
d1b48ee14601 Improve description of the example.
Fred Drake <fdrake@acm.org>
parents: 10313
diff changeset
128
d1b48ee14601 Improve description of the example.
Fred Drake <fdrake@acm.org>
parents: 10313
diff changeset
129 \subsection{Traceback Example \label{traceback-example}}
d1b48ee14601 Improve description of the example.
Fred Drake <fdrake@acm.org>
parents: 10313
diff changeset
130
d1b48ee14601 Improve description of the example.
Fred Drake <fdrake@acm.org>
parents: 10313
diff changeset
131 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
132 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
133 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
134 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
135
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 \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
137 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
138
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 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
140 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
141 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
142 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
143 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
144 print "Exception in user code:"
8519
61a9c529455a # Untabified the example :-(
Guido van Rossum <guido@python.org>
parents: 8518
diff changeset
145 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
146 traceback.print_exc(file=sys.stdout)
8519
61a9c529455a # Untabified the example :-(
Guido van Rossum <guido@python.org>
parents: 8518
diff changeset
147 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
148
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 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
150 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
151 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
152 \end{verbatim}