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 valid_fields = getattr(cls, '_fields', ())
24 new_node = cls(
25 **{
26 field: self._visit(getattr(node, field))
27 for field in node._fields
28 if hasattr(node, field) and field in valid_fields
29 }
30 )
32 for attr in node._attributes:
33 try:
34 setattr(new_node, attr, getattr(node, attr))
35 except AttributeError:
36 pass
37 return new_node
39 return Translator
42AstToGAst = _generate_translators(gast)
44GAstToAst = _generate_translators(ast)