From ff04d1bfc186a0563728707a3dc08ebdfc1a6f59 Mon Sep 17 00:00:00 2001 From: Massimiliano Culpo Date: Fri, 8 Apr 2022 00:58:20 +0200 Subject: Use the non-deprecated `MetaPathFinder` interface (#29745) * Extract the MetaPathFinder and Loaders for packages in their own classes https://peps.python.org/pep-0451/ Currently, RepoPath and Repo implement the (deprecated) interface of MetaPathFinder (find_module) and of Loader (load_module). This commit extracts both of them and places the code in their own classes. The MetaPathFinder interface is updated to contain both the deprecated "find_module" (for Python 2.7 support) and the recommended "find_spec". Update of the Loader interface is deferred at a subsequent commit. * Move the lines to be prepended inside "RepoLoader" Also adjust the naming of a few variables too * Remove spack.util.imp, since code is only used in spack.repo * Remove support from loading Python modules Python > 3 but < 3.5 * Remove `Repo._create_namespace` This function was interacting badly with the MetaPathFinder and causing issues with "normal" imports. Removing the function allows to do things like: ```python import spack.pkg.builtin.mpich cls = spack.pkg.builtin.mpich.Mpich ``` * Remove code needed to trigger the Singleton evaluation The finder is coded in a way to trigger the Singleton, so we don't need external code now that we register it at module level into `sys.meta_path`. * Add unit tests --- share/spack/qa/run-unit-tests | 3 +++ 1 file changed, 3 insertions(+) (limited to 'share') diff --git a/share/spack/qa/run-unit-tests b/share/spack/qa/run-unit-tests index 25ff09a35e..32c69b94a5 100755 --- a/share/spack/qa/run-unit-tests +++ b/share/spack/qa/run-unit-tests @@ -40,6 +40,9 @@ bin/spack help -a spack -p --lines 20 spec mpileaks%gcc ^dyninst@10.0.0 ^elfutils@0.170 $coverage_run $(which spack) bootstrap status --dev --optional +# Check that we can import Spack packages directly as a first import +$coverage_run $(which spack) python -c "import spack.pkg.builtin.mpileaks; repr(spack.pkg.builtin.mpileaks.Mpileaks)" + #----------------------------------------------------------- # Run unit tests with code coverage #----------------------------------------------------------- -- cgit v1.2.3-70-g09d2