##// END OF EJS Templates
generalize to arbitrary errors by depth
felixzhuologist -
Show More
@@ -97,25 +97,27 b' class _AsyncSyntaxErrorVisitor(ast.NodeVisitor):'
97 the implementation involves wrapping the repl in an async function, it
97 the implementation involves wrapping the repl in an async function, it
98 is erroneously allowed (e.g. yield or return at the top level)
98 is erroneously allowed (e.g. yield or return at the top level)
99 """
99 """
100 def __init__(self, is_toplevel=True):
100 def __init__(self):
101 self.is_toplevel = is_toplevel
101 self.depth = 0
102 super().__init__()
102 super().__init__()
103
103
104 def generic_visit(self, node):
104 def generic_visit(self, node):
105 func_types = (ast.FunctionDef, ast.AsyncFunctionDef)
105 func_types = (ast.FunctionDef, ast.AsyncFunctionDef)
106 toplevel_invalid_types = (ast.Return, ast.Yield, ast.YieldFrom)
106 invalid_types_by_depth = {
107 inner_invalid_types = (ast.Nonlocal,)
107 0: (ast.Return, ast.Yield, ast.YieldFrom),
108
108 1: (ast.Nonlocal,)
109 if isinstance(node, func_types) and self.is_toplevel:
109 }
110 self.is_toplevel = False
110
111 should_traverse = self.depth < max(invalid_types_by_depth.keys())
112 if isinstance(node, func_types) and should_traverse:
113 self.depth += 1
111 super().generic_visit(node)
114 super().generic_visit(node)
112 elif self.is_toplevel and isinstance(node, toplevel_invalid_types):
115 elif isinstance(node, invalid_types_by_depth[self.depth]):
113 raise SyntaxError()
114 elif not self.is_toplevel and isinstance(node, inner_invalid_types):
115 raise SyntaxError()
116 raise SyntaxError()
116 else:
117 else:
117 super().generic_visit(node)
118 super().generic_visit(node)
118
119
120
119 def _async_parse_cell(cell: str) -> ast.AST:
121 def _async_parse_cell(cell: str) -> ast.AST:
120 """
122 """
121 This is a compatibility shim for pre-3.7 when async outside of a function
123 This is a compatibility shim for pre-3.7 when async outside of a function
General Comments 0
You need to be logged in to leave comments. Login now