mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-26 20:31:27 -07:00
Support using importlib on py>=3 to avoid imp deprecation (#54883)
* Support using importlib on py>=3 to avoid imp deprecation * Add changelog fragment * importlib coverage for py3 * Ansiballz execute should use importlib too * recursive module_utils finder should utilize importlib too * don't be dumb * Fix up units * Clean up tests * Prefer importlib.util in plugin loader when available * insert the module into sys.modules * 3 before 2 for consistency * ci_complete * Address importlib.util.find_spec returning None
This commit is contained in:
parent
6d645c127f
commit
2732cde031
4 changed files with 120 additions and 63 deletions
|
@ -8,7 +8,6 @@ from __future__ import (absolute_import, division, print_function)
|
|||
__metaclass__ = type
|
||||
|
||||
import glob
|
||||
import imp
|
||||
import os
|
||||
import os.path
|
||||
import pkgutil
|
||||
|
@ -28,6 +27,12 @@ from ansible.utils.collection_loader import AnsibleCollectionLoader, AnsibleFlat
|
|||
from ansible.utils.display import Display
|
||||
from ansible.utils.plugin_docs import add_fragments
|
||||
|
||||
try:
|
||||
import importlib.util
|
||||
imp = None
|
||||
except ImportError:
|
||||
import imp
|
||||
|
||||
# HACK: keep Python 2.6 controller tests happy in CI until they're properly split
|
||||
try:
|
||||
from importlib import import_module
|
||||
|
@ -535,9 +540,15 @@ class PluginLoader:
|
|||
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore", RuntimeWarning)
|
||||
with open(to_bytes(path), 'rb') as module_file:
|
||||
# to_native is used here because imp.load_source's path is for tracebacks and python's traceback formatting uses native strings
|
||||
module = imp.load_source(to_native(full_name), to_native(path), module_file)
|
||||
if imp is None:
|
||||
spec = importlib.util.spec_from_file_location(to_native(full_name), to_native(path))
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
sys.modules[full_name] = module
|
||||
else:
|
||||
with open(to_bytes(path), 'rb') as module_file:
|
||||
# to_native is used here because imp.load_source's path is for tracebacks and python's traceback formatting uses native strings
|
||||
module = imp.load_source(to_native(full_name), to_native(path), module_file)
|
||||
return module
|
||||
|
||||
def _update_object(self, obj, name, path):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue