ansible_mitogen: Remove Python 2.4 and 2.5 backward compatibility fallbacks

Because ansible_mitogen >= 0.3 supports Ansible >= 2.10 and Ansible 2.10
requires supports Python >= 2.7 on controllers and Python >= 2.6 on targets
these are dead weight.

See
- https://docs.ansible.com/ansible/latest/reference_appendices/release_and_maintenance.html#ansible-core-support-matrix
- tox.ini
pull/1127/head
Alex Willmer 2 months ago
parent c6cf08ab39
commit b1fd6038bf

@ -83,7 +83,6 @@ import multiprocessing
import os import os
import struct import struct
import mitogen.core
import mitogen.parent import mitogen.parent
@ -265,7 +264,7 @@ class LinuxPolicy(FixedPolicy):
for x in range(16): for x in range(16):
chunks.append(struct.pack('<Q', mask & shiftmask)) chunks.append(struct.pack('<Q', mask & shiftmask))
mask >>= 64 mask >>= 64
return mitogen.core.b('').join(chunks) return b''.join(chunks)
def _get_thread_ids(self): def _get_thread_ids(self):
try: try:

@ -477,7 +477,7 @@ def read_file(path):
finally: finally:
os.close(fd) os.close(fd)
return mitogen.core.b('').join(bits) return b''.join(bits)
def _propagate_deps(invocation, planner, context): def _propagate_deps(invocation, planner, context):

@ -61,10 +61,9 @@ import mitogen.utils
import ansible import ansible
import ansible.constants as C import ansible.constants as C
import ansible.errors import ansible.errors
import ansible_mitogen.logging import ansible_mitogen.logging
import ansible_mitogen.services import ansible_mitogen.services
from mitogen.core import b
import ansible_mitogen.affinity import ansible_mitogen.affinity
@ -639,7 +638,7 @@ class MuxProcess(object):
try: try:
# Let the parent know our listening socket is ready. # Let the parent know our listening socket is ready.
mitogen.core.io_op(self.model.child_sock.send, b('1')) mitogen.core.io_op(self.model.child_sock.send, b'1')
# Block until the socket is closed, which happens on parent exit. # Block until the socket is closed, which happens on parent exit.
mitogen.core.io_op(self.model.child_sock.recv, 1) mitogen.core.io_op(self.model.child_sock.recv, 1)
finally: finally:

