Skip to content

Commit 0682054

Browse files
Merge pull request #38046 from jclarkeSTFC/38011_fix_module_load_error_release
Fix module load error
2 parents b873377 + 500dd66 commit 0682054

File tree

9 files changed

+17
-42
lines changed

9 files changed

+17
-42
lines changed

Framework/PythonInterface/mantid/kernel/plugins.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
import os as _os
1515
from traceback import format_exc
16-
import sys
17-
import importlib.util
1816
from importlib.machinery import SourceFileLoader
1917
from . import logger, Logger, config
2018

@@ -46,16 +44,7 @@ def run(self):
4644
name = _os.path.basename(pathname) # Including extension
4745
name = _os.path.splitext(name)[0]
4846
self._logger.debug("Loading python plugin %s" % pathname)
49-
loader = SourceFileLoader(name, pathname)
50-
spec = importlib.util.spec_from_loader(name, loader)
51-
module = importlib.util.module_from_spec(spec)
52-
loader.exec_module(module)
53-
# It's better to let import handle editing sys.modules, but this code used to call
54-
# load_module, which would edit sys.modules, but now load_module is deprecated.
55-
# We edit sys.modules here so that legacy user scripts will not have to be
56-
# edited in order to keep working.
57-
sys.modules[name] = module
58-
return module
47+
return SourceFileLoader(name, pathname).load_module()
5948

6049

6150
# ======================================================================================================================

Framework/PythonInterface/test/python/mantid/kernel/ConfigServiceTest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# NScD Oak Ridge National Laboratory, European Spallation Source,
66
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
77
# SPDX - License - Identifier: GPL - 3.0 +
8+
import inspect
89
import os
910
import sys
1011
import testhelpers
@@ -141,7 +142,7 @@ def test_log_level_get_set(self):
141142

142143
def test_properties_documented(self):
143144
# location of the rst file relative to this file this will break if either moves
144-
doc_filename = os.path.split(__file__)[0]
145+
doc_filename = os.path.split(inspect.getfile(self.__class__))[0]
145146
doc_filename = os.path.join(doc_filename, "../../../../../../docs/source/concepts/PropertiesFile.rst")
146147
doc_filename = os.path.abspath(doc_filename)
147148

Framework/PythonInterface/test/python/plugins/algorithms/FindGlobalBMatrixTest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
TransformHKL,
1212
)
1313
import numpy as np
14-
from FindGoniometerFromUB import getR
14+
from plugins.algorithms.FindGoniometerFromUB import getR
1515

1616

1717
def add_peaksHKL(ws_list, Hs, Ks, L):

Framework/PythonInterface/test/testhelpers/testrunner.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
It is intended to be used as a launcher script for a given unit test file.
1212
The reports are output to the current working directory.
1313
"""
14-
import importlib.util
1514
from importlib.machinery import SourceFileLoader
1615
import os
1716
import sys
@@ -34,12 +33,7 @@ def main(argv):
3433

3534
# Load the test and copy over any module variables so that we have
3635
# the same environment defined here
37-
38-
test_module_name = module_name(pathname)
39-
test_loader = SourceFileLoader(test_module_name, pathname)
40-
test_spec = importlib.util.spec_from_loader(test_module_name, test_loader)
41-
test_module = importlib.util.module_from_spec(test_spec)
42-
test_loader.exec_module(test_module)
36+
test_module = SourceFileLoader(module_name(pathname), pathname).load_module()
4337
test_module_globals = dir(test_module)
4438
this_globals = globals()
4539
for key in test_module_globals:

Testing/SystemTests/lib/systemtests/systemtesting.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,14 +1153,18 @@ def loadTestsFromModule(self, filename):
11531153
modname = modname.split(".py")[0]
11541154
tests = []
11551155
try:
1156-
spec = importlib.util.spec_from_file_location(modname, filename)
1156+
spec = importlib.util.spec_from_file_location("", filename)
11571157
mod = importlib.util.module_from_spec(spec)
11581158
spec.loader.exec_module(mod)
11591159

1160-
module_classes = dict(inspect.getmembers(mod, inspect.isclass))
1161-
module_classes = [x for x in module_classes if self.isValidTestClass(module_classes[x]) and x != "MantidSystemTest"]
1162-
for test_name in module_classes:
1163-
tests.append(TestSuite(self._runner.getTestDir(), modname, test_name, filename))
1160+
mod_attrs = dir(mod)
1161+
for key in mod_attrs:
1162+
value = getattr(mod, key)
1163+
if key == "MantidSystemTest" or not inspect.isclass(value):
1164+
continue
1165+
if self.isValidTestClass(value):
1166+
test_name = key
1167+
tests.append(TestSuite(self._runner.getTestDir(), modname, test_name, filename))
11641168
module_loaded = True
11651169
except Exception:
11661170
print("Error importing module '{}':".format(modname))

Testing/Tools/cxxtest/sample/SCons/SConstruct

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,8 @@ cxxtest_path = '../..'
66
# without having to copy it into a particular path.
77
# for nicer examples you *should* use, see the cxxtest builder tests in the
88
# build_tools/SCons/test directory.
9-
import importlib.util
109
from importlib.machinery import SourceFileLoader
11-
12-
cxxtest_name = 'cxxtest'
13-
loader = SourceFileLoader(cxxtest_name, cxxtestbuilder_path)
14-
spec = importlib.util.spec_from_loader(cxxtest_name, loader)
15-
cxxtest = importlib.util.module_from_spec(spec)
16-
loader.exec_module(cxxtest)
10+
cxxtest = SourceFileLoader('cxxtest', cxxtestbuilder_path).load_module()
1711

1812
# First build the 'real' library, when working on an embedded system
1913
# this may involve a cross compiler.

Testing/Tools/cxxtest/test/unit/SConstruct

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@ CxxTestBuilder_path = '../../build_tools/SCons/cxxtest.py'
22
CxxTest_dir = '../..'
33

44
# First a little python magic to pull in CxxTestBuilder
5-
import importlib.util
65
from importlib.machinery import SourceFileLoader
7-
8-
cxxtest_name = 'cxxtest'
9-
loader = SourceFileLoader(cxxtest_name, CxxTestBuilder_path)
10-
spec = importlib.util.spec_from_loader(cxxtest_name, loader)
11-
cxxtest = importlib.util.module_from_spec(spec)
12-
loader.exec_module(cxxtest)
6+
cxxtest = SourceFileLoader('cxxtest', CxxTestBuilder_path).load_module()
137
env = Environment()
148
cxxtest.generate(env, CXXTEST_INSTALL_DIR=CxxTest_dir)
159

docs/source/release/v6.11.0/Framework/Python/Bugfixes/37031.rst

Lines changed: 0 additions & 1 deletion
This file was deleted.

scripts/Diffraction/single_crystal/sxd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from mantid.kernel import logger
1111
from Diffraction.single_crystal.base_sx import BaseSX, PEAK_TYPE, INTEGRATION_TYPE
1212
from mantid.api import AnalysisDataService as ADS
13-
from FindGoniometerFromUB import getR
13+
from plugins.algorithms.FindGoniometerFromUB import getR
1414
from os import path
1515

1616
tof_min = 700

0 commit comments

Comments
 (0)