Model

Control flow graph for Python programs.

class py2cfg.model.Block(id: int)[source]

Basic block in a control flow graph.

Contains a list of statements executed in a program without any control jumps. A block of statements is exited through one of its exits. Exits are a list of Links that represent control flow jumps.

at() → int[source]

Get the line number of the first statement of the block in the program.

end() → int[source]

Get the line number of the last statement of the block in the program.

get_calls() → str[source]

Get a string containing the calls to other functions inside the block.

Returns

A string containing the names of the functions called inside the block.

get_source() → str[source]

Get a string containing the Python source code corresponding to the statements in the block.

Returns

A string containing the source code of the statements.

is_empty() → bool[source]

Check if the block is empty.

Returns

A boolean indicating if the block is empty (True) or not (False).

class py2cfg.model.CFG(name: str, asynchr: bool = False, short: bool = True)[source]

Control flow graph (CFG).

A control flow graph is composed of basic blocks and links between them representing control flow jumps. It has a unique entry block and several possible ‘final’ blocks (blocks with no exits representing the end of the CFG).

bsearch(lineno: int, lst: Optional[list] = None) → Optional[py2cfg.model.Block][source]

Search for a block at line

build_key(**kwargs)[source]

Build key graph

build_visual(filepath: str, format: str, calls: bool = True, show: bool = True, cleanup=True, directory=None, interactive=False, build_keys=True, build_own=False) → str[source]

Build a visualisation of the CFG with graphviz and output it in a DOT file.

Parameters
  • filename – The name of the output file in which the visualisation must be saved.

  • format – The format to use for the output file (PDF, …).

  • show – A boolean indicating whether to automatically open the output file after building the visualisation.

highlight_blocks(lines, blocks=None) → int[source]

In interactive mode, highlight the block at lineno

outline_block(lineno, lst=None) → int[source]

In interactive mode, outline the block at lineno

own_blocks() → Iterator[py2cfg.model.Block][source]

Generator that yields all blocks in the current graph, excluding any subgraphs

class py2cfg.model.FuncBlock(*args, **kwargs)[source]

Link between blocks in a control flow graph.

Represents a control flow jump between two blocks. Contains an exitcase in the form of an expression, representing the case in which the associated control jump is made.

get_exitcase() → str[source]

Get a string containing the Python source code corresponding to the exitcase of the Link.

Returns

A string containing the source code.

jumpfrom() → Optional[int][source]

Return the line of source end

jumpto() → Optional[int][source]

Return the line of target start

class py2cfg.model.TryBlock(*args, **kwargs)[source]
get_source() → str[source]

Get a string containing the Python source code corresponding to the statements in the block.

Returns

A string containing the source code of the statements.