@ -40,7 +40,9 @@ from __future__ import absolute_import, division, print_function
__metaclass__ = type __metaclass__ = type
import atexit import atexit
import ctypes
import json import json
import logging
import os import os
import re import re
import shlex import shlex
@ -52,17 +54,8 @@ import types
import mitogen.core import mitogen.core
import ansible_mitogen.target # TODO: circular import import ansible_mitogen.target # TODO: circular import
from mitogen.core import b
from mitogen.core import bytes_partition
from mitogen.core import str_rpartition
from mitogen.core import to_text from mitogen.core import to_text
try:
import ctypes
except ImportError:
# Python 2.4
ctypes = None
try: try:
# Python >= 3.4, PEP 451 ModuleSpec API # Python >= 3.4, PEP 451 ModuleSpec API
import importlib.machinery import importlib.machinery
@ -82,9 +75,6 @@ try:
except ImportError: except ImportError:
from pipes import quote as shlex_quote from pipes import quote as shlex_quote
# Absolute imports for <2.5.
logging = __import__('logging')
# 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
@ -95,9 +85,8 @@ ansible.module_utils.basic._ANSIBLE_ARGS = '{}'
# explicit call to res_init() on each task invocation. BSD-alikes export it # explicit call to res_init() on each task invocation. BSD-alikes export it
# directly, Linux #defines it as "__res_init". # directly, Linux #defines it as "__res_init".
libc__res_init = None libc__res_init = None
if ctypes: libc = ctypes.CDLL(None)
libc = ctypes.CDLL(None) for symbol in 'res_init', '__res_init':
for symbol in 'res_init', '__res_init':
try: try:
libc__res_init = getattr(libc, symbol) libc__res_init = getattr(libc, symbol)
except AttributeError: except AttributeError:
@ -217,13 +206,13 @@ class EnvironmentFileWatcher(object):
for line in fp: for line in fp:
# ' #export foo=some var ' -> ['#export', 'foo=some var '] # ' #export foo=some var ' -> ['#export', 'foo=some var ']
bits = shlex_split_b(line) bits = shlex_split_b(line)
if (not bits) or bits[0].startswith(b('#')): if (not bits) or bits[0].startswith(b'#'):
continue continue
if bits[0] == b('export'): if bits[0] == b'export':
bits.pop(0) bits.pop(0)
key, sep, value = bytes_partition(b(' ').join(bits), b('=')) key, sep, value = b' '.join(bits).partition(b'=')
if key and sep: if key and sep:
yield key, value yield key, value
@ -596,7 +585,7 @@ class ModuleUtilsImporter(object):
mod.__path__ = [] mod.__path__ = []
mod.__package__ = str(fullname) mod.__package__ = str(fullname)
else: else:
mod.__package__ = str(str_rpartition(to_text(fullname), '.')[0]) mod.__package__ = str(to_text(fullname).rpartition('.')[0])
exec(code, mod.__dict__) exec(code, mod.__dict__)
self._loaded.add(fullname) self._loaded.add(fullname)
return mod return mod
@ -819,7 +808,7 @@ class ScriptRunner(ProgramRunner):
self.interpreter_fragment = interpreter_fragment self.interpreter_fragment = interpreter_fragment
self.is_python = is_python self.is_python = is_python
b_ENCODING_STRING = b('# -*- coding: utf-8 -*-') b_ENCODING_STRING = b'# -*- coding: utf-8 -*-'
def _get_program(self): def _get_program(self):
return self._rewrite_source( return self._rewrite_source(
@ -852,13 +841,13 @@ class ScriptRunner(ProgramRunner):
# While Ansible rewrites the #! using ansible_*_interpreter, it is # While Ansible rewrites the #! using ansible_*_interpreter, it is
# never actually used to execute the script, instead it is a shell # never actually used to execute the script, instead it is a shell
# fragment consumed by shell/__init__.py::build_module_command(). # fragment consumed by shell/__init__.py::build_module_command().
new = [b('#!') + utf8(self.interpreter_fragment)] new = [b'#!' + utf8(self.interpreter_fragment)]
if self.is_python: if self.is_python:
new.append(self.b_ENCODING_STRING) new.append(self.b_ENCODING_STRING)
_, _, rest = bytes_partition(s, b('\n')) _, _, rest = s.partition(b'\n')
new.append(rest) new.append(rest)
return b('\n').join(new) return b'\n'.join(new)
class NewStyleRunner(ScriptRunner): class NewStyleRunner(ScriptRunner):
@ -971,8 +960,7 @@ class NewStyleRunner(ScriptRunner):
# change the default encoding. This hack was removed from Ansible long ago, # change the default encoding. This hack was removed from Ansible long ago,
# but not before permeating into many third party modules. # but not before permeating into many third party modules.
PREHISTORIC_HACK_RE = re.compile( PREHISTORIC_HACK_RE = re.compile(
b(r'reload\s*\(\s*sys\s*\)\s*' br'reload\s*\(\s*sys\s*\)\s*sys\s*\.\s*setdefaultencoding\([^)]+\)',
r'sys\s*\.\s*setdefaultencoding\([^)]+\)')
) )
def _setup_program(self): def _setup_program(self):
@ -980,7 +968,7 @@ class NewStyleRunner(ScriptRunner):
context=self.service_context, context=self.service_context,
path=self.path, path=self.path,
) )
self.source = self.PREHISTORIC_HACK_RE.sub(b(''), source) self.source = self.PREHISTORIC_HACK_RE.sub(b'', source)
def _get_code(self): def _get_code(self):
try: try:
@ -998,7 +986,7 @@ class NewStyleRunner(ScriptRunner):
if mitogen.core.PY3: if mitogen.core.PY3:
main_module_name = '__main__' main_module_name = '__main__'
else: else:
main_module_name = b('__main__') main_module_name = b'__main__'
def _handle_magic_exception(self, mod, exc): def _handle_magic_exception(self, mod, exc):
""" """
@ -1030,7 +1018,7 @@ class NewStyleRunner(ScriptRunner):
approximation of the original package hierarchy, so that relative approximation of the original package hierarchy, so that relative
imports function correctly. imports function correctly.
""" """
pkg, sep, modname = str_rpartition(self.py_module_name, '.') pkg, sep, _ = self.py_module_name.rpartition('.')
if not sep: if not sep:
return None return None
if mitogen.core.PY3: if mitogen.core.PY3:
@ -1073,7 +1061,7 @@ class NewStyleRunner(ScriptRunner):
class JsonArgsRunner(ScriptRunner): class JsonArgsRunner(ScriptRunner):
JSON_ARGS = b('<<INCLUDE_ANSIBLE_MODULE_JSON_ARGS>>') JSON_ARGS = b'<<INCLUDE_ANSIBLE_MODULE_JSON_ARGS>>'
def _get_args_contents(self): def _get_args_contents(self):
return json.dumps(self.args).encode() return json.dumps(self.args).encode()

