diff --git a/platformio/compat.py b/platformio/compat.py index 712f65bc..1359bdad 100644 --- a/platformio/compat.py +++ b/platformio/compat.py @@ -29,6 +29,8 @@ def get_filesystem_encoding(): if PY2: + import imp + # pylint: disable=undefined-variable string_types = (str, unicode) @@ -76,8 +78,12 @@ if PY2: pathname = _magic_check.sub(r"[\1]", pathname) return drive + pathname + def load_python_module(name, pathname): + return imp.load_source(name, pathname) + else: + import importlib.util from glob import escape as glob_escape # pylint: disable=no-name-in-module string_types = (str,) @@ -107,3 +113,9 @@ else: if isinstance(obj, string_types): return obj return json.dumps(obj, ensure_ascii=False, sort_keys=True) + + def load_python_module(name, pathname): + spec = importlib.util.spec_from_file_location(name, pathname) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module diff --git a/platformio/managers/platform.py b/platformio/managers/platform.py index 4ab20b8d..124a4245 100644 --- a/platformio/managers/platform.py +++ b/platformio/managers/platform.py @@ -16,14 +16,13 @@ import base64 import os import re import sys -from imp import load_source from os.path import basename, dirname, isdir, isfile, join import click import semantic_version from platformio import __version__, app, exception, fs, util -from platformio.compat import PY2, hashlib_encode_data, is_bytes +from platformio.compat import PY2, hashlib_encode_data, is_bytes, load_python_module from platformio.managers.core import get_core_package_dir from platformio.managers.package import BasePkgManager, PackageManager from platformio.proc import ( @@ -230,12 +229,10 @@ class PlatformFactory(object): @staticmethod def load_module(name, path): - module = None try: - module = load_source("platformio.managers.platform.%s" % name, path) + return load_python_module("platformio.managers.platform.%s" % name, path) except ImportError: raise exception.UnknownPlatform(name) - return module @classmethod def newPlatform(cls, name, requirements=None):