Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/gast/astn.py: 88%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import ast
2import gast
5def _generate_translators(to):
7 class Translator(ast.NodeTransformer):
9 def _visit(self, node):
10 if isinstance(node, ast.AST):
11 return self.visit(node)
12 elif isinstance(node, list):
13 return [self._visit(n) for n in node]
14 else:
15 return node
17 def generic_visit(self, node):
18 class_name = type(node).__name__
19 if not hasattr(to, class_name):
20 # handle nodes that are not part of the AST
21 return
22 cls = getattr(to, class_name)
23 new_node = cls(
24 **{
25 field: self._visit(getattr(node, field))
26 for field in node._fields
27 if hasattr(node, field)
28 }
29 )
31 for attr in node._attributes:
32 try:
33 setattr(new_node, attr, getattr(node, attr))
34 except AttributeError:
35 pass
36 return new_node
38 return Translator
41AstToGAst = _generate_translators(gast)
43GAstToAst = _generate_translators(ast)