diff --git a/changelogs/fragments/65372-misc-context-manager.yml b/changelogs/fragments/65372-misc-context-manager.yml new file mode 100644 index 00000000000..94fca3baa67 --- /dev/null +++ b/changelogs/fragments/65372-misc-context-manager.yml @@ -0,0 +1,5 @@ +bugfixes: + - spacewalk inventory - improve file handling by using a context manager. + - intersight_rest_api, intersight_info - improve file handling by using a context manager. + - one_vm - improve file handling by using a context manager. + - postgresql_query - improve file handling by using a context manager. diff --git a/contrib/inventory/spacewalk.py b/contrib/inventory/spacewalk.py index e94298c308d..dc96b1fe3bc 100755 --- a/contrib/inventory/spacewalk.py +++ b/contrib/inventory/spacewalk.py @@ -89,7 +89,8 @@ def spacewalk_report(name): p.wait() fh.close() - lines = open(cache_filename, 'r').readlines() + with open(cache_filename, 'r') as f: + lines = f.readlines() keys = lines[0].strip().split(',') # add 'spacewalk_' prefix to the keys keys = ['spacewalk_' + key for key in keys] diff --git a/lib/ansible/module_utils/remote_management/intersight.py b/lib/ansible/module_utils/remote_management/intersight.py index fa50f815a43..714a8e70cf1 100644 --- a/lib/ansible/module_utils/remote_management/intersight.py +++ b/lib/ansible/module_utils/remote_management/intersight.py @@ -111,7 +111,8 @@ class IntersightModule(): self.module.fail_json(msg='cryptography is required for this module') self.host = self.module.params['api_uri'] self.public_key = self.module.params['api_key_id'] - self.private_key = open(self.module.params['api_private_key'], 'r').read() + with open(self.module.params['api_private_key'], 'r') as f: + self.private_key = f.read() self.digest_algorithm = 'rsa-sha256' self.response_list = [] diff --git a/lib/ansible/modules/cloud/opennebula/one_vm.py b/lib/ansible/modules/cloud/opennebula/one_vm.py index 41532467df5..19155f9b976 100644 --- a/lib/ansible/modules/cloud/opennebula/one_vm.py +++ b/lib/ansible/modules/cloud/opennebula/one_vm.py @@ -1303,7 +1303,8 @@ def get_connection_info(module): if authfile is None: authfile = os.path.join(os.environ.get("HOME"), ".one", "one_auth") try: - authstring = open(authfile, "r").read().rstrip() + with open(authfile, "r") as fp: + authstring = fp.read().rstrip() username = authstring.split(":")[0] password = authstring.split(":")[1] except (OSError, IOError): diff --git a/lib/ansible/modules/database/postgresql/postgresql_query.py b/lib/ansible/modules/database/postgresql/postgresql_query.py index d7b1e375d2d..b0ac546df84 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_query.py +++ b/lib/ansible/modules/database/postgresql/postgresql_query.py @@ -270,7 +270,8 @@ def main(): if path_to_script: try: - query = open(path_to_script, 'r').read() + with open(path_to_script, 'r') as f: + query = f.read() except Exception as e: module.fail_json(msg="Cannot read file '%s' : %s" % (path_to_script, to_native(e))) diff --git a/test/legacy/cleanup_ec2.py b/test/legacy/cleanup_ec2.py index 65a8cc22641..826a720b1f8 100644 --- a/test/legacy/cleanup_ec2.py +++ b/test/legacy/cleanup_ec2.py @@ -63,7 +63,8 @@ def delete_aws_eips(get_func, attr, opts): # the file might not be there if the integration test wasn't run try: - eip_log = open(opts.eip_log, 'r').read().splitlines() + with open(opts.eip_log, 'r') as f: + eip_log = f.read().splitlines() except IOError: print('%s not found.' % opts.eip_log) return