Package org.python.util
Class InteractiveInterpreter
- java.lang.Object
-
- org.python.util.PythonInterpreter
-
- org.python.util.InteractiveInterpreter
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
- Direct Known Subclasses:
InteractiveConsole
public class InteractiveInterpreter extends PythonInterpreter
This class provides the interface for compiling and running code that supports an interactive interpreter.
-
-
Constructor Summary
Constructors Constructor Description InteractiveInterpreter()
Construct an InteractiveInterpreter with all default characteristics: default state (fromPy.getSystemState()
), and a new empty dictionary of local variables.InteractiveInterpreter(PyObject locals)
Construct an InteractiveInterpreter with state (fromPy.getSystemState()
), and the specified dictionary of local variables.InteractiveInterpreter(PyObject locals, PySystemState systemState)
Construct an InteractiveInterpreter with, and system state the specified dictionary of local variables.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
interrupt(ThreadState ts)
Pause the current code, sneak an exception raiser into sys.trace_func, and then continue the code hoping that Jython will get control to do the break;void
resetbuffer()
void
runcode(PyObject code)
Execute a code object.boolean
runsource(java.lang.String source)
Compile and run some source in the interpreter, in the modeCompileMode.single
which is used for incremental compilation at the interactive console, known as "".boolean
runsource(java.lang.String source, java.lang.String filename)
Compile and run some source in the interpreter, in the modeCompileMode.single
which is used for incremental compilation at the interactive console.boolean
runsource(java.lang.String source, java.lang.String filename, CompileMode kind)
Compile and run some source in the interpreter, according to theCompileMode
given.void
showexception(PyException exc)
void
write(java.lang.String data)
-
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
-
-
-
-
Constructor Detail
-
InteractiveInterpreter
public InteractiveInterpreter()
Construct an InteractiveInterpreter with all default characteristics: default state (fromPy.getSystemState()
), and a new empty dictionary of local variables.
-
InteractiveInterpreter
public InteractiveInterpreter(PyObject locals)
Construct an InteractiveInterpreter with state (fromPy.getSystemState()
), and the specified dictionary of local variables.- Parameters:
locals
- dictionary to use, or ifnull
, a new empty one will be created
-
InteractiveInterpreter
public InteractiveInterpreter(PyObject locals, PySystemState systemState)
Construct an InteractiveInterpreter with, and system state the specified dictionary of local variables.- Parameters:
locals
- dictionary to use, or ifnull
, a new empty one will be createdsystemState
- interpreter state, or ifnull
usePy.getSystemState()
-
-
Method Detail
-
runsource
public boolean runsource(java.lang.String source)
Compile and run some source in the interpreter, in the modeCompileMode.single
which is used for incremental compilation at the interactive console, known as "".- Parameters:
source
- Python code- Returns:
true
to indicate a partial statement was entered
-
runsource
public boolean runsource(java.lang.String source, java.lang.String filename)
Compile and run some source in the interpreter, in the modeCompileMode.single
which is used for incremental compilation at the interactive console.- Parameters:
source
- Python codefilename
- name with which to label this console input (e.g. in error messages).- Returns:
true
to indicate a partial statement was entered
-
runsource
public boolean runsource(java.lang.String source, java.lang.String filename, CompileMode kind)
Compile and run some source in the interpreter, according to theCompileMode
given. This method supports incremental compilation and interpretation through the return value, wheretrue
signifies that more input is expected in order to complete the Python statement. An interpreter can use this to decide whether to usesys.ps1
(">>>
") orsys.ps2
("...
") to prompt the next line. The arguments are the same as the mandatory ones in the Pythoncompile()
command.One the following can happen:
- The input is incorrect; compilation raised an exception (SyntaxError or OverflowError). A
syntax traceback will be printed by calling
showexception(PyException)
. Return isfalse
. - The input is incomplete, and more input is required; compilation returned no code.
Nothing happens. Return is
true
. - The input is complete; compilation returned a code object. The code is executed by
calling
runcode(PyObject)
(which also handles run-time exceptions, except for SystemExit). Return isfalse
.
- Parameters:
source
- Python codefilename
- name with which to label this console input (e.g. in error messages).kind
- of compilation required:CompileMode.eval
,CompileMode.exec
orCompileMode.single
- Returns:
true
to indicate a partial statement was provided
- The input is incorrect; compilation raised an exception (SyntaxError or OverflowError). A
syntax traceback will be printed by calling
-
runcode
public void runcode(PyObject code)
Execute a code object. When an exception occurs,showexception(PyException)
is called to display a stack trace, except in the case of SystemExit, which is re-raised.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 with it.
-
showexception
public void showexception(PyException exc)
-
write
public void write(java.lang.String data)
-
resetbuffer
public void resetbuffer()
-
interrupt
public void interrupt(ThreadState ts)
Pause the current code, sneak an exception raiser into sys.trace_func, and then continue the code hoping that Jython will get control to do the break;
-
-