From 34088a8b7f89f1eac158fa395c410395d677bb0f Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Tue, 24 Sep 2024 17:06:17 +0100 Subject: [PATCH] ansible_mitogen: Consolidate Python 2 & 3 compatibility Rough guidelines, in decending preference: - Use mitogen.core if possible - Use ansible.module_utils.six if possible - Embed a getattr() or try/except viewkeys() et al can't be brought into mitogen.core because that package still targets Python 2.4. dict.viewkeys() were introduced in Python 2.7. --- ansible_mitogen/mixins.py | 6 +----- .../plugins/connection/mitogen_local.py | 8 +------- ansible_mitogen/runner.py | 11 +++-------- ansible_mitogen/services.py | 16 ++-------------- ansible_mitogen/target.py | 9 +++------ docs/changelog.rst | 2 ++ 6 files changed, 12 insertions(+), 40 deletions(-) diff --git a/ansible_mitogen/mixins.py b/ansible_mitogen/mixins.py index d67174bd..38f351ed 100644 --- a/ansible_mitogen/mixins.py +++ b/ansible_mitogen/mixins.py @@ -35,11 +35,6 @@ import pwd import random import traceback -try: - from shlex import quote as shlex_quote -except ImportError: - from pipes import quote as shlex_quote - import ansible import ansible.constants import ansible.plugins @@ -48,6 +43,7 @@ import ansible.utils.unsafe_proxy import ansible.vars.clean from ansible.module_utils.common.text.converters import to_bytes, to_text +from ansible.module_utils.six.moves import shlex_quote from ansible.parsing.utils.jsonify import jsonify import mitogen.core diff --git a/ansible_mitogen/plugins/connection/mitogen_local.py b/ansible_mitogen/plugins/connection/mitogen_local.py index 6ff86733..2d1e7052 100644 --- a/ansible_mitogen/plugins/connection/mitogen_local.py +++ b/ansible_mitogen/plugins/connection/mitogen_local.py @@ -42,13 +42,7 @@ except ImportError: import ansible_mitogen.connection import ansible_mitogen.process - -if sys.version_info > (3,): - viewkeys = dict.keys -elif sys.version_info > (2, 7): - viewkeys = dict.viewkeys -else: - viewkeys = lambda dct: set(dct) +viewkeys = getattr(dict, 'viewkeys', dict.keys) def dict_diff(old, new): diff --git a/ansible_mitogen/runner.py b/ansible_mitogen/runner.py index 16e43059..b60e537c 100644 --- a/ansible_mitogen/runner.py +++ b/ansible_mitogen/runner.py @@ -52,6 +52,8 @@ import tempfile import traceback import types +from ansible.module_utils.six.moves import shlex_quote + import mitogen.core import ansible_mitogen.target # TODO: circular import from mitogen.core import to_text @@ -70,12 +72,6 @@ try: except ImportError: from io import StringIO -try: - from shlex import quote as shlex_quote -except ImportError: - from pipes import quote as shlex_quote - - # Prevent accidental import of an Ansible module from hanging on stdin read. import ansible.module_utils.basic ansible.module_utils.basic._ANSIBLE_ARGS = '{}' @@ -92,7 +88,6 @@ for symbol in 'res_init', '__res_init': except AttributeError: pass -iteritems = getattr(dict, 'iteritems', dict.items) LOG = logging.getLogger(__name__) @@ -600,7 +595,7 @@ class TemporaryEnvironment(object): def __init__(self, env=None): self.original = dict(os.environ) self.env = env or {} - for key, value in iteritems(self.env): + for key, value in mitogen.core.iteritems(self.env): key = mitogen.core.to_text(key) value = mitogen.core.to_text(value) if value is None: diff --git a/ansible_mitogen/services.py b/ansible_mitogen/services.py index 3e9de652..abc0e379 100644 --- a/ansible_mitogen/services.py +++ b/ansible_mitogen/services.py @@ -50,6 +50,8 @@ import threading import ansible.constants +from ansible.module_utils.six import reraise + import mitogen.core import mitogen.service import ansible_mitogen.loaders @@ -66,20 +68,6 @@ LOG = logging.getLogger(__name__) ansible_mitogen.loaders.shell_loader.get('sh') -if sys.version_info[0] == 3: - def reraise(tp, value, tb): - if value is None: - value = tp() - if value.__traceback__ is not tb: - raise value.with_traceback(tb) - raise value -else: - exec( - "def reraise(tp, value, tb=None):\n" - " raise tp, value, tb\n" - ) - - def _get_candidate_temp_dirs(): try: # >=2.5 diff --git a/ansible_mitogen/target.py b/ansible_mitogen/target.py index b79dc492..ee4cb398 100644 --- a/ansible_mitogen/target.py +++ b/ansible_mitogen/target.py @@ -55,12 +55,6 @@ import types import mitogen.core import mitogen.parent import mitogen.service -try: - reduce -except NameError: - # Python 3.x. - from functools import reduce - # Ansible since PR #41749 inserts "import __main__" into # ansible.module_utils.basic. Mitogen's importer will refuse such an import, so @@ -70,6 +64,9 @@ if not sys.modules.get(str('__main__')): sys.modules[str('__main__')] = types.ModuleType(str('__main__')) import ansible.module_utils.json_utils + +from ansible.module_utils.six.moves import reduce + import ansible_mitogen.runner diff --git a/docs/changelog.rst b/docs/changelog.rst index 31aa51ad..66a8077e 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -27,6 +27,8 @@ Unreleased fallbacks for Python 2.4 & 2.5. * :gh:issue:`1127` :mod:`ansible_mitogen`: Remove fallback imports for Ansible releases before 2.10 +* :gh:issue:`1127` :mod:`ansible_mitogen`: Consolidate Python 2 & 3 + compatibility v0.3.10 (2024-09-20)