org.python.util

Class PythonInterpreter

public class PythonInterpreter extends Object

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

Version: 1.0, 02/23/97

Author: Jim Hugunin

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
voidcleanup()
PyObjecteval(String s)
Evaluate a string as Python source and return the result
voidexec(String s)
Execute a string of Python source in the local namespace
voidexec(PyObject code)
Execute a Python code object in the local namespace
voidexecfile(String s)
Execute a file of Python source in the local namespace
voidexecfile(InputStream s)
voidexecfile(InputStream s, String name)
PyObjectget(String name)
Get the value of a variable in the local namespace
Objectget(String name, Class javaclass)
Get the value of a variable in the local namespace Value will be returned as an instance of the given Java class.
PyObjectgetLocals()
static voidinitialize(Properties preProperties, Properties postProperties, String[] argv)
Initializes the jython runtime.
voidset(String name, Object value)
Set a variable in the local namespace
voidset(String name, PyObject value)
Set a variable in the local namespace
voidsetErr(PyObject outStream)
voidsetErr(Writer outStream)
voidsetErr(OutputStream outStream)
voidsetLocals(PyObject d)
voidsetOut(PyObject outStream)
Set the Python object to use for the standard output stream
voidsetOut(Writer outStream)
Set a java.io.Writer to use for the standard output stream
voidsetOut(OutputStream outStream)
Set a java.io.OutputStream to use for the standard output stream

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

Parameters: dict the dictionary to use

PythonInterpreter

public PythonInterpreter(PyObject dict, PySystemState systemState)

Method Detail

cleanup

public void cleanup()

eval

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

Parameters: s the string to evaluate

exec

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

Parameters: s the string to execute

exec

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

Parameters: code the code object to execute

execfile

public void execfile(String s)
Execute a file of Python source in the local namespace

Parameters: s the name of the file to execute

execfile

public void execfile(InputStream s)

execfile

public void execfile(InputStream s, String name)

get

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

Parameters: name the name of the variable

get

public Object get(String name, Class 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

getLocals

public PyObject getLocals()

initialize

public static void initialize(Properties preProperties, Properties postProperties, 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.

set

public void set(String name, 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(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

setErr

public void setErr(PyObject outStream)

setErr

public void setErr(Writer outStream)

setErr

public void setErr(OutputStream outStream)

setLocals

public void setLocals(PyObject d)

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(Writer outStream)
Set a java.io.Writer to use for the standard output stream

Parameters: outStream Writer to use as output stream

setOut

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

Parameters: outStream OutputStream to use as output stream

Jython homepage