public final class Jep
extends java.lang.Object
implements java.io.Closeable
Embeds CPython in Java. Each Jep instance can be considered a Python sub-intepreter, mostly sandboxed from other Jep instances. However, it is not guaranteed to be completely sandboxed, as one sub-interpreter may be able to affect another when using CPython extensions or referencing the same Java objects in different sub-interpreters.
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)
Creates a new
Jep instance and its associated
sub-interpreter. |
Jep(boolean interactive,
java.lang.String includePath)
Creates a new
Jep instance and its associated
sub-interpreter. |
Jep(boolean interactive,
java.lang.String includePath,
java.lang.ClassLoader cl)
Creates a new
Jep instance and its associated
sub-interpreter. |
Jep(boolean interactive,
java.lang.String includePath,
java.lang.ClassLoader cl,
ClassEnquirer ce)
Creates a new
Jep instance and its associated
sub-interpreter. |
Jep(JepConfig config) |
Modifier and Type | Method and Description |
---|---|
void |
close()
Shuts down the Python sub-interpreter.
|
jep.python.PyModule |
createModule(java.lang.String name)
Create a Python module on the interpreter.
|
boolean |
eval(java.lang.String str)
Evaluate Python statements.
|
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.
|
java.lang.Object |
invoke(java.lang.String name,
java.lang.Object... args)
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)
Sets interpreter settings for the top Python interpreter.
|
void |
setInteractive(boolean v)
Changes behavior of
eval(String) . |
static void |
setSharedModulesArgv(java.lang.String... argv)
Sets the sys.argv values on the top interpreter.
|
jep.python.PyObject |
trackObject(jep.python.PyObject obj)
Track Python objects we create so they can be smoothly shutdown with no
risk of crashes due to bad reference counting.
|
jep.python.PyObject |
trackObject(jep.python.PyObject obj,
boolean inc)
Track Python objects we create so they can be smoothly shutdown with no
risk of crashes due to bad reference counting.
|
public Jep() throws JepException
Jep
instance and its associated
sub-interpreter.JepException
- if an error occurspublic Jep(boolean interactive) throws JepException
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 occurspublic Jep(boolean interactive, java.lang.String includePath) throws JepException
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 occurspublic Jep(boolean interactive, java.lang.String includePath, java.lang.ClassLoader cl) throws JepException
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 occurspublic Jep(boolean interactive, java.lang.String includePath, java.lang.ClassLoader cl, ClassEnquirer ce) throws JepException
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
public static void setInitParams(PyConfig config) throws JepException
config
- the python configuration to use.JepException
public static void setSharedModulesArgv(java.lang.String... argv) throws JepException
argv
- the arguments to be set on Python's sys.argv for the top/main
interpreterJepException
public 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
- must be a valid Python function name in globals dictargs
- args to pass to the function in orderObject
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.
Python is pretty picky about what it accepts here. The general syntax
jep.eval("a = 5");
String a = (String) jep.getValue("a");
will work.
str
- the name of the Python variable to get from the
sub-interpreter's global scopeObject
valueJepException
- 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 occurspublic jep.python.PyObject trackObject(jep.python.PyObject obj) throws JepException
obj
- a PyObject
valueJepException
- if an error occurspublic jep.python.PyObject trackObject(jep.python.PyObject obj, boolean inc) throws JepException
obj
- a PyObject
valueinc
- should trackObject incref()JepException
- if an error occurspublic jep.python.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 void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable