module documentation

Utilities used in vm.py.

Class ClassMatch Undocumented
Class FinallyStateTracker Track return state for try/except/finally blocks.
Class FindIgnoredTypeComments A visitor that finds type comments that will be ignored.
Function adjust_block_returns Adjust line numbers for return statements in with blocks.
Function build_function_args_tuple Undocumented
Function build_map_unpack Merge a list of kw dicts into a single dict.
Function call_binary_operator Map a binary operator to "magic methods" (__add__ etc.).
Function call_inplace_operator Try to call a method like __iadd__, possibly fall back to __add__.
Function check_for_deleted Undocumented
Function copy_dict_without_keys Create a copy of the input dict with some keys deleted.
Function ensure_unpacked_starargs Unpack starargs if it has not been done already.
Function get_closure_var_name Undocumented
Function get_name_error_details Gets a detailed error message for [name-error].
Function jump_if Implementation of various _JUMP_IF bytecodes.
Function load_closure_cell Retrieve the value out of a closure cell.
Function log_opcode Write a multi-line log message, including backtrace and stack.
Function make_class Create a class with the name, bases and methods given.
Function make_function Create a function or closure given the arguments.
Function match_class Pick attributes out of a class instance for pattern matching.
Function match_keys Pick values out of a mapping for pattern matching.
Function match_mapping See if var is a map for pattern matching.
Function match_sequence See if var is a sequence for pattern matching.
Function merge_indefinite_iterables Undocumented
Function pop_and_unpack_list Pop count iterables off the stack and concatenate.
Function process_function_type_comment Modifies annotations from a function type comment.
Function push_block Undocumented
Function to_coroutine Convert any awaitables and generators in obj to coroutines.
Function unpack_and_build Undocumented
Function unpack_iterable Unpack an iterable.
Function update_excluded_types Update the excluded_types attribute of functions in the current frame.
Variable log Undocumented
Class _Block Undocumented
Class _NameErrorDetails Base class for detailed name error messages.
Class _NameInInnerClassErrorDetails Undocumented
Class _NameInOuterClassErrorDetails Name error details for a name defined in an outer class.
Class _NameInOuterFunctionErrorDetails Name error details for a name defined in an outer function.
Function _base Undocumented
Function _binding_to_coroutine Helper for _to_coroutine.
Function _call_binop_on_bindings Call a binary operator on two cfg.Binding objects.
Function _check_defaults Check parameter defaults against annotations.
Function _check_final_members Check if the new class overrides a final attribute or method.
Function _convert_keys Undocumented
Function _expand_generic_protocols Expand Protocol[T, ...] to Protocol, Generic[T, ...].
Function _filter_out_metaclasses Process the temporary classes created by six.with_metaclass.
Function _get_annotation Extract an annotation from terms in `a | b | ...`.
Function _get_scopes Gets the class or function objects for a sequence of nested scope names.
Function _maybe_union Attempt to evaluate a '|' operation as a typing.Union.
Function _merge_tuple_bindings Merge a set of heterogeneous tuples from var's bindings.
Function _module_name Undocumented
Function _overrides Check whether subcls_var overrides or newly defines the given attribute.
Function _process_base_class Process a base class for InterpreterClass creation.
Function _var_is_fixed_length_tuple Undocumented
Function _var_maybe_unknown Undocumented
Constant _FUNCTION_TYPE_COMMENT_RE Undocumented
Constant _TRUNCATE Undocumented
Constant _TRUNCATE_STR Undocumented
Variable _repr_obj Undocumented
def adjust_block_returns(code, block_returns): (source)

Adjust line numbers for return statements in with blocks.

def build_function_args_tuple(node, seq, ctx): (source)

Undocumented

def build_map_unpack(state, arg_list, ctx): (source)

Merge a list of kw dicts into a single dict.

def call_binary_operator(state, name, x, y, report_errors, ctx): (source)

Map a binary operator to "magic methods" (__add__ etc.).

def call_inplace_operator(state, iname, x, y, ctx): (source)

Try to call a method like __iadd__, possibly fall back to __add__.

def check_for_deleted(state, name, var, ctx): (source)

Undocumented

def copy_dict_without_keys(node, obj_var: cfg.Variable, keys_var: cfg.Variable, ctx) -> cfg.Variable: (source)

Create a copy of the input dict with some keys deleted.

def ensure_unpacked_starargs(node, starargs, ctx): (source)

Unpack starargs if it has not been done already.

def get_closure_var_name(frame, arg): (source)

Undocumented

def get_name_error_details(state, name: str, ctx) -> Optional[_NameErrorDetails]: (source)

Gets a detailed error message for [name-error].

def jump_if(state, op, ctx, pop=False, jump_if_val=False, or_pop=False): (source)

Implementation of various _JUMP_IF bytecodes. Args: state: Initial FrameState. op: An opcode. ctx: The current context. pop: True if a value is popped off the stack regardless. jump_if_val: True or False (indicates which value will lead to a jump). or_pop: True if a value is popped off the stack only when the jump is not taken. Returns: The new FrameState.

def load_closure_cell(state, op, check_bindings, ctx): (source)

Retrieve the value out of a closure cell. Used to generate the 'closure' tuple for MAKE_CLOSURE. Each entry in that tuple is typically retrieved using LOAD_CLOSURE. Args: state: The current VM state. op: The opcode. op.arg is the index of a "cell variable": This corresponds to an entry in co_cellvars or co_freevars and is a variable that's bound into a closure. check_bindings: Whether to check the retrieved value for bindings. ctx: The current context. Returns: A new state.

def log_opcode(op, state, frame, stack_size): (source)

Write a multi-line log message, including backtrace and stack.

