public class Jep
extends java.lang.Object
implements java.lang.AutoCloseable
Embeds CPython in Java. Each Jep provides access to a Python interpreter and
maintains an independent global namespace for Python variables. Values can be
passed from Java to Python using the various set() methods. Various methods,
such as eval(String)
and invoke(String, Object...)
can be
used to execute Python code. Python variables can be accessed using
getValue(String)
.
In general, methods called on a Jep instance must be called from the same thread that created the instance. To maintain stability, avoid having two Jep instances running on the same thread at the same time. Instead provide different threads or close() one before instantiating another on the same thread. Jep instances should always be closed when no longer needed to prevent memory leaks.
Constructor and Description |
---|
Jep()
Creates a new
Jep instance and its associated
sub-interpreter. |
Jep(boolean interactive)
Deprecated.
Please use
Jep(JepConfig) instead. |
Jep(boolean interactive,
java.lang.String includePath)
Deprecated.
Please use
Jep(JepConfig) instead. |
Jep(boolean interactive,
java.lang.String includePath,
java.lang.ClassLoader cl)
Deprecated.
Please use
Jep(JepConfig) instead. |
Jep(boolean interactive,
java.lang.String includePath,
java.lang.ClassLoader cl,
ClassEnquirer ce)
Deprecated.
Please use
Jep(JepConfig) instead. |
Jep(JepConfig config) |
Modifier and Type | Method and Description |
---|---|
void |
close()
Shuts down the Python sub-interpreter.
|
PyModule |
createModule(java.lang.String name)
Deprecated.
This will be removed in a future release.
|
boolean |
eval(java.lang.String str)
Evaluate Python statements.
|
MemoryManager |
getMemoryManager()
Gets the memory manager associated with this Jep instance.
|
byte[] |
getValue_bytearray(java.lang.String str)
Retrieves a Python string object as a Java byte[].
|
java.lang.Object |
getValue(java.lang.String str)
Retrieves a value from this Python sub-interpreter.
|
<T> T |
getValue(java.lang.String str,
java.lang.Class<T> clazz)
Like
getValue(String) but allows specifying the return type. |
java.lang.Object |
invoke(java.lang.String name,
java.util.Map<java.lang.String,java.lang.Object> kwargs)
Invokes a Python function.
|
java.lang.Object |
invoke(java.lang.String name,
java.lang.Object... args)
Invokes a Python function.
|
java.lang.Object |
invoke(java.lang.String name,
java.lang.Object[] args,
java.util.Map<java.lang.String,java.lang.Object> kwargs)
Invokes a Python function.
|
boolean |
isInteractive()
Gets whether or not this sub-interpreter is interactive.
|
void |
isValidThread()
Checks if the current thread is valid for the method call.
|
void |
runScript(java.lang.String script)
Runs a Python script.
|
void |
runScript(java.lang.String script,
java.lang.ClassLoader cl)
Runs a Python script.
|
void |
set(java.lang.String name,
boolean v)
Sets the Java boolean into the sub-interpreter's global scope with the
specified variable name.
|
void |
set(java.lang.String name,
boolean[] v)
Sets the Java boolean[] into the sub-interpreter's global scope with the
specified variable name.
|
void |
set(java.lang.String name,
byte b)
Sets the Java byte into the sub-interpreter's global scope with the
specified variable name.
|
void |
set(java.lang.String name,
byte[] v)
Sets the Java byte[] into the sub-interpreter's global scope with the
specified variable name.
|
void |
set(java.lang.String name,
char v)
Sets the Java char into the sub-interpreter's global scope with the
specified variable name.
|
void |
set(java.lang.String name,
char[] v)
Sets the Java char[] into the sub-interpreter's global scope with the
specified variable name.
|
void |
set(java.lang.String name,
double v)
Sets the Java double into the sub-interpreter's global scope with the
specified variable name.
|
void |
set(java.lang.String name,
double[] v)
Sets the Java double[] into the sub-interpreter's global scope with the
specified variable name.
|
void |
set(java.lang.String name,
float v)
Sets the Java float into the sub-interpreter's global scope with the
specified variable name.
|
void |
set(java.lang.String name,
float[] v)
Sets the Java float[] into the sub-interpreter's global scope with the
specified variable name.
|
void |
set(java.lang.String name,
int v)
Sets the Java int into the sub-interpreter's global scope with the
specified variable name.
|
void |
set(java.lang.String name,
int[] v)
Sets the Java int[] into the sub-interpreter's global scope with the
specified variable name.
|
void |
set(java.lang.String name,
long v)
Sets the Java long into the sub-interpreter's global scope with the
specified variable name.
|
void |
set(java.lang.String name,
long[] v)
Sets the Java long[] into the sub-interpreter's global scope with the
specified variable name.
|
void |
set(java.lang.String name,
java.lang.Object v)
Sets the Java Object into the sub-interpreter's global scope with the
specified variable name.
|
void |
set(java.lang.String name,
short v)
Sets the Java short into the sub-interpreter's global scope with the
specified variable name.
|
void |
set(java.lang.String name,
short[] v)
Sets the Java short[] into the sub-interpreter's global scope with the
specified variable name.
|
void |
set(java.lang.String name,
java.lang.String v)
Sets the Java String into the sub-interpreter's global scope with the
specified variable name.
|
void |
setClassLoader(java.lang.ClassLoader cl)
Sets the default classloader.
|
static void |
setInitParams(PyConfig config)
Deprecated.
Please use
MainInterpreter.setInitParams(PyConfig)
instead. |
void |
setInteractive(boolean v)
Changes behavior of
eval(String) . |
static void |
setSharedModulesArgv(java.lang.String... argv)
Deprecated.
Please use
MainInterpreter.setSharedModulesArgv(String...)
instead. |
public Jep() throws JepException
Jep
instance and its associated
sub-interpreter.JepException
- if an error occurs@Deprecated public Jep(boolean interactive) throws JepException
Jep(JepConfig)
instead.Jep
instance and its associated
sub-interpreter.interactive
- whether eval(String)
should support the slower
behavior of potentially waiting for multiple statementsJepException
- if an error occurs@Deprecated public Jep(boolean interactive, java.lang.String includePath) throws JepException
Jep(JepConfig)
instead.Jep
instance and its associated
sub-interpreter.interactive
- whether eval(String)
should support the slower
behavior of potentially waiting for multiple statementsincludePath
- a path of directories separated by File.pathSeparator that
will be appended to the sub-intepreter's sys.path
JepException
- if an error occurs@Deprecated public Jep(boolean interactive, java.lang.String includePath, java.lang.ClassLoader cl) throws JepException
Jep(JepConfig)
instead.Jep
instance and its associated
sub-interpreter.interactive
- whether eval(String)
should support the slower
behavior of potentially waiting for multiple statementsincludePath
- a path of directories separated by File.pathSeparator that
will be appended to the sub-intepreter's sys.path
cl
- the ClassLoader to use when importing Java classes from PythonJepException
- if an error occurs@Deprecated public Jep(boolean interactive, java.lang.String includePath, java.lang.ClassLoader cl, ClassEnquirer ce) throws JepException
Jep(JepConfig)
instead.Jep
instance and its associated
sub-interpreter.interactive
- whether eval(String)
should support the slower
behavior of potentially waiting for multiple statementsincludePath
- a path of directories separated by File.pathSeparator that
will be appended to the sub-intepreter's sys.path
cl
- the ClassLoader to use when importing Java classes from Pythonce
- a ClassEnquirer
to determine which imports are
Python vs Java, or null for the default ClassList
JepException
- if an error occurspublic Jep(JepConfig config) throws JepException
JepException
@Deprecated public static void setInitParams(PyConfig config) throws JepException
MainInterpreter.setInitParams(PyConfig)
instead.config
- the python configuration to use.JepException
- if an error occurs@Deprecated public static void setSharedModulesArgv(java.lang.String... argv) throws JepException
MainInterpreter.setSharedModulesArgv(String...)
instead.argv
- the arguments to be set on Python's sys.argv for the top/main
interpreterJepException
- if an error occurspublic void isValidThread() throws JepException
JepException
- if an error occurspublic void runScript(java.lang.String script) throws JepException
script
- a String
absolute path to script file.JepException
- if an error occurspublic void runScript(java.lang.String script, java.lang.ClassLoader cl) throws JepException
script
- a String
absolute path to script file.cl
- a ClassLoader
value, may be null.JepException
- if an error occurspublic java.lang.Object invoke(java.lang.String name, java.lang.Object... args) throws JepException
name
- a Python function name in globals dict or the name of a global
object and method using dot notation.args
- args to pass to the function in orderObject
valueJepException
- if an error occurspublic java.lang.Object invoke(java.lang.String name, java.util.Map<java.lang.String,java.lang.Object> kwargs) throws JepException
name
- a Python function name in globals dict or the name of a global
object and method using dot notation.kwargs
- a Map of keyword argsObject
valueJepException
- if an error occurspublic java.lang.Object invoke(java.lang.String name, java.lang.Object[] args, java.util.Map<java.lang.String,java.lang.Object> kwargs) throws JepException
name
- must be a valid Python function name in globals dictargs
- args to pass to the function in orderkwargs
- a Map of keyword argsObject
valueJepException
- if an error occurspublic boolean eval(java.lang.String str) throws JepException
Evaluate Python statements.
In interactive mode, Jep may not immediately execute the given lines of code. In that case, eval() returns false and the statement is stored and is appended to the next incoming string.
If you're running an unknown number of statements, finish with
eval(null)
to flush the statement buffer.
Interactive mode is slower than a straight eval call since it has to compile the code strings to detect the end of the block. Non-interactive mode is faster, but code blocks must be complete. For example:
interactive mode == false
jep.eval("if(Test):\n print('Hello world')");
interactive mode == true
jep.eval("if(Test):");
jep.eval(" print('Hello world')");
jep.eval(null);
Also, Python does not readily return object values from eval(). Use
getValue(String)
instead.
str
- a String
statement to evalJepException
- if an error occurspublic java.lang.Object getValue(java.lang.String str) throws JepException
Retrieves a value from this Python sub-interpreter. Supports retrieving:
For Python containers, such as lists and dictionaries, getValue will recursively move through the container and convert each item. If the type of the value retrieved is not supported, Jep will fall back to returning a String representation of the object. This fallback behavior will probably change in the future and should not be relied upon.
str
- the name of the Python variable to get from the interpreter's
global scopeObject
valueJepException
- if an error occurspublic <T> T getValue(java.lang.String str, java.lang.Class<T> clazz) throws JepException
getValue(String)
but allows specifying the return type. If
Jep cannot convert the variable to the specified type then a JepException
is thrown. This can be used to safely ensure that the return value is an
expected type. The following table describes what conversions are
currently possible.
Python Class | Java Classes | Notes |
---|---|---|
str/unicode | String , Character |
Character conversion will fail if the str is longer than 1. |
bool | Boolean |
|
int/long | Long , Integer , Short , Byte |
Conversion fails if the number is outside the valid range for the Java type |
float | Double , Float |
|
list, tuple | List , array |
When a tuple is converted to a List it is unmodifiable. |
dict | Map |
|
function, method | Any FunctionalInterface | |
numpy.ndarray | NDArray |
Only if Jep was built with numpy support |
NoneType | Any(null) | |
Jep objects such as PyJObjects and jarrays will be returned if the Java type of the wrapped object is compatible. | ||
Anything else | String , PyObject |
String conversion will likely be removed in future versions of Jep so it is unsafe to depend on this behavior. |
T
- the generic type of the return typestr
- the name of the Python variable to get from the interpreter's
global scopeclazz
- the Java class of the return type.JepException
- if an error occurspublic byte[] getValue_bytearray(java.lang.String str) throws JepException
str
- the name of the Python variable to get from the
sub-interpreter's global scopeObject
arrayJepException
- if an error occurs@Deprecated public PyModule createModule(java.lang.String name) throws JepException
name
- a String
valuePyModule
valueJepException
- if an error occurspublic void setClassLoader(java.lang.ClassLoader cl)
cl
- a ClassLoader
valuepublic void setInteractive(boolean v)
eval(String)
. Interactive mode can wait for
further Python statements to be evaled, while non-interactive mode can
only execute complete Python statements.v
- if the sub-interpreter should run in interactive modepublic boolean isInteractive()
public void set(java.lang.String name, java.lang.Object v) throws JepException
name
- the Python name for the variablev
- an Object
valueJepException
- if an error occurspublic void set(java.lang.String name, java.lang.String v) throws JepException
name
- the Python name for the variablev
- a String
valueJepException
- if an error occurspublic void set(java.lang.String name, boolean v) throws JepException
name
- the Python name for the variablev
- a boolean
valueJepException
- if an error occurspublic void set(java.lang.String name, int v) throws JepException
name
- the Python name for the variablev
- an int
valueJepException
- if an error occurspublic void set(java.lang.String name, short v) throws JepException
name
- the Python name for the variablev
- an int
valueJepException
- if an error occurspublic void set(java.lang.String name, char[] v) throws JepException
name
- the Python name for the variablev
- a char[]
valueJepException
- if an error occurspublic void set(java.lang.String name, char v) throws JepException
name
- the Python name for the variablev
- a char
valueJepException
- if an error occurspublic void set(java.lang.String name, byte b) throws JepException
name
- the Python name for the variableb
- a byte
valueJepException
- if an error occurspublic void set(java.lang.String name, long v) throws JepException
name
- the Python name for the variablev
- a long
valueJepException
- if an error occurspublic void set(java.lang.String name, double v) throws JepException
name
- the Python name for the variablev
- a double
valueJepException
- if an error occurspublic void set(java.lang.String name, float v) throws JepException
name
- the Python name for the variablev
- a float
valueJepException
- if an error occurspublic void set(java.lang.String name, boolean[] v) throws JepException
name
- the Python name for the variablev
- a boolean[]
valueJepException
- if an error occurspublic void set(java.lang.String name, int[] v) throws JepException
name
- the Python name for the variablev
- an int[]
valueJepException
- if an error occurspublic void set(java.lang.String name, short[] v) throws JepException
name
- the Python name for the variablev
- a short[]
valueJepException
- if an error occurspublic void set(java.lang.String name, byte[] v) throws JepException
name
- the Python name for the variablev
- a byte[]
valueJepException
- if an error occurspublic void set(java.lang.String name, long[] v) throws JepException
name
- the Python name for the variablev
- a long[]
valueJepException
- if an error occurspublic void set(java.lang.String name, double[] v) throws JepException
name
- the Python name for the variablev
- a double[]
valueJepException
- if an error occurspublic void set(java.lang.String name, float[] v) throws JepException
name
- the Python name for the variablev
- a float[]
valueJepException
- if an error occurspublic MemoryManager getMemoryManager()
public void close() throws JepException
close
in interface java.lang.AutoCloseable
JepException