Top Description Fields Constructors Methods
org.python.util

public Class PythonInterpreter

extends Object
implements AutoCloseable, Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable
Known Direct Subclasses
org.python.util.InteractiveInterpreter
Imports
java.io.Closeable, .Reader, .StringReader, java.util.Properties, org.python.antlr.base.mod, org.python.core.CodeFlag, .CompileMode, .CompilerFlags, .Options, .ParserFacade, .Py, .PyCode, .PyException, .PyFile, .PyFileReader, .PyFileWriter, .PyModule, .PyObject, .PyString, .PySystemState, .__builtin__

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

Field Summary

Modifier and TypeField and Description
protected CompilerFlags
private volatile boolean
pack-priv PyObject
protected PySystemState
protected static ThreadLocal<Object[]>
protected final boolean

Constructor Summary

AccessConstructor and Description
public
PythonInterpreter()

Creates a new interpreter with an empty local namespace.

public
PythonInterpreter(PyObject
a Python mapping object (e.g., a dictionary) for use as the namespace
dict
)

Creates a new interpreter with a specified local namespace.

public
protected
PythonInterpreter(PyObject dict, PySystemState systemState, boolean useThreadLocalState)

Method Summary

Modifier and TypeMethod and Description
public void
public void
close()

Implements java.lang.AutoCloseable.close, java.io.Closeable.close.

Closes this resource, relinquishing any underlying resources.

public PyCode
compile(String script)

Compiles a string of Python source as either an expression (if possible) or a module.

public PyCode
compile(Reader reader)

public PyCode
compile(String script, String filename)

public PyCode
compile(Reader reader, String filename)

public PyObject
eval(String s)

Evaluates a string as a Python expression and returns the result.

public PyObject
eval(PyObject code)

Evaluates a Python code object and returns the result.

public void
exec(String s)

Executes a string of Python source in the local namespace.

public void
exec(PyObject code)

Executes a Python code object in the local namespace.

public void
execfile(String filename)

Executes a file of Python source in the local namespace.

public void
public void
public PyObject

Returns:

the value of the variable, or null if that name isn't assigned
get
(String
the name of the variable
name
)

Returns the value of a variable in the local namespace.

public <T> T

Returns:

the value of the variable as the given class, or null if that name isn't assigned
get
(String
the name of the variable
name
,
Class<T>
the class of object to return
javaclass
)

Returns the value of a variable in the local namespace.

public PyObject
public PySystemState
public static void
initialize(Properties
A set of properties. Typically System.getProperties() is used. preProperties override properties from the registry file.
preProperties
,
Properties
Another 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 override system properties and registry properties.
postProperties
,
String[]
Command line arguments, assigned to sys.argv.
argv
)

Initializes the Jython runtime.

public void
set(String
the name of the variable
name
,
Object
the object to set the variable to (as converted to an appropriate Python object)
value
)

Sets a variable in the local namespace.

public void
set(String
the name of the variable
name
,
PyObject
the Python object to set the variable to
value
)

Sets a variable in the local namespace.

public void
setErr(PyObject
Python file-like object to use as the error output stream
outStream
)

Sets a Python object to use for the standard output stream, sys.stderr.

public void
setErr(Writer
to use as the error output stream
outStream
)

Sets a java.io.Writer to use for the standard output stream, sys.stdout.

public void
setErr(OutputStream outStream)

public void
setIn(PyObject
a Python file-like object to use as the input stream
inStream
)

Sets a Python object to use for the standard input stream, sys.stdin.

public void
setIn(Reader
to use as the input stream
inStream
)

Sets a Reader to use for the standard input stream, sys.stdin.

public void
setIn(InputStream
InputStream to use as input stream
inStream
)

Sets a java.io.InputStream to use for the standard input stream.

public void
public void
setOut(PyObject
Python file-like object to use as the output stream
outStream
)

Sets a Python object to use for the standard output stream, sys.stdout.

public void
setOut(Writer
to use as the output stream
outStream
)

Sets a java.io.Writer to use for the standard output stream, sys.stdout.

public void
setOut(OutputStream
OutputStream to use as output stream
outStream
)

Sets a java.io.OutputStream to use for the standard output stream.

protected void
public static PythonInterpreter
threadLocalStateInterpreter(PyObject
a Python mapping object (e.g., a dictionary) for use as the default namespace
dict
)

Creates a new interpreter with the ability to maintain a separate local namespace for each thread (set by invoking setLocals()).

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cflagsback to summary
protected CompilerFlags cflags
closedback to summary
private volatile boolean closed
globalsback to summary
pack-priv PyObject globals
systemStateback to summary
protected PySystemState systemState
threadLocalsback to summary
protected static ThreadLocal<Object[]> threadLocals
useThreadLocalStateback to summary
protected final boolean useThreadLocalState

Constructor Detail

PythonInterpreterback to summary
public PythonInterpreter()

Creates a new interpreter with an empty local namespace.

PythonInterpreterback to summary
public PythonInterpreter(PyObject dict)

Creates a new interpreter with a specified local namespace.

Parameters
dict:PyObject

a Python mapping object (e.g., a dictionary) for use as the namespace

PythonInterpreterback to summary
public PythonInterpreter(PyObject dict, PySystemState systemState)
PythonInterpreterback to summary
protected PythonInterpreter(PyObject dict, PySystemState systemState, boolean useThreadLocalState)

Method Detail

cleanupback to summary
public void cleanup()
closeback to summary
public void close()

Implements java.lang.AutoCloseable.close, java.io.Closeable.close.

Doc from java.lang.AutoCloseable.close.

