Removed use of deprecated 'imp' module in the frontend. multi_processing

Sat, 08 Feb 2020 16:52:48 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 08 Feb 2020 16:52:48 +0100
branch
multi_processing
changeset 7402
bc62b4e1aed4
parent 7397
f8d2f6dd6636
child 7403
7446a7eacfc3

Removed use of deprecated 'imp' module in the frontend.

eric6/PluginManager/PluginManager.py file | annotate | diff | comparison | revisions
eric6/PluginManager/PluginUninstallDialog.py file | annotate | diff | comparison | revisions
eric6/Utilities/ClassBrowsers/__init__.py file | annotate | diff | comparison | revisions
eric6/Utilities/ClassBrowsers/pyclbr.py file | annotate | diff | comparison | revisions
eric6/Utilities/ModuleParser.py file | annotate | diff | comparison | revisions
diff -r f8d2f6dd6636 -r bc62b4e1aed4 eric6/PluginManager/PluginManager.py
--- a/eric6/PluginManager/PluginManager.py	Thu Feb 06 19:04:28 2020 +0100
+++ b/eric6/PluginManager/PluginManager.py	Sat Feb 08 16:52:48 2020 +0100
@@ -9,8 +9,9 @@
 
 import os
 import sys
-import imp
 import zipfile
