From 4baf18c573c17cf9cd5716b28dbf38a32b57aaff Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Tue, 22 Mar 2022 16:19:53 +0100 Subject: [PATCH] Remove more Python 2.x compatibility code from controller. (#77320) --- changelogs/fragments/77320-python-2.yml | 2 ++ lib/ansible/executor/module_common.py | 4 ++-- lib/ansible/plugins/connection/__init__.py | 13 ++----------- lib/ansible/utils/cmd_functions.py | 3 +-- 4 files changed, 7 insertions(+), 15 deletions(-) create mode 100644 changelogs/fragments/77320-python-2.yml diff --git a/changelogs/fragments/77320-python-2.yml b/changelogs/fragments/77320-python-2.yml new file mode 100644 index 00000000000..c4bb4ecbadf --- /dev/null +++ b/changelogs/fragments/77320-python-2.yml @@ -0,0 +1,2 @@ +minor_changes: + - "Remove more Python 2.x compatibility code from controller (https://github.com/ansible/ansible/pull/77320)." diff --git a/lib/ansible/executor/module_common.py b/lib/ansible/executor/module_common.py index f906be67a86..470b085190f 100644 --- a/lib/ansible/executor/module_common.py +++ b/lib/ansible/executor/module_common.py @@ -1314,8 +1314,8 @@ def _extract_interpreter(b_module_data): if b_lines[0].startswith(b"#!"): b_shebang = b_lines[0].strip() - # shlex.split on python-2.6 needs bytes. On python-3.x it needs text - cli_split = shlex.split(to_native(b_shebang[2:], errors='surrogate_or_strict')) + # shlex.split needs text on Python 3 + cli_split = shlex.split(to_text(b_shebang[2:], errors='surrogate_or_strict')) # convert args to text cli_split = [to_text(a, errors='surrogate_or_strict') for a in cli_split] diff --git a/lib/ansible/plugins/connection/__init__.py b/lib/ansible/plugins/connection/__init__.py index 289ef137d4d..fd555c8e394 100644 --- a/lib/ansible/plugins/connection/__init__.py +++ b/lib/ansible/plugins/connection/__init__.py @@ -109,17 +109,8 @@ class ConnectionBase(AnsiblePlugin): list ['-o', 'Foo=1', '-o', 'Bar=foo bar'] that can be added to the argument list. The list will not contain any empty elements. """ - try: - # Python 2.6.x shlex doesn't handle unicode type so we have to - # convert args to byte string for that case. More efficient to - # try without conversion first but python2.6 doesn't throw an - # exception, it merely mangles the output: - # >>> shlex.split(u't e') - # ['t\x00\x00\x00', '\x00\x00\x00e\x00\x00\x00'] - return [to_text(x.strip()) for x in shlex.split(to_bytes(argstring)) if x.strip()] - except AttributeError: - # In Python3, shlex.split doesn't work on a byte string. - return [to_text(x.strip()) for x in shlex.split(argstring) if x.strip()] + # In Python3, shlex.split doesn't work on a byte string. + return [to_text(x.strip()) for x in shlex.split(argstring) if x.strip()] @property @abstractmethod diff --git a/lib/ansible/utils/cmd_functions.py b/lib/ansible/utils/cmd_functions.py index c35e2b35b0e..d4edb2f45fd 100644 --- a/lib/ansible/utils/cmd_functions.py +++ b/lib/ansible/utils/cmd_functions.py @@ -30,8 +30,7 @@ from ansible.module_utils._text import to_bytes def run_cmd(cmd, live=False, readsize=10): cmdargs = shlex.split(cmd) - # subprocess should be passed byte strings. (on python2.6 it must be - # passed byte strtings) + # subprocess should be passed byte strings. cmdargs = [to_bytes(a, errors='surrogate_or_strict') for a in cmdargs] p = subprocess.Popen(cmdargs, stdout=subprocess.PIPE, stderr=subprocess.PIPE)