Skip to content

Commit

Permalink
Stop using the deprecated imp module
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Algarvio <[email protected]>
  • Loading branch information
s0undt3ch committed Feb 22, 2023
1 parent d54318d commit 5ea8dc5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 37 deletions.
37 changes: 12 additions & 25 deletions salt/utils/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
Template render systems
"""
import codecs
import importlib.machinery
import importlib.util
import logging
import os
import pathlib
import sys
import tempfile
import traceback
from pathlib import Path

import jinja2
import jinja2.ext
Expand All @@ -31,17 +33,6 @@
from salt.utils.odict import OrderedDict
from salt.utils.versions import Version

if sys.version_info[:2] >= (3, 5):
import importlib.machinery # pylint: disable=no-name-in-module,import-error
import importlib.util # pylint: disable=no-name-in-module,import-error

USE_IMPORTLIB = True
else:
import imp

USE_IMPORTLIB = False


log = logging.getLogger(__name__)


Expand Down Expand Up @@ -118,7 +109,7 @@ def generate_sls_context(tmplpath, sls):

if tmplpath:
# Normalize template path
template = str(Path(tmplpath).as_posix())
template = str(pathlib.Path(tmplpath).as_posix())

# Determine proper template name without root
if not sls:
Expand Down Expand Up @@ -663,18 +654,14 @@ def py(sfn, string=False, **kwargs): # pylint: disable=C0103
base_fname = os.path.basename(sfn)
name = base_fname.split(".")[0]

if USE_IMPORTLIB:
# pylint: disable=no-member
loader = importlib.machinery.SourceFileLoader(name, sfn)
spec = importlib.util.spec_from_file_location(name, sfn, loader=loader)
if spec is None:
raise ImportError()
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
# pylint: enable=no-member
sys.modules[name] = mod
else:
mod = imp.load_source(name, sfn)
loader = importlib.machinery.SourceFileLoader(name, sfn)
spec = importlib.util.spec_from_file_location(name, sfn, loader=loader)
if spec is None:
raise ImportError()
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
# pylint: enable=no-member
sys.modules[name] = mod

# File templates need these set as __var__
if "__env__" not in kwargs and "saltenv" in kwargs:
Expand Down
17 changes: 8 additions & 9 deletions tests/unit/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import collections
import compileall
import copy
import imp
import inspect
import logging
import os
Expand All @@ -35,15 +34,15 @@

def remove_bytecode(module_path):
paths = [module_path + "c"]
if hasattr(imp, "get_tag"):
modname, ext = os.path.splitext(module_path.split(os.sep)[-1])
paths.append(
os.path.join(
os.path.dirname(module_path),
"__pycache__",
"{}.{}.pyc".format(modname, imp.get_tag()),
)
cache_tag = sys.implementation.cache_tag
modname, ext = os.path.splitext(module_path.split(os.sep)[-1])
paths.append(
os.path.join(
os.path.dirname(module_path),
"__pycache__",
"{}.{}.pyc".format(modname, cache_tag),
)
)
for path in paths:
if os.path.exists(path):
os.unlink(path)
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/utils/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import posixpath
import sys
import tempfile
import types

import pytest

Expand Down Expand Up @@ -79,14 +80,12 @@ def test_mixed_unicode_and_binary(self):
assert actual == expected

def __patch_path(self):
import imp

modules = list(self.BUILTIN_MODULES[:])
modules.pop(modules.index("posix"))
modules.append("nt")

code = """'''Salt unittest loaded NT module'''"""
module = imp.new_module("nt")
module = types.ModuleType("nt")
exec(code, module.__dict__)
sys.modules["nt"] = module

Expand Down

0 comments on commit 5ea8dc5

Please sign in to comment.