##// END OF EJS Templates
generalize to arbitrary errors by depth
felixzhuologist -
Show More
@@ -97,25 +97,27 b' class _AsyncSyntaxErrorVisitor(ast.NodeVisitor):'
97 97 the implementation involves wrapping the repl in an async function, it
98 98 is erroneously allowed (e.g. yield or return at the top level)
99 99 """
100 def __init__(self, is_toplevel=True):
101 self.is_toplevel = is_toplevel
100 def __init__(self):
101 self.depth = 0
102 102 super().__init__()
103 103
104 104 def generic_visit(self, node):
105 105 func_types = (ast.FunctionDef, ast.AsyncFunctionDef)
106 toplevel_invalid_types = (ast.Return, ast.Yield, ast.YieldFrom)
107 inner_invalid_types = (ast.Nonlocal,)
108
109 if isinstance(node, func_types) and self.is_toplevel:
110 self.is_toplevel = False
106 invalid_types_by_depth = {
107 0: (ast.Return, ast.Yield, ast.YieldFrom),
108 1: (ast.Nonlocal,)
109 }
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 114 super().generic_visit(node)
112 elif self.is_toplevel and isinstance(node, toplevel_invalid_types):
113 raise SyntaxError()
114 elif not self.is_toplevel and isinstance(node, inner_invalid_types):
115 elif isinstance(node, invalid_types_by_depth[self.depth]):
115 116 raise SyntaxError()
116 117 else:
117 118 super().generic_visit(node)
118 119
120
119 121 def _async_parse_cell(cell: str) -> ast.AST:
120 122 """
121 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