Builder

Control flow graph builder.

class py2cfg.builder.CFGBuilder(short: bool = True, treebuf: Optional[DefaultDict[str, Deque]] = None)[source]

Control flow graph builder.

A control flow graph builder is an ast.NodeVisitor that can walk through a program’s AST and iteratively build the corresponding CFG.

add_exit(block: py2cfg.model.Block, nextblock: py2cfg.model.Block, exitcase: Union[_ast.Compare, None, _ast.BoolOp, _ast.expr] = None)None[source]

Add a new exit to a block.

Parameters
  • block – A block to which an exit must be added.

  • nextblock – The block to which control jumps from the new exit.

  • exitcase – An AST node representing the ‘case’ (or condition) leading to the exit from the block in the program.

add_statement(block: py2cfg.model.Block, statement: Union[_ast.stmt, _ast.Call])None[source]

Add a statement to a block.

Parameters
  • block – A Block object to which a statement must be added.

  • statement – An AST node representing the statement that must be added to the current block.

build(name: str, tree: _ast.Module, asynchr: bool = False, entry_id: int = 0)py2cfg.model.CFG[source]

Build a CFG from an AST.

Parameters
  • name – The name of the CFG being built.

  • tree – The root of the AST from which the CFG must be built.

  • async – Boolean indicating whether the CFG being built represents an asynchronous function or not. When the CFG of a Python program is being built, it is considered like a synchronous ‘main’ function.

  • entry_id – Value for the id of the entry block of the CFG.

Returns

The CFG produced from the AST.

build_from_file(name: str, filepath: str)py2cfg.model.CFG[source]

Build a CFG from some Python source file.

Parameters
  • name – The name of the CFG being built.

  • filepath – The path to the file containing the Python source code to build the CFG from.

Returns

The CFG produced from the source file.

build_from_src(name: str, src: str)py2cfg.model.CFG[source]

Build a CFG from some Python source code.

Parameters
  • name – The name of the CFG being built.

  • src – A string containing the source code to build the CFG from.

Returns

The CFG produced from the source code.

clean_cfg(block: py2cfg.model.Block, visited: Set[py2cfg.model.Block])None[source]

Remove the useless (empty) blocks from a CFG.

Parameters
  • block – The block from which to start traversing the CFG to clean it.

  • visited – A list of blocks that already have been visited by clean_cfg (recursive function).

new_block(statement=None)py2cfg.model.Block[source]

Create a new block with a new id.

Returns

A Block object with a new unique id.

new_classCFG(node: _ast.ClassDef, asynchr: bool = False)None[source]

Create a new sub-CFG for a class definition and add it to the class CFGs of the CFG being built.

Parameters
  • node – The AST node containing the class definition.

  • async – Boolean indicating whether the class for which the CFG is being built is asynchronous or not.

new_func_block()py2cfg.model.FuncBlock[source]

Create a new function block with a new id.

Returns

A FuncBlock object with a new unique id.

new_functionCFG(node: _ast.FunctionDef, asynchr: bool = False)None[source]

Create a new sub-CFG for a function definition and add it to the function CFGs of the CFG being built.

Parameters
  • node – The AST node containing the function definition.

  • async – Boolean indicating whether the function for which the CFG is being built is asynchronous or not.

new_loopguard()py2cfg.model.Block[source]

Create a new block for a loop’s guard if the current block is not empty. Links the current block to the new loop guard.

Returns

The block to be used as new loop guard.

class py2cfg.builder.TryEnum(value)[source]

An enumeration.

py2cfg.builder.invert(node: Union[_ast.Compare, _ast.expr])Union[ast.NameConstant, _ast.UnaryOp, _ast.Compare][source]

Invert the operation in an ast node object (get its negation).

Parameters

node – An ast node object.

Returns

An ast node object containing the inverse (negation) of the input node.

py2cfg.builder.merge_exitcases(exit1: Optional[Union[_ast.Compare, _ast.BoolOp, _ast.expr]], exit2: Optional[Union[_ast.Compare, _ast.BoolOp, _ast.expr]])Optional[Union[_ast.Compare, _ast.BoolOp, _ast.expr]][source]

Merge the exitcases of two Links.

Parameters
  • exit1 – The exitcase of a Link object.

  • exit2 – Another exitcase to merge with exit1.

Returns

The merged exitcases.