+import types
+import importlib
 
 from PyQt5.QtCore import (
     pyqtSignal, QObject, QDate, QFile, QFileInfo, QUrl, QIODevice
@@ -400,7 +401,10 @@
         """
         try:
             fname = "{0}.py".format(os.path.join(directory, name))
-            module = imp.load_source(name, fname)
+            spec = importlib.util.spec_from_file_location(name, fname)
+            module = importlib.util.module_from_spec(spec)
+            sys.modules[module.__name__] = module
+            spec.loader.exec_module(module)
             if not hasattr(module, "autoactivate"):
                 module.error = self.tr(
                     "Module is missing the 'autoactivate' attribute.")
@@ -425,7 +429,7 @@
             module.eric6PluginModuleFilename = fname
             self.__modulesCount += 1
             if reload_:
-                imp.reload(module)
+                importlib.reload(module)
                 self.initOnDemandPlugin(name)
                 try:
                     pluginObject = self.__onDemandInactivePlugins[name]
@@ -436,7 +440,7 @@
         except PluginLoadError:
             print("Error loading plug-in module:", name)
         except Exception as err:
-            module = imp.new_module(name)
+            module = types.ModuleType(name)
             module.error = self.tr(
                 "Module failed to load. Error: {0}").format(str(err))
             self.__failedModules[name] = module
diff -r f8d2f6dd6636 -r bc62b4e1aed4 eric6/PluginManager/PluginUninstallDialog.py
--- a/eric6/PluginManager/PluginUninstallDialog.py	Thu Feb 06 19:04:28 2020 +0100
+++ b/eric6/PluginManager/PluginUninstallDialog.py	Sat Feb 08 16:52:48 2020 +0100
@@ -10,7 +10,7 @@
 
 import sys
 import os
-import imp
+import importlib
 import shutil
 import glob
 
@@ -116,7 +116,9 @@
         
         if pluginDirectory not in sys.path:
             sys.path.insert(2, pluginDirectory)
-        module = imp.load_source(pluginName, pluginFile)
+        spec = importlib.util.spec_from_file_location(pluginName, pluginFile)
+        module = importlib.util.module_from_spec(spec)
+        spec.loader.exec_module(module)
         if not hasattr(module, "packageName"):
             E5MessageBox.critical(
                 self,
diff -r f8d2f6dd6636 -r bc62b4e1aed4 eric6/Utilities/ClassBrowsers/__init__.py
--- a/eric6/Utilities/ClassBrowsers/__init__.py	Thu Feb 06 19:04:28 2020 +0100
+++ b/eric6/Utilities/ClassBrowsers/__init__.py	Sat Feb 08 16:52:48 2020 +0100
@@ -21,11 +21,11 @@
 
 
 import os
-import imp
+import importlib
 
 import Preferences
 
-PY_SOURCE = imp.PY_SOURCE
+PY_SOURCE = 1
 PTL_SOURCE = 128
 RB_SOURCE = 129
 IDL_SOURCE = 130
@@ -154,23 +154,27 @@
                 return (open(pathname), pathname, (ext, 'r', PTL_SOURCE))
         raise ImportError
     
+    elif (
+        name.lower().endswith(
+            tuple(Preferences.getPython("PythonExtensions") +
+                  Preferences.getPython("Python3Extensions"))) or
+        isPyFile
+    ):
+        for p in path:      # search in path
+            pathname = os.path.join(p, name)
+            if os.path.exists(pathname):
+                return (open(pathname), pathname, (ext, 'r', PY_SOURCE))
+    raise ImportError
+    
+    # standard Python module file
     if name.lower().endswith('.py'):
         name = name[:-3]
     
-    try:
-        return imp.find_module(name, path)
-    except ImportError:
-        if (
-            name.lower().endswith(
-                tuple(Preferences.getPython("PythonExtensions") +
-                      Preferences.getPython("Python3Extensions"))) or
-            isPyFile
-        ):
-            for p in path:      # search in path
-                pathname = os.path.join(p, name)
-                if os.path.exists(pathname):
-                    return (open(pathname), pathname, (ext, 'r', PY_SOURCE))
+    spec = importlib.machinery.PathFinder.find_spec(name, path)
+    if spec is None:
         raise ImportError
-    except SyntaxError:
-        # re-raise as an import error
-        raise ImportError
+    if isinstance(spec.loader, importlib.machinery.SourceFileLoader):
+        ext = os.path.splitext(spec.origin)[-1]
+        return (open(spec.origin), spec.origin, (ext, 'r', PY_SOURCE))
+    
+    raise ImportError
diff -r f8d2f6dd6636 -r bc62b4e1aed4 eric6/Utilities/ClassBrowsers/pyclbr.py
--- a/eric6/Utilities/ClassBrowsers/pyclbr.py	Thu Feb 06 19:04:28 2020 +0100
+++ b/eric6/Utilities/ClassBrowsers/pyclbr.py	Sat Feb 08 16:52:48 2020 +0100
@@ -14,7 +14,6 @@
 
 
 import sys
-import imp
 import re
 
 import Utilities
@@ -370,14 +369,6 @@
         fullpath = path[:] + sys.path[:]
         f, file, (suff, mode, type) = ClassBrowsers.find_module(
             module, fullpath, isPyFile)
-    if module.endswith(".py") and type == imp.PKG_DIRECTORY:
-        return dictionary
-    if type == imp.PKG_DIRECTORY:
-        dictionary['__path__'] = [file]
-        _modules[module] = dictionary
-        path = [file] + path
-        f, file, (suff, mode, type) = ClassBrowsers.find_module(
-            '__init__', [file])
     if f:
         f.close()
     if type not in SUPPORTED_TYPES:
diff -r f8d2f6dd6636 -r bc62b4e1aed4 eric6/Utilities/ModuleParser.py
--- a/eric6/Utilities/ModuleParser.py	Thu Feb 06 19:04:28 2020 +0100
+++ b/eric6/Utilities/ModuleParser.py	Sat Feb 08 16:52:48 2020 +0100
@@ -19,7 +19,7 @@
 
 import sys
 import os
-import imp
+import importlib
 import re
 
 import Utilities
@@ -31,15 +31,17 @@
 
 TABWIDTH = 4
 
+SEARCH_ERROR = 0
+PY_SOURCE = 1
 PTL_SOURCE = 128
 RB_SOURCE = 129
 
-SUPPORTED_TYPES = [imp.PY_SOURCE, PTL_SOURCE, RB_SOURCE]
+SUPPORTED_TYPES = [PY_SOURCE, PTL_SOURCE, RB_SOURCE]
 TYPE_MAPPING = {
-    "Python": imp.PY_SOURCE,
-    "Python2": imp.PY_SOURCE,
-    "Python3": imp.PY_SOURCE,
-    "MicroPython": imp.PY_SOURCE,
+    "Python": PY_SOURCE,
+    "Python2": PY_SOURCE,
+    "Python3": PY_SOURCE,
+    "MicroPython": PY_SOURCE,
     "Ruby": RB_SOURCE,
 }
 
@@ -423,7 +425,7 @@
         self.from_imports = {}
         self.package = '.'.join(name.split('.')[:-1])
         self.type = moduleType
-        if moduleType in [imp.PY_SOURCE, PTL_SOURCE]:
+        if moduleType in [PY_SOURCE, PTL_SOURCE]:
             self._getnext = _py_getnext
         elif moduleType == RB_SOURCE:
             self._getnext = _rb_getnext
@@ -499,7 +501,7 @@
         
         @param src the source text to be scanned (string)
         """
-        if self.type in [imp.PY_SOURCE, PTL_SOURCE]:
+        if self.type in [PY_SOURCE, PTL_SOURCE]:
             self.__py_scan(src)
         elif self.type == RB_SOURCE:
             self.__rb_scan(src)
@@ -1277,7 +1279,7 @@
         
         @return type of the modules's source (string)
         """
-        if self.type in [imp.PY_SOURCE, PTL_SOURCE]:
+        if self.type in [PY_SOURCE, PTL_SOURCE]:
             py3ExtList = Preferences.getDebugger("Python3Extensions").split()
             if self.file.endswith(tuple(py3ExtList)):
                 moduleType = "Python3"
@@ -1650,14 +1652,21 @@
                                 ('.rb', 'r', RB_SOURCE))
                     else:
                         return (open(pathname), pathname,
-                                (ext, 'r', imp.PY_SOURCE))
+                                (ext, 'r', PY_SOURCE))
             raise ImportError
     
     # standard Python module file
     if name.lower().endswith('.py'):
         name = name[:-3]
     
-    return imp.find_module(name, path)
+    spec = importlib.machinery.PathFinder.find_spec(name, path)
+    if spec is None:
+        raise ImportError
+    if isinstance(spec.loader, importlib.machinery.SourceFileLoader):
+        ext = os.path.splitext(spec.origin)[-1]
+        return (open(spec.origin), spec.origin, (ext, 'r', PY_SOURCE))
+    
+    raise ImportError
 
 
 def resetParsedModules():

eric ide

mercurial