Mercurial > cpython-lru_cache-weakref
annotate Doc/lib/libtraceback.tex @ 39042:c02fa187e0f4 legacy-trunk
Part of SF patch #1484695. This removes dead code. The chksum was
already verified in .frombuf() on the lines above. If there was
a problem an exception is raised, so there was no way this condition
could have been true.
| author | Neal Norwitz <nnorwitz@gmail.com> |
|---|---|
| date | Mon, 10 Jul 2006 00:23:17 +0000 |
| parents | 98653d9f9cc3 |
| children | 7ada16a14968 |
| rev | line source |
|---|---|
| 8918 | 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 | 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 | 12 ``wrapper'' around the interpreter. |
| 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 |
|
6673171fc37e
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 | 18 \obindex{traceback} |
| 2460 | 19 |
| 20 The module defines the following functions: | |
| 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 | 23 Print up to \var{limit} stack trace entries from \var{traceback}. If |
| 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 | 28 \end{funcdesc} |
| 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 | 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 | 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 | 37 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
|
38 \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
|
39 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
|
40 with a caret indicating the approximate position of the error. |
| 2460 | 41 \end{funcdesc} |
| 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}}} |
|
24384
6673171fc37e
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, |
|
6673171fc37e
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 |
|
6673171fc37e
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 |
|
6673171fc37e
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 |
|
6673171fc37e
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 |
|
36043
98653d9f9cc3
Patch #1376914: traceback.format_exc() has no "file" argument
Georg Brandl <georg@python.org>
parents:
31183
diff
changeset
|
51 \begin{funcdesc}{format_exc}{\optional{limit}} |
|
30862
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
27539
diff
changeset
|
52 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
|
53 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
|
54 \versionadded{2.4} |
|
30862
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
27539
diff
changeset
|
55 \end{funcdesc} |
|
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
27539
diff
changeset
|
56 |
|
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
|
57 \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
|
58 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
|
59 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
|
60 \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
|
61 |
|
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 \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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 \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
|
68 |
|
10313
fa18b0fc0c18
extract_tb(): Only describe this function once. Problem reported by
Fred Drake <fdrake@acm.org>
parents:
8918
diff
changeset
|
69 \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
|
70 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
|
71 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
|
72 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
|
73 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
|
74 ``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
|
75 \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
|
76 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
|
77 \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
|
78 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
|
79 \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
|
80 |
|
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 \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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 \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
|
87 |
|
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 \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
|
89 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
|
90 \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
|
91 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
|
92 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
|
93 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
|
94 items whose source text line is not \code{None}. |
| 2460 | 95 \end{funcdesc} |
| 96 | |
|
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
|
97 \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
|
98 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
|
99 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
|
100 \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
|
101 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
|
102 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
|
103 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
|
104 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
|
105 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
|
106 \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
|
107 |
|
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 \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
|
109 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
|
110 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
|
111 \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
|
112 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
|
113 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
|
114 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
|
115 \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
|
116 |
|
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 \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
|
118 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
|
119 \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
|
120 |
|
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 \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
|
122 A shorthand for \code{format_list(extract_stack(\var{f}, \var{limit}))}. |
| 2460 | 123 \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
|
124 |
|
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 \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
|
126 This function returns the current line number set in the traceback |
| 24844 | 127 object. This function was necessary because in versions of Python |
| 27539 | 128 prior to 2.3 when the \programopt{-O} flag was passed to Python the |
| 24844 | 129 \code{\var{tb}.tb_lineno} was not updated correctly. This function |
| 130 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
|
131 \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
|
132 |
|
11078
d1b48ee14601
Improve description of the example.
Fred Drake <fdrake@acm.org>
parents:
10313
diff
changeset
|
133 |
|
d1b48ee14601
Improve description of the example.
Fred Drake <fdrake@acm.org>
parents:
10313
diff
changeset
|
134 \subsection{Traceback Example \label{traceback-example}} |
|
d1b48ee14601
Improve description of the example.
Fred Drake <fdrake@acm.org>
parents:
10313
diff
changeset
|
135 |
|
d1b48ee14601
Improve description of the example.
Fred Drake <fdrake@acm.org>
parents:
10313
diff
changeset
|
136 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
|
137 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
|
138 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
|
139 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
|
140 |
|
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 \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
|
142 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
|
143 |
|
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 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
|
145 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
|
146 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
|
147 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
|
148 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
|
149 print "Exception in user code:" |
|
8519
61a9c529455a
# Untabified the example :-(
Guido van Rossum <guido@python.org>
parents:
8518
diff
changeset
|
150 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
|
151 traceback.print_exc(file=sys.stdout) |
|
8519
61a9c529455a
# Untabified the example :-(
Guido van Rossum <guido@python.org>
parents:
8518
diff
changeset
|
152 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
|
153 |
|
b56981c0d61e
Documented that by default the output goes to stderr, and that a file
Guido van Rossum <guido@python.org>
parents:
7877
diff
changeset
|
154 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
|
155 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
|
156 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
|
157 \end{verbatim} |
