Substitute __import__ with importlib.import_module (#45965)

This commit is contained in:
Massimiliano Culpo 2024-08-23 21:41:26 +02:00 committed by GitHub
parent 906799eec5
commit 47e79c32fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 15 additions and 49 deletions

View File

@ -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 @@
def _python_import(module: str) -> bool:
try:
__import__(module)
importlib.import_module(module)
except ImportError:
return False
return True

View File

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

View File

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

View File

@ -20,6 +20,7 @@
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 @@ def _populate_hooks(cls):
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

View File

@ -15,6 +15,7 @@
import functools
import glob
import hashlib
import importlib
import inspect
import io
import os
@ -868,7 +869,7 @@ def module(cls):
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):

View File

@ -365,9 +365,9 @@ def __init__(self, namespace):
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))

View File

@ -12,7 +12,7 @@
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 @@ def __init__(self, module_patches, class_patches):
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)

View File

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