@ -39,6 +39,7 @@ __metaclass__ = type
import errno import errno
import grp import grp
import json import json
import logging
import operator import operator
import os import os
import pwd import pwd
@ -51,26 +52,15 @@ import tempfile
import traceback import traceback
import types import types
# Absolute imports for <2.5.
logging = __import__('logging')
import mitogen.core import mitogen.core
import mitogen.parent import mitogen.parent
import mitogen.service import mitogen.service
from mitogen.core import b
try: try:
reduce reduce
except NameError: except NameError:
# Python 3.x. # Python 3.x.
from functools import reduce from functools import reduce
try:
BaseException
except NameError:
# Python 2.4
BaseException = Exception
# 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
@ -615,8 +605,8 @@ def exec_args(args, in_data='', chdir=None, shell=None, emulate_tty=False):
stdout, stderr = proc.communicate(in_data) stdout, stderr = proc.communicate(in_data)
if emulate_tty: if emulate_tty:
stdout = stdout.replace(b('\n'), b('\r\n')) stdout = stdout.replace(b'\n', b'\r\n')
return proc.returncode, stdout, stderr or b('') return proc.returncode, stdout, stderr or b''
def exec_command(cmd, in_data='', chdir=None, shell=None, emulate_tty=False): def exec_command(cmd, in_data='', chdir=None, shell=None, emulate_tty=False):

@ -23,6 +23,8 @@ Unreleased
* :gh:issue:`1127` :mod:`mitogen`: Consolidate mitogen backward compatibility * :gh:issue:`1127` :mod:`mitogen`: Consolidate mitogen backward compatibility
fallbacks and polyfills into :mod:`mitogen.core` fallbacks and polyfills into :mod:`mitogen.core`
* :gh:issue:`1127` :mod:`ansible_mitogen`: Remove backward compatibility
fallbacks for Python 2.4 & 2.5.
v0.3.10 (2024-09-20) v0.3.10 (2024-09-20)

@ -11,17 +11,6 @@ import socket
import sys import sys
try:
all
except NameError:
# Python 2.4
def all(it):
for elem in it:
if not elem:
return False
return True
def main(): def main():
module = AnsibleModule(argument_spec={}) module = AnsibleModule(argument_spec={})
module.exit_json( module.exit_json(

@ -48,6 +48,9 @@
# ansible == 9.x ansible-core ~= 2.16.0 # ansible == 9.x ansible-core ~= 2.16.0
# ansible == 10.x ansible-core ~= 2.17.0 # ansible == 10.x ansible-core ~= 2.17.0
# See also
# - https://docs.ansible.com/ansible/latest/reference_appendices/release_and_maintenance.html#ansible-core-support-matrix
[tox] [tox]
envlist = envlist =
init, init,

Loading…
Cancel
Save