def visit_Nonlocal(self, nonl): for name in nonl.names: old_role = self.scope.lookup_role(name) msg = "" if old_role & SYM_GLOBAL: msg = "name '%s' is nonlocal and global" % (name, ) if old_role & SYM_PARAM: msg = "name '%s' is parameter and nonlocal" % (name, ) if isinstance(self.scope, ModuleScope): msg = "nonlocal declaration not allowed at module level" if msg is not "": raise SyntaxError(msg, nonl.lineno, nonl.col_offset) if (old_role & (SYM_USED | SYM_ASSIGNED) and not (name == '__class__' and self.scope._hide_bound_from_nested_scopes)): if old_role & SYM_ASSIGNED: msg = "name '%s' is assigned to before nonlocal declaration" \ % (name,) else: msg = "name '%s' is used prior to nonlocal declaration" % \ (name,) misc.syntax_warning(self.space, msg, self.compile_info.filename, nonl.lineno, nonl.col_offset) self.note_symbol(name, SYM_NONLOCAL)
def visit_Global(self, glob): for name in glob.names: old_role = self.scope.lookup_role(name) if (self.scope._hide_bound_from_nested_scopes and name == '__class__'): msg = ("'global __class__' inside a class statement is not " "implemented in PyPy") raise SyntaxError(msg, glob.lineno, glob.col_offset, filename=self.compile_info.filename) if old_role & SYM_PARAM: msg = "name '%s' is parameter and global" % (name, ) raise SyntaxError(msg, glob.lineno, glob.col_offset) if old_role & SYM_NONLOCAL: msg = "name '%s' is nonlocal and global" % (name, ) raise SyntaxError(msg, glob.lineno, glob.col_offset) if old_role & (SYM_USED | SYM_ASSIGNED): if old_role & SYM_ASSIGNED: msg = "name '%s' is assigned to before global declaration"\ % (name,) else: msg = "name '%s' is used prior to global declaration" % \ (name,) misc.syntax_warning(self.space, msg, self.compile_info.filename, glob.lineno, glob.col_offset) self.note_symbol(name, SYM_GLOBAL)
def visit_ImportFrom(self, imp): for alias in imp.names: if self._visit_alias(alias): if self.scope.note_import_star(imp): msg = "import * only allowed at module level" misc.syntax_warning( self.space, msg, self.compile_info.filename, imp.lineno, imp.col_offset)
def visit_Global(self, glob): for name in glob.names: old_role = self.scope.lookup_role(name) if old_role & (SYM_USED | SYM_ASSIGNED): if old_role & SYM_ASSIGNED: msg = "name '%s' is assigned to before global declaration" % (name,) else: msg = "name '%s' is used prior to global declaration" % (name,) misc.syntax_warning(self.space, msg, self.compile_info.filename, glob.lineno, glob.col_offset) self.note_symbol(name, SYM_GLOBAL)
def visit_Global(self, glob): for name in glob.names: old_role = self.scope.lookup_role(name) if old_role & (SYM_USED | SYM_ASSIGNED): if old_role & SYM_ASSIGNED: msg = "name '%s' is assigned to before global declaration" \ % (name,) else: msg = "name '%s' is used prior to global declaration" % \ (name,) misc.syntax_warning(self.space, msg, self.compile_info.filename, glob.lineno, glob.col_offset) self.note_symbol(name, SYM_GLOBAL)
def visit_Nonlocal(self, nonl): for name in nonl.names: old_role = self.scope.lookup_role(name) if old_role & (SYM_USED | SYM_ASSIGNED): if old_role & SYM_ASSIGNED: msg = "name '%s' is assigned to before nonlocal declaration" \ % (name,) else: msg = "name '%s' is used prior to nonlocal declaration" % \ (name,) misc.syntax_warning(self.space, msg, self.compile_info.filename, nonl.lineno, nonl.col_offset) self.note_symbol(name, SYM_NONLOCAL)
def _visit_comprehension(self, node, comps, *consider): outer = comps[0] assert isinstance(outer, ast.comprehension) outer.iter.walkabout(self) new_scope = FunctionScope("<genexpr>", node.lineno, node.col_offset) self.push_scope(new_scope, node) self.implicit_arg(0) outer.target.walkabout(self) self.visit_sequence(outer.ifs) self.visit_sequence(comps[1:]) for item in list(consider): item.walkabout(self) self.pop_scope() # http://bugs.python.org/issue10544: was never fixed in CPython, # but we can at least issue a SyntaxWarning in the meantime if new_scope.is_generator: msg = ("'yield' inside a list or generator comprehension behaves " "unexpectedly (http://bugs.python.org/issue10544)") misc.syntax_warning(self.space, msg, self.compile_info.filename, node.lineno, node.col_offset)