public class PythonInterpreter
extends java.lang.Object
implements java.lang.AutoCloseable, java.io.Closeable
Constructor and Description |
---|
PythonInterpreter()
Creates a new interpreter with an empty local namespace.
|
PythonInterpreter(PyObject dict)
Creates a new interpreter with a specified local namespace.
|
PythonInterpreter(PyObject dict,
PySystemState systemState) |
Modifier and Type | Method and Description |
---|---|
void |
cleanup() |
void |
close() |
PyCode |
compile(java.io.Reader reader) |
PyCode |
compile(java.io.Reader reader,
java.lang.String filename) |
PyCode |
compile(java.lang.String script)
Compiles a string of Python source as either an expression (if possible) or a module.
|
PyCode |
compile(java.lang.String script,
java.lang.String filename) |
PyObject |
eval(PyObject code)
Evaluates a Python code object and returns the result.
|
PyObject |
eval(java.lang.String s)
Evaluates a string as a Python expression and returns the result.
|
void |
exec(PyObject code)
Executes a Python code object in the local namespace.
|
void |
exec(java.lang.String s)
Executes 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)
Executes a file of Python source in the local namespace.
|
PyObject |
get(java.lang.String name)
Returns the value of a variable in the local namespace.
|
<T> T |
get(java.lang.String name,
java.lang.Class<T> javaclass)
Returns the value of a variable in the local namespace.
|
PyObject |
getLocals() |
PySystemState |
getSystemState() |
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)
Sets a variable in the local namespace.
|
void |
set(java.lang.String name,
PyObject value)
Sets a variable in the local namespace.
|
void |
setErr(java.io.OutputStream outStream) |
void |
setErr(PyObject outStream)
Sets a Python object to use for the standard output stream,
sys.stderr . |
void |
setErr(java.io.Writer outStream)
Sets a
Writer to use for the standard output stream, sys.stdout . |
void |
setIn(java.io.InputStream inStream)
Sets a
InputStream to use for the standard input stream. |
void |
setIn(PyObject inStream)
Sets a Python object to use for the standard input stream,
sys.stdin . |
void |
setIn(java.io.Reader inStream)
Sets a
Reader to use for the standard input stream, sys.stdin . |
void |
setLocals(PyObject d) |
void |
setOut(java.io.OutputStream outStream)
Sets a
OutputStream to use for the standard output stream. |
void |
setOut(PyObject outStream)
Sets a Python object to use for the standard output stream,
sys.stdout . |
void |
setOut(java.io.Writer outStream)
Sets a
Writer to use for the standard output stream, sys.stdout . |
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()).
|
public PythonInterpreter()
public PythonInterpreter(PyObject dict)
dict
- a Python mapping object (e.g., a dictionary) for use as the namespacepublic PythonInterpreter(PyObject dict, PySystemState systemState)
public static void initialize(java.util.Properties preProperties, java.util.Properties postProperties, java.lang.String[] argv)
preProperties
- A set of properties. Typically System.getProperties() is used.
preProperties override properties from the registry file.postProperties
- 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
- Command line arguments, assigned to sys.argv.public static PythonInterpreter threadLocalStateInterpreter(PyObject dict)
dict
- a Python mapping object (e.g., a dictionary) for use as the default namespacepublic PySystemState getSystemState()
public void setIn(PyObject inStream)
sys.stdin
. This
stream is used in a byte-oriented way, through calls to read
and
readline
on the object.inStream
- a Python file-like object to use as the input streampublic void setIn(java.io.Reader inStream)
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
.inStream
- to use as the input streampublic void setIn(java.io.InputStream inStream)
InputStream
to use for the standard input stream.inStream
- InputStream to use as input streampublic void setOut(PyObject outStream)
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:
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 char s |
call o.toString() first |
Other PyObject f |
invoke f.write(str(o)) |
invoke f.write(o) |
invoke f.write(str(o)) |
outStream
- Python file-like object to use as the output streampublic void setOut(java.io.Writer outStream)
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.outStream
- to use as the output streampublic void setOut(java.io.OutputStream outStream)
OutputStream
to use for the standard output stream.outStream
- OutputStream to use as output streampublic void setErr(PyObject outStream)
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)
.outStream
- Python file-like object to use as the error output streampublic void setErr(java.io.Writer outStream)
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.outStream
- to use as the error output streampublic void setErr(java.io.OutputStream outStream)
public PyObject eval(java.lang.String s)
public void exec(java.lang.String s)
public void exec(PyObject code)
public void execfile(java.lang.String filename)
public void execfile(java.io.InputStream s)
public void execfile(java.io.InputStream s, java.lang.String name)
public PyCode compile(java.lang.String script)
public PyCode compile(java.io.Reader reader)
public PyCode compile(java.lang.String script, java.lang.String filename)
public PyCode compile(java.io.Reader reader, java.lang.String filename)
public PyObject getLocals()
public void setLocals(PyObject d)
public void set(java.lang.String name, java.lang.Object value)
name
- the name of the variablevalue
- the object to set the variable to (as converted to an appropriate Python object)public void set(java.lang.String name, PyObject value)
name
- the name of the variablevalue
- the Python object to set the variable topublic PyObject get(java.lang.String name)
name
- the name of the variablepublic <T> T get(java.lang.String name, java.lang.Class<T> javaclass)
interp.get("foo", Object.class)
will return the most appropriate generic Java
object.name
- the name of the variablejavaclass
- the class of object to returnpublic void cleanup()
public void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable