Skip to content

Conversation

@teojgo
Copy link
Contributor

@teojgo teojgo commented May 29, 2018

  • Use the absolute path of the filename if the normalized
    path is not a directory.

Fixes #302

* Use the absolute path of the filename if the normalized
  path is not a directory.
@teojgo
Copy link
Contributor Author

teojgo commented May 29, 2018

Maybe this needs a little bit of redesign. Should we allow loading of modules that don't exist?

@teojgo teojgo added this to the ReFrame sprint 2018w20 milestone May 29, 2018
if os.path.isdir(filename):
filename = os.path.join(filename, '__init__.py')
else:
filename = os.path.abspath(filename)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is correct, because you always trigger the _do_import_module_from_file. This is not exactly the same as using import_module since it does not treat correctly namespace packages.

I see that we already normalise the filename argument, this meaning that it won't contain any .. components inside it, except possibly at the beginning. So, we just need to make sure that we call correctly the importlib.import_module in order to do a relative import.

self.assertIs(module, sys.modules.get('reframe'))

def test_load_file_starts_with_ddot(self):
parent_dir = os.path.basename(os.path.abspath(os.pardir))
Copy link
Contributor

Choose a reason for hiding this comment

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

Better use .. instead of os.pardir to be clearer and consistent with other usages.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, I wouldn't write the test this way. I'd instead change temporarily to a directory and do a relative import from there. This is the faulty scenario in any case. You can use the utility.os_ext.change_dir context manager for doing the directory change safely.

@vkarak vkarak changed the title Use abs path if the given filename isn't a dir Treat correctly relative imports in 'import_module_from_file' May 30, 2018
filename = os.path.join(filename, '__init__.py')

if filename.startswith('..'):
filename = os.path.abspath(filename)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that this is the only solution. It seems we cannot use the importlib.import_module(), because we don't which package to attach to. Am I right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, that is correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tested and it now works both with settings and checks defined with relative paths.


def test_load_dir_starts_with_ddot(self):
with os_ext.change_dir('reframe'):
filename = '..%s/reframe' % os.getcwd()
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think that this does the intended thing, because os.getcwd() returns an absolute path. I don't think it's needed. In any case the change_dir() assumes that we are running the unit tests from top-level directory.

@vkarak vkarak merged commit ee1087a into master Jun 1, 2018
@vkarak vkarak deleted the bugfix/relative_path_use branch June 1, 2018 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants