Ejemplo n.º 1
0
 def test_builtin_lookup(self):
     self.assertEquals(lookup.builtin_lookup('__dict__')[1], ())
     intstmts = lookup.builtin_lookup('int')[1]
     self.assertEquals(len(intstmts), 1)
     self.assertIsInstance(intstmts[0], nodes.Class)
     self.assertEquals(intstmts[0].name, 'int')
     self.assertIs(intstmts[0], nodes.const_factory(1)._proxied)
Ejemplo n.º 2
0
 def visit_name(self, node):
     """check that a name is defined if the current scope and doesn't
     redefine a built-in
     """
     name = node.name
     stmt = node.statement()
     frame = stmt.scope()
     # if the name node is used as a function default argument's value, then
     # start from the parent frame of the function instead of the function
     # frame
     if is_func_default(node) or is_ancestor_name(frame, node):
         start_index = len(self._to_consume) - 2
     else:
         start_index = len(self._to_consume) - 1
     # iterates through parent scopes, from the inner to the outer
     for i in range(start_index, -1, -1):
         to_consume, consumed, scope_type = self._to_consume[i]
         # if the current scope is a class scope but it's not the inner
         # scope, ignore it. This prevents to access this scope instead of
         # the globals one in function members when there are some common
         # names
         if scope_type == 'class' and i != start_index:
             continue
         # the name has already been consumed, only check it's not a loop
         # variable used outside the loop
         if consumed.has_key(name):
             self._loopvar_name(node, name)
             break
         # mark the name as consumed if it's defined in this scope
         # (ie no KeyError is raise by "to_consume[name]"
         try:
             consumed[name] = to_consume[name]
             # checks for use before assigment
             # FIXME: the last condition should just check attribute access
             # is protected by a try: except NameError: (similar to #9219)
             defnode = assign_parent(to_consume[name][0])
             if defnode is not None:
                 defstmt = defnode.statement()
                 defframe = defstmt.frame()
                 maybee0601 = True
                 if not frame is defframe:
                     maybee0601 = False
                 elif defframe.parent is None:
                     # we are at the module level, check the name is not
                     # defined in builtins
                     if builtin_lookup(name)[1]:
                         maybee0601 = False
                 else:
                     # we are in a local scope, check the name is not
                     # defined in global or builtin scope
                     if defframe.root().lookup(name)[1]:
                         maybee0601 = False
                 if (maybee0601
                     and stmt.source_line() <= defstmt.source_line()
                     and not is_defined_before(node)
                     and not are_exclusive(stmt, defstmt)):
                     self.add_message('E0601', args=name, node=node)
             del to_consume[name]
             # check it's not a loop variable used outside the loop
             self._loopvar_name(node, name)
             break
         except KeyError:
             continue
     else:
         # we have not found the name, if it isn't a builtin, that's an
         # undefined name !
         if not self._is_builtin(name):
             self.add_message('E0602', args=name, node=node)
Ejemplo n.º 3
0
 def visit_name(self, node):
     """check that a name is defined if the current scope and doesn't
     redefine a built-in
     """
     name = node.name
     stmt = node.statement()
     # probably "is_statement == True" missing somewhere in astng
     assert stmt.fromlineno, (stmt, node, node.fromlineno)
     frame = stmt.scope()
     # if the name node is used as a function default argument's value or as
     # a decorator, then start from the parent frame of the function instead
     # of the function frame - and thus open an inner class scope
     if (is_func_default(node) or is_func_decorator(node)
         or is_ancestor_name(frame, node)):
         start_index = len(self._to_consume) - 2
     else:
         start_index = len(self._to_consume) - 1
     # iterates through parent scopes, from the inner to the outer
     for i in range(start_index, -1, -1):
         to_consume, consumed, scope_type = self._to_consume[i]
         # if the current scope is a class scope but it's not the inner
         # scope, ignore it. This prevents to access this scope instead of
         # the globals one in function members when there are some common
         # names
         if scope_type == 'class' and i != start_index:
             continue
         # the name has already been consumed, only check it's not a loop
         # variable used outside the loop
         if consumed.has_key(name):
             self._loopvar_name(node, name)
             break
         # mark the name as consumed if it's defined in this scope
         # (i.e. no KeyError is raised by "to_consume[name]")
         try:
             consumed[name] = to_consume[name]
         except KeyError:
             continue
         else:
             # checks for use before assigment
             defnode = assign_parent(to_consume[name][0])
             if defnode is not None:
                 defstmt = defnode.statement()
                 defframe = defstmt.frame()
                 maybee0601 = True
                 if not frame is defframe:
                     maybee0601 = False
                 elif defframe.parent is None:
                     # we are at the module level, check the name is not
                     # defined in builtins
                     if name in defframe.scope_attrs or builtin_lookup(name)[1]:
                         maybee0601 = False
                 else:
                     # we are in a local scope, check the name is not
                     # defined in global or builtin scope
                     if defframe.root().lookup(name)[1]:
                         maybee0601 = False
                 if (maybee0601
                     and stmt.fromlineno <= defstmt.fromlineno
                     and not is_defined_before(node)
                     and not are_exclusive(stmt, defstmt, ('NameError', 'Exception', 'BaseException'))):
                     if defstmt is stmt and isinstance(node, (astng.DelName,
                                                              astng.AssName)):
                         self.add_message('E0602', args=name, node=node)
                     else:
                         self.add_message('E0601', args=name, node=node)
             if not isinstance(node, astng.AssName): # Aug AssName
                 del to_consume[name]
             else:
                 del consumed[name]
             # check it's not a loop variable used outside the loop
             self._loopvar_name(node, name)
             break
     else:
         # we have not found the name, if it isn't a builtin, that's an
         # undefined name !
         if not (name in astng.Module.scope_attrs or is_builtin(name)
                 or name in self.config.additional_builtins):
             self.add_message('E0602', args=name, node=node)