Fixed "DeprecationWarning: the imp module is deprecated in favour of importlib" PY2/PY3

This commit is contained in:
Ivan Kravets
2019-09-24 00:17:08 +03:00
parent 392fe1cbd0
commit ca29b4e370
2 changed files with 14 additions and 5 deletions

View File

@ -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

View File

@ -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):