eric6/Plugins/CheckerPlugins/CodeStyleChecker/PathLib/PathlibChecker.py

branch
maintenance
changeset 8273
698ae46f40a4
parent 8243
cc717c2ae956
--- a/eric6/Plugins/CheckerPlugins/CodeStyleChecker/PathLib/PathlibChecker.py	Fri Apr 02 11:59:41 2021 +0200
+++ b/eric6/Plugins/CheckerPlugins/CodeStyleChecker/PathLib/PathlibChecker.py	Sat May 01 14:27:20 2021 +0200
@@ -9,10 +9,11 @@
 """
 
 import ast
-import sys
+import copy
+import contextlib
 
 
-class PathlibChecker(object):
+class PathlibChecker:
     """
     Class implementing a checker for functions that can be replaced by use of
     the pathlib module.
@@ -65,7 +66,8 @@
         "py.path.local": "P401",
     }
     
-    def __init__(self, source, filename, selected, ignored, expected, repeat):
+    def __init__(self, source, filename, tree, selected, ignored, expected,
+                 repeat):
         """
         Constructor
         
@@ -73,6 +75,8 @@
         @type list of str
         @param filename name of the source file
         @type str
+        @param tree AST tree of the source code
+        @type ast.Module
         @param selected list of selected codes
         @type list of str
         @param ignored list of codes to be ignored
@@ -88,6 +92,7 @@
         self.__repeat = repeat
         self.__filename = filename
         self.__source = source[:]
+        self.__tree = copy.deepcopy(tree)
         
         # statistics counters
         self.counters = {}
@@ -147,29 +152,6 @@
                 }
             )
     
-    def __reportInvalidSyntax(self):
-        """
-        Private method to report a syntax error.
-        """
-        exc_type, exc = sys.exc_info()[:2]
-        if len(exc.args) > 1:
-            offset = exc.args[1]
-            if len(offset) > 2:
-                offset = offset[1:3]
-        else:
-            offset = (1, 0)
-        self.__error(offset[0] - 1, offset[1] or 0,
-                     'M901', exc_type.__name__, exc.args[0])
-    
-    def __generateTree(self):
-        """
-        Private method to generate an AST for our source.
-        
-        @return generated AST
-        @rtype ast.AST
-        """
-        return ast.parse("".join(self.__source), self.__filename)
-    
     def run(self):
         """
         Public method to check the given source against functions
@@ -183,12 +165,6 @@
             # don't do anything, if no codes were selected
             return
         
-        try:
-            self.__tree = self.__generateTree()
-        except (SyntaxError, TypeError):
-            self.__reportInvalidSyntax()
-            return
-        
         visitor = PathlibVisitor(self.__checkForReplacement)
         visitor.visit(self.__tree)
     
@@ -202,12 +178,9 @@
         @param name resolved name of the node
         @type str
         """
-        try:
+        with contextlib.suppress(KeyError):
             errorCode = self.Function2Code[name]
             self.__error(node.lineno - 1, node.col_offset, errorCode)
-        except KeyError:
-            # name is not in our list of replacements
-            pass
 
 
 class PathlibVisitor(ast.NodeVisitor):
@@ -222,7 +195,7 @@
             AST node and the resolved name
         @type func
         """
-        super(PathlibVisitor, self).__init__()
+        super().__init__()
         
         self.__checkCallback = checkCallback
         self.__importAlias = {}
@@ -285,12 +258,10 @@
         @return resolved name
         @rtype str
         """
-        try:
+        with contextlib.suppress(KeyError, IndexError):
             attr = self.__importAlias[self.__names[-1]]
             self.__names[-1] = attr
-        except (KeyError, IndexError):
             # do nothing if there is no such name or the names list is empty
-            pass
         
         return ".".join(reversed(self.__names))
     

eric ide

mercurial