##// END OF EJS Templates
check for nonlocal inside toplevel functions
felixzhuologist -
Show More
@@ -97,19 +97,25 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):
101 self.is_toplevel = is_toplevel
102 super().__init__()
100
103
101 def generic_visit(self, node):
104 def generic_visit(self, node):
102 func_types = (ast.FunctionDef, ast.AsyncFunctionDef)
105 func_types = (ast.FunctionDef, ast.AsyncFunctionDef)
103 invalid_types = (ast.Return, ast.Yield, ast.YieldFrom)
106 toplevel_invalid_types = (ast.Return, ast.Yield, ast.YieldFrom)
107 inner_invalid_types = (ast.Nonlocal,)
104
108
105 if isinstance(node, func_types):
109 if isinstance(node, func_types) and self.is_toplevel:
106 return # Don't recurse into functions
110 self.is_toplevel = False
107 elif isinstance(node, invalid_types):
111 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):
108 raise SyntaxError()
115 raise SyntaxError()
109 else:
116 else:
110 super().generic_visit(node)
117 super().generic_visit(node)
111
118
112
113 def _async_parse_cell(cell: str) -> ast.AST:
119 def _async_parse_cell(cell: str) -> ast.AST:
114 """
120 """
115 This is a compatibility shim for pre-3.7 when async outside of a function
121 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