Skip to content

Commit

Permalink
Substitute __import__ with importlib.import_module (spack#45965)
Browse files Browse the repository at this point in the history
  • Loading branch information
alalazo authored Aug 23, 2024
1 parent 906799e commit 47e79c3
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 49 deletions.
3 changes: 2 additions & 1 deletion lib/spack/spack/bootstrap/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,7 +29,7 @@

def _python_import(module: str) -> bool:
try:
__import__(module)
importlib.import_module(module)
except ImportError:
return False
return True
Expand Down
5 changes: 3 additions & 2 deletions lib/spack/spack/cmd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

import argparse
import importlib
import os
import re
import sys
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion lib/spack/spack/compilers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
system and configuring Spack to use multiple compilers.
"""
import collections
import importlib
import os
import sys
import warnings
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 2 additions & 5 deletions lib/spack/spack/hooks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/spack/spack/package_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import functools
import glob
import hashlib
import importlib
import inspect
import io
import os
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions lib/spack/spack/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions lib/spack/spack/subprocess_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
35 changes: 0 additions & 35 deletions lib/spack/spack/util/classes.py

This file was deleted.

0 comments on commit 47e79c3

Please sign in to comment.