Close all open filehandle (#50544)

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2019-01-11 20:44:08 +05:30 committed by Brian Coca
commit db8702cdb8
21 changed files with 81 additions and 47 deletions

View file

@ -80,15 +80,16 @@ def load_privatekey(path, passphrase=None):
"""Load the specified OpenSSL private key."""
try:
if passphrase:
privatekey = crypto.load_privatekey(crypto.FILETYPE_PEM,
open(path, 'rb').read(),
to_bytes(passphrase))
else:
privatekey = crypto.load_privatekey(crypto.FILETYPE_PEM,
open(path, 'rb').read())
with open(path, 'rb') as b_priv_key_fh:
priv_key_detail = b_priv_key_fh.read()
return privatekey
if passphrase:
return crypto.load_privatekey(crypto.FILETYPE_PEM,
priv_key_detail,
to_bytes(passphrase))
else:
return crypto.load_privatekey(crypto.FILETYPE_PEM,
priv_key_detail)
except (IOError, OSError) as exc:
raise OpenSSLObjectError(exc)
@ -97,9 +98,9 @@ def load_certificate(path):
"""Load the specified certificate."""
try:
cert_content = open(path, 'rb').read()
cert = crypto.load_certificate(crypto.FILETYPE_PEM, cert_content)
return cert
with open(path, 'rb') as cert_fh:
cert_content = cert_fh.read()
return crypto.load_certificate(crypto.FILETYPE_PEM, cert_content)
except (IOError, OSError) as exc:
raise OpenSSLObjectError(exc)
@ -108,9 +109,9 @@ def load_certificate_request(path):
"""Load the specified certificate signing request."""
try:
csr_content = open(path, 'rb').read()
csr = crypto.load_certificate_request(crypto.FILETYPE_PEM, csr_content)
return csr
with open(path, 'rb') as csr_fh:
csr_content = csr_fh.read()
return crypto.load_certificate_request(crypto.FILETYPE_PEM, csr_content)
except (IOError, OSError) as exc:
raise OpenSSLObjectError(exc)

View file

@ -195,7 +195,9 @@ class LinuxVirtual(Virtual):
# Check whether this is a RHEV hypervisor (is vdsm running ?)
for f in glob.glob('/proc/[0-9]*/comm'):
try:
if open(f).read().rstrip() == 'vdsm':
with open(f) as virt_fh:
comm_content = virt_fh.read().rstrip()
if comm_content == 'vdsm':
virtual_facts['virtualization_type'] = 'RHEV'
break
except Exception:

View file

@ -231,7 +231,9 @@ class ACIModule(object):
self.params['certificate_name'] = os.path.basename(os.path.splitext(self.params['private_key'])[0])
try:
sig_key = load_privatekey(FILETYPE_PEM, open(self.params['private_key'], 'r').read())
with open(self.params['private_key'], 'r') as priv_key_fh:
private_key_content = priv_key_fh.read()
sig_key = load_privatekey(FILETYPE_PEM, private_key_content)
except Exception:
self.module.fail_json(msg='Cannot load private key %s' % self.params['private_key'])

View file

@ -203,7 +203,8 @@ class NetworkConfig(object):
self._items = self.parse(s)
def loadfp(self, fp):
return self.load(open(fp).read())
with open(fp) as f:
return self.load(f.read())
def parse(self, lines, comment_tokens=None):
toplevel = re.compile(r'\S')

View file

@ -88,12 +88,11 @@ class RabbitClient():
''' Consider some file size limits here '''
def _read_file(self, path):
try:
fh = open(path, "rb").read()
with open(path, "rb") as file_handle:
return file_handle.read()
except IOError as e:
self.module.fail_json(msg="Unable to open file %s: %s" % (path, to_native(e)))
return fh
@staticmethod
def _check_file_mime_type(path):
mime = MimeTypes()