Closes this resource, relinquishing any underlying resources. This method is invoked automatically on objects managed by the try-with-resources statement.

Annotations
@Override
compileback to summary
public PyCode compile(String script)

Compiles a string of Python source as either an expression (if possible) or a module. Designed for use by a JSR 223 implementation: "the Scripting API does not distinguish between scripts which return values and those which do not, nor do they make the corresponding distinction between evaluating or executing objects." (SCR.4.2.1)

compileback to summary
public PyCode compile(Reader reader)
compileback to summary
public PyCode compile(String script, String filename)
compileback to summary
public PyCode compile(Reader reader, String filename)
evalback to summary
public PyObject eval(String s)

Evaluates a string as a Python expression and returns the result.

evalback to summary
public PyObject eval(PyObject code)

Evaluates a Python code object and returns the result.

execback to summary
public void exec(String s)

Executes a string of Python source in the local namespace. In some environments, such as Windows, Unicode characters in the script will be converted into ascii question marks (?). This can be avoided by first compiling the fragment using PythonInterpreter.compile(), and using the overridden form of this method which takes a PyCode object. Code page declarations are not supported.

execback to summary
public void exec(PyObject code)

Executes a Python code object in the local namespace.

execfileback to summary
public void execfile(String filename)

Executes a file of Python source in the local namespace.

execfileback to summary
public void execfile(InputStream s)
execfileback to summary
public void execfile(InputStream s, String name)
getback to summary
public PyObject get(String name)

Returns the value of a variable in the local namespace.

Parameters
name:String

the name of the variable

Returns:PyObject

the value of the variable, or null if that name isn't assigned

getback to summary
public <T> T get(String name, Class<T> javaclass)

Returns the value of a variable in the local namespace. The 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:String

the name of the variable

javaclass:Class<T>

the class of object to return

Returns:T

the value of the variable as the given class, or null if that name isn't assigned

getLocalsback to summary
public PyObject getLocals()
getSystemStateback to summary
public PySystemState getSystemState()
initializeback to summary
public static void initialize(Properties preProperties, Properties postProperties, String[] argv)

Initializes the Jython runtime. This should only be called once, before any other Python objects (including PythonInterpreter) are created.

Parameters
preProperties:Properties

A set of properties. Typically System.getProperties() is used. preProperties override properties from the registry file.

postProperties:Properties

Another 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 override system properties and registry properties.

argv:String[]

Command line arguments, assigned to sys.argv.

setback to summary
public void set(String name, Object value)

Sets a variable in the local namespace.

Parameters
name:String

the name of the variable

value:Object

the object to set the variable to (as converted to an appropriate Python object)

setback to summary
public void set(String name, PyObject value)

Sets a variable in the local namespace.

Parameters
name:String

the name of the variable

value:PyObject

the Python object to set the variable to

setErrback to summary
public void setErr(PyObject outStream)

Sets a Python object to use for the standard output stream, sys.stderr. This stream is used in a byte-oriented way (mostly) that depends on the type of file-like object, in the same way as setOut(PyObject).

Parameters
outStream:PyObject

Python file-like object to use as the error output stream

setErrback to summary
public void setErr(Writer outStream)

Sets a java.io.Writer to use for the standard output stream, sys.stdout. The behaviour as implemented is to output each object o by calling o.toString() and writing this as UTF-16.

Parameters
outStream:Writer

to use as the error output stream

setErrback to summary
public void setErr(OutputStream outStream)
setInback to summary
public void setIn(PyObject inStream)

Sets a Python object to use for the standard input stream, sys.stdin. This stream is used in a byte-oriented way, through calls to read and readline on the object.

Parameters
inStream:PyObject

a Python file-like object to use as the input stream

setInback to summary
public void setIn(Reader inStream)

Sets a Reader to use for the standard input stream, sys.stdin. This stream is wrapped such that characters will be narrowed to bytes. A character greater than U+00FF will raise a Java IllegalArgumentException from within PyString.

Parameters
inStream:Reader

to use as the input stream

setInback to summary
public void setIn(InputStream inStream)

Sets a java.io.InputStream to use for the standard input stream.

Parameters
inStream:InputStream

InputStream to use as input stream

setLocalsback to summary
public void setLocals(PyObject d)
setOutback to summary
public void setOut(PyObject outStream)

Sets a Python object to use for the standard output stream, sys.stdout. This stream is used in a byte-oriented way (mostly) that depends on the type of file-like object. The behaviour as implemented is:

Stream behaviour for various object types
Python type of object o written
str/bytes unicode Any other type
PyFile as bytes directly respect PyFile#encoding call str(o) first
PyFileWriter each byte value as a char write as Java chars call o.toString() first
Other PyObject f invoke f.write(str(o)) invoke f.write(o) invoke f.write(str(o))
Parameters
outStream:PyObject

Python file-like object to use as the output stream

setOutback to summary
public void setOut(Writer outStream)

Sets a java.io.Writer to use for the standard output stream, sys.stdout. The behaviour as implemented is to output each object o by calling o.toString() and writing this as UTF-16.

Parameters
outStream:Writer

to use as the output stream

setOutback to summary
public void setOut(OutputStream outStream)

Sets a java.io.OutputStream to use for the standard output stream.

Parameters
outStream:OutputStream

OutputStream to use as output stream

setSystemStateback to summary
protected void setSystemState()
threadLocalStateInterpreterback to summary
public static PythonInterpreter threadLocalStateInterpreter(PyObject dict)

Creates a new interpreter with the ability to maintain a separate local namespace for each thread (set by invoking setLocals()).

Parameters
dict:PyObject

a Python mapping object (e.g., a dictionary) for use as the default namespace