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
Return type

None

blocks: Tuple[Tuple[code_data.Instruction, ...], ...]#
filename: str#
first_line_number: int#
name: str#
stacksize: int#
type: Optional[code_data.Function] = None#
freevars: tuple[str, ...] = ()#
future_annotations: bool = False#
classmethod from_code(code)#

Parse a CodeType into python data structure.

Parameters

code (types.CodeType) –

Return type

code_data.CodeData

to_code()#

Convert the code data type back to code.

Return type

types.CodeType

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

code_data.CodeData

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

dict

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

code_data.CodeData

all_code_data()#

Return all the code data recursively, including itself.

Return type

Iterator[code_data.CodeData]

class code_data.Instruction(name, arg=<factory>, _n_args_override=None, line_number=None, _line_offsets_override=())#

An instruction in the bytecode.

Parameters
Return type

None

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]#
line_number: Optional[int] = None#
class code_data.Jump(target, relative=False)#

A jump argument.

Parameters
  • target (int) –

  • relative (bool) –

Return type

None

target: int#
relative: bool = False#
class code_data.Name(name, _index_override=None)#

A name argument.

Parameters
Return type

None

name: str#
class code_data.Varname(varname, _index_override=None)#

A varname argument.

Parameters
Return type

None

varname: str#
class code_data.Constant(constant, _index_override=None)#

A constant argument.

Parameters
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.Freevar(freevar)#

A freevar argument.

Parameters

freevar (str) –

Return type

None

freevar: str#
class code_data.Cellvar(cellvar, _index_override=None)#

A cellvar argument.

Parameters
Return type

None

cellvar: str#
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

positional_only: tuple[str, ...] = ()#
positional_or_keyword: tuple[str, ...] = ()#
var_positional: Optional[str] = None#
keyword_only: tuple[str, ...] = ()#
var_keyword: Optional[str] = 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
Return type

None

args: code_data.Args#
docstring: Optional[str] = None#
type: Optional[Literal['GENERATOR', 'COROUTINE', 'ASYNC_GENERATOR']] = None#
class code_data.AdditionalLine(line, additional_offsets=())#

An additional line of code, that was not used in the instructions

Parameters
Return type

None

line: Optional[int]#
additional_offsets: tuple[int, ...] = ()#