A ProgramGraph represents a Python program or function.
Method | __init__ |
Constructs an empty ProgramGraph with no root. |
Method | add |
Adds an edge between two nodes in the graph. |
Method | add |
Adds a new edge between two nodes in the graph. |
Method | add |
Adds a ProgramGraphNode to this graph. |
Method | add |
Adds a node to the program graph. |
Method | all |
Undocumented |
Method | children |
Yields the (direct) AST children of an AST program graph node. |
Method | contains |
Undocumented |
Method | copy |
Returns a new program graph containing only the subtree rooted at NODE. |
Method | copy |
Returns a new program graph in which the subtree of NODE is removed. |
Method | dump |
Returns a string representation for debugging. |
Method | get |
Undocumented |
Method | get |
Returns the node in the program graph corresponding to an object. |
Method | get |
Gets a ProgramGraph node for the given read or write. |
Method | get |
Undocumented |
Method | get |
Undocumented |
Method | get |
Gets a ProgramGraph node for the given integer id. |
Method | get |
Undocumented |
Method | get |
Undocumented |
Method | get |
Undocumented |
Method | get |
Generates the nodes in the program graph containing the query source. |
Method | get |
Undocumented |
Method | incoming |
Returns the incoming neighbors of a program graph node. |
Method | neighbors |
Returns the incoming and outgoing neighbors of a program graph node. |
Method | outgoing |
Returns the outgoing neighbors of a program graph node. |
Method | parent |
Returns the AST parent of an AST program graph node. |
Method | reconstruct |
Reconstruct all internal ProgramGraphNode.ast_node references. |
Method | remove |
Removes an edge from the graph. |
Method | to |
Convert the program graph to a Python AST. |
Method | walk |
Yields the nodes that correspond to the descendants of node in the AST. |
Instance Variable | ast |
Maps from an AST node's object id to the corresponding AST program graph node, if it exists. |
Instance Variable | child |
Maps from node id to a list of that node's AST children node ids. |
Instance Variable | edges |
A list of the edges (from_node.id, to_node.id, edge type) in the graph. |
Instance Variable | neighbors |
Maps from node id to a list of that node's neighboring edges. |
Instance Variable | nodes |
Maps from node id to the ProgramGraphNode with that id. |
Instance Variable | parent |
Maps from node id to that node's AST parent node id. |
Instance Variable | root |
The id of the root ProgramGraphNode. |
Property | root |
Undocumented |
Method | _build |
Helper method: builds an AST and optionally sets ast_node references. |
Adds a new edge between two nodes in the graph.
Both nodes must already be part of the graph.
Parameters | |
n1 | Specifies the from node of the edge. Can be any object type accepted by get_node. |
n2 | Specifies the to node of the edge. Can be any object type accepted by get_node. |
edge | The type of edge. Can be any integer in the pb.Edge enum. |
field | For AST edges, a string describing the Python AST field |
Returns | |
The new edge. |
Adds a ProgramGraphNode to this graph.
Parameters | |
node | The ProgramGraphNode that should be added. |
Returns | |
The node that was added. | |
Raises | |
ValueError | the node has already been added to this graph. |
Yields the (direct) AST children of an AST program graph node.
Parameters | |
node | A ProgramGraphNode. |
Yields | |
The AST children of node, which are ProgramGraphNode objects. |
Returns a new program graph containing only the subtree rooted at NODE.
All edges that connect nodes in the subtree are included, both AST edges and other types of edges.
Parameters | |
node | A node in this program graph |
Returns | |
A new ProgramGraph object whose root is NODE |
Returns a new program graph in which the subtree of NODE is removed.
In the new graph, the subtree headed by NODE is replaced by a single node of type PLACEHOLDER, which is connected to the AST parent of NODE by the same edge type as in the original graph.
The new program graph will share structure (i.e. the ProgramGraphNode objects) with the original graph.
Parameters | |
node | A node in this program graph |
Returns | |
A new ProgramGraph object with NODE replaced |
Returns the node in the program graph corresponding to an object.
Parameters | |
obj | Can be an integer, AST node, ProgramGraphNode, or program graph node protobuf. |
Raises | |
ValueError | no node exists in the program graph matching obj. |
Generates the nodes in the program graph containing the query source.
Parameters | |
source | The query source. |
Returns | |
A generator of all nodes in the program graph with an Instruction with source that includes the query source. |
Returns the incoming neighbors of a program graph node.
Parameters | |
node | A ProgramGraphNode. |
edge | If provided, only edges of this type are considered. |
Returns | |
The incoming neighbors of node, which are ProgramGraphNode objects but not necessarily AST nodes. |
Returns the incoming and outgoing neighbors of a program graph node.
Parameters | |
node | A ProgramGraphNode. |
edge | If provided, only edges of this type are considered. |
Returns | |
The incoming and outgoing neighbors of node, which are ProgramGraphNode objects but not necessarily AST nodes. |
Returns the outgoing neighbors of a program graph node.
Parameters | |
node | A ProgramGraphNode. |
edge | If provided, only edges of this type are considered. |
Returns | |
The outgoing neighbors of node, which are ProgramGraphNode objects but not necessarily AST nodes. |
Returns the AST parent of an AST program graph node.
Parameters | |
node | A ProgramGraphNode. |
Returns | |
The node's AST parent, which is also a ProgramGraphNode. |
Reconstruct all internal ProgramGraphNode.ast_node references.
After calling this method, all nodes of type AST_NODE will have their
ast_node
property refer to subtrees of a reconstructed AST object, and
self.ast_id_to_program_graph_node will contain only entries from this new
AST.
Note that only AST nodes reachable by fields from the root node will be converted; this should be all of them but this is not checked.
Removes an edge from the graph.
If there are multiple copies of the same edge, only one copy is removed.
Parameters | |
edge | The edge, a pb.Edge proto. |
Yields the nodes that correspond to the descendants of node in the AST.
Parameters | |
node | the node in the program graph corresponding to the root of the AST subtree that should be walked. If None, defaults to the root of the program graph. |
Yields | |
All nodes corresponding to descendants of node in the AST. |