Top Fields Constructors Methods
org.python.compiler

public Class Module

Additional top-level classes in compilation unit: PyIntegerConstant, PyFloatConstant, PyComplexConstant, PyStringConstant, PyUnicodeConstant, PyLongConstant, PyCodeConstant, PyBytecodeConstant.

extends Object
implements Opcodes, ClassConstants, CompilationContext
Class Inheritance
All Implemented Interfaces
org.python.compiler.CompilationContext, org.python.compiler.ClassConstants, org.objectweb.asm.Opcodes
Static Imports
org.python.core.RegistryKey.PYTHON_CPYTHON, org.python.util.CodegenUtils.ci, .CodegenUtils.p, .CodegenUtils.sig

Field Summary

Modifier and TypeField and Description
private static final char[]
pack-priv ClassFile
pack-priv List<PyCodeConstant>
pack-priv Hashtable<Constant, Constant>
constants

The pool of Python Constants

private static String
pack-priv Constant
pack-priv Future
private static String
pack-priv boolean
pack-priv Constant
private static final int
private static final int
pack-priv long
protected Hashtable<String, String>
oversized_methods

Table of oversized methods represented as CPython bytecode.

private static String
pack-priv Hashtable<PythonTree, ScopeInfo>
private int
pack-priv String
private static String
private static final int

Constructor Summary

AccessConstructor and Description
public
Module(String name, String filename, boolean linenumbers)

public
Module(String name, String filename, boolean linenumbers, long mtime)

public
Module(String name)

Method Summary

Modifier and TypeMethod and Description
protected static void
_module_init(mod node, Module module, boolean printResults, CompilerFlags cflags)

public void
pack-priv void
public void
public void
addInit()

This block of code writes out the various standard methods

public void
public void
private static String

Returns:

the string encoding the data
base64encodeToString
(byte[]
to encode
data
)

Implement a simplified base64 encoding compatible with the decoding in BytecodeLoader.

pack-priv Constant
codeConstant(mod tree, String name, boolean fast_locals, String className, boolean classBody, boolean printResults, int firstlineno, ScopeInfo scope, CompilerFlags cflags)

pack-priv Constant
codeConstant(mod tree, String name, boolean fast_locals, String className, Str classDoc, boolean classBody, boolean printResults, int firstlineno, ScopeInfo scope, CompilerFlags cflags)

public static void
compile(mod node, OutputStream ostream, String name, String filename, boolean linenumbers, boolean printResults, CompilerFlags cflags)

public static void
compile(mod
AST of the module to write
node
,
OutputStream
stream onto which to write it
ostream
,
String name, String filename, boolean linenumbers, boolean printResults, CompilerFlags cflags, long mtime)

Create and write a Python module as a Java class file.

pack-priv Constant
complexConstant(double value)

public void
emitNum(Num node, Code code)

public boolean
public void
emitStr(Str node, Code code)

public void
private Constant
pack-priv Constant
floatConstant(double value)

public String
public Future
public ScopeInfo
private static void
insert_code_str_to_classfile(String
of the method or function being generated
name
,
String
Base64 encoded CPython byte code
code_str
,
Module
currently being defined as a class file
module
)

This method stores Base64 encoded Python byte code in one or more String literals.

pack-priv Constant
integerConstant(int value)

private static PyBytecode
loadPyBytecode(String filename, boolean try_cpython)

pack-priv Constant
private static String
pack-priv Constant
pack-priv Constant
public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

base64encback to summary
private static final char[] base64enc

Look-up table for base64encodeToString(byte[]).

classfileback to summary
pack-priv ClassFile classfile
codesback to summary
pack-priv List<PyCodeConstant> codes
constantsback to summary
pack-priv Hashtable<Constant, Constant> constants

The pool of Python Constants

CPYTHON_CMD_MSGback to summary
private static String CPYTHON_CMD_MSG
filenameback to summary
pack-priv Constant filename
futuresback to summary
pack-priv Future futures
LARGE_METHOD_MSGback to summary
private static String LARGE_METHOD_MSG
linenumbersback to summary
pack-priv boolean linenumbers
mainCodeback to summary
pack-priv Constant mainCode
MAX_SETTINGS_PER_SETTERback to summary
private static final int MAX_SETTINGS_PER_SETTER
maxLiteralback to summary
private static final int maxLiteral
mtimeback to summary
pack-priv long mtime
oversized_methodsback to summary
protected Hashtable<String, String> oversized_methods

Table of oversized methods represented as CPython bytecode.

PLEASE_PROVIDE_MSGback to summary
private static String PLEASE_PROVIDE_MSG
scopesback to summary
pack-priv Hashtable<PythonTree, ScopeInfo> scopes
setter_countback to summary
private int setter_count
sfilenameback to summary
pack-priv String sfilename
TRIED_CREATE_PYC_MSGback to summary
private static String TRIED_CREATE_PYC_MSG
USE_SETTERS_LIMITback to summary
private static final int USE_SETTERS_LIMIT

