check finder type before passing path (#76448)

* check finder type before passing path

ci_complete

* Reduce nesting

* Test find_module does not cause a traceback with Python 3 FileFinder

* Update lib/ansible/utils/collection_loader/_collection_finder.py
pull/76526/head
Sloane Hertel 3 years ago committed by GitHub
parent c819c1725d
commit ed6581e4db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -43,6 +43,13 @@ try:
except ImportError: except ImportError:
pass pass
try:
from importlib.machinery import FileFinder
except ImportError:
HAS_FILE_FINDER = False
else:
HAS_FILE_FINDER = True
# NB: this supports import sanity test providing a different impl # NB: this supports import sanity test providing a different impl
try: try:
from ._collection_meta import _meta_yml_to_dict from ._collection_meta import _meta_yml_to_dict
@ -231,13 +238,14 @@ class _AnsibleCollectionFinder:
def find_spec(self, fullname, path, target=None): def find_spec(self, fullname, path, target=None):
loader = self._get_loader(fullname, path) loader = self._get_loader(fullname, path)
if loader:
if loader is None:
return None
spec = spec_from_loader(fullname, loader) spec = spec_from_loader(fullname, loader)
if spec is not None and hasattr(loader, '_subpackage_search_paths'): if spec is not None and hasattr(loader, '_subpackage_search_paths'):
spec.submodule_search_locations = loader._subpackage_search_paths spec.submodule_search_locations = loader._subpackage_search_paths
return spec return spec
else:
return None
# Implements a path_hook finder for iter_modules (since it's only path based). This finder does not need to actually # Implements a path_hook finder for iter_modules (since it's only path based). This finder does not need to actually
@ -299,23 +307,29 @@ class _AnsiblePathHookFinder:
def find_module(self, fullname, path=None): def find_module(self, fullname, path=None):
# we ignore the passed in path here- use what we got from the path hook init # we ignore the passed in path here- use what we got from the path hook init
finder = self._get_finder(fullname) finder = self._get_finder(fullname)
if finder is not None:
return finder.find_module(fullname, path=[self._pathctx]) if finder is None:
else:
return None return None
elif HAS_FILE_FINDER and isinstance(finder, FileFinder):
# this codepath is erroneously used under some cases in py3,
# and the find_module method on FileFinder does not accept the path arg
# see https://github.com/pypa/setuptools/pull/2918
return finder.find_module(fullname)
else:
return finder.find_module(fullname, path=[self._pathctx])
def find_spec(self, fullname, target=None): def find_spec(self, fullname, target=None):
split_name = fullname.split('.') split_name = fullname.split('.')
toplevel_pkg = split_name[0] toplevel_pkg = split_name[0]
finder = self._get_finder(fullname) finder = self._get_finder(fullname)
if finder is not None:
if toplevel_pkg == 'ansible_collections': if finder is None:
return None
elif toplevel_pkg == 'ansible_collections':
return finder.find_spec(fullname, path=[self._pathctx]) return finder.find_spec(fullname, path=[self._pathctx])
else: else:
return finder.find_spec(fullname) return finder.find_spec(fullname)
else:
return None
def iter_modules(self, prefix): def iter_modules(self, prefix):
# NB: this currently represents only what's on disk, and does not handle package redirection # NB: this currently represents only what's on disk, and does not handle package redirection

@ -9,6 +9,7 @@ import sys
from ansible.module_utils.six import PY3, string_types from ansible.module_utils.six import PY3, string_types
from ansible.module_utils.compat.importlib import import_module from ansible.module_utils.compat.importlib import import_module
from ansible.modules import ping as ping_module
from ansible.utils.collection_loader import AnsibleCollectionConfig, AnsibleCollectionRef from ansible.utils.collection_loader import AnsibleCollectionConfig, AnsibleCollectionRef
from ansible.utils.collection_loader._collection_finder import ( from ansible.utils.collection_loader._collection_finder import (
_AnsibleCollectionFinder, _AnsibleCollectionLoader, _AnsibleCollectionNSPkgLoader, _AnsibleCollectionPkgLoader, _AnsibleCollectionFinder, _AnsibleCollectionLoader, _AnsibleCollectionNSPkgLoader, _AnsibleCollectionPkgLoader,
@ -28,6 +29,17 @@ def teardown(*args, **kwargs):
# BEGIN STANDALONE TESTS - these exercise behaviors of the individual components without the import machinery # BEGIN STANDALONE TESTS - these exercise behaviors of the individual components without the import machinery
@pytest.mark.skipif(not PY3, reason='Testing Python 2 codepath (find_module) on Python 3')
def test_find_module_py3():
dir_to_a_file = os.path.dirname(ping_module.__file__)
path_hook_finder = _AnsiblePathHookFinder(_AnsibleCollectionFinder(), dir_to_a_file)
# setuptools may fall back to find_module on Python 3 if find_spec returns None
# see https://github.com/pypa/setuptools/pull/2918
assert path_hook_finder.find_spec('missing') is None
assert path_hook_finder.find_module('missing') is None
def test_finder_setup(): def test_finder_setup():
# ensure scalar path is listified # ensure scalar path is listified
f = _AnsibleCollectionFinder(paths='/bogus/bogus') f = _AnsibleCollectionFinder(paths='/bogus/bogus')

Loading…
Cancel
Save