Skip to content

Commit 1f3dd7c

Browse files
author
ocaisa
authored
Merge pull request #3544 from boegel/no_sys_modules_cleanup
don't clean up imported modules after verifying imports of included Python modules
2 parents 3536ac7 + 482860a commit 1f3dd7c

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

easybuild/tools/include.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,16 @@ def set_up_eb_package(parent_path, eb_pkg_name, subpkgs=None, pkg_init_body=None
125125

126126
def verify_imports(pymods, pypkg, from_path):
127127
"""Verify that import of specified modules from specified package and expected location works."""
128-
saved_modules = sys.modules.copy()
129128

130129
for pymod in pymods:
131130
pymod_spec = '%s.%s' % (pypkg, pymod)
131+
132+
# force re-import if the specified modules was already imported;
133+
# this is required to ensure that an easyblock that is included via --include-easyblocks-from-pr
134+
# gets preference over one that is included via --include-easyblocks
135+
if pymod_spec in sys.modules:
136+
del sys.modules[pymod_spec]
137+
132138
try:
133139
pymod = __import__(pymod_spec, fromlist=[pypkg])
134140
# different types of exceptions may be thrown, not only ImportErrors
@@ -142,10 +148,6 @@ def verify_imports(pymods, pypkg, from_path):
142148

143149
_log.debug("Import of %s from %s verified", pymod_spec, from_path)
144150

145-
# restore sys.modules to its original state (not only verified modules but also their dependencies)
146-
sys.modules.clear()
147-
sys.modules.update(saved_modules)
148-
149151

150152
def is_software_specific_easyblock(module):
151153
"""Determine whether Python module at specified location is a software-specific easyblock."""
@@ -279,7 +281,7 @@ def include_toolchains(tmpdir, paths):
279281
sys.modules[tcpkg].__path__.insert(0, os.path.join(toolchains_path, 'easybuild', 'toolchains', subpkg))
280282

281283
# sanity check: verify that included toolchain modules can be imported (from expected location)
282-
verify_imports([os.path.splitext(mns)[0] for mns in included_toolchains], 'easybuild.toolchains', tcs_dir)
284+
verify_imports([os.path.splitext(tc)[0] for tc in included_toolchains], 'easybuild.toolchains', tcs_dir)
283285
for subpkg in toolchain_subpkgs:
284286
pkg = '.'.join(['easybuild', 'toolchains', subpkg])
285287
loc = os.path.join(tcs_dir, subpkg)

test/framework/include.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ def test_include_easyblocks_priority(self):
156156
self.assertFalse(os.path.samefile(os.path.dirname(os.path.dirname(foo_pyc_path)), test_easyblocks))
157157
self.assertTrue(os.path.samefile(foo_real_py_path, os.path.join(myeasyblocks, 'foo.py')))
158158

159-
# check that the included easyblock is not loaded
160-
self.assertFalse('easybuild.easyblocks.foo' in sys.modules)
159+
# 'undo' import of foo easyblock
160+
del sys.modules['easybuild.easyblocks.foo']
161161

162162
def test_include_mns(self):
163163
"""Test include_module_naming_schemes()."""

test/framework/options.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3047,6 +3047,7 @@ def test_xxx_include_easyblocks_from_pr(self):
30473047
return
30483048

30493049
orig_local_sys_path = sys.path[:]
3050+
30503051
fd, dummylogfn = tempfile.mkstemp(prefix='easybuild-dummy', suffix='.log')
30513052
os.close(fd)
30523053

@@ -3095,10 +3096,6 @@ def test_xxx_include_easyblocks_from_pr(self):
30953096
del sys.modules['easybuild.easyblocks.generic.cmakemake']
30963097
os.remove(os.path.join(self.test_prefix, 'foo.py'))
30973098
sys.path = orig_local_sys_path
3098-
import easybuild.easyblocks
3099-
reload(easybuild.easyblocks)
3100-
import easybuild.easyblocks.generic
3101-
reload(easybuild.easyblocks.generic)
31023099

31033100
# include test cmakemake easyblock
31043101
cmm_txt = '\n'.join([

0 commit comments

Comments
 (0)