Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't modify values of 'paths' list passed as argument to prepend_paths #1300

Merged
merged 2 commits into from
Jun 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions easybuild/tools/module_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,18 +226,21 @@ def prepend_paths(self, key, paths, allow_abs=False):
self.log.debug("Wrapping %s into a list before using it to prepend path %s" % (paths, key))
paths = [paths]

for i, path in enumerate(paths):
abspaths = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, we were changing the list while iterating over it? 👎

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we were updating elements in a list while iterating over it, which is OK

What's not OK is that we're modifying the list that is being passed to this method...

for path in paths:
if os.path.isabs(path) and not allow_abs:
raise EasyBuildError("Absolute path %s passed to prepend_paths which only expects relative paths.",
path)
elif not os.path.isabs(path):
# prepend $root (= installdir) for (non-empty) relative paths
if path:
paths[i] = os.path.join('$root', path)
abspaths.append(os.path.join('$root', path))
else:
paths[i] = '$root'
abspaths.append('$root')
else:
abspaths.append(path)

statements = [template % (key, p) for p in paths]
statements = [template % (key, p) for p in abspaths]
return ''.join(statements)

def use(self, paths):
Expand Down
5 changes: 4 additions & 1 deletion test/framework/module_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ def test_prepend_paths(self):
"prepend-path\tkey\t\t$root/path2\n",
"prepend-path\tkey\t\t$root\n",
])
self.assertEqual(expected, self.modgen.prepend_paths("key", ["path1", "path2", '']))
paths = ['path1', 'path2', '']
self.assertEqual(expected, self.modgen.prepend_paths("key", paths))
# 2nd call should still give same result, no side-effects like manipulating passed list 'paths'!
self.assertEqual(expected, self.modgen.prepend_paths("key", paths))

expected = "prepend-path\tbar\t\t$root/foo\n"
self.assertEqual(expected, self.modgen.prepend_paths("bar", "foo"))
Expand Down