def make_class(node, props, ctx): (source)

Create a class with the name, bases and methods given. Args: node: The current CFG node. props: class_mixin.ClassBuilderProperties required to build the class ctx: The current context. Returns: A node and an instance of class_type.

def make_function(name, node, code, globs, defaults, kw_defaults, closure, annotations, opcode, ctx): (source)

Create a function or closure given the arguments.

def match_class(node, obj_var: cfg.Variable, cls_var: cfg.Variable, keys_var: cfg.Variable, posarg_count: int, ctx) -> ClassMatch: (source)

Pick attributes out of a class instance for pattern matching.

def match_keys(node, obj_var: cfg.Variable, keys_var: cfg.Variable, ctx) -> Optional[cfg.Variable]: (source)

Pick values out of a mapping for pattern matching.

def match_mapping(node, obj_var: cfg.Variable, ctx) -> bool: (source)

See if var is a map for pattern matching.

def match_sequence(obj_var: cfg.Variable) -> bool: (source)

See if var is a sequence for pattern matching.

def merge_indefinite_iterables(node, target, iterables_to_merge): (source)

Undocumented

def pop_and_unpack_list(state, count, ctx): (source)

Pop count iterables off the stack and concatenate.

def process_function_type_comment(node, op, func, ctx): (source)

Modifies annotations from a function type comment. Checks if a type comment is present for the function. If so, the type comment is used to populate annotations. It is an error to have a type comment when annotations is not empty. Args: node: The current node. op: An opcode (used to determine filename and line number). func: An abstract.InterpreterFunction. ctx: The current context.

def push_block(state, t, level=None): (source)

Undocumented

def to_coroutine(state, obj, top, ctx): (source)

Convert any awaitables and generators in obj to coroutines. Implements the GET_AWAITABLE opcode, which returns obj unchanged if it is a coroutine or generator and otherwise resolves obj.__await__ (https://docs.python.org/3/library/dis.html#opcode-GET_AWAITABLE). So that we don't have to handle awaitable generators specially, our implementation converts generators to coroutines. Args: state: The current state. obj: The object, a cfg.Variable. top: Whether this is the top-level recursive call, to prevent incorrectly recursing into the result of obj.__await__. ctx: The current context. Returns: A tuple of the state and a cfg.Variable of coroutines.

def unpack_and_build(state, count, build_concrete, container_type, ctx): (source)

Undocumented

def unpack_iterable(node, var, ctx): (source)

Unpack an iterable.

def update_excluded_types(node, ctx): (source)

Update the excluded_types attribute of functions in the current frame.

Undocumented

def _base(cls): (source)

Undocumented

def _binding_to_coroutine(state, b, bad_bindings, ret, top, ctx): (source)

Helper for _to_coroutine. Args: state: The current state. b: A cfg.Binding. bad_bindings: Bindings that are not coroutines. ret: A return variable that this helper will add to. top: Whether this is the top-level recursive call. ctx: The current context. Returns: The state.

def _call_binop_on_bindings(node, name, xval, yval, ctx): (source)

Call a binary operator on two cfg.Binding objects.

def _check_defaults(node, method, ctx): (source)

Check parameter defaults against annotations.

def _check_final_members(cls, class_dict, ctx): (source)

Check if the new class overrides a final attribute or method.

def _convert_keys(keys_var: cfg.Variable): (source)

Undocumented

def _expand_generic_protocols(node, bases, ctx): (source)

Expand Protocol[T, ...] to Protocol, Generic[T, ...].

def _filter_out_metaclasses(bases, ctx): (source)

Process the temporary classes created by six.with_metaclass. six.with_metaclass constructs an anonymous class holding a metaclass and a list of base classes; if we find instances in `bases`, store the first metaclass we find and remove all metaclasses from `bases`. Args: bases: The list of base classes for the class being constructed. ctx: The current context. Returns: A tuple of (metaclass, base classes)

def _get_annotation(node, var, ctx): (source)

Extract an annotation from terms in `a | b | ...`.

Gets the class or function objects for a sequence of nested scope names. For example, if the code under analysis is: class Foo: def f(self): def g(): ... then when called with ['Foo', 'f', 'g'], this method returns [InterpreterClass(Foo), InterpreterFunction(f), InterpreterFunction(g)]. Arguments: state: The current state. names: A sequence of names for consecutive nested scopes in the module under analysis. Must start with a module-level name. ctx: The current context. Returns: The class or function object corresponding to each name in 'names'.

def _maybe_union(node, x, y, ctx): (source)

Attempt to evaluate a '|' operation as a typing.Union.

def _merge_tuple_bindings(var, ctx): (source)

Merge a set of heterogeneous tuples from var's bindings.

def _module_name(frame): (source)

Undocumented

def _overrides(subcls, supercls, attr): (source)

Check whether subcls_var overrides or newly defines the given attribute. Args: subcls: A potential subclass. supercls: A potential superclass. attr: An attribute name. Returns: True if subcls_var is a subclass of supercls_var and overrides or newly defines the attribute. False otherwise.

def _process_base_class(node, base, ctx): (source)

Process a base class for InterpreterClass creation.

def _var_is_fixed_length_tuple(var: cfg.Variable) -> bool: (source)

Undocumented

def _var_maybe_unknown(var: cfg.Variable) -> bool: (source)

Undocumented

_FUNCTION_TYPE_COMMENT_RE = (source)

Undocumented

Value
re.compile(r'^\((.*)\)\s*->\s*(\S.*?)\s*$')
_TRUNCATE: int = (source)

Undocumented

Value
120
_TRUNCATE_STR: int = (source)

Undocumented

Value
72
_repr_obj = (source)

Undocumented