diff options
author | Massimiliano Culpo <massimiliano.culpo@gmail.com> | 2024-08-23 21:41:26 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-23 21:41:26 +0200 |
commit | 47e79c32fdd820b3ce825f62619c693e4a57d549 (patch) | |
tree | bbb4e5b42cb558e227fc7831496bafe1731210eb | |
parent | 906799eec510527077422cb913997bb3eec56aa3 (diff) | |
download | spack-47e79c32fdd820b3ce825f62619c693e4a57d549.tar.gz spack-47e79c32fdd820b3ce825f62619c693e4a57d549.tar.bz2 spack-47e79c32fdd820b3ce825f62619c693e4a57d549.tar.xz spack-47e79c32fdd820b3ce825f62619c693e4a57d549.zip |
Substitute `__import__` with `importlib.import_module` (#45965)
-rw-r--r-- | lib/spack/spack/bootstrap/_common.py | 3 | ||||
-rw-r--r-- | lib/spack/spack/cmd/__init__.py | 5 | ||||
-rw-r--r-- | lib/spack/spack/compilers/__init__.py | 3 | ||||
-rw-r--r-- | lib/spack/spack/hooks/__init__.py | 7 | ||||
-rw-r--r-- | lib/spack/spack/package_base.py | 3 | ||||
-rw-r--r-- | lib/spack/spack/repo.py | 4 | ||||
-rw-r--r-- | lib/spack/spack/subprocess_context.py | 4 | ||||
-rw-r--r-- | lib/spack/spack/util/classes.py | 35 |
8 files changed, 15 insertions, 49 deletions
diff --git a/lib/spack/spack/bootstrap/_common.py b/lib/spack/spack/bootstrap/_common.py index 2afc6ea17c..e56890f22b 100644 --- a/lib/spack/spack/bootstrap/_common.py +++ b/lib/spack/spack/bootstrap/_common.py @@ -4,6 +4,7 @@ # SPDX-License-Identifier: (Apache-2.0 OR MIT) """Common basic functions used through the spack.bootstrap package""" import fnmatch +import importlib import os.path import re import sys @@ -28,7 +29,7 @@ QueryInfo = Dict[str, "spack.spec.Spec"] def _python_import(module: str) -> bool: try: - __import__(module) + importlib.import_module(module) except ImportError: return False return True diff --git a/lib/spack/spack/cmd/__init__.py b/lib/spack/spack/cmd/__init__.py index 48f8b5b9c1..362dcb8d93 100644 --- a/lib/spack/spack/cmd/__init__.py +++ b/lib/spack/spack/cmd/__init__.py @@ -4,6 +4,7 @@ # SPDX-License-Identifier: (Apache-2.0 OR MIT) import argparse +import importlib import os import re import sys @@ -114,8 +115,8 @@ def get_module(cmd_name): try: # Try to import the command from the built-in directory - module_name = "%s.%s" % (__name__, pname) - module = __import__(module_name, fromlist=[pname, SETUP_PARSER, DESCRIPTION], level=0) + module_name = f"{__name__}.{pname}" + module = importlib.import_module(module_name) tty.debug("Imported {0} from built-in commands".format(pname)) except ImportError: module = spack.extensions.get_module(cmd_name) diff --git a/lib/spack/spack/compilers/__init__.py b/lib/spack/spack/compilers/__init__.py index a42cd82d9d..b44fa92ab2 100644 --- a/lib/spack/spack/compilers/__init__.py +++ b/lib/spack/spack/compilers/__init__.py @@ -7,6 +7,7 @@ system and configuring Spack to use multiple compilers. """ import collections +import importlib import os import sys import warnings @@ -651,7 +652,7 @@ def class_for_compiler_name(compiler_name): submodule_name = compiler_name.replace("-", "_") module_name = ".".join(["spack", "compilers", submodule_name]) - module_obj = __import__(module_name, fromlist=[None]) + module_obj = importlib.import_module(module_name) cls = getattr(module_obj, mod_to_class(compiler_name)) # make a note of the name in the module so we can get to it easily. diff --git a/lib/spack/spack/hooks/__init__.py b/lib/spack/spack/hooks/__init__.py index 029f9ca7ba..0e7e303692 100644 --- a/lib/spack/spack/hooks/__init__.py +++ b/lib/spack/spack/hooks/__init__.py @@ -20,6 +20,7 @@ This can be used to implement support for things like module systems (e.g. modules, lmod, etc.) or to add other custom features. """ +import importlib from llnl.util.lang import ensure_last, list_modules @@ -46,11 +47,7 @@ class _HookRunner: for name in relative_names: module_name = __name__ + "." + name - # When importing a module from a package, __import__('A.B', ...) - # returns package A when 'fromlist' is empty. If fromlist is not - # empty it returns the submodule B instead - # See: https://stackoverflow.com/a/2725668/771663 - module_obj = __import__(module_name, fromlist=[None]) + module_obj = importlib.import_module(module_name) cls._hooks.append((module_name, module_obj)) @property diff --git a/lib/spack/spack/package_base.py b/lib/spack/spack/package_base.py index 9b594f7f39..f4b7c2b4c1 100644 --- a/lib/spack/spack/package_base.py +++ b/lib/spack/spack/package_base.py @@ -15,6 +15,7 @@ import copy import functools import glob import hashlib +import importlib import inspect import io import os @@ -868,7 +869,7 @@ class PackageBase(WindowsRPath, PackageViewMixin, RedistributionMixin, metaclass We use this to add variables to package modules. This makes install() methods easier to write (e.g., can call configure()) """ - return __import__(cls.__module__, fromlist=[cls.__name__]) + return importlib.import_module(cls.__module__) @classproperty def namespace(cls): diff --git a/lib/spack/spack/repo.py b/lib/spack/spack/repo.py index 1a8f3fb6e8..d565910289 100644 --- a/lib/spack/spack/repo.py +++ b/lib/spack/spack/repo.py @@ -365,9 +365,9 @@ class SpackNamespace(types.ModuleType): def __getattr__(self, name): """Getattr lazily loads modules if they're not already loaded.""" - submodule = self.__package__ + "." + name + submodule = f"{self.__package__}.{name}" try: - setattr(self, name, __import__(submodule)) + setattr(self, name, importlib.import_module(submodule)) except ImportError: msg = "'{0}' object has no attribute {1}" raise AttributeError(msg.format(type(self), name)) diff --git a/lib/spack/spack/subprocess_context.py b/lib/spack/spack/subprocess_context.py index cd0c24676d..f96e530e97 100644 --- a/lib/spack/spack/subprocess_context.py +++ b/lib/spack/spack/subprocess_context.py @@ -12,7 +12,7 @@ installations performed in Spack unit tests may include additional modifications to global state in memory that must be replicated in the child process. """ - +import importlib import io import multiprocessing import pickle @@ -118,7 +118,7 @@ class TestPatches: def restore(self): for module_name, attr_name, value in self.module_patches: value = pickle.load(value) - module = __import__(module_name) + module = importlib.import_module(module_name) setattr(module, attr_name, value) for class_fqn, attr_name, value in self.class_patches: value = pickle.load(value) diff --git a/lib/spack/spack/util/classes.py b/lib/spack/spack/util/classes.py deleted file mode 100644 index 5e0b373ef8..0000000000 --- a/lib/spack/spack/util/classes.py +++ /dev/null @@ -1,35 +0,0 @@ -# Copyright 2013-2024 Lawrence Livermore National Security, LLC and other -# Spack Project Developers. See the top-level COPYRIGHT file for details. -# -# SPDX-License-Identifier: (Apache-2.0 OR MIT) -import inspect - -import llnl.util.tty as tty -from llnl.util.lang import list_modules, memoized - -from spack.util.naming import mod_to_class - -__all__ = ["list_classes"] - - -@memoized -def list_classes(parent_module, mod_path): - """Given a parent path (e.g., spack.platforms or spack.analyzers), - use list_modules to derive the module names, and then mod_to_class - to derive class names. Import the classes and return them in a list - """ - classes = [] - - for name in list_modules(mod_path): - mod_name = "%s.%s" % (parent_module, name) - class_name = mod_to_class(name) - mod = __import__(mod_name, fromlist=[class_name]) - if not hasattr(mod, class_name): - tty.die("No class %s defined in %s" % (class_name, mod_name)) - cls = getattr(mod, class_name) - if not inspect.isclass(cls): - tty.die("%s.%s is not a class" % (mod_name, class_name)) - - classes.append(cls) - - return classes |