Package org.python.core
Class BytecodeLoader
java.lang.Object
org.python.core.BytecodeLoader
Utility class for loading compiled Python modules and Java classes defined in Python modules.
-
Nested Class Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
fixPyBytecode
(Class<? extends ContainsPyBytecode> c) This method looks for Python-Bytecode stored in String literals.static Class<?>
Turn the Java class file data into a Java class.static Class<?>
Turn the Java class file data into a Java class.static PyCode
Turn the Java class file data for a compiled Python module into aPyCode
object, by constructing an instance of the named class and calling the instance'sPyRunnable.getMain()
.
-
Constructor Details
-
BytecodeLoader
public BytecodeLoader()
-
-
Method Details
-
makeClass
Turn the Java class file data into a Java class.- Parameters:
name
- fully-qualified binary name of the classdata
- a class file as a byte arrayreferents
- super-classes and interfaces that the new class will reference.
-
makeClass
Turn the Java class file data into a Java class.- Parameters:
name
- the name of the classreferents
- super-classes and interfaces that the new class will reference.data
- a class file as a byte array
-
fixPyBytecode
public static void fixPyBytecode(Class<? extends ContainsPyBytecode> c) throws IllegalAccessException, NoSuchFieldException, IOException, ClassNotFoundException This method looks for Python-Bytecode stored in String literals. While Java supports rather long strings, constrained only by int-addressing of arrays, it supports only up to 65535 characters in literals (not sure how escape-sequences are counted). To circumvent this limitation, the code is automatically splitted into several literals with the following naming-scheme. - The marker-interface 'ContainsPyBytecode' indicates that a class contains (static final) literals of the following scheme: - a prefix of '___' indicates a bytecode-containing string literal - a number indicating the number of parts follows - '0_' indicates that no splitting occurred - otherwise another number follows, naming the index of the literal - indexing starts at 0 Examples: ___0_method1 contains bytecode for method1 ___2_0_method2 contains first part of method2's bytecode ___2_1_method2 contains second part of method2's bytecode Note that this approach is provisional. In future, Jython might contain the bytecode directly as bytecode-objects. The current approach was feasible with much less complicated JVM bytecode-manipulation, but needs special treatment after class-loading. -
makeCode
Turn the Java class file data for a compiled Python module into aPyCode
object, by constructing an instance of the named class and calling the instance'sPyRunnable.getMain()
.- Parameters:
name
- fully-qualified binary name of the classdata
- a class file as a byte arrayfilename
- to provide to the constructor of the named class- Returns:
- the
PyCode
object produced by the named class'getMain
-