diff --git a/Framework/PythonInterface/mantid/kernel/plugins.py b/Framework/PythonInterface/mantid/kernel/plugins.py index 602a5850107f..713d88ef8720 100644 --- a/Framework/PythonInterface/mantid/kernel/plugins.py +++ b/Framework/PythonInterface/mantid/kernel/plugins.py @@ -13,6 +13,8 @@ import os as _os from traceback import format_exc +import sys +import importlib.util from importlib.machinery import SourceFileLoader from . import logger, Logger, config @@ -44,7 +46,16 @@ def run(self): name = _os.path.basename(pathname) # Including extension name = _os.path.splitext(name)[0] self._logger.debug("Loading python plugin %s" % pathname) - return SourceFileLoader(name, pathname).load_module() + loader = SourceFileLoader(name, pathname) + spec = importlib.util.spec_from_loader(name, loader) + module = importlib.util.module_from_spec(spec) + loader.exec_module(module) + # It's better to let import handle editing sys.modules, but this code used to call + # load_module, which would edit sys.modules, but now load_module is deprecated. + # We edit sys.modules here so that legacy user scripts will not have to be + # edited in order to keep working. + sys.modules[name] = module + return module # ====================================================================================================================== diff --git a/Framework/PythonInterface/test/python/mantid/kernel/ConfigServiceTest.py b/Framework/PythonInterface/test/python/mantid/kernel/ConfigServiceTest.py index ced0f26bc87b..40c8c79b01ac 100644 --- a/Framework/PythonInterface/test/python/mantid/kernel/ConfigServiceTest.py +++ b/Framework/PythonInterface/test/python/mantid/kernel/ConfigServiceTest.py @@ -5,7 +5,6 @@ # NScD Oak Ridge National Laboratory, European Spallation Source, # Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS # SPDX - License - Identifier: GPL - 3.0 + -import inspect import os import sys import testhelpers @@ -142,7 +141,7 @@ def test_log_level_get_set(self): def test_properties_documented(self): # location of the rst file relative to this file this will break if either moves - doc_filename = os.path.split(inspect.getfile(self.__class__))[0] + doc_filename = os.path.split(__file__)[0] doc_filename = os.path.join(doc_filename, "../../../../../../docs/source/concepts/PropertiesFile.rst") doc_filename = os.path.abspath(doc_filename) diff --git a/Framework/PythonInterface/test/testhelpers/testrunner.py b/Framework/PythonInterface/test/testhelpers/testrunner.py index 3955fb249db0..674f61f4652f 100644 --- a/Framework/PythonInterface/test/testhelpers/testrunner.py +++ b/Framework/PythonInterface/test/testhelpers/testrunner.py @@ -11,6 +11,7 @@ It is intended to be used as a launcher script for a given unit test file. The reports are output to the current working directory. """ +import importlib.util from importlib.machinery import SourceFileLoader import os import sys @@ -33,7 +34,12 @@ def main(argv): # Load the test and copy over any module variables so that we have # the same environment defined here - test_module = SourceFileLoader(module_name(pathname), pathname).load_module() + + test_module_name = module_name(pathname) + test_loader = SourceFileLoader(test_module_name, pathname) + test_spec = importlib.util.spec_from_loader(test_module_name, test_loader) + test_module = importlib.util.module_from_spec(test_spec) + test_loader.exec_module(test_module) test_module_globals = dir(test_module) this_globals = globals() for key in test_module_globals: diff --git a/Testing/SystemTests/lib/systemtests/systemtesting.py b/Testing/SystemTests/lib/systemtests/systemtesting.py index 44707644a885..4fa5aed48f21 100644 --- a/Testing/SystemTests/lib/systemtests/systemtesting.py +++ b/Testing/SystemTests/lib/systemtests/systemtesting.py @@ -1153,18 +1153,14 @@ def loadTestsFromModule(self, filename): modname = modname.split(".py")[0] tests = [] try: - spec = importlib.util.spec_from_file_location("", filename) + spec = importlib.util.spec_from_file_location(modname, filename) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) - mod_attrs = dir(mod) - for key in mod_attrs: - value = getattr(mod, key) - if key == "MantidSystemTest" or not inspect.isclass(value): - continue - if self.isValidTestClass(value): - test_name = key - tests.append(TestSuite(self._runner.getTestDir(), modname, test_name, filename)) + module_classes = dict(inspect.getmembers(mod, inspect.isclass)) + module_classes = [x for x in module_classes if self.isValidTestClass(module_classes[x]) and x != "MantidSystemTest"] + for test_name in module_classes: + tests.append(TestSuite(self._runner.getTestDir(), modname, test_name, filename)) module_loaded = True except Exception: print("Error importing module '{}':".format(modname)) diff --git a/Testing/Tools/cxxtest/sample/SCons/SConstruct b/Testing/Tools/cxxtest/sample/SCons/SConstruct index 58e661efe546..308176e17d09 100644 --- a/Testing/Tools/cxxtest/sample/SCons/SConstruct +++ b/Testing/Tools/cxxtest/sample/SCons/SConstruct @@ -6,8 +6,14 @@ cxxtest_path = '../..' # without having to copy it into a particular path. # for nicer examples you *should* use, see the cxxtest builder tests in the # build_tools/SCons/test directory. +import importlib.util from importlib.machinery import SourceFileLoader -cxxtest = SourceFileLoader('cxxtest', cxxtestbuilder_path).load_module() + +cxxtest_name = 'cxxtest' +loader = SourceFileLoader(cxxtest_name, cxxtestbuilder_path) +spec = importlib.util.spec_from_loader(cxxtest_name, loader) +cxxtest = importlib.util.module_from_spec(spec) +loader.exec_module(cxxtest) # First build the 'real' library, when working on an embedded system # this may involve a cross compiler. diff --git a/Testing/Tools/cxxtest/test/unit/SConstruct b/Testing/Tools/cxxtest/test/unit/SConstruct index 1da2323dd7b2..13108f0f6162 100644 --- a/Testing/Tools/cxxtest/test/unit/SConstruct +++ b/Testing/Tools/cxxtest/test/unit/SConstruct @@ -2,8 +2,14 @@ CxxTestBuilder_path = '../../build_tools/SCons/cxxtest.py' CxxTest_dir = '../..' # First a little python magic to pull in CxxTestBuilder +import importlib.util from importlib.machinery import SourceFileLoader -cxxtest = SourceFileLoader('cxxtest', CxxTestBuilder_path).load_module() + +cxxtest_name = 'cxxtest' +loader = SourceFileLoader(cxxtest_name, CxxTestBuilder_path) +spec = importlib.util.spec_from_loader(cxxtest_name, loader) +cxxtest = importlib.util.module_from_spec(spec) +loader.exec_module(cxxtest) env = Environment() cxxtest.generate(env, CXXTEST_INSTALL_DIR=CxxTest_dir) diff --git a/docs/source/release/v6.11.0/Framework/Python/Bugfixes/37031.rst b/docs/source/release/v6.11.0/Framework/Python/Bugfixes/37031.rst new file mode 100644 index 000000000000..c9be694da7db --- /dev/null +++ b/docs/source/release/v6.11.0/Framework/Python/Bugfixes/37031.rst @@ -0,0 +1 @@ +- Fixed error in the log about `load_module()` being deprecated in Python 3.12. \ No newline at end of file