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.
pull/1127/head
Alex Willmer 2 months ago
parent 0a908d76da
commit 34088a8b7f

@ -35,11 +35,6 @@ import pwd
import random import random
import traceback import traceback
try:
from shlex import quote as shlex_quote
except ImportError:
from pipes import quote as shlex_quote
import ansible import ansible
import ansible.constants import ansible.constants
import ansible.plugins import ansible.plugins
@ -48,6 +43,7 @@ import ansible.utils.unsafe_proxy
import ansible.vars.clean import ansible.vars.clean
from ansible.module_utils.common.text.converters import to_bytes, to_text 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 from ansible.parsing.utils.jsonify import jsonify
import mitogen.core import mitogen.core

@ -42,13 +42,7 @@ except ImportError:
import ansible_mitogen.connection import ansible_mitogen.connection
import ansible_mitogen.process import ansible_mitogen.process
viewkeys = getattr(dict, 'viewkeys', dict.keys)
if sys.version_info > (3,):
viewkeys = dict.keys
elif sys.version_info > (2, 7):
viewkeys = dict.viewkeys
else:
viewkeys = lambda dct: set(dct)
def dict_diff(old, new): def dict_diff(old, new):

@ -52,6 +52,8 @@ import tempfile
import traceback import traceback
import types import types
from ansible.module_utils.six.moves import shlex_quote
import mitogen.core import mitogen.core
import ansible_mitogen.target # TODO: circular import import ansible_mitogen.target # TODO: circular import
from mitogen.core import to_text from mitogen.core import to_text
@ -70,12 +72,6 @@ try:
except ImportError: except ImportError:
from io import StringIO 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. # Prevent accidental import of an Ansible module from hanging on stdin read.
import ansible.module_utils.basic import ansible.module_utils.basic
ansible.module_utils.basic._ANSIBLE_ARGS = '{}' ansible.module_utils.basic._ANSIBLE_ARGS = '{}'
@ -92,7 +88,6 @@ for symbol in 'res_init', '__res_init':
except AttributeError: except AttributeError:
pass pass
iteritems = getattr(dict, 'iteritems', dict.items)
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -600,7 +595,7 @@ class TemporaryEnvironment(object):
def __init__(self, env=None): def __init__(self, env=None):
self.original = dict(os.environ) self.original = dict(os.environ)
self.env = env or {} 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) key = mitogen.core.to_text(key)
value = mitogen.core.to_text(value) value = mitogen.core.to_text(value)
if value is None: if value is None:

@ -50,6 +50,8 @@ import threading
import ansible.constants import ansible.constants
from ansible.module_utils.six import reraise
import mitogen.core import mitogen.core
import mitogen.service import mitogen.service
import ansible_mitogen.loaders import ansible_mitogen.loaders
@ -66,20 +68,6 @@ LOG = logging.getLogger(__name__)
ansible_mitogen.loaders.shell_loader.get('sh') 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(): def _get_candidate_temp_dirs():
try: try:
# >=2.5 # >=2.5

@ -55,12 +55,6 @@ import types
import mitogen.core import mitogen.core
import mitogen.parent import mitogen.parent
import mitogen.service import mitogen.service
try:
reduce
except NameError:
# Python 3.x.
from functools import reduce
# Ansible since PR #41749 inserts "import __main__" into # Ansible since PR #41749 inserts "import __main__" into
# ansible.module_utils.basic. Mitogen's importer will refuse such an import, so # 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__')) sys.modules[str('__main__')] = types.ModuleType(str('__main__'))
import ansible.module_utils.json_utils import ansible.module_utils.json_utils
from ansible.module_utils.six.moves import reduce
import ansible_mitogen.runner import ansible_mitogen.runner

@ -27,6 +27,8 @@ Unreleased
fallbacks for Python 2.4 & 2.5. fallbacks for Python 2.4 & 2.5.
* :gh:issue:`1127` :mod:`ansible_mitogen`: Remove fallback imports for Ansible * :gh:issue:`1127` :mod:`ansible_mitogen`: Remove fallback imports for Ansible
releases before 2.10 releases before 2.10
* :gh:issue:`1127` :mod:`ansible_mitogen`: Consolidate Python 2 & 3
compatibility
v0.3.10 (2024-09-20) v0.3.10 (2024-09-20)

Loading…
Cancel
Save