annotate Doc/lib/libcode.tex @ 13625:fdfd1049f36e legacy-trunk

Rob W. W. Hooft's spelling fixes for the Library Reference. I hope SourceForge doesn't choke on this batch :-) I'm not entirely sure this is 100% correct. The patch changes an \index{persistency} to \index{presistence}, and I don't know what \index{} does. But it seems to do so persi--er, consistently, so I hope it isn't a problem.
author Thomas Wouters <thomas@python.org>
date Sun, 16 Jul 2000 19:01:10 +0000
parents e82e82b3b9ad
children 07a2dc1b72ae
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{code} ---
12223
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
2 Interpreter base classes}
8716
760dcf197048 Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents: 7877
diff changeset
3 \declaremodule{standard}{code}
760dcf197048 Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents: 7877
diff changeset
4
12223
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
5 \modulesynopsis{Base classes for interactive Python interpreters.}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
6
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
7
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
8 The \code{code} module provides facilities to implement
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
9 read-eval-print loops in Python. Two classes and convenience
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
10 functions are included which can be used to build applications which
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
11 provide an interactive interpreter prompt.
8716
760dcf197048 Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents: 7877
diff changeset
12
5842
35b14d78c61d added code.py; codehack.py is obsolete
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
12223
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
14 \begin{classdesc}{InteractiveInterpreter}{\optional{locals}}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
15 This class deals with parsing and interpreter state (the user's
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
16 namespace); it does not deal with input buffering or prompting or
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
17 input file naming (the filename is always passed in explicitly).
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
18 The optional \var{locals} argument specifies the dictionary in
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
19 which code will be executed; it defaults to a newly created
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
20 dictionary with key \code{'__name__'} set to \code{'__console__'}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
21 and key \code{'__doc__'} set to \code{None}.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
22 \end{classdesc}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
23
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
24 \begin{classdesc}{InteractiveConsole}{\optional{locals\optional{, filename}}}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
25 Closely emulate the behavior of the interactive Python interpreter.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
26 This class builds on \class{InteractiveInterpreter} and adds
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
27 prompting using the familiar \code{sys.ps1} and \code{sys.ps2}, and
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
28 input buffering.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
29 \end{classdesc}
5842
35b14d78c61d added code.py; codehack.py is obsolete
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
35b14d78c61d added code.py; codehack.py is obsolete
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
12223
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
32 \begin{funcdesc}{interact}{\optional{banner\optional{,
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
33 readfunc\optional{, local}}}}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
34 Convenience function to run a read-eval-print loop. This creates a
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
35 new instance of \class{InteractiveConsole} and sets \var{readfunc}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
36 to be used as the \method{raw_input()} method, if provided. If
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
37 \var{local} is provided, it is passed to the
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
38 \class{InteractiveConsole} constructor for use as the default
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
39 namespace for the interpreter loop. The \method{interact()} method
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
40 of the instance is then run with \var{banner} passed as the banner
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
41 to use, if provided. The console object is discarded after use.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
42 \end{funcdesc}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
43
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
44 \begin{funcdesc}{compile_command}{source\optional{,
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
45 filename\optional{, symbol}}}
5842
35b14d78c61d added code.py; codehack.py is obsolete
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 This function is useful for programs that want to emulate Python's
35b14d78c61d added code.py; codehack.py is obsolete
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 interpreter main loop (a.k.a. the read-eval-print loop). The tricky
35b14d78c61d added code.py; codehack.py is obsolete
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 part is to determine when the user has entered an incomplete command
12223
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
49 that can be completed by entering more text (as opposed to a
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
50 complete command or a syntax error). This function
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
51 \emph{almost} always makes the same decision as the real interpreter
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
52 main loop.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
53
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
54 \var{source} is the source string; \var{filename} is the optional
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
55 filename from which source was read, defaulting to \code{'<input>'};
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
56 and \var{symbol} is the optional grammar start symbol, which should
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
57 be either \code{'single'} (the default) or \code{'eval'}.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
58
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
59 Returns a code object (the same as \code{compile(\var{source},
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
60 \var{filename}, \var{symbol})}) if the command is complete and
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
61 valid; \code{None} if the command is incomplete; raises
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
62 \exception{SyntaxError} if the command is complete and contains a
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
63 syntax error, or raises \exception{OverflowError} if the command
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
64 includes a numeric constant which exceeds the range of the
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
65 appropriate numeric type.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
66 \end{funcdesc}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
67
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
68
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
69 \subsection{Interactive Interpreter Objects
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
70 \label{interpreter-objects}}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
71
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
72 \begin{methoddesc}{runsource}{source\optional{, filename\optional{, symbol}}}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
73 Compile and run some source in the interpreter.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
74 Arguments are the same as for \function{compile_command()}; the
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
75 default for \var{filename} is \code{'<input>'}, and for
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
76 \var{symbol} is \code{'single'}. One several things can happen:
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
77
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
78 \begin{itemize}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
79 \item
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
80 The input is incorrect; \function{compile_command()} raised an
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
81 exception (\exception{SyntaxError} or \exception{OverflowError}). A
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
82 syntax traceback will be printed by calling the
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
83 \method{showsyntaxerror()} method. \method{runsource()} returns
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
84 \code{0}.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
85
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
86 \item
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
87 The input is incomplete, and more input is required;
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
88 \function{compile_command()} returned \code{None}.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
89 \method{runsource()} returns \code{1}.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
90
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
91 \item
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
92 The input is complete; \function{compile_command()} returned a code
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
93 object. The code is executed by calling the \method{runcode()} (which
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
94 also handles run-time exceptions, except for \exception{SystemExit}).
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
95 \method{runsource()} returns \code{0}.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
96 \end{itemize}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
97
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
98 The return value can be used to decide whether to use
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
99 \code{sys.ps1} or \code{sys.ps2} to prompt the next line.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
100 \end{methoddesc}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
101
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
102 \begin{methoddesc}{runcode}{code}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
103 Execute a code object.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
104 When an exception occurs, \method{showtraceback()} is called to
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
105 display a traceback. All exceptions are caught except
13625
fdfd1049f36e Rob W. W. Hooft's spelling fixes for the Library Reference. I hope
Thomas Wouters <thomas@python.org>
parents: 12223
diff changeset
106 \exception{SystemExit}, which is allowed to propagate.
5842
35b14d78c61d added code.py; codehack.py is obsolete
Guido van Rossum <guido@python.org>
parents:
diff changeset
107
12223
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
108 A note about \exception{KeyboardInterrupt}: this exception may occur
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
109 elsewhere in this code, and may not always be caught. The caller
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
110 should be prepared to deal with it.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
111 \end{methoddesc}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
112
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
113 \begin{methoddesc}{showsyntaxerror}{\optional{filename}}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
114 Display the syntax error that just occurred. This does not display
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
115 a stack trace because there isn't one for syntax errors.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
116 If \var{filename} is given, it is stuffed into the exception instead
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
117 of the default filename provided by Python's parser, because it
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
118 always uses \code{'<string>'} when reading from a string.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
119 The output is written by the \method{write()} method.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
120 \end{methoddesc}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
121
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
122 \begin{methoddesc}{showtraceback}{}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
123 Display the exception that just occurred. We remove the first stack
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
124 item because it is within the interpreter object implementation.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
125 The output is written by the \method{write()} method.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
126 \end{methoddesc}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
127
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
128 \begin{methoddesc}{write}{data}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
129 Write a string to standard output. Derived classes should override
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
130 this to provide the appropriate output handling as needed.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
131 \end{methoddesc}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
132
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
133
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
134 \subsection{Interactive Console Objects
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
135 \label{console-objects}}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
136
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
137 The \class{InteractiveConsole} class is a subclass of
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
138 \class{InteractiveInterpreter}, and so offers all the methods of the
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
139 interpreter objects as well as the following additions.
5842
35b14d78c61d added code.py; codehack.py is obsolete
Guido van Rossum <guido@python.org>
parents:
diff changeset
140
12223
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
141 \begin{methoddesc}{interact}{\optional{banner}}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
142 Closely emulate the interactive Python console.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
143 The optional banner argument specify the banner to print before the
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
144 first interaction; by default it prints a banner similar to the one
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
145 printed by the standard Python interpreter, followed by the class
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
146 name of the console object in parentheses (so as not to confuse this
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
147 with the real interpreter -- since it's so close!).
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
148 \end{methoddesc}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
149
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
150 \begin{methoddesc}{push}{line}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
151 Push a line of source text to the interpreter.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
152 The line should not have a trailing newline; it may have internal
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
153 newlines. The line is appended to a buffer and the interpreter's
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
154 \method{runsource()} method is called with the concatenated contents
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
155 of the buffer as source. If this indicates that the command was
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
156 executed or invalid, the buffer is reset; otherwise, the command is
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
157 incomplete, and the buffer is left as it was after the line was
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
158 appended. The return value is \code{1} if more input is required,
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
159 \code{0} if the line was dealt with in some way (this is the same as
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
160 \method{runsource()}).
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
161 \end{methoddesc}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
162
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
163 \begin{methoddesc}{resetbuffer}{}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
164 Remove any unhandled source text from the input buffer.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
165 \end{methoddesc}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
166
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
167 \begin{methoddesc}{raw_input}{\optional{prompt}}
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
168 Write a prompt and read a line. The returned line does not include
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
169 the trailing newline. When the user enters the \EOF{} key sequence,
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
170 \exception{EOFError} is raised. The base implementation uses the
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
171 built-in function \function{raw_input()}; a subclass may replace this
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
172 with a different implementation.
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 9963
diff changeset
173 \end{methoddesc}