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