From 380be7581e7170fd8d8e31e7d8954132085d238b Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Thu, 11 Jun 2020 00:12:36 +0200 Subject: [PATCH] Address compat issue for collection loading on py26 (#68219) (#69402) * Address compat issue for collection loading on py26 * Move import_module shim to utils for compat across the codebase * Enable collection tests on py2.6 * Update changelog fragment * Simplify code using sys.moduls * Move compat to module_utils/compat/importlib * Add back errantly deleted newline * Remove hack comment Co-Authored-By: Matt Clay Co-authored-by: Matt Clay (cherry picked from commit da3a90e88e027deef152f969641d0fddc1c51758) Co-authored-by: Matt Martz --- .../fragments/py26-collection-loader.yml | 2 ++ lib/ansible/executor/module_common.py | 8 +------- .../executor/powershell/module_manifest.py | 7 +------ lib/ansible/module_utils/compat/importlib.py | 18 ++++++++++++++++++ lib/ansible/plugins/loader.py | 7 +------ lib/ansible/template/__init__.py | 7 +------ lib/ansible/utils/collection_loader.py | 7 +------ test/integration/targets/collections/aliases | 1 - .../collections_plugin_namespace/aliases | 1 - .../collections_relative_imports/aliases | 1 - 10 files changed, 25 insertions(+), 34 deletions(-) create mode 100644 changelogs/fragments/py26-collection-loader.yml create mode 100644 lib/ansible/module_utils/compat/importlib.py diff --git a/changelogs/fragments/py26-collection-loader.yml b/changelogs/fragments/py26-collection-loader.yml new file mode 100644 index 00000000000..d0a2e6adc52 --- /dev/null +++ b/changelogs/fragments/py26-collection-loader.yml @@ -0,0 +1,2 @@ +bugfixes: +- Create an ``import_module`` compat util, for use across the codebase, to allow collection loading to work properly on Python26 diff --git a/lib/ansible/executor/module_common.py b/lib/ansible/executor/module_common.py index df63a6e44bf..d7a4742275c 100644 --- a/lib/ansible/executor/module_common.py +++ b/lib/ansible/executor/module_common.py @@ -37,6 +37,7 @@ from ansible.errors import AnsibleError from ansible.executor.interpreter_discovery import InterpreterDiscoveryRequiredError from ansible.executor.powershell import module_manifest as ps_manifest from ansible.module_utils._text import to_bytes, to_text, to_native +from ansible.module_utils.compat.importlib import import_module from ansible.plugins.loader import module_utils_loader # Must import strategy and use write_locks from there # If we import write_locks directly then we end up binding a @@ -53,13 +54,6 @@ try: 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 -except ImportError: - import_module = __import__ - # if we're on a Python that doesn't have FNFError, redefine it as IOError (since that's what we'll see) try: FileNotFoundError diff --git a/lib/ansible/executor/powershell/module_manifest.py b/lib/ansible/executor/powershell/module_manifest.py index f2b0cf2ad00..2f6acc7dc27 100644 --- a/lib/ansible/executor/powershell/module_manifest.py +++ b/lib/ansible/executor/powershell/module_manifest.py @@ -14,15 +14,10 @@ import re from distutils.version import LooseVersion -# HACK: keep Python 2.6 controller tests happy in CI until they're properly split -try: - from importlib import import_module -except ImportError: - import_module = __import__ - from ansible import constants as C from ansible.errors import AnsibleError from ansible.module_utils._text import to_bytes, to_native, to_text +from ansible.module_utils.compat.importlib import import_module from ansible.plugins.loader import ps_module_utils_loader diff --git a/lib/ansible/module_utils/compat/importlib.py b/lib/ansible/module_utils/compat/importlib.py new file mode 100644 index 00000000000..eee0ddf7bcf --- /dev/null +++ b/lib/ansible/module_utils/compat/importlib.py @@ -0,0 +1,18 @@ +# Copyright (c) 2020 Matt Martz +# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause) + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import sys + +try: + from importlib import import_module +except ImportError: + # importlib.import_module returns the tail + # whereas __import__ returns the head + # compat to work like importlib.import_module + def import_module(name): + __import__(name) + return sys.modules[name] diff --git a/lib/ansible/plugins/loader.py b/lib/ansible/plugins/loader.py index c383a5eb322..848cdd020fb 100644 --- a/lib/ansible/plugins/loader.py +++ b/lib/ansible/plugins/loader.py @@ -18,6 +18,7 @@ from collections import defaultdict from ansible import constants as C from ansible.errors import AnsibleError from ansible.module_utils._text import to_bytes, to_text, to_native +from ansible.module_utils.compat.importlib import import_module from ansible.module_utils.six import string_types from ansible.parsing.utils.yaml import from_yaml from ansible.parsing.yaml.loader import AnsibleLoader @@ -32,12 +33,6 @@ try: 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 -except ImportError: - import_module = __import__ - display = Display() diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index 1343bf227cc..5ec05a48a4c 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -43,6 +43,7 @@ from ansible.errors import AnsibleError, AnsibleFilterError, AnsibleUndefinedVar from ansible.module_utils.six import iteritems, string_types, text_type from ansible.module_utils._text import to_native, to_text, to_bytes from ansible.module_utils.common._collections_compat import Sequence, Mapping, MutableMapping +from ansible.module_utils.compat.importlib import import_module from ansible.plugins.loader import filter_loader, lookup_loader, test_loader from ansible.template.safe_eval import safe_eval from ansible.template.template import AnsibleJ2Template @@ -51,12 +52,6 @@ from ansible.utils.collection_loader import AnsibleCollectionRef from ansible.utils.display import Display from ansible.utils.unsafe_proxy import wrap_var -# HACK: keep Python 2.6 controller tests happy in CI until they're properly split -try: - from importlib import import_module -except ImportError: - import_module = __import__ - display = Display() diff --git a/lib/ansible/utils/collection_loader.py b/lib/ansible/utils/collection_loader.py index 838b405c14c..b26636253cc 100644 --- a/lib/ansible/utils/collection_loader.py +++ b/lib/ansible/utils/collection_loader.py @@ -12,16 +12,11 @@ import sys from types import ModuleType from ansible.module_utils._text import to_bytes, to_native, to_text +from ansible.module_utils.compat.importlib import import_module from ansible.module_utils.six import iteritems, string_types, with_metaclass from ansible.utils.path import cs_open from ansible.utils.singleton import Singleton -# HACK: keep Python 2.6 controller tests happy in CI until they're properly split -try: - from importlib import import_module -except ImportError: - import_module = __import__ - _SYNTHETIC_PACKAGES = { # these provide fallback package definitions when there are no on-disk paths 'ansible_collections': dict(type='pkg_only', allow_external_subpackages=True), diff --git a/test/integration/targets/collections/aliases b/test/integration/targets/collections/aliases index f44f511f199..011ac1a68e5 100644 --- a/test/integration/targets/collections/aliases +++ b/test/integration/targets/collections/aliases @@ -1,5 +1,4 @@ posix shippable/posix/group4 shippable/windows/group2 -skip/python2.6 windows diff --git a/test/integration/targets/collections_plugin_namespace/aliases b/test/integration/targets/collections_plugin_namespace/aliases index 54ea5a3b078..a6dafcf8cd8 100644 --- a/test/integration/targets/collections_plugin_namespace/aliases +++ b/test/integration/targets/collections_plugin_namespace/aliases @@ -1,2 +1 @@ shippable/posix/group1 -skip/python2.6 diff --git a/test/integration/targets/collections_relative_imports/aliases b/test/integration/targets/collections_relative_imports/aliases index 54ea5a3b078..a6dafcf8cd8 100644 --- a/test/integration/targets/collections_relative_imports/aliases +++ b/test/integration/targets/collections_relative_imports/aliases @@ -1,2 +1 @@ shippable/posix/group1 -skip/python2.6