Skip to content

Commit

Permalink
Merge pull request #38127 from jclarkeSTFC/37031_remove_deprecated_lo…
Browse files Browse the repository at this point in the history
…ad_module_again

Revert "Revert "Removed uses of load_module()""
  • Loading branch information
SilkeSchomann authored Oct 7, 2024
2 parents 25060cc + 4acc67c commit 7daad5d
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 15 deletions.
13 changes: 12 additions & 1 deletion Framework/PythonInterface/mantid/kernel/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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


# ======================================================================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
8 changes: 7 additions & 1 deletion Framework/PythonInterface/test/testhelpers/testrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
14 changes: 5 additions & 9 deletions Testing/SystemTests/lib/systemtests/systemtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
8 changes: 7 additions & 1 deletion Testing/Tools/cxxtest/sample/SCons/SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 7 additions & 1 deletion Testing/Tools/cxxtest/test/unit/SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixed error in the log about `load_module()` being deprecated in Python 3.12.

0 comments on commit 7daad5d

Please sign in to comment.