code_data
code_data
#
Transform Python code objects into data, and vice versa.
- class code_data.CodeData(blocks, filename, first_line_number, name, stacksize, type=None, freevars=(), future_annotations=False, _nested=False, _additional_line=None, _additional_args=())#
The CodeData is a data class which contains the same information to reconstruct the Python CodeType, but is easier to deal with, then the bytecode pieces in there:
A code object is what is seralized on disk as PYC file. It is the lowest abstraction level CPython provides before execution.
This class is meant to a be a data description of a code object, where the types of the attributes can help us understand what the different possible options are.
All recursive code object are translated to code data as well.
Going back and forth to a code data object is gauranteed to be isomporphic, meaning all the data is preserved:
assert CodeData.from_code(code).to_code == code
- Parameters
blocks (Tuple[Tuple[code_data.Instruction, ...], ...]) –
filename (str) –
first_line_number (int) –
name (str) –
stacksize (int) –
type (Optional[code_data.Function]) –
future_annotations (bool) –
_nested (bool) –
_additional_line (Optional[code_data.AdditionalLine]) –
_additional_args (Tuple[Union[code_data.Name, code_data.Varname, code_data.Cellvar, code_data.Constant], ...]) –
- Return type
None
- blocks: Tuple[Tuple[code_data.Instruction, ...], ...]#
- type: Optional[code_data.Function] = None#
- classmethod from_code(code)#
Parse a CodeType into python data structure.
- Parameters
code (types.CodeType) –
- Return type
- to_code()#
Convert the code data type back to code.
- Return type
- classmethod from_json_data(json_data)#
Parse a JSON data structure into a CodeData.
The JSON structure must be of the schema code_data.JSON_SCHEMA
- Parameters
json_data (dict) –
- Return type
- to_json_data()#
Convert the code data to a JSON data structure.
The schema of the returned json is available at code_data.JSON_SCHEMA
- Return type
- normalize()#
Removes all fields from the bytecode that do not effect its semantics, but only its serialization.
This includes things like the order of the co_consts array, the number of extended args for some bytecodes, etc.
- Return type
- all_code_data()#
Return all the code data recursively, including itself.
- Return type
- class code_data.Instruction(name, arg=<factory>, _n_args_override=None, line_number=None, _line_offsets_override=())#
An instruction in the bytecode.
- Parameters
name (str) –
arg (Union[int, code_data.Jump, code_data.Name, code_data.Varname, code_data.Constant, code_data.Freevar, code_data.Cellvar, code_data.NoArg]) –
- Return type
None
- class code_data.Jump(target, relative=False)#
A jump argument.
- class code_data.Name(name, _index_override=None)#
A name argument.
- class code_data.Varname(varname, _index_override=None)#
A varname argument.
- class code_data.Constant(constant, _index_override=None)#
A constant argument.
- Parameters
constant (Union[FrozenSet[Union[FrozenSet[InnerConstant], Tuple[InnerConstant, ...], str, None, bytes, bool, float, int, complex]], Tuple[Union[FrozenSet[InnerConstant], Tuple[InnerConstant, ...], str, None, bytes, bool, float, int, complex], ...], str, None, bytes, bool, float, int, complex, code_data.CodeData]) –
- Return type
None
- constant: Union[FrozenSet[Union[FrozenSet[InnerConstant], Tuple[InnerConstant, ...], str, None, bytes, bool, float, int, complex]], Tuple[Union[FrozenSet[InnerConstant], Tuple[InnerConstant, ...], str, None, bytes, bool, float, int, complex], ...], str, None, bytes, bool, float, int, complex, code_data.CodeData]#
- class code_data.Cellvar(cellvar, _index_override=None)#
A cellvar argument.
- class code_data.NoArg(_arg=0)#
Represents an argument for an opcode with an arg.
It stores the value override, to recreate it byte-for-byte, but this value is unused.
- Parameters
_arg (int) –
- Return type
None
- class code_data.Args(positional_only=(), positional_or_keyword=(), var_positional=None, keyword_only=(), var_keyword=None)#
Holds the different possible args for a function
- Parameters
- Return type
None
- property parameters: collections.OrderedDict[str, inspect._ParameterKind]#
Returns the names of the args, in order, mapping to their kind.
- class code_data.Function(args=<factory>, docstring=None, type=None)#
A block of code in a function.
- Parameters
args (code_data.Args) –
type (Optional[Literal['GENERATOR', 'COROUTINE', 'ASYNC_GENERATOR']]) –
- Return type
None
- args: code_data.Args#
- type: Optional[Literal['GENERATOR', 'COROUTINE', 'ASYNC_GENERATOR']] = None#