eric6/Utilities/ModuleParser.py

branch
maintenance
changeset 8273
698ae46f40a4
parent 8142
43248bafe9b2
parent 8260
2161475d9639
child 8400
b3eefd7e58d1
--- a/eric6/Utilities/ModuleParser.py	Fri Apr 02 11:59:41 2021 +0200
+++ b/eric6/Utilities/ModuleParser.py	Sat May 01 14:27:20 2021 +0200
@@ -18,6 +18,7 @@
 import os
 import importlib.machinery
 import re
+import contextlib
 
 import Utilities
 from functools import reduce
@@ -349,7 +350,7 @@
 _modules = {}                           # cache of modules we've seen
 
 
-class VisibilityBase(object):
+class VisibilityBase:
     """
     Class implementing the visibility aspect of all objects.
     """
@@ -396,7 +397,7 @@
         self.visibility = 2
 
 
-class Module(object):
+class Module:
     """
     Class to represent a Python module.
     """
@@ -593,7 +594,7 @@
                     )
                 else:
                     meth_pyqtSig = None
-                lineno = lineno + src.count('\n', last_lineno_pos, start)
+                lineno += src.count('\n', last_lineno_pos, start)
                 last_lineno_pos = start
                 if modifierType and modifierIndent == thisindent:
                     if modifierType == "@staticmethod":
@@ -708,7 +709,7 @@
             elif m.start("Class") >= 0:
                 # we found a class definition
                 thisindent = _indent(m.group("ClassIndent"))
-                lineno = lineno + src.count('\n', last_lineno_pos, start)
+                lineno += src.count('\n', last_lineno_pos, start)
                 last_lineno_pos = start
                 # close all classes indented at least as much
                 while classstack and classstack[-1][1] >= thisindent:
@@ -772,7 +773,7 @@
                 classstack.append((cur_class, thisindent))
             
             elif m.start("Attribute") >= 0:
-                lineno = lineno + src.count('\n', last_lineno_pos, start)
+                lineno += src.count('\n', last_lineno_pos, start)
                 last_lineno_pos = start
                 index = -1
                 while index >= -len(classstack):
@@ -788,7 +789,7 @@
             
             elif m.start("Main") >= 0:
                 # 'main' part of the script, reset class stack
-                lineno = lineno + src.count('\n', last_lineno_pos, start)
+                lineno += src.count('\n', last_lineno_pos, start)
                 last_lineno_pos = start
                 classstack = []
             
@@ -796,7 +797,7 @@
                 thisindent = _indent(m.group("VariableIndent"))
                 variable_name = m.group("VariableName")
                 isSignal = m.group("VariableSignal") != ""
-                lineno = lineno + src.count('\n', last_lineno_pos, start)
+                lineno += src.count('\n', last_lineno_pos, start)
                 last_lineno_pos = start
                 if thisindent == 0:
                     # global variable
@@ -863,9 +864,8 @@
                 conditionalsstack.append(thisindent)
                 deltaindentcalculated = 0
             
-            elif m.start("Comment") >= 0:
-                if modulelevel:
-                    continue
+            elif m.start("Comment") >= 0 and modulelevel:
+                continue
             
             modulelevel = False
     
@@ -874,7 +874,8 @@
         Private method to scan the source text of a Python module and retrieve
         the relevant information.
         
