Module opshin.rewrite.rewrite_orig_name
Expand source code
from ast import *
from copy import copy
from ..util import CompilingNodeTransformer
"""
Rewrites all occurrences of names to contain a pointer to the original name for good
"""
class RewriteOrigName(CompilingNodeTransformer):
step = "Assigning the orig_id/orig_name field with the variable name"
def visit_Name(self, node: Name) -> Name:
nc = copy(node)
nc.orig_id = node.id
return nc
def visit_ClassDef(self, node: ClassDef) -> ClassDef:
node_cp = copy(node)
node_cp.orig_name = node.name
node_cp.body = [self.visit(n) for n in node.body]
return node_cp
def visit_NoneType(self, node: None) -> None:
return None
def visit_FunctionDef(self, node: FunctionDef) -> FunctionDef:
node_cp = copy(node)
node_cp.orig_name = node.name
node_cp.args = copy(node.args)
node_cp.args.args = []
for a in node.args.args:
a_cp = self.visit(a)
a_cp.orig_arg = a.arg
node_cp.args.args.append(a_cp)
node_cp.returns = self.visit(node.returns)
node_cp.decorator_list = [self.visit(l) for l in node.decorator_list]
node_cp.body = [self.visit(s) for s in node.body]
return node_cp
Classes
class RewriteOrigName
-
A :class:
NodeVisitor
subclass that walks the abstract syntax tree and allows modification of nodes.The
NodeTransformer
will walk the AST and use the return value of the visitor methods to replace or remove the old node. If the return value of the visitor method isNone
, the node will be removed from its location, otherwise it is replaced with the return value. The return value may be the original node in which case no replacement takes place.Here is an example transformer that rewrites all occurrences of name lookups (
foo
) todata['foo']
::class RewriteName(NodeTransformer):
def visit_Name(self, node): return Subscript( value=Name(id='data', ctx=Load()), slice=Constant(value=node.id), ctx=node.ctx )
Keep in mind that if the node you're operating on has child nodes you must either transform the child nodes yourself or call the :meth:
generic_visit
method for the node first.For nodes that were part of a collection of statements (that applies to all statement nodes), the visitor may also return a list of nodes rather than just a single node.
Usually you use the transformer like this::
node = YourTransformer().visit(node)
Expand source code
class RewriteOrigName(CompilingNodeTransformer): step = "Assigning the orig_id/orig_name field with the variable name" def visit_Name(self, node: Name) -> Name: nc = copy(node) nc.orig_id = node.id return nc def visit_ClassDef(self, node: ClassDef) -> ClassDef: node_cp = copy(node) node_cp.orig_name = node.name node_cp.body = [self.visit(n) for n in node.body] return node_cp def visit_NoneType(self, node: None) -> None: return None def visit_FunctionDef(self, node: FunctionDef) -> FunctionDef: node_cp = copy(node) node_cp.orig_name = node.name node_cp.args = copy(node.args) node_cp.args.args = [] for a in node.args.args: a_cp = self.visit(a) a_cp.orig_arg = a.arg node_cp.args.args.append(a_cp) node_cp.returns = self.visit(node.returns) node_cp.decorator_list = [self.visit(l) for l in node.decorator_list] node_cp.body = [self.visit(s) for s in node.body] return node_cp
Ancestors
- CompilingNodeTransformer
- TypedNodeTransformer
- ast.NodeTransformer
- ast.NodeVisitor
Class variables
var step
Methods
def visit(self, node)
-
Inherited from:
CompilingNodeTransformer
.visit
Visit a node.
def visit_ClassDef(self, node: ast.ClassDef) ‑> ast.ClassDef
def visit_FunctionDef(self, node: ast.FunctionDef) ‑> ast.FunctionDef
def visit_Name(self, node: ast.Name) ‑> ast.Name
def visit_NoneType(self, node: None) ‑> None