org.python.util
Class PythonInterpreter

java.lang.Object
  extended by org.python.util.PythonInterpreter
Direct Known Subclasses:
InteractiveInterpreter

public class PythonInterpreter
extends java.lang.Object

The PythonInterpreter class is a standard wrapper for a Jython interpreter for use embedding in a Java application.


Constructor Summary
PythonInterpreter()
          Create a new Interpreter with an empty dictionary
PythonInterpreter(PyObject dict)
          Create a new interpreter with the given dictionary to use as its namespace
PythonInterpreter(PyObject dict, PySystemState systemState)
           
 
Method Summary
 void cleanup()
           
 PyObject eval(java.lang.String s)
          Evaluate a string as Python source and return the result
 void exec(PyObject code)
          Execute a Python code object in the local namespace
 void exec(java.lang.String s)
          Execute a string of Python source in the local namespace
 void execfile(java.io.InputStream s)
           
 void execfile(java.io.InputStream s, java.lang.String name)
           
 void execfile(java.lang.String filename)
          Execute a file of Python source in the local namespace
 PyObject get(java.lang.String name)
          Get the value of a variable in the local namespace
<T> T
get(java.lang.String name, java.lang.Class<T> javaclass)
          Get the value of a variable in the local namespace Value will be returned as an instance of the given Java class.
 PyObject getLocals()
           
static void initialize(java.util.Properties preProperties, java.util.Properties postProperties, java.lang.String[] argv)
          Initializes the jython runtime.
 void set(java.lang.String name, java.lang.Object value)
          Set a variable in the local namespace
 void set(java.lang.String name, PyObject value)
          Set a variable in the local namespace
 void setErr(java.io.OutputStream outStream)
           
 void setErr(PyObject outStream)
           
 void setLocals(PyObject d)
           
 void setOut(java.io.OutputStream outStream)
          Set a java.io.OutputStream to use for the standard output stream
 void setOut(PyObject outStream)
          Set the Python object to use for the standard output stream
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PythonInterpreter

public PythonInterpreter()
Create a new Interpreter with an empty dictionary


PythonInterpreter

public PythonInterpreter(PyObject dict)
Create a new interpreter with the given dictionary to use as its namespace


PythonInterpreter

public PythonInterpreter(PyObject dict,
                         PySystemState systemState)
Method Detail

initialize

public static void initialize(java.util.Properties preProperties,
                              java.util.Properties postProperties,
                              java.lang.String[] argv)
Initializes the jython runtime. This should only be called once, and should be called before any other python objects are created (including a PythonInterpreter).

Parameters:
preProperties - A set of properties. Typically System.getProperties() is used.
postProperties - An other set of properties. Values like python.home, python.path and all other values from the registry files can be added to this property set. PostProperties will override system properties and registry properties.
argv - Command line argument. These values will assigned to sys.argv.

setOut

public void setOut(PyObject outStream)
Set the Python object to use for the standard output stream

Parameters:
outStream - Python file-like object to use as output stream

setOut

public void setOut(java.io.OutputStream outStream)
Set a java.io.OutputStream to use for the standard output stream

Parameters:
outStream - OutputStream to use as output stream

setErr

public void setErr(PyObject outStream)

setErr

public void setErr(java.io.OutputStream outStream)

eval

public PyObject eval(java.lang.String s)
Evaluate a string as Python source and return the result


exec

public void exec(java.lang.String s)
Execute a string of Python source in the local namespace


exec

public void exec(PyObject code)
Execute a Python code object in the local namespace


execfile

public void execfile(java.lang.String filename)
Execute a file of Python source in the local namespace


execfile

public void execfile(java.io.InputStream s)

execfile

public void execfile(java.io.InputStream s,
                     java.lang.String name)

getLocals

public PyObject getLocals()

setLocals

public void setLocals(PyObject d)

set

public void set(java.lang.String name,
                java.lang.Object value)
Set a variable in the local namespace

Parameters:
name - the name of the variable
value - the value to set the variable to. Will be automatically converted to an appropriate Python object.

set

public void set(java.lang.String name,
                PyObject value)
Set a variable in the local namespace

Parameters:
name - the name of the variable
value - the value to set the variable to

get

public PyObject get(java.lang.String name)
Get the value of a variable in the local namespace

Parameters:
name - the name of the variable
Returns:
the value of the variable, or null if that name isn't assigned

get

public <T> T get(java.lang.String name,
                 java.lang.Class<T> javaclass)
Get the value of a variable in the local namespace Value will be returned as an instance of the given Java class. interp.get("foo", Object.class) will return the most appropriate generic Java object.

Parameters:
name - the name of the variable
javaclass - the class of object to return
Returns:
the value of the variable as the given class, or null if that name isn't assigned

cleanup

public void cleanup()


Jython homepage