Package org.python.util
Class PythonInterpreter
java.lang.Object
org.python.util.PythonInterpreter
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
InteractiveInterpreter
The PythonInterpreter class is a standard wrapper for a Jython interpreter for embedding in a
Java application.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates 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) -
Method Summary
Modifier and TypeMethodDescriptionvoidcleanup()voidclose()Compiles a string of Python source as either an expression (if possible) or a module.Evaluates a string as a Python expression and returns the result.Evaluates a Python code object and returns the result.voidExecutes a string of Python source in the local namespace.voidExecutes a Python code object in the local namespace.voidvoidexecfile(InputStream s, String name) voidExecutes a file of Python source in the local namespace.Returns the value of a variable in the local namespace.<T> TReturns the value of a variable in the local namespace.static voidinitialize(Properties preProperties, Properties postProperties, String[] argv) Initializes the Jython runtime.voidSets a variable in the local namespace.voidSets a variable in the local namespace.voidsetErr(OutputStream outStream) voidSets aWriterto use for the standard output stream,sys.stdout.voidSets a Python object to use for the standard output stream,sys.stderr.voidsetIn(InputStream inStream) Sets aInputStreamto use for the standard input stream.voidSets aReaderto use for the standard input stream,sys.stdin.voidSets a Python object to use for the standard input stream,sys.stdin.voidvoidsetOut(OutputStream outStream) Sets aOutputStreamto use for the standard output stream.voidSets aWriterto use for the standard output stream,sys.stdout.voidSets a Python object to use for the standard output stream,sys.stdout.static PythonInterpreterCreates a new interpreter with the ability to maintain a separate local namespace for each thread (set by invoking setLocals()).
-
Constructor Details
-
PythonInterpreter
public PythonInterpreter()Creates a new interpreter with an empty local namespace. -
PythonInterpreter
Creates a new interpreter with a specified local namespace.- Parameters:
dict- a Python mapping object (e.g., a dictionary) for use as the namespace
-
PythonInterpreter
-
-
Method Details
-
initialize
Initializes the Jython runtime. This should only be called once, before any other Python objects (including PythonInterpreter) are created.- Parameters:
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.
-
threadLocalStateInterpreter
Creates a new interpreter with the ability to maintain a separate local namespace for each thread (set by invoking setLocals()).- Parameters:
dict- a Python mapping object (e.g., a dictionary) for use as the default namespace
-
getSystemState
-
setIn
Sets a Python object to use for the standard input stream,sys.stdin. This stream is used in a byte-oriented way, through calls toreadandreadlineon the object.- Parameters:
inStream- a Python file-like object to use as the input stream
-
setIn
Sets aReaderto use for the standard input stream,sys.stdin. This stream is wrapped such that characters will be narrowed to bytes. A character greater thanU+00FFwill raise a JavaIllegalArgumentExceptionfrom withinPyString.- Parameters:
inStream- to use as the input stream
-
setIn
Sets aInputStreamto use for the standard input stream.- Parameters:
inStream- InputStream to use as input stream
-
setOut
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 owrittenstr/bytesunicodeAny other type PyFileas bytes directly respect PyFile.encodingcall str(o)firstPyFileWritereach byte value as a charwrite as Java charscall o.toString()firstOther PyObjectfinvoke f.write(str(o))invoke f.write(o)invoke f.write(str(o))- Parameters:
outStream- Python file-like object to use as the output stream
-
setOut
Sets aWriterto use for the standard output stream,sys.stdout. The behaviour as implemented is to output each objectoby callingo.toString()and writing this as UTF-16.- Parameters:
outStream- to use as the output stream
-
setOut
Sets aOutputStreamto use for the standard output stream.- Parameters:
outStream- OutputStream to use as output stream
-
setErr
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 assetOut(PyObject).- Parameters:
outStream- Python file-like object to use as the error output stream
-
setErr
Sets aWriterto use for the standard output stream,sys.stdout. The behaviour as implemented is to output each objectoby callingo.toString()and writing this as UTF-16.- Parameters:
outStream- to use as the error output stream
-
setErr
-
eval
Evaluates a string as a Python expression and returns the result. -
eval
Evaluates a Python code object and returns the result. -
exec
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. -
exec
Executes a Python code object in the local namespace. -
execfile
Executes a file of Python source in the local namespace. -
execfile
-
execfile
-
compile
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) -
compile
-
compile
-
compile
-
getLocals
-
setLocals
-
set
Sets a variable in the local namespace.- Parameters:
name- the name of the variablevalue- the object to set the variable to (as converted to an appropriate Python object)
-
set
Sets a variable in the local namespace.- Parameters:
name- the name of the variablevalue- the Python object to set the variable to
-
get
Returns 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
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- the name of the variablejavaclass- 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() -
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-