equip.bytecode package¶
Submodules¶
equip.bytecode.code¶
Parsing and representation of the supplied bytecode.
copyright: |
|
---|---|
license: | Apache 2, see LICENSE for more details. |
-
class
equip.bytecode.code.
BytecodeObject
(pyc_file, lazy_load=True)[source]¶ Bases:
object
This class parses the bytecode from a file and constructs the representation from it. The result is:
- One module (type:
ModuleDeclaration
) - The bytecode expanded into intelligible structure.
- Construction of nested declarations, and hierarchy of declaration types.
-
accept
(visitor)[source]¶ Runs the visitor over the nested declarations found in the this module, or the entire bytecode if it’s a BytecodeVisitor.
-
add_enter_code
(python_code, import_code=None)[source]¶ Adds enter callback in the module. The callback code (both
import_code
andpython_code
) is wrapped in a main test if statement:if __name__ == '__main__': import_code python_code
Parameters: - python_code – Python code to inject before the module gets executed (if it’s executed under main). The code is not executed if it’s not under main.
- import_code – Python code that contains the import statements that might be required
by the injected
python_code
. Defaults to None.
-
add_exit_code
(python_code, import_code=None)[source]¶ Adds exit callback in the module. The callback code (both
import_code
andpython_code
) is wrapped in a main test if statement:if __name__ == '__main__': import_code python_code
Parameters: - python_code – Python code to inject after the module gets executed (if it’s executed under main). The code is not executed if it’s not under main.
- import_code – Python code that contains the import statements that might be required
by the injected
python_code
. Defaults to None.
-
build_representation
()[source]¶ Builds the internal representation of declarations and how they relate to each other. It works by creating a map of type/method declaration indices, and then associate the bytecode for each of them.
When all declarations are created, the parenting process runs and creates the tree structure of the decalrations, such as:
ModuleDeclaration() - TypeDeclaration(name='SomeClass') - MethodDeclaration#lineno(name='methodOfSomeClass') - MethodDeclaration#lineno(name='otherMethodOfSomeClass')
This representation is required to run the visitors.
-
static
build_tree
(root, indent='')[source]¶ Returns a string that represents the tree of
Declaration
types.
-
declarations
¶ Returns a set of all the declarations found in the current bytecode.
-
static
find_classes_methods
(bytecode)[source]¶ Finds the indices of the classes and methods declared in the bytecode. This is done by matching code_object of the declaration and the
MAKE_FUNCTION
orBUILD_CLASS
opcode.
-
get_decl
(code_object=None, method_name=None, type_name=None)[source]¶ Returns the declaration associated to the code_object
co
, or supplied name.Warning: This is only valid until the rewriter is called on the declarations.
Parameters: - code_object – Python code object type
- method_name – Name of the method.
- type_name – Name of the type.
-
static
get_formal_params
(code_object)[source]¶ Returns the ordered list of formal parameters (arguments) of a method.
Parameters: code_object – The code object of the method.
-
static
get_imports_from_bytecode
(code_object, bytecode)[source]¶ Parses the import statements from the bytecode and constructs a list of
ImportDeclaration
.
-
static
get_last_import_ref
(bytecode, code_object)[source]¶ Find the last reference of an import statement in the bytecode.
-
has_changes
¶ Returns True if any change was performed on the module. This is used to know if we need to rewrite or not a pyc file.
-
parse
()[source]¶ Parses the binary file (pyc) and extract the bytecode out of it. Keeps the magic number as well as the timestamp for serialization.
-
static
parse_code_object
(code_object, bytecode)[source]¶ Parses the bytecode (
co_code
field of the code object) and dereferences theoparg
for later analysis.Parameters: - code_object – The code object containing the bytecode to analyze
- bytecode – The list that will be used to append the expanded bytecode sequences.
- One module (type:
equip.bytecode.decl¶
Structured representation of Module, Types, Method, Imports.
copyright: |
|
---|---|
license: | Apache 2, see LICENSE for more details. |
-
class
equip.bytecode.decl.
Declaration
(kind, _code_object)[source]¶ Bases:
object
Base class for the declaration types of object.
-
FIELD
= 4¶
-
IMPORT
= 5¶
-
METHOD
= 3¶
-
MODULE
= 1¶
-
TYPE
= 2¶
-
add_child
(child)[source]¶ Adds a child to this declaration.
Parameters: child – A Declaration
that is a child of the current declaration.
-
bytecode
¶ Returns the bytecode associated with this declaration.
-
bytecode_object
¶
-
children
¶ Returns the children of this declaration.
-
code_object
¶
-
end_lineno
¶ Returns the end line number of the declaration.
-
has_changes
¶
-
is_field
()¶
-
is_import
()¶
-
is_method
()¶
-
is_module
()¶
-
is_type
()¶
-
kind
¶
-
lines
¶ A tuple of start/end line numbers that encapsulates this declaration.
-
parent
¶ Returns the parent of this declaration or
None
if there is no parent (e.g., for aModuleDeclaration
).
-
parent_class
¶ Returns the parent class (a
TypeDeclaration
) for this declaration.
-
parent_method
¶ Returns the parent method (a
MethodDeclaration
) for this declaration.
-
parent_module
¶ Returns the parent module (a
ModuleDeclaration
) for this declaration.
-
start_lineno
¶ Returns the start line number of the declaration.
-
-
class
equip.bytecode.decl.
FieldDeclaration
(field_name, code_object)[source]¶ Bases:
equip.bytecode.decl.Declaration
-
field_name
¶
-
-
class
equip.bytecode.decl.
ImportDeclaration
(code_object)[source]¶ Bases:
equip.bytecode.decl.Declaration
Models an import statement. It handles relatives/absolute imports, as well as aliases.
-
aliases
¶
-
dots
¶
-
live_names
¶
-
root
¶
-
star
¶
-
-
class
equip.bytecode.decl.
MethodDeclaration
(method_name, code_object)[source]¶ Bases:
equip.bytecode.decl.Declaration
The declaration of a method or a function.
-
body
¶
-
formal_parameters
¶
-
is_lambda
¶
-
labels
¶
-
method_name
¶
-
nested_types
¶
-
-
class
equip.bytecode.decl.
ModuleDeclaration
(module_path, code_object)[source]¶ Bases:
equip.bytecode.decl.Declaration
The module is the object that captures everything under one pyc file. It contains nested classes and functions, as well as import statements.
-
classes
¶
-
functions
¶
-
imports
¶
-
module_path
¶
-
-
class
equip.bytecode.decl.
TypeDeclaration
(type_name, code_object)[source]¶ Bases:
equip.bytecode.decl.Declaration
Represent a class declaration. It has a name, as well as a hierarchy (superclass). The type contains several methods and fields, and can have nested types.
-
fields
¶
-
methods
¶ Returns a list of
MethodDeclaration
that belong to this type.
-
nested_types
¶ Returns a list of
TypeDeclaration
that belong to this type.
-
superclasses
¶
-
type_name
¶ Returns the name of the type.
-
equip.bytecode.utils¶
Utilities for bytecode interaction.
copyright: |
|
---|---|
license: | Apache 2, see LICENSE for more details. |