InteractiveInterpreter (Jython API documentation)
Date: 2022-09-10

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.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `java.lang.StringBuilder` | `buffer`
    `java.lang.String` | `filename`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `InteractiveInterpreter()` Construct an InteractiveInterpreter with all default characteristics: default state (from `Py.getSystemState()`), and a new empty dictionary of local variables.
    `InteractiveInterpreter(PyObject locals)` Construct an InteractiveInterpreter with state (from `Py.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 and 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 mode `CompileMode.single` which is used for incremental compilation at the interactive console, known as `<input>`.
    `boolean` | `runsource(java.lang.String source, java.lang.String filename)` Compile and run some source in the interpreter, in the mode `CompileMode.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 the `CompileMode` 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`

    - ### Methods inherited from class java.lang.Object

      `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### buffer

          public java.lang.StringBuilder buffer

    - #### filename

          public java.lang.String filename

  - ### Constructor Detail

    - #### InteractiveInterpreter

          public InteractiveInterpreter()

      Construct an InteractiveInterpreter with all default characteristics: default state (from `Py.getSystemState()`), and a new empty dictionary of local variables.

    - #### InteractiveInterpreter

          public InteractiveInterpreter(PyObject locals)

      Construct an InteractiveInterpreter with state (from `Py.getSystemState()`), and the specified dictionary of local variables.
      Parameters:
        `locals` - dictionary to use, or if `null`, 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 if `null`, a new empty one will be created
        `systemState` - interpreter state, or if `null` use `Py.getSystemState()`

  - ### Method Detail

    - #### runsource

          public boolean runsource(java.lang.String source)

      Compile and run some source in the interpreter, in the mode `CompileMode.single` which is used for incremental compilation at the interactive console, known as `<input>`.
      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 mode `CompileMode.single` which is used for incremental compilation at the interactive console.
      Parameters:
        `source` - Python code
        `filename` - 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 the `CompileMode` given. This method supports incremental compilation and interpretation through the return value, where `true` signifies that more input is expected in order to complete the Python statement. An interpreter can use this to decide whether to use `sys.ps1` ("`>>>` ") or `sys.ps2` ("`...` ") to prompt the next line. The arguments are the same as the mandatory ones in the Python `compile()` command.

      One the following can happen:

      1. The input is incorrect; compilation raised an exception (SyntaxError or OverflowError). A syntax traceback will be printed by calling `showexception(PyException)`. Return is `false`.
      2. The input is incomplete, and more input is required; compilation returned no code. Nothing happens. Return is `true`.
      3. 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 is `false`.
      Parameters:
        `source` - Python code
        `filename` - name with which to label this console input (e.g. in error messages).
        `kind` - of compilation required: `CompileMode.eval`, `CompileMode.exec` or `CompileMode.single`
      Returns:
        `true` to indicate a partial statement was provided

    - #### 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;

Jython homepage