From 619ea5b7b3ee286250afb568bd55d97249c16ade Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Wed, 12 Nov 2025 21:59:09 +0100 Subject: [PATCH] [PR #11106/f785e9c7 backport][stable-12] replace batch of redundant to_native()/to_text() occurrences (#11140) replace batch of redundant to_native()/to_text() occurrences (#11106) * replace batch of redundant to_native()/to_text() occurrences * add changelog frag * snap sanity * rolling back snap for now * more cases in redhat_subscription (cherry picked from commit f785e9c7803dc7194e8f44ed441bb330ba127147) Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --- changelogs/fragments/11106-tonative-5.yml | 17 +++++++++++++++++ plugins/modules/flatpak_remote.py | 9 ++++----- plugins/modules/hg.py | 5 ++--- plugins/modules/ipa_pwpolicy.py | 3 +-- plugins/modules/iso_customize.py | 3 +-- plugins/modules/nomad_job.py | 3 +-- plugins/modules/nosh.py | 7 +++---- plugins/modules/ocapi_command.py | 10 +++------- plugins/modules/ocapi_info.py | 10 +++------- plugins/modules/packet_project.py | 3 +-- plugins/modules/packet_volume.py | 2 +- plugins/modules/portage.py | 5 ++--- plugins/modules/redhat_subscription.py | 8 ++++---- plugins/modules/xml.py | 7 ++++--- 14 files changed, 47 insertions(+), 45 deletions(-) create mode 100644 changelogs/fragments/11106-tonative-5.yml diff --git a/changelogs/fragments/11106-tonative-5.yml b/changelogs/fragments/11106-tonative-5.yml new file mode 100644 index 0000000000..f6bcb6251a --- /dev/null +++ b/changelogs/fragments/11106-tonative-5.yml @@ -0,0 +1,17 @@ +minor_changes: + - filesize - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11106). + - flatpak_remote - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11106). + - hg - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11106). + - ip_netns - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11106). + - ipa_pwpolicy - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11106). + - iso_customize - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11106). + - ldap_search - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11106). + - nomad_job - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11106). + - nosh - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11106). + - ocapi_command - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11106). + - ocapi_info - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11106). + - packet_project - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11106). + - packet_volume - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11106). + - portage - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11106). + - redhat_subscription - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11106). + - xml - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11106). diff --git a/plugins/modules/flatpak_remote.py b/plugins/modules/flatpak_remote.py index b122ff1f94..5d54acc35f 100644 --- a/plugins/modules/flatpak_remote.py +++ b/plugins/modules/flatpak_remote.py @@ -113,7 +113,6 @@ command: """ from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.common.text.converters import to_bytes, to_native def add_remote(module, binary, name, flatpakrepo_url, method): @@ -141,7 +140,7 @@ def remote_exists(module, binary, name, method): listed_remote = line.split() if len(listed_remote) == 0: continue - if listed_remote[0] == to_native(name): + if listed_remote[0] == name: return True return False @@ -171,7 +170,7 @@ def remote_enabled(module, binary, name, method): listed_remote = line.split() if len(listed_remote) == 0: continue - if listed_remote[0] == to_native(name): + if listed_remote[0] == name: return len(listed_remote) == 1 or "disabled" not in listed_remote[1].split(",") return False @@ -219,7 +218,7 @@ def main(): if not binary: module.fail_json(msg=f"Executable '{executable}' was not found on the system.", **result) - remote_already_exists = remote_exists(module, binary, to_bytes(name), method) + remote_already_exists = remote_exists(module, binary, name, method) if state == "present" and not remote_already_exists: add_remote(module, binary, name, flatpakrepo_url, method) @@ -227,7 +226,7 @@ def main(): remove_remote(module, binary, name, method) if state == "present": - remote_already_enabled = remote_enabled(module, binary, to_bytes(name), method) + remote_already_enabled = remote_enabled(module, binary, name, method) if enabled and not remote_already_enabled: enable_remote(module, binary, name, method) diff --git a/plugins/modules/hg.py b/plugins/modules/hg.py index 62e3bfe9e2..14e6de9720 100644 --- a/plugins/modules/hg.py +++ b/plugins/modules/hg.py @@ -90,7 +90,6 @@ EXAMPLES = r""" import os from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.common.text.converters import to_native class Hg: @@ -123,14 +122,14 @@ class Hg: if rc != 0: self.module.fail_json(msg=err) else: - return to_native(out).strip("\n") + return out.strip("\n") def get_remote_revision(self): (rc, out, err) = self._command(["id", self.repo]) if rc != 0: self.module.fail_json(msg=err) else: - return to_native(out).strip("\n") + return out.strip("\n") def has_local_mods(self): now = self.get_revision() diff --git a/plugins/modules/ipa_pwpolicy.py b/plugins/modules/ipa_pwpolicy.py index 63166953d9..cf96e48b7f 100644 --- a/plugins/modules/ipa_pwpolicy.py +++ b/plugins/modules/ipa_pwpolicy.py @@ -158,7 +158,6 @@ import traceback from ansible.module_utils.basic import AnsibleModule from ansible_collections.community.general.plugins.module_utils.ipa import IPAClient, ipa_argument_spec -from ansible.module_utils.common.text.converters import to_native class PwPolicyIPAClient(IPAClient): @@ -223,7 +222,7 @@ def get_pwpolicy_dict( for option, value in pwpolicy_options.items(): if value is not None: - pwpolicy[option] = to_native(value) + pwpolicy[option] = str(value) for option, value in pwpolicy_boolean_options.items(): if value is not None: diff --git a/plugins/modules/iso_customize.py b/plugins/modules/iso_customize.py index d5ba06f4db..640834c9e6 100644 --- a/plugins/modules/iso_customize.py +++ b/plugins/modules/iso_customize.py @@ -102,7 +102,6 @@ import os from ansible_collections.community.general.plugins.module_utils import deps from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.common.text.converters import to_native with deps.declare("pycdlib"): import pycdlib @@ -278,7 +277,7 @@ def iso_rebuild(module, src_iso, dest_iso, delete_files_list, add_files_list): iso.write(dest_iso) except Exception as err: - msg = f"Failed to rebuild ISO {src_iso} with error: {to_native(err)}" + msg = f"Failed to rebuild ISO {src_iso} with error: {err}" module.fail_json(msg=msg) finally: if iso: diff --git a/plugins/modules/nomad_job.py b/plugins/modules/nomad_job.py index 342d3d6a40..a3b443401d 100644 --- a/plugins/modules/nomad_job.py +++ b/plugins/modules/nomad_job.py @@ -181,8 +181,7 @@ def run(): job = dict() job["job"] = job_json except nomad.api.exceptions.BadRequestNomadException as err: - msg = f"{err.nomad_resp.reason} {err.nomad_resp.text}" - module.fail_json(msg=to_native(msg)) + module.fail_json(msg=f"{err.nomad_resp.reason} {err.nomad_resp.text}") try: job_id = job_json.get("ID") plan = nomad_client.job.plan_job(job_id, job, diff=True) diff --git a/plugins/modules/nosh.py b/plugins/modules/nosh.py index 0a628c9fe3..7137b6192c 100644 --- a/plugins/modules/nosh.py +++ b/plugins/modules/nosh.py @@ -326,7 +326,6 @@ import json from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.service import fail_if_missing -from ansible.module_utils.common.text.converters import to_native def run_sys_ctl(module, args): @@ -342,7 +341,7 @@ def get_service_path(module, service): if rc != 0: fail_if_missing(module, False, service, msg="host") else: - return to_native(out).strip() + return out.strip() def service_is_enabled(module, service_path): @@ -352,7 +351,7 @@ def service_is_enabled(module, service_path): def service_is_preset_enabled(module, service_path): (rc, out, err) = run_sys_ctl(module, ["preset", "--dry-run", service_path]) - return to_native(out).strip().startswith("enable") + return out.strip().startswith("enable") def service_is_loaded(module, service_path): @@ -366,7 +365,7 @@ def get_service_status(module, service_path): if err is not None and err: module.fail_json(msg=err) else: - json_out = json.loads(to_native(out).strip()) + json_out = json.loads(out.strip()) status = json_out[service_path] # descend past service path header return status diff --git a/plugins/modules/ocapi_command.py b/plugins/modules/ocapi_command.py index 6ed55dff28..3308b30a4f 100644 --- a/plugins/modules/ocapi_command.py +++ b/plugins/modules/ocapi_command.py @@ -209,15 +209,11 @@ def main(): # Check that Category is valid if category not in CATEGORY_COMMANDS_ALL: - module.fail_json( - msg=to_native(f"Invalid Category '{category}'. Valid Categories = {list(CATEGORY_COMMANDS_ALL.keys())}") - ) + module.fail_json(msg=f"Invalid Category '{category}'. Valid Categories = {list(CATEGORY_COMMANDS_ALL.keys())}") # Check that the command is valid if command not in CATEGORY_COMMANDS_ALL[category]: - module.fail_json( - msg=to_native(f"Invalid Command '{command}'. Valid Commands = {CATEGORY_COMMANDS_ALL[category]}") - ) + module.fail_json(msg=f"Invalid Command '{command}'. Valid Commands = {CATEGORY_COMMANDS_ALL[category]}") # Organize by Categories / Commands if category == "Chassis": @@ -232,7 +228,7 @@ def main(): if command == "FWUpload": update_image_path = module.params.get("update_image_path") if update_image_path is None: - module.fail_json(msg=to_native("Missing update_image_path.")) + module.fail_json(msg="Missing update_image_path.") result = ocapi_utils.upload_firmware_image(update_image_path) elif command == "FWUpdate": result = ocapi_utils.update_firmware_image() diff --git a/plugins/modules/ocapi_info.py b/plugins/modules/ocapi_info.py index 3a302df1de..d32083c116 100644 --- a/plugins/modules/ocapi_info.py +++ b/plugins/modules/ocapi_info.py @@ -178,21 +178,17 @@ def main(): # Check that Category is valid if category not in CATEGORY_COMMANDS_ALL: - module.fail_json( - msg=to_native(f"Invalid Category '{category}'. Valid Categories = {list(CATEGORY_COMMANDS_ALL.keys())}") - ) + module.fail_json(msg=f"Invalid Category '{category}'. Valid Categories = {list(CATEGORY_COMMANDS_ALL.keys())}") # Check that the command is valid if command not in CATEGORY_COMMANDS_ALL[category]: - module.fail_json( - msg=to_native(f"Invalid Command '{command}'. Valid Commands = {CATEGORY_COMMANDS_ALL[category]}") - ) + module.fail_json(msg=f"Invalid Command '{command}'. Valid Commands = {CATEGORY_COMMANDS_ALL[category]}") # Organize by Categories / Commands if category == "Jobs": if command == "JobStatus": if module.params.get("job_name") is None: - module.fail_json(msg=to_native("job_name required for JobStatus command.")) + module.fail_json(msg="job_name required for JobStatus command.") job_uri = urljoin(base_uri, f"Jobs/{module.params['job_name']}") result = ocapi_utils.get_job_status(job_uri) diff --git a/plugins/modules/packet_project.py b/plugins/modules/packet_project.py index 4c39cf4ffa..b7b974a5bc 100644 --- a/plugins/modules/packet_project.py +++ b/plugins/modules/packet_project.py @@ -120,7 +120,6 @@ id: """ from ansible.module_utils.basic import AnsibleModule, env_fallback -from ansible.module_utils.common.text.converters import to_native HAS_PACKET_SDK = True @@ -163,7 +162,7 @@ def act_on_project(target_state, module, packet_conn): result_dict["id"] = matching_projects[0].id else: if len(matching_projects) > 1: - _msg = f"More than projects matched for module call with state = absent: {to_native(matching_projects)}" + _msg = f"More than projects matched for module call with state = absent: {matching_projects}" module.fail_json(msg=_msg) if len(matching_projects) == 1: diff --git a/plugins/modules/packet_volume.py b/plugins/modules/packet_volume.py index e49f4af37e..fce57f6ddb 100644 --- a/plugins/modules/packet_volume.py +++ b/plugins/modules/packet_volume.py @@ -249,7 +249,7 @@ def act_on_volume(target_state, module, packet_conn): else: if len(matching_volumes) > 1: - _msg = f"More than one volume matches in module call for absent state: {to_native(matching_volumes)}" + _msg = f"More than one volume matches in module call for absent state: {matching_volumes}" module.fail_json(msg=_msg) if len(matching_volumes) == 1: diff --git a/plugins/modules/portage.py b/plugins/modules/portage.py index 51c42e1133..1e7a408969 100644 --- a/plugins/modules/portage.py +++ b/plugins/modules/portage.py @@ -260,7 +260,6 @@ import traceback from ansible.module_utils.basic import AnsibleModule, missing_required_lib from ansible.module_utils.common.respawn import has_respawned, respawn_module -from ansible.module_utils.common.text.converters import to_native try: @@ -407,12 +406,12 @@ def emerge_packages(module, packages): """Add the --flag=value pair.""" if isinstance(flag_val, bool): - args.extend((arg, to_native("y" if flag_val else "n"))) + args.extend((arg, "y" if flag_val else "n")) elif not flag_val: """If the value is 0 or 0.0: add the flag, but not the value.""" args.append(arg) else: - args.extend((arg, to_native(flag_val))) + args.extend((arg, str(flag_val))) cmd, (rc, out, err) = run_emerge(module, packages, *args) if rc != 0: diff --git a/plugins/modules/redhat_subscription.py b/plugins/modules/redhat_subscription.py index f4fc888577..bc1385fa71 100644 --- a/plugins/modules/redhat_subscription.py +++ b/plugins/modules/redhat_subscription.py @@ -1151,7 +1151,7 @@ def main(): try: syspurpose_changed = SysPurpose().update_syspurpose(syspurpose) except Exception as err: - module.fail_json(msg=f"Failed to update syspurpose attributes: {to_native(err)}") + module.fail_json(msg=f"Failed to update syspurpose attributes: {err}") # Ensure system is registered if state == "present": @@ -1164,12 +1164,12 @@ def main(): try: rhsm.sync_syspurpose() except Exception as e: - module.fail_json(msg=f"Failed to synchronize syspurpose attributes: {to_native(e)}") + module.fail_json(msg=f"Failed to synchronize syspurpose attributes: {e}") if pool_ids: try: result = rhsm.update_subscriptions_by_pool_ids(pool_ids) except Exception as e: - module.fail_json(msg=f"Failed to update subscriptions for '{server_hostname}': {to_native(e)}") + module.fail_json(msg=f"Failed to update subscriptions for '{server_hostname}': {e}") else: module.exit_json(**result) else: @@ -1207,7 +1207,7 @@ def main(): else: subscribed_pool_ids = [] except Exception as e: - module.fail_json(msg=f"Failed to register with '{server_hostname}': {to_native(e)}") + module.fail_json(msg=f"Failed to register with '{server_hostname}': {e}") else: module.exit_json( changed=True, diff --git a/plugins/modules/xml.py b/plugins/modules/xml.py index 40686c6f25..d94a02032b 100644 --- a/plugins/modules/xml.py +++ b/plugins/modules/xml.py @@ -370,13 +370,14 @@ LXML_IMP_ERR = None try: from lxml import etree, objectify + LXML_VERSION_STR = ".".join(str(f) for f in etree.LXML_VERSION) HAS_LXML = True except ImportError: LXML_IMP_ERR = traceback.format_exc() HAS_LXML = False from ansible.module_utils.basic import AnsibleModule, json_dict_bytes_to_unicode, missing_required_lib -from ansible.module_utils.common.text.converters import to_bytes, to_native +from ansible.module_utils.common.text.converters import to_bytes _IDENT = r"[a-zA-Z-][a-zA-Z0-9_\-\.]*" _NSIDENT = f"{_IDENT}|{_IDENT}:{_IDENT}" @@ -925,9 +926,9 @@ def main(): # Check if we have lxml 2.3.0 or newer installed if not HAS_LXML: module.fail_json(msg=missing_required_lib("lxml"), exception=LXML_IMP_ERR) - elif LooseVersion(".".join(to_native(f) for f in etree.LXML_VERSION)) < LooseVersion("2.3.0"): + elif LooseVersion(LXML_VERSION_STR) < LooseVersion("2.3.0"): module.fail_json(msg="The xml ansible module requires lxml 2.3.0 or newer installed on the managed machine") - elif LooseVersion(".".join(to_native(f) for f in etree.LXML_VERSION)) < LooseVersion("3.0.0"): + elif LooseVersion(LXML_VERSION_STR) < LooseVersion("3.0.0"): module.warn("Using lxml version lower than 3.0.0 does not guarantee predictable element attribute order.") infile = None