You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
6.5 KiB

4 years ago
  1. from gast.astn import AstToGAst, GAstToAst
  2. import gast
  3. import ast
  4. import sys
  5. class Ast3ToGAst(AstToGAst):
  6. def visit_Name(self, node):
  7. new_node = gast.Name(
  8. self._visit(node.id),
  9. self._visit(node.ctx),
  10. None,
  11. )
  12. return ast.copy_location(new_node, node)
  13. def visit_arg(self, node):
  14. new_node = gast.Name(
  15. self._visit(node.arg),
  16. gast.Param(),
  17. self._visit(node.annotation),
  18. )
  19. return ast.copy_location(new_node, node)
  20. def visit_ExceptHandler(self, node):
  21. if node.name:
  22. new_node = gast.ExceptHandler(
  23. self._visit(node.type),
  24. gast.Name(node.name, gast.Store(), None),
  25. self._visit(node.body))
  26. return ast.copy_location(new_node, node)
  27. else:
  28. return self.generic_visit(node)
  29. if sys.version_info.minor < 5:
  30. def visit_Call(self, node):
  31. if node.starargs:
  32. star = gast.Starred(self._visit(node.starargs), gast.Load())
  33. ast.copy_location(star, node)
  34. starred = [star]
  35. else:
  36. starred = []
  37. if node.kwargs:
  38. kwargs = [gast.keyword(None, self._visit(node.kwargs))]
  39. else:
  40. kwargs = []
  41. new_node = gast.Call(
  42. self._visit(node.func),
  43. self._visit(node.args) + starred,
  44. self._visit(node.keywords) + kwargs,
  45. )
  46. ast.copy_location(new_node, node)
  47. return new_node
  48. if 2 <= sys.version_info.minor <= 3:
  49. def _make_annotated_arg(self, parent, identifier, annotation):
  50. if identifier is None:
  51. return None
  52. new_node = gast.Name(
  53. self._visit(identifier),
  54. gast.Param(),
  55. self._visit(annotation),
  56. )
  57. return ast.copy_location(new_node, parent)
  58. def visit_arguments(self, node):
  59. new_node = gast.arguments(
  60. [self._visit(n) for n in node.args],
  61. self._make_annotated_arg(node,
  62. node.vararg,
  63. self._visit(node.varargannotation)),
  64. [self._visit(n) for n in node.kwonlyargs],
  65. self._visit(node.kw_defaults),
  66. self._make_annotated_arg(node,
  67. node.kwarg,
  68. self._visit(node.kwargannotation)),
  69. self._visit(node.defaults),
  70. )
  71. return new_node
  72. if sys.version_info.minor < 6:
  73. def visit_comprehension(self, node):
  74. new_node = gast.comprehension(
  75. target=self._visit(node.target),
  76. iter=self._visit(node.iter),
  77. ifs=self._visit(node.ifs),
  78. is_async=0,
  79. )
  80. return ast.copy_location(new_node, node)
  81. class GAstToAst3(GAstToAst):
  82. def _make_arg(self, node):
  83. if node is None:
  84. return None
  85. new_node = ast.arg(
  86. self._visit(node.id),
  87. self._visit(node.annotation),
  88. )
  89. return ast.copy_location(new_node, node)
  90. def visit_Name(self, node):
  91. new_node = ast.Name(
  92. self._visit(node.id),
  93. self._visit(node.ctx),
  94. )
  95. return ast.copy_location(new_node, node)
  96. def visit_ExceptHandler(self, node):
  97. if node.name:
  98. new_node = ast.ExceptHandler(
  99. self._visit(node.type),
  100. node.name.id,
  101. self._visit(node.body))
  102. return ast.copy_location(new_node, node)
  103. else:
  104. return self.generic_visit(node)
  105. if sys.version_info.minor < 5:
  106. def visit_Call(self, node):
  107. if node.args and isinstance(node.args[-1], gast.Starred):
  108. args = node.args[:-1]
  109. starargs = node.args[-1].value
  110. else:
  111. args = node.args
  112. starargs = None
  113. if node.keywords and node.keywords[-1].arg is None:
  114. keywords = node.keywords[:-1]
  115. kwargs = node.keywords[-1].value
  116. else:
  117. keywords = node.keywords
  118. kwargs = None
  119. new_node = ast.Call(
  120. self._visit(node.func),
  121. self._visit(args),
  122. self._visit(keywords),
  123. self._visit(starargs),
  124. self._visit(kwargs),
  125. )
  126. ast.copy_location(new_node, node)
  127. return new_node
  128. def visit_ClassDef(self, node):
  129. self.generic_visit(node)
  130. new_node = ast.ClassDef(
  131. name=self._visit(node.name),
  132. bases=self._visit(node.bases),
  133. keywords=self._visit(node.keywords),
  134. body=self._visit(node.body),
  135. decorator_list=self._visit(node.decorator_list),
  136. starargs=None,
  137. kwargs=None,
  138. )
  139. return ast.copy_location(new_node, node)
  140. if 2 <= sys.version_info.minor <= 3:
  141. def visit_arguments(self, node):
  142. if node.vararg is None:
  143. vararg = None
  144. varargannotation = None
  145. else:
  146. vararg = node.vararg.id
  147. varargannotation = self._visit(node.vararg.annotation)
  148. if node.kwarg is None:
  149. kwarg = None
  150. kwargannotation = None
  151. else:
  152. kwarg = node.kwarg.id
  153. kwargannotation = self._visit(node.kwarg.annotation)
  154. new_node = ast.arguments(
  155. [self._make_arg(n) for n in node.args],
  156. vararg, varargannotation,
  157. [self._make_arg(n) for n in node.kwonlyargs],
  158. kwarg, kwargannotation,
  159. self._visit(node.defaults),
  160. self._visit(node.kw_defaults),
  161. )
  162. return new_node
  163. else:
  164. def visit_arguments(self, node):
  165. new_node = ast.arguments(
  166. [self._make_arg(n) for n in node.args],
  167. self._make_arg(node.vararg),
  168. [self._make_arg(n) for n in node.kwonlyargs],
  169. self._visit(node.kw_defaults),
  170. self._make_arg(node.kwarg),
  171. self._visit(node.defaults),
  172. )
  173. return new_node
  174. def ast_to_gast(node):
  175. return Ast3ToGAst().visit(node)
  176. def gast_to_ast(node):
  177. return GAstToAst3().visit(node)