-        @param src the source text to be scanned (string)
+        @param src the source text to be scanned
+        @type str
         """
         lineno, last_lineno_pos = 1, 0
         classstack = []  # stack of (class, indent) pairs
@@ -900,7 +901,7 @@
                 )
                 meth_sig = m.group("MethodSignature")
                 meth_sig = meth_sig and meth_sig.replace('\\\n', '') or ''
-                lineno = lineno + src.count('\n', last_lineno_pos, start)
+                lineno += src.count('\n', last_lineno_pos, start)
                 last_lineno_pos = start
                 if meth_name.startswith('self.'):
                     meth_name = meth_name[5:]
@@ -927,10 +928,7 @@
                         if cur_class is None:
                             continue
                         
-                        if (
-                            isinstance(cur_class, Class) or
-                            isinstance(cur_class, RbModule)
-                        ):
+                        if isinstance(cur_class, (Class, RbModule)):
                             # it's a class/module method
                             f = Function(None, meth_name,
                                          None, lineno, meth_sig)
@@ -971,20 +969,11 @@
                 if cur_obj:
                     cur_obj.addDescription(contents)
             
-            elif m.start("String") >= 0:
-                pass
-            
-            elif m.start("Comment") >= 0:
-                pass
-            
-            elif m.start("ClassIgnored") >= 0:
-                pass
-            
             elif m.start("Class") >= 0:
                 # we found a class definition
                 thisindent = indent
                 indent += 1
-                lineno = lineno + src.count('\n', last_lineno_pos, start)
+                lineno += src.count('\n', last_lineno_pos, start)
                 last_lineno_pos = start
                 # close all classes/modules indented at least as much
                 while classstack and classstack[-1][1] >= thisindent:
@@ -1035,7 +1024,7 @@
                 # we found a module definition
                 thisindent = indent
                 indent += 1
-                lineno = lineno + src.count('\n', last_lineno_pos, start)
+                lineno += src.count('\n', last_lineno_pos, start)
                 last_lineno_pos = start
                 # close all classes/modules indented at least as much
                 while classstack and classstack[-1][1] >= thisindent:
@@ -1087,7 +1076,7 @@
                         if (
                             classstack[index][0] is not None and
                             not isinstance(classstack[index][0], Function) and
-                            not classstack[index][1] >= indent
+                            classstack[index][1] < indent
                         ):
                             parent = classstack[index][0]
                             actype = (
@@ -1112,14 +1101,14 @@
                             index -= 1
 
             elif m.start("Attribute") >= 0:
-                lineno = lineno + src.count('\n', last_lineno_pos, start)
+                lineno += src.count('\n', last_lineno_pos, start)
                 last_lineno_pos = start
                 index = -1
                 while index >= -len(classstack):
                     if (
                         classstack[index][0] is not None and
                         not isinstance(classstack[index][0], Function) and
-                        not classstack[index][1] >= indent
+                        classstack[index][1] < indent
                     ):
                         attrName = m.group("AttributeName")
                         attr = Attribute(
@@ -1142,14 +1131,14 @@
                     lastGlobalEntry = None
             
             elif m.start("Attr") >= 0:
-                lineno = lineno + src.count('\n', last_lineno_pos, start)
+                lineno += src.count('\n', last_lineno_pos, start)
                 last_lineno_pos = start
                 index = -1
                 while index >= -len(classstack):
                     if (
                         classstack[index][0] is not None and
                         not isinstance(classstack[index][0], Function) and
-                        not classstack[index][1] >= indent
+                        classstack[index][1] < indent
                     ):
                         parent = classstack[index][0]
                         if m.group("AttrType") is None:
@@ -1183,10 +1172,7 @@
                                 )
                                 if access == "_accessor":
                                     attr.setPublic()
-                                elif (
-                                    access == "_reader" or
-                                    access == "_writer"
-                                ):
+                                elif access in ("_reader", "_writer"):
                                     if attr.isPrivate():
                                         attr.setProtected()
                                     elif attr.isProtected():
@@ -1211,7 +1197,12 @@
                     else:
                         indent = 0
             
-            elif m.start("BeginEnd") >= 0:
+            elif (
+                m.start("String") >= 0 or
+                m.start("Comment") >= 0 or
+                m.start("ClassIgnored") >= 0 or
+                m.start("BeginEnd") >= 0
+            ):
                 pass
     
     def createHierarchy(self):
@@ -1549,14 +1540,13 @@
     """
     global _modules
     
-    if extensions is None:
-        _extensions = ['.py', '.pyw', '.ptl', '.rb']
-    else:
-        _extensions = extensions[:]
-    try:
+    _extensions = (
+        ['.py', '.pyw', '.ptl', '.rb']
+        if extensions is None else
+        extensions[:]
+    )
+    with contextlib.suppress(ValueError):
         _extensions.remove('.py')
-    except ValueError:
-        pass
     
     modname = module
     
@@ -1617,11 +1607,9 @@
         return _modules[modname]
     
     mod = Module(modname, file, moduleType)
-    try:
+    with contextlib.suppress(UnicodeError, OSError):
         src = Utilities.readEncodedFile(file)[0]
         mod.scan(src)
-    except (UnicodeError, OSError):
-        pass
     if caching:
         _modules[modname] = mod
     return mod
@@ -1661,15 +1649,24 @@
                     pathname = os.path.join(p, name)
                     if ext == '.ptl':
                         # Quixote page template
-                        return (open(pathname), pathname,
-                                ('.ptl', 'r', PTL_SOURCE))
+                        return (
+                            open(pathname), pathname,
+                            # __IGNORE_WARNING_Y115__
+                            ('.ptl', 'r', PTL_SOURCE)
+                        )
                     elif ext == '.rb':
                         # Ruby source file
-                        return (open(pathname), pathname,
-                                ('.rb', 'r', RB_SOURCE))
+                        return (
+                            open(pathname), pathname,
+                            # __IGNORE_WARNING_Y115__
+                            ('.rb', 'r', RB_SOURCE)
+                        )
                     else:
-                        return (open(pathname), pathname,
-                                (ext, 'r', PY_SOURCE))
+                        return (
+                            open(pathname), pathname,
+                            # __IGNORE_WARNING_Y115__
+                            (ext, 'r', PY_SOURCE)
+                        )
             raise ImportError
     
     # standard Python module file
@@ -1682,6 +1679,7 @@
     if isinstance(spec.loader, importlib.machinery.SourceFileLoader):
         ext = os.path.splitext(spec.origin)[-1]
         return (open(spec.origin), spec.origin, (ext, 'r', PY_SOURCE))
+        # __IGNORE_WARNING_Y115__
     
     raise ImportError
 

eric ide

mercurial