diff --git a/changelogs/fragments/11344-pyupgrade-4.yml b/changelogs/fragments/11344-pyupgrade-4.yml new file mode 100644 index 0000000000..7d7da4c78c --- /dev/null +++ b/changelogs/fragments/11344-pyupgrade-4.yml @@ -0,0 +1,33 @@ +minor_changes: + - manageiq_alert_profiles - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - modprobe - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - mssql_db - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - nagios - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - nosh - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - omapi_host - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - one_vm - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - pam_limits - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - pamd - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - parted - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - pubnub_blocks - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - pulp_repo - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - read_csv - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - redhat_subscription - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - rhsm_repository - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - runit - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - sensu_check - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - sensu_client - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - sensu_handler - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - sensu_subscription - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - seport - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - serverless - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - solaris_zone - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - sorcery - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - spotinst_aws_elastigroup - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - sudoers - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - svc - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - timezone - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - wakeonlan - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - xfs_quota - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - zypper - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). + - zypper_repository - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11344). diff --git a/plugins/modules/manageiq_alert_profiles.py b/plugins/modules/manageiq_alert_profiles.py index 6cf01ef4ff..bb4f1ddf10 100644 --- a/plugins/modules/manageiq_alert_profiles.py +++ b/plugins/modules/manageiq_alert_profiles.py @@ -184,7 +184,7 @@ class ManageIQAlertProfiles: # alert which currently exist in the profile if "alert_definitions" in old_profile: # we use get_alert_href to have a direct href to the alert - existing_alerts = set(self.get_alert_href(alert) for alert in old_profile["alert_definitions"]) + existing_alerts = {self.get_alert_href(alert) for alert in old_profile["alert_definitions"]} else: # no alerts in this profile existing_alerts = set() diff --git a/plugins/modules/modprobe.py b/plugins/modules/modprobe.py index 532ca5df90..fc12e0351c 100644 --- a/plugins/modules/modprobe.py +++ b/plugins/modules/modprobe.py @@ -258,7 +258,7 @@ class Modprobe: if line.rstrip().endswith(module_file): is_loaded = True break - except (IOError, OSError) as e: + except OSError as e: self.module.fail_json(msg=f"{e}", exception=traceback.format_exc(), **self.result) return is_loaded diff --git a/plugins/modules/mssql_db.py b/plugins/modules/mssql_db.py index 4fbef15856..1ee5557963 100644 --- a/plugins/modules/mssql_db.py +++ b/plugins/modules/mssql_db.py @@ -131,7 +131,7 @@ def db_delete(conn, cursor, db): def db_import(conn, cursor, module, db, target): if os.path.isfile(target): - with open(target, "r") as backup: + with open(target) as backup: sqlQuery = f"USE [{db}]\n" for line in backup: if line is None: diff --git a/plugins/modules/nagios.py b/plugins/modules/nagios.py index 0aeaa23ef3..60eb5da07f 100644 --- a/plugins/modules/nagios.py +++ b/plugins/modules/nagios.py @@ -422,7 +422,7 @@ class Nagios: fp.write(cmd) fp.flush() self.command_results.append(cmd.strip()) - except IOError: + except OSError: self.module.fail_json(msg="unable to write to nagios command file", cmdfile=self.cmdfile) def _fmt_dt_str(self, cmd, host, duration, author=None, comment=None, start=None, svc=None, fixed=1, trigger=0): diff --git a/plugins/modules/nosh.py b/plugins/modules/nosh.py index 7137b6192c..ab9a4b4d79 100644 --- a/plugins/modules/nosh.py +++ b/plugins/modules/nosh.py @@ -371,7 +371,7 @@ def get_service_status(module, service_path): def service_is_running(service_status): - return service_status["DaemontoolsEncoreState"] in set(["starting", "started", "running"]) + return service_status["DaemontoolsEncoreState"] in {"starting", "started", "running"} def handle_enabled(module, result, service_path): diff --git a/plugins/modules/omapi_host.py b/plugins/modules/omapi_host.py index 3ad9d0c212..1a0f0a5383 100644 --- a/plugins/modules/omapi_host.py +++ b/plugins/modules/omapi_host.py @@ -129,7 +129,6 @@ lease: """ import binascii -import socket import struct import traceback @@ -168,7 +167,7 @@ class OmapiHostManager: self.module.fail_json( msg=f"Unable to open OMAPI connection. Ensure 'host', 'port', 'key' and 'key_name' are valid. Exception was: {e}" ) - except socket.error as e: + except OSError as e: self.module.fail_json(msg=f"Unable to connect to OMAPI server: {e}") def get_host(self, macaddr): diff --git a/plugins/modules/one_vm.py b/plugins/modules/one_vm.py index 1ad5a5e8d6..9e6775a43b 100644 --- a/plugins/modules/one_vm.py +++ b/plugins/modules/one_vm.py @@ -1628,11 +1628,11 @@ def get_connection_info(module): if authfile is None: authfile = os.path.join(os.environ.get("HOME"), ".one", "one_auth") try: - with open(authfile, "r") as fp: + with open(authfile) as fp: authstring = fp.read().rstrip() username = authstring.split(":")[0] password = authstring.split(":")[1] - except (OSError, IOError): + except OSError: module.fail_json(msg=f"Could not find or read ONE_AUTH file at '{authfile}'") except Exception: module.fail_json(msg=f"Error occurs when read ONE_AUTH file at '{authfile}'") diff --git a/plugins/modules/pam_limits.py b/plugins/modules/pam_limits.py index 5bc0239c25..022f1d58ee 100644 --- a/plugins/modules/pam_limits.py +++ b/plugins/modules/pam_limits.py @@ -348,7 +348,7 @@ def main(): nf.flush() - with open(nf.name, "r") as content: + with open(nf.name) as content: content_new = content.read() if not module.check_mode: diff --git a/plugins/modules/pamd.py b/plugins/modules/pamd.py index e8cfee5424..c09dfdbda7 100644 --- a/plugins/modules/pamd.py +++ b/plugins/modules/pamd.py @@ -787,9 +787,9 @@ def main(): # Open the file and read the content or fail try: - with open(fname, "r") as service_file_obj: + with open(fname) as service_file_obj: content = service_file_obj.read() - except IOError as e: + except OSError as e: # If unable to read the file, fail out module.fail_json(msg=f"Unable to open/read PAM module file {fname} with error {e}.") @@ -875,7 +875,7 @@ def main(): with open(temp_file.name, "w") as fd: fd.write(str(service)) - except IOError: + except OSError: module.fail_json(msg=f"Unable to create temporary file {temp_file}") module.atomic_move(temp_file.name, os.path.realpath(fname)) diff --git a/plugins/modules/parted.py b/plugins/modules/parted.py index 1d4f8b1080..3397a8d6fb 100644 --- a/plugins/modules/parted.py +++ b/plugins/modules/parted.py @@ -569,9 +569,9 @@ def read_record(file_path, default=None): Reads the first line of a file and returns it. """ try: - with open(file_path, "r") as f: + with open(file_path) as f: return f.readline().strip() - except IOError: + except OSError: return default diff --git a/plugins/modules/pubnub_blocks.py b/plugins/modules/pubnub_blocks.py index 58cba5e7d5..a849f3356c 100644 --- a/plugins/modules/pubnub_blocks.py +++ b/plugins/modules/pubnub_blocks.py @@ -512,7 +512,7 @@ def _content_of_file_at_path(path): """ content = None if path and os.path.exists(path): - with open(path, mode="rt") as opened_file: + with open(path) as opened_file: b_content = opened_file.read() try: content = to_text(b_content, errors="surrogate_or_strict") diff --git a/plugins/modules/pulp_repo.py b/plugins/modules/pulp_repo.py index a9f934788a..1de265ee24 100644 --- a/plugins/modules/pulp_repo.py +++ b/plugins/modules/pulp_repo.py @@ -541,19 +541,19 @@ def main(): if importer_ssl_ca_cert is not None: importer_ssl_ca_cert_file_path = os.path.abspath(importer_ssl_ca_cert) if os.path.isfile(importer_ssl_ca_cert_file_path): - with open(importer_ssl_ca_cert_file_path, "r") as importer_ssl_ca_cert_file_object: + with open(importer_ssl_ca_cert_file_path) as importer_ssl_ca_cert_file_object: importer_ssl_ca_cert = importer_ssl_ca_cert_file_object.read() if importer_ssl_client_cert is not None: importer_ssl_client_cert_file_path = os.path.abspath(importer_ssl_client_cert) if os.path.isfile(importer_ssl_client_cert_file_path): - with open(importer_ssl_client_cert_file_path, "r") as importer_ssl_client_cert_file_object: + with open(importer_ssl_client_cert_file_path) as importer_ssl_client_cert_file_object: importer_ssl_client_cert = importer_ssl_client_cert_file_object.read() if importer_ssl_client_key is not None: importer_ssl_client_key_file_path = os.path.abspath(importer_ssl_client_key) if os.path.isfile(importer_ssl_client_key_file_path): - with open(importer_ssl_client_key_file_path, "r") as importer_ssl_client_key_file_object: + with open(importer_ssl_client_key_file_path) as importer_ssl_client_key_file_object: importer_ssl_client_key = importer_ssl_client_key_file_object.read() server = pulp_server(module, pulp_host, repo_type, wait_for_completion=wait_for_completion) diff --git a/plugins/modules/read_csv.py b/plugins/modules/read_csv.py index edfd4586dc..3be3b90c9c 100644 --- a/plugins/modules/read_csv.py +++ b/plugins/modules/read_csv.py @@ -189,7 +189,7 @@ def main(): try: with open(path, "rb") as f: data = f.read() - except (IOError, OSError) as e: + except OSError as e: module.fail_json(msg=f"Unable to open file: {e}") reader = read_csv(data, dialect, fieldnames) diff --git a/plugins/modules/redhat_subscription.py b/plugins/modules/redhat_subscription.py index bc1385fa71..c73850a305 100644 --- a/plugins/modules/redhat_subscription.py +++ b/plugins/modules/redhat_subscription.py @@ -1034,9 +1034,9 @@ class SysPurpose: """ current_syspurpose = {} try: - with open(self.path, "r") as fp: + with open(self.path) as fp: content = fp.read() - except IOError: + except OSError: pass else: current_syspurpose = json.loads(content) diff --git a/plugins/modules/rhsm_repository.py b/plugins/modules/rhsm_repository.py index 4f96ea4824..e2a77693e4 100644 --- a/plugins/modules/rhsm_repository.py +++ b/plugins/modules/rhsm_repository.py @@ -199,7 +199,7 @@ def repository_modify(module, rhsm, state, name, purge=False): # Disable all enabled repos on the system that are not in the task and not # marked as disabled by the task if purge: - enabled_repo_ids = set(repo["id"] for repo in updated_repo_list if repo["enabled"]) + enabled_repo_ids = {repo["id"] for repo in updated_repo_list if repo["enabled"]} matched_repoids_set = set(matched_existing_repo.keys()) difference = enabled_repo_ids.difference(matched_repoids_set) if len(difference) > 0: diff --git a/plugins/modules/runit.py b/plugins/modules/runit.py index cda921d9e0..4a8ec97fc5 100644 --- a/plugins/modules/runit.py +++ b/plugins/modules/runit.py @@ -241,7 +241,7 @@ def main(): sv.enable() else: sv.disable() - except (OSError, IOError) as e: + except OSError as e: module.fail_json(msg=f"Could not change service link: {e}") if state is not None and state != sv.state: diff --git a/plugins/modules/sensu_check.py b/plugins/modules/sensu_check.py index 7ccaa577e6..2f1082d407 100644 --- a/plugins/modules/sensu_check.py +++ b/plugins/modules/sensu_check.py @@ -193,9 +193,9 @@ def sensu_check(module, path, name, state="present", backup=False): stream = None try: try: - stream = open(path, "r") + stream = open(path) config = json.load(stream) - except IOError as e: + except OSError as e: if e.errno == 2: # File not found, non-fatal if state == "absent": reasons.append("file did not exist and state is `absent'") @@ -323,7 +323,7 @@ def sensu_check(module, path, name, state="present", backup=False): try: stream = open(path, "w") stream.write(json.dumps(config, indent=2) + "\n") - except IOError as e: + except OSError as e: module.fail_json(msg=f"{e}", exception=traceback.format_exc()) finally: if stream: diff --git a/plugins/modules/sensu_client.py b/plugins/modules/sensu_client.py index c469c3682e..110818dcb9 100644 --- a/plugins/modules/sensu_client.py +++ b/plugins/modules/sensu_client.py @@ -252,8 +252,8 @@ def main(): # Load the current config, if there is one, so we can compare current_config = None try: - current_config = json.load(open(path, "r")) - except (IOError, ValueError): + current_config = json.load(open(path)) + except (OSError, ValueError): # File either doesn't exist or it is invalid JSON pass @@ -277,7 +277,7 @@ def main(): with open(path, "w") as client: client.write(json.dumps(config, indent=4)) module.exit_json(msg="Client configuration updated", changed=True, config=config["client"], file=path) - except (OSError, IOError) as e: + except OSError as e: module.fail_json(msg=f"Unable to write file {path}: {e}") diff --git a/plugins/modules/sensu_handler.py b/plugins/modules/sensu_handler.py index 6a2b044116..8f7a702747 100644 --- a/plugins/modules/sensu_handler.py +++ b/plugins/modules/sensu_handler.py @@ -252,8 +252,8 @@ def main(): # Load the current config, if there is one, so we can compare current_config = None try: - current_config = json.load(open(path, "r")) - except (IOError, ValueError): + current_config = json.load(open(path)) + except (OSError, ValueError): # File either doesn't exist or it is invalid JSON pass @@ -285,7 +285,7 @@ def main(): module.exit_json( msg="Handler configuration updated", changed=True, config=config["handlers"][name], file=path, name=name ) - except (OSError, IOError) as e: + except OSError as e: module.fail_json(msg=f"Unable to write file {path}: {e}") diff --git a/plugins/modules/sensu_subscription.py b/plugins/modules/sensu_subscription.py index 162f7daf6e..204f1cded2 100644 --- a/plugins/modules/sensu_subscription.py +++ b/plugins/modules/sensu_subscription.py @@ -80,7 +80,7 @@ def sensu_subscription(module, path, name, state="present", backup=False): try: config = json.load(open(path)) - except IOError as e: + except OSError as e: if e.errno == 2: # File not found, non-fatal if state == "absent": reasons.append("file did not exist and state is 'absent'") @@ -126,7 +126,7 @@ def sensu_subscription(module, path, name, state="present", backup=False): module.backup_local(path) try: open(path, "w").write(json.dumps(config, indent=2) + "\n") - except IOError as e: + except OSError as e: module.fail_json(msg=f"Failed to write to file {path}: {e}", exception=traceback.format_exc()) return changed, reasons diff --git a/plugins/modules/seport.py b/plugins/modules/seport.py index 28c4ffae1d..d4daf7a6ce 100644 --- a/plugins/modules/seport.py +++ b/plugins/modules/seport.py @@ -229,7 +229,7 @@ def semanage_port_add(module, ports, proto, setype, do_reload, serange="s0", ses else: seport.modify(port, proto, serange, setype) - except (ValueError, IOError, KeyError, OSError, RuntimeError) as e: + except (ValueError, OSError, KeyError, RuntimeError) as e: module.fail_json(msg=f"{e.__class__.__name__}: {e}\n", exception=traceback.format_exc()) return change @@ -270,7 +270,7 @@ def semanage_port_del(module, ports, proto, setype, do_reload, sestore="", local if not module.check_mode: seport.delete(port, proto) - except (ValueError, IOError, KeyError, OSError, RuntimeError) as e: + except (ValueError, OSError, KeyError, RuntimeError) as e: module.fail_json(msg=f"{e.__class__.__name__}: {e}\n", exception=traceback.format_exc()) return change diff --git a/plugins/modules/serverless.py b/plugins/modules/serverless.py index 92766d29f5..ebf67460cf 100644 --- a/plugins/modules/serverless.py +++ b/plugins/modules/serverless.py @@ -138,7 +138,7 @@ def read_serverless_config(module): with open(full_path) as sls_config: config = yaml.safe_load(sls_config.read()) return config - except IOError as e: + except OSError as e: module.fail_json(msg=f"Could not open serverless.yml in {full_path}. err: {e}") diff --git a/plugins/modules/solaris_zone.py b/plugins/modules/solaris_zone.py index 09ccc6e508..43e543bb0c 100644 --- a/plugins/modules/solaris_zone.py +++ b/plugins/modules/solaris_zone.py @@ -282,7 +282,7 @@ class Zone: def configure_password(self): shadow = f"{self.path}/root/etc/shadow" if self.root_password: - with open(shadow, "r") as f: + with open(shadow) as f: lines = f.readlines() for i in range(0, len(lines)): diff --git a/plugins/modules/sorcery.py b/plugins/modules/sorcery.py index c7d9781fdd..4c4ee3df6e 100644 --- a/plugins/modules/sorcery.py +++ b/plugins/modules/sorcery.py @@ -369,7 +369,7 @@ def match_depends(module): try: shutil.copy2(sorcery_depends_orig, sorcery_depends) - except IOError: + except OSError: module.fail_json(msg="failed to copy depends.check file") else: sorcery_depends = os.path.join(SORCERY_STATE_DIR, "depends") @@ -441,7 +441,7 @@ def match_depends(module): sys.stdout.write(line) else: sys.stdout.write(line) - except IOError: + except OSError: module.fail_json(msg="I/O error on the depends file") finally: fi.close() @@ -453,7 +453,7 @@ def match_depends(module): with open(sorcery_depends, "a") as fl: for k in depends_new: fl.write(f"{spell}:{k}:{depends[k]}:optional::\n") - except IOError: + except OSError: module.fail_json(msg="I/O error on the depends file") depends_ok = False @@ -461,7 +461,7 @@ def match_depends(module): if module.check_mode: try: os.remove(sorcery_depends) - except IOError: + except OSError: module.fail_json(msg="failed to clean up depends.backup file") return depends_ok @@ -545,7 +545,7 @@ def manage_spells(module): # back up original queue try: os.rename(sorcery_queue, f"{sorcery_queue}.backup") - except IOError: + except OSError: module.fail_json(msg="failed to backup the update queue") # see update_codex() @@ -567,7 +567,7 @@ def manage_spells(module): if module.check_mode: try: os.rename(f"{sorcery_queue}.backup", sorcery_queue) - except IOError: + except OSError: module.fail_json(msg="failed to restore the update queue") return (True, "would have updated the system") diff --git a/plugins/modules/spotinst_aws_elastigroup.py b/plugins/modules/spotinst_aws_elastigroup.py index 9931af570a..4c58a86419 100644 --- a/plugins/modules/spotinst_aws_elastigroup.py +++ b/plugins/modules/spotinst_aws_elastigroup.py @@ -1434,13 +1434,13 @@ def main(): credentials_path = module.params.get("credentials_path") try: - with open(credentials_path, "r") as creds: + with open(credentials_path) as creds: for line in creds: eq_index = line.find("=") var_name = line[:eq_index].strip() string_value = line[eq_index + 1 :].strip() creds_file_loaded_vars[var_name] = string_value - except IOError: + except OSError: pass # End of creds file retrieval diff --git a/plugins/modules/sudoers.py b/plugins/modules/sudoers.py index 60394c6bfd..32d43327e7 100644 --- a/plugins/modules/sudoers.py +++ b/plugins/modules/sudoers.py @@ -201,7 +201,7 @@ class Sudoers: return os.path.exists(self.file) def matches(self): - with open(self.file, "r") as f: + with open(self.file) as f: content_matches = f.read() == self.content() current_mode = os.stat(self.file).st_mode & 0o777 diff --git a/plugins/modules/svc.py b/plugins/modules/svc.py index 2cff67e1d4..6005620451 100644 --- a/plugins/modules/svc.py +++ b/plugins/modules/svc.py @@ -273,7 +273,7 @@ def main(): svc.enable() else: svc.disable() - except (OSError, IOError) as e: + except OSError as e: module.fail_json(msg=f"Could not change service link: {e}") if state is not None and state != svc.state: @@ -290,7 +290,7 @@ def main(): open(d_file, "a").close() else: os.unlink(d_file) - except (OSError, IOError) as e: + except OSError as e: module.fail_json(msg=f"Could not change downed file: {e} ") module.exit_json(changed=changed, svc=svc.report()) diff --git a/plugins/modules/timezone.py b/plugins/modules/timezone.py index 6f09082c80..23e0b7e9a9 100644 --- a/plugins/modules/timezone.py +++ b/plugins/modules/timezone.py @@ -376,9 +376,9 @@ class NosystemdTimezone(Timezone): self.conf_files["name"] = "/etc/sysconfig/clock" self.conf_files["hwclock"] = "/etc/sysconfig/clock" try: - with open(self.conf_files["name"], "r") as f: + with open(self.conf_files["name"]) as f: sysconfig_clock = f.read() - except IOError as err: + except OSError as err: if self._allow_ioerror(err, "name"): # If the config file doesn't exist detect the distribution and set regexps. if distribution == "SuSE": @@ -427,9 +427,9 @@ class NosystemdTimezone(Timezone): """ # Read the file try: - with open(filename, "r") as file: + with open(filename) as file: lines = file.readlines() - except IOError as err: + except OSError as err: if self._allow_ioerror(err, key): lines = [] else: @@ -452,16 +452,16 @@ class NosystemdTimezone(Timezone): try: with open(filename, "w") as file: file.writelines(lines) - except IOError: + except OSError: self.abort(f'tried to configure {key} using a file "{filename}", but could not write to it') self.msg.append(f"Added 1 line and deleted {len(matched_indices)} line(s) on {filename}") def _get_value_from_config(self, key, phase): filename = self.conf_files[key] try: - with open(filename, mode="r") as file: + with open(filename) as file: status = file.read() - except IOError as err: + except OSError as err: if self._allow_ioerror(err, key): if key == "hwclock": return "n/a" @@ -602,7 +602,7 @@ class SmartOSTimezone(Timezone): """ if key == "name": try: - with open("/etc/default/init", "r") as f: + with open("/etc/default/init") as f: for line in f: m = re.match("^TZ=(.*)$", line.strip()) if m: @@ -783,7 +783,7 @@ class AIXTimezone(Timezone): def __get_timezone(self): """Return the current value of TZ= in /etc/environment""" try: - with open("/etc/environment", "r") as f: + with open("/etc/environment") as f: etcenvironment = f.read() except Exception: self.module.fail_json(msg="Issue reading contents of /etc/environment") diff --git a/plugins/modules/wakeonlan.py b/plugins/modules/wakeonlan.py index f8558d4b1f..34cf685f52 100644 --- a/plugins/modules/wakeonlan.py +++ b/plugins/modules/wakeonlan.py @@ -104,7 +104,7 @@ def wakeonlan(module, mac, broadcast, port): if not module.check_mode: try: sock.sendto(data, (broadcast, port)) - except socket.error as e: + except OSError as e: sock.close() module.fail_json(msg=f"{e}", exception=traceback.format_exc()) diff --git a/plugins/modules/xfs_quota.py b/plugins/modules/xfs_quota.py index 7d6afc22f2..322754d329 100644 --- a/plugins/modules/xfs_quota.py +++ b/plugins/modules/xfs_quota.py @@ -438,7 +438,7 @@ def exec_quota(module, xfs_quota_bin, cmd, mountpoint): def get_fs_by_mountpoint(mountpoint): mpr = None - with open("/proc/mounts", "r") as s: + with open("/proc/mounts") as s: for line in s.readlines(): mp = line.strip().split() if len(mp) == 6 and mp[1] == mountpoint and mp[2] == "xfs": @@ -450,7 +450,7 @@ def get_fs_by_mountpoint(mountpoint): def get_project_id(name): prjid = None - with open("/etc/projid", "r") as s: + with open("/etc/projid") as s: for line in s.readlines(): line = line.strip().partition(":") if line[0] == name: diff --git a/plugins/modules/zypper.py b/plugins/modules/zypper.py index 530ac8fa39..f20cec172d 100644 --- a/plugins/modules/zypper.py +++ b/plugins/modules/zypper.py @@ -577,7 +577,7 @@ def repo_refresh(m): def get_fs_type_and_readonly_state(mount_point): - with open("/proc/mounts", "r") as file: + with open("/proc/mounts") as file: for line in file.readlines(): fields = line.split() path = fields[1] diff --git a/plugins/modules/zypper_repository.py b/plugins/modules/zypper_repository.py index 832b5e4e66..512dc169c7 100644 --- a/plugins/modules/zypper_repository.py +++ b/plugins/modules/zypper_repository.py @@ -403,7 +403,7 @@ def main(): try: with open(repo, encoding="utf-8") as file: repofile_text = file.read() - except IOError: + except OSError: module.fail_json(msg="Error opening .repo file from provided path") repofile = configparser.ConfigParser()