Package org.python.util
Class InteractiveConsole
java.lang.Object
org.python.util.PythonInterpreter
org.python.util.InteractiveInterpreter
org.python.util.InteractiveConsole
- All Implemented Interfaces:
Closeable
,AutoCloseable
This class provides the read, execute, print loop needed by a Python console; it is not actually
a console itself. The primary capability is the
interact()
method, which repeatedly
calls raw_input(PyObject)
, and hence __builtin__.raw_input(PyObject)
, in order
to get lines, and push(String)
them into the interpreter. The built-in
raw_input()
method prompts on sys.stdout
and reads from
sys.stdin
, the standard console. These may be redirected using
PythonInterpreter.setOut(java.io.OutputStream)
and PythonInterpreter.setIn(java.io.InputStream)
, as may also
sys.stderr
.-
Field Summary
Fields inherited from class org.python.util.InteractiveInterpreter
buffer
-
Constructor Summary
ConstructorDescriptionConstruct an interactive console, which will "run" wheninteract()
is called.InteractiveConsole
(PyObject locals) Construct an interactive console, which will "run" wheninteract()
is called.InteractiveConsole
(PyObject locals, String filename) Construct an interactive console, which will "run" wheninteract()
is called.InteractiveConsole
(PyObject locals, String filename, boolean replaceRawInput) Full-feature constructor for an interactive console, which will "run" wheninteract()
is called. -
Method Summary
Modifier and TypeMethodDescriptionvoid
static String
Returns the banner to print before the first interaction: "Jython <version> on <platform>
".void
interact()
Operate a Python console, as ininteract(String, PyObject)
, on the standard input.void
Operate a Python console by repeatedly callingraw_input(PyObject, PyObject)
and interpreting the lines read.boolean
Push a line to the interpreter.Write a prompt and read a line from standard input.Write a prompt and read a line from a file.Methods inherited from class org.python.util.InteractiveInterpreter
interrupt, resetbuffer, runcode, runsource, runsource, runsource, showexception, write
Methods inherited from class org.python.util.PythonInterpreter
cleanup, close, compile, compile, compile, compile, eval, eval, exec, exec, execfile, execfile, execfile, get, get, getLocals, getSystemState, initialize, set, set, setErr, setErr, setErr, setIn, setIn, setIn, setLocals, setOut, setOut, setOut, threadLocalStateInterpreter
-
Field Details
-
CONSOLE_FILENAME
Note: This field is actually final; don't modify.To work around an issue in javadoc with Java 8 we cannot have it final for now, see issue 2539 for details.
-
filename
-
-
Constructor Details
-
InteractiveConsole
public InteractiveConsole()Construct an interactive console, which will "run" wheninteract()
is called. The name of the console (e.g. in error messages) will beCONSOLE_FILENAME
. -
InteractiveConsole
Construct an interactive console, which will "run" wheninteract()
is called. The name of the console (e.g. in error messages) will beCONSOLE_FILENAME
.- Parameters:
locals
- dictionary to use, or ifnull
, a new empty one will be created
-
InteractiveConsole
Construct an interactive console, which will "run" wheninteract()
is called.- Parameters:
locals
- dictionary to use, or ifnull
, a new empty one will be createdfilename
- name with which to label this console input (e.g. in error messages).
-
InteractiveConsole
Full-feature constructor for an interactive console, which will "run" wheninteract()
is called. This version allows the caller to replace the built-in raw_input() methods withraw_input(PyObject)
andraw_input(PyObject, PyObject)
, which may be overridden in a sub-class.- Parameters:
locals
- dictionary to use, or ifnull
, a new empty one will be createdfilename
- name with which to label this console inputreplaceRawInput
- if true, hook this class'sraw_input
into the built-ins.
-
-
Method Details
-
interact
public void interact()Operate a Python console, as ininteract(String, PyObject)
, on the standard input. The standard input may have been redirected byPythonInterpreter.setIn(java.io.InputStream)
or its variants. The banner (printed before first input) is obtained by callinggetDefaultBanner()
. -
getDefaultBanner
Returns the banner to print before the first interaction: "Jython <version> on <platform>
".- Returns:
- the banner.
-
interact
Operate a Python console by repeatedly callingraw_input(PyObject, PyObject)
and interpreting the lines read. An end of file causes the method to return.- Parameters:
banner
- to print before accepting input, or ifnull
, no banner.file
- from which to read commands, or ifnull
, read the console.
-
_interact
-
push
Push a line to the interpreter. The line should not have a trailing newline; it may have internal newlines. The line is appended to a buffer and the interpreter's 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 1 if more input is required, 0 if the line was dealt with in some way (this is the same as runsource()). -
raw_input
Write a prompt and read a line from standard input. The returned line does not include the trailing newline. When the user enters the EOF key sequence, EOFError is raised. The base implementation uses the built-in function raw_input(); a subclass may replace this with a different implementation. -
raw_input
Write a prompt and read a line from a file.
-