changeset cc62e47041e0 in tryton:5.2
details: https://hg.tryton.org/tryton?cmd=changeset;node=cc62e47041e0
description:
        Use FileFinder and SourceFileLoader to load plugins

        On frozen environment using import_module does not work because the 
plugins
        are not in the PYTHONPATH.

        issue8334
        review253491002
        (grafted from f20c2e42084ad4370f58793ca88bc93d0e4759a8)
diffstat:

 tryton/plugins/__init__.py |  12 ++++++++++--
 1 files changed, 10 insertions(+), 2 deletions(-)

diffs (29 lines):

diff -r 23592c13e192 -r cc62e47041e0 tryton/plugins/__init__.py
--- a/tryton/plugins/__init__.py        Mon May 06 14:46:44 2019 +0200
+++ b/tryton/plugins/__init__.py        Mon May 13 22:39:27 2019 +0200
@@ -23,15 +23,23 @@
 
     imported = set()
     for path in paths:
+        finder = importlib.machinery.FileFinder(
+            path, (
+                importlib.machinery.SourceFileLoader,
+                importlib.machinery.SOURCE_SUFFIXES))
         for plugin in os.listdir(path):
             module = os.path.splitext(plugin)[0]
             if (module.startswith('_') or module in imported):
                 continue
             module = 'tryton.plugins.%s' % module
+            spec = finder.find_spec(module)
+            if not spec:
+                continue
+            module = importlib.util.module_from_spec(spec)
             try:
-                module = importlib.import_module(module)
-                MODULES.append(module)
+                spec.loader.exec_module(module)
             except ImportError:
                 continue
             else:
+                MODULES.append(module)
                 imported.add(module.__name__)

Reply via email to