eric6/Plugins/CheckerPlugins/CodeStyleChecker/MiscellaneousChecker.py

changeset 7637
c878e8255972
parent 7610
df7025fe26a3
child 7639
422fd05e9c91
--- a/eric6/Plugins/CheckerPlugins/CodeStyleChecker/MiscellaneousChecker.py	Mon Jun 22 17:55:06 2020 +0200
+++ b/eric6/Plugins/CheckerPlugins/CodeStyleChecker/MiscellaneousChecker.py	Tue Jun 23 17:24:18 2020 +0200
@@ -151,14 +151,9 @@
         self.__pep3101FormatRegex = re.compile(
             r'^(?:[^\'"]*[\'"][^\'"]*[\'"])*\s*%|^\s*%')
         
-        if sys.version_info >= (3, 0):
-            import builtins
-            self.__builtins = [b for b in dir(builtins)
-                               if b not in self.BuiltinsWhiteList]
-        else:
-            import __builtin__
-            self.__builtins = [b for b in dir(__builtin__)
-                               if b not in self.BuiltinsWhiteList]
+        import builtins
+        self.__builtins = [b for b in dir(builtins)
+                           if b not in self.BuiltinsWhiteList]
 
         # statistics counters
         self.counters = {}
@@ -292,13 +287,6 @@
         @rtype ast.AST
         """
         source = "".join(self.__source)
-        # Check type for py2: if not str it's unicode
-        if sys.version_info[0] == 2:
-            try:
-                source = source.encode('utf-8')
-            except UnicodeError:
-                pass
-        
         return compile(source, self.__filename, 'exec', ast.PyCF_ONLY_AST)
     
     def run(self):
@@ -533,7 +521,7 @@
         visitor.visit(self.__tree)
         for node in visitor.nodes:
             text = node.s
-            if sys.version_info[0] > 2 and isinstance(text, bytes):
+            if isinstance(text, bytes):
                 try:
                     text = text.decode(coding)
                 except UnicodeDecodeError:
@@ -711,22 +699,13 @@
             elif any(isinstance(node, functionDef)
                      for functionDef in functionDefs):
                 # (asynchronous) function definition
-                if sys.version_info >= (3, 0):
-                    for arg in node.args.args:
-                        if (
-                            isinstance(arg, ast.arg) and
-                            arg.arg in self.__builtins
-                        ):
-                            self.__error(arg.lineno - 1, arg.col_offset,
-                                         "M132", arg.arg)
-                else:
-                    for arg in node.args.args:
-                        if (
-                            isinstance(arg, ast.Name) and
-                            arg.id in self.__builtins
-                        ):
-                            self.__error(arg.lineno - 1, arg.col_offset,
-                                         "M132", arg.id)
+                for arg in node.args.args:
+                    if (
+                        isinstance(arg, ast.arg) and
+                        arg.arg in self.__builtins
+                    ):
+                        self.__error(arg.lineno - 1, arg.col_offset,
+                                     "M132", arg.arg)
     
     def __checkComprehensions(self):
         """
@@ -962,23 +941,20 @@
         """
         Private method to check use of naive datetime functions.
         """
-        if sys.version_info[0] >= 3:
-            # this check is only performed for Python 3
-            
-            # step 1: generate an augmented node tree containing parent info
-            #         for each child node
-            tree = self.__generateTree()
-            for node in ast.walk(tree):
-                for childNode in ast.iter_child_nodes(node):
-                    childNode._dtCheckerParent = node
-            
-            # step 2: perform checks and report issues
-            visitor = DateTimeVisitor()
-            visitor.visit(tree)
-            for violation in visitor.violations:
-                node = violation[0]
-                reason = violation[1]
-                self.__error(node.lineno - 1, node.col_offset, reason)
+        # step 1: generate an augmented node tree containing parent info
+        #         for each child node
+        tree = self.__generateTree()
+        for node in ast.walk(tree):
+            for childNode in ast.iter_child_nodes(node):
+                childNode._dtCheckerParent = node
+        
+        # step 2: perform checks and report issues
+        visitor = DateTimeVisitor()
+        visitor.visit(tree)
+        for violation in visitor.violations:
+            node = violation[0]
+            reason = violation[1]
+            self.__error(node.lineno - 1, node.col_offset, reason)
     
     def __checkSysVersion(self):
         """
@@ -1384,16 +1360,12 @@
         @param node reference to the node to be processed
         @type ast.Call
         """
-        if sys.version_info >= (3, 0):
-            validPaths = ("six", "future.utils", "builtins")
-            methodsDict = {
-                "M521": ("iterkeys", "itervalues", "iteritems", "iterlists"),
-                "M522": ("viewkeys", "viewvalues", "viewitems", "viewlists"),
-                "M523": ("next",),
-            }
-        else:
-            validPaths = ()
-            methodsDict = {}
+        validPaths = ("six", "future.utils", "builtins")
+        methodsDict = {
+            "M521": ("iterkeys", "itervalues", "iteritems", "iterlists"),
+            "M522": ("viewkeys", "viewvalues", "viewitems", "viewlists"),
+            "M523": ("next",),
+        }
         
         if isinstance(node.func, ast.Attribute):
             for code, methods in methodsDict.items():
@@ -1450,13 +1422,12 @@
         """
         callPath = list(composeCallPath(node))
         
-        if '.'.join(callPath) == 'sys.maxint' and sys.version_info >= (3, 0):
+        if '.'.join(callPath) == 'sys.maxint':
             self.violations.append((node, "M504"))
         
         elif (
             len(callPath) == 2 and
-            callPath[1] == 'message' and
-            sys.version_info >= (2, 6)
+            callPath[1] == 'message'
         ):
             name = callPath[0]
             for elem in reversed(self.__nodeStack[:-1]):
@@ -1475,7 +1446,7 @@
             # By using 'hasattr' below we're ignoring starred arguments, slices
             # and tuples for simplicity.
             assignTargets = {t.id for t in node.targets if hasattr(t, 'id')}
-            if '__metaclass__' in assignTargets and sys.version_info >= (3, 0):
+            if '__metaclass__' in assignTargets:
                 self.violations.append((node, "M524"))
         
         elif len(node.targets) == 1:
@@ -1785,13 +1756,10 @@
         @type ast.AST
         @return flag indicating the node contains a None value
         """
-        if sys.version_info[0] > 2:
-            return (
-                AstUtilities.isNameConstant(node) and
-                AstUtilities.getValue(node) is None
-            )
-        else:
-            return isinstance(node, ast.Name) and node.id == "None"
+        return (
+            AstUtilities.isNameConstant(node) and
+            AstUtilities.getValue(node) is None
+        )
     
     def __resultExists(self):
         """
@@ -1850,7 +1818,6 @@
         try:
             okNodes = (ast.Return, ast.Raise, ast.While, ast.Try)
         except AttributeError:
-            # Py2
             okNodes = (ast.Return, ast.Raise, ast.While)
         if not isinstance(node, okNodes):
             self.violations.append((node, "M833"))

eric ide

mercurial