fix delegated loading when path is not a directory (#69713)

* find_module can't pop ImportError- we need to just translate to `None` since this is a normal condition with files on sys.path (eg `/usr/lib/python36.zip`)
* added test
This commit is contained in:
Matt Davis 2020-05-26 15:55:17 -07:00 committed by GitHub
parent 5fc01c0a1d
commit fa81cc6a0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View file

@ -228,7 +228,12 @@ class _AnsiblePathHookFinder:
if PY3: if PY3:
# create or consult our cached file finder for this path # create or consult our cached file finder for this path
if not self._file_finder: if not self._file_finder:
try:
self._file_finder = _AnsiblePathHookFinder._filefinder_path_hook(self._pathctx) self._file_finder = _AnsiblePathHookFinder._filefinder_path_hook(self._pathctx)
except ImportError:
# FUTURE: log at a high logging level? This is normal for things like python36.zip on the path, but
# might not be in some other situation...
return None
spec = self._file_finder.find_spec(fullname) spec = self._file_finder.find_spec(fullname)
if not spec: if not spec:

View file

@ -273,6 +273,15 @@ def test_path_hook_setup():
assert repr(_AnsiblePathHookFinder(object(), '/bogus/path')) == "_AnsiblePathHookFinder(path='/bogus/path')" assert repr(_AnsiblePathHookFinder(object(), '/bogus/path')) == "_AnsiblePathHookFinder(path='/bogus/path')"
def test_path_hook_importerror():
# ensure that AnsiblePathHookFinder.find_module swallows ImportError from path hook delegation on Py3, eg if the delegated
# path hook gets passed a file on sys.path (python36.zip)
reset_collections_loader_state()
path_to_a_file = os.path.join(default_test_collection_paths[0], 'ansible_collections/testns/testcoll/plugins/action/my_action.py')
# it's a bug if the following pops an ImportError...
assert _AnsiblePathHookFinder(_AnsibleCollectionFinder(), path_to_a_file).find_module('foo.bar.my_action') is None
def test_new_or_existing_module(): def test_new_or_existing_module():
module_name = 'blar.test.module' module_name = 'blar.test.module'
pkg_name = module_name.rpartition('.')[0] pkg_name = module_name.rpartition('.')[0]