Constructor Detail

Moduleback to summary
public Module(String name, String filename, boolean linenumbers)
Moduleback to summary
public Module(String name, String filename, boolean linenumbers, long mtime)
Moduleback to summary
public Module(String name)

Method Detail

_module_initback to summary
protected static void _module_init(mod node, Module module, boolean printResults, CompilerFlags cflags) throws Exception
addBootstrapback to summary
public void addBootstrap() throws IOException
addConstantsback to summary
pack-priv void addConstants(Code c) throws IOException
addFunctionsback to summary
public void addFunctions() throws IOException
addInitback to summary
public void addInit() throws IOException

This block of code writes out the various standard methods

addMainback to summary
public void addMain() throws IOException
addRunnableback to summary
public void addRunnable() throws IOException
base64encodeToStringback to summary
private static String base64encodeToString(byte[] data)

Implement a simplified base64 encoding compatible with the decoding in BytecodeLoader. This encoder adds no '=' padding or line-breaks. equivalent to binascii.b2a_base64(bytes).rstrip('=\n').

Parameters
data:byte[]

to encode

Returns:String

the string encoding the data

codeConstantback to summary
pack-priv Constant codeConstant(mod tree, String name, boolean fast_locals, String className, boolean classBody, boolean printResults, int firstlineno, ScopeInfo scope, CompilerFlags cflags) throws Exception
codeConstantback to summary
pack-priv Constant codeConstant(mod tree, String name, boolean fast_locals, String className, Str classDoc, boolean classBody, boolean printResults, int firstlineno, ScopeInfo scope, CompilerFlags cflags) throws Exception
compileback to summary
public static void compile(mod node, OutputStream ostream, String name, String filename, boolean linenumbers, boolean printResults, CompilerFlags cflags) throws Exception
compileback to summary
public static void compile(mod node, OutputStream ostream, String name, String filename, boolean linenumbers, boolean printResults, CompilerFlags cflags, long mtime) throws Exception

Create and write a Python module as a Java class file.

Parameters
node:mod

AST of the module to write

ostream:OutputStream

stream onto which to write it

complexConstantback to summary
pack-priv Constant complexConstant(double value)
emitNumback to summary
public void emitNum(Num node, Code code) throws Exception
emitPrimitiveArraySettersback to summary
public boolean emitPrimitiveArraySetters(List<? extends PythonTree> nodes, Code code) throws Exception
emitStrback to summary
public void emitStr(Str node, Code code) throws Exception
errorback to summary
public void error(String msg, boolean err, PythonTree node) throws Exception

Implements org.python.compiler.CompilationContext.error.

Annotations
@Override
findConstantback to summary
private Constant findConstant(Constant c)
floatConstantback to summary
pack-priv Constant floatConstant(double value)
getFilenameback to summary
public String getFilename()

Implements org.python.compiler.CompilationContext.getFilename.

Annotations
@Override
getFuturesback to summary
public Future getFutures()

Implements org.python.compiler.CompilationContext.getFutures.

Annotations
@Override
getScopeInfoback to summary
public ScopeInfo getScopeInfo(PythonTree node)

Implements org.python.compiler.CompilationContext.getScopeInfo.

Annotations
@Override
insert_code_str_to_classfileback to summary
private static void insert_code_str_to_classfile(String name, String code_str, Module module) throws IOException

This method stores Base64 encoded Python byte code in one or more String literals.

While Java String objects are limited only by the address range of arrays, the class file standard only supports literals representable in at most 65535 bytes of modified UTF-8. This method us used only with base64 Strings (therefore ASCII without nulls) and so each character occupies exactly 1 byte in the class file after encoding to UTF-8.

To work within the 65535 byte limitation, the code_str is split 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 far less complicated JVM bytecode-manipulation, but needs special treatment after class-loading.
Parameters
name:String

of the method or function being generated

code_str:String

Base64 encoded CPython byte code

module:Module

currently being defined as a class file

integerConstantback to summary
pack-priv Constant integerConstant(int value)
loadPyBytecodeback to summary
private static PyBytecode loadPyBytecode(String filename, boolean try_cpython) throws RuntimeException
longConstantback to summary
pack-priv Constant longConstant(String value)
serializePyBytecodeback to summary
private static String serializePyBytecode(PyBytecode btcode) throws IOException
stringConstantback to summary
pack-priv Constant stringConstant(String value)
unicodeConstantback to summary
pack-priv Constant unicodeConstant(String value)
writeback to summary
public void write(OutputStream stream) throws IOException