code— Interpreter base classes
Source code: Lib/code.py
code module provides facilities to implement read-eval-print loops in
Python. Two classes and convenience functions are included which can be used to
build applications which provide an interactive interpreter prompt.
InteractiveInterpreterand adds prompting using the familiar
sys.ps2, and input buffering.
interact(banner=None, readfunc=None, local=None, exitmsg=None)
Convenience function to run a read-eval-print loop. This creates a new
InteractiveConsole and sets readfunc to be used as
InteractiveConsole.raw_input() method, if provided. If local is
provided, it is passed to the
InteractiveConsole constructor for
use as the default namespace for the interpreter loop. The
method of the instance is then run with banner and exitmsg passed as the
banner and exit message to use, if provided. The console object is discarded
Changed in version 3.6: Added exitmsg parameter.
compile_command(source, filename="<input>", symbol="single")
This function is useful for programs that want to emulate Python’s interpreter main loop (a.k.a. the read-eval-print loop). The tricky part is to determine when the user has entered an incomplete command that can be completed by entering more text (as opposed to a complete command or a syntax error). This function almost always makes the same decision as the real interpreter main loop.
source is the source string; filename is the optional filename from which
source was read, defaulting to
'<input>'; and symbol is the optional
grammar start symbol, which should be
'single' (the default),
Returns a code object (the same as
compile(source, filename, symbol)) if the
command is complete and valid;
None if the command is incomplete; raises
SyntaxError if the command is complete and contains a syntax error, or
ValueError if the command contains an
runsource(source, filename="<input>", symbol="single")
Compile and run some source in the interpreter. Arguments are the same as for
compile_command(); the default for filename is
'<input>', and for
'single'. One of several things can happen:
The input is complete;
compile_command() returned a code object. The
code is executed by calling the
runcode() (which also handles run-time
exceptions, except for
The return value can be used to decide whether to use
to prompt the next line.
A note about
KeyboardInterrupt: this exception may occur elsewhere in
this code, and may not always be caught. The caller should be prepared to deal
'<string>'when reading from a string. The output is written by the
Display the exception that just occurred. We remove the first stack item
because it is within the interpreter object implementation. The output is
written by the
Changed in version 3.5: The full chained traceback is displayed instead of just the primary traceback.
sys.stderr). Derived classes should override this to provide the appropriate output handling as needed.
Closely emulate the interactive Python console. The optional banner argument specify the banner to print before the first interaction; by default it prints a banner similar to the one printed by the standard Python interpreter, followed by the class name of the console object in parentheses (so as not to confuse this with the real interpreter – since it’s so close!).
The optional exitmsg argument specifies an exit message printed when exiting.
Pass the empty string to suppress the exit message. If exitmsg is not given or
None, a default message is printed.
Changed in version 3.4: To suppress printing any banner, pass an empty string.
Changed in version 3.6: Print an exit message when exiting.
runsource()method is called with the concatenated contents of the buffer as source. If this indicates that the command was executed or invalid, the buffer is reset; otherwise, the command is incomplete, and the buffer is left as it was after the line was appended. The return value is
Trueif more input is required,
Falseif the line was dealt with in some way (this is the same as
EOFErroris raised. The base implementation reads from
sys.stdin; a subclass may replace this with a different implementation.