bpo-22831: Use "with" to avoid possible fd leaks in tools (part 2). (GH-10927)

This commit is contained in:
Serhiy Storchaka 2019-03-30 08:33:02 +02:00 committed by GitHub
parent afbb7a371f
commit 172bb39452
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 249 additions and 259 deletions

View file

@ -29,9 +29,8 @@ def fetch_server_certificate (host, port):
return None
else:
tn = tempfile.mktemp()
fp = open(tn, "wb")
fp.write(m.group(1) + b"\n")
fp.close()
with open(tn, "wb") as fp:
fp.write(m.group(1) + b"\n")
try:
tn2 = (outfile or tempfile.mktemp())
status, output = subproc(r'openssl x509 -in "%s" -out "%s"' %
@ -39,9 +38,8 @@ def fetch_server_certificate (host, port):
if status != 0:
raise RuntimeError('OpenSSL x509 failed with status %s and '
'output: %r' % (status, output))
fp = open(tn2, 'rb')
data = fp.read()
fp.close()
with open(tn2, 'rb') as fp:
data = fp.read()
os.unlink(tn2)
return data
finally:
@ -49,9 +47,8 @@ def fetch_server_certificate (host, port):
if sys.platform.startswith("win"):
tfile = tempfile.mktemp()
fp = open(tfile, "w")
fp.write("quit\n")
fp.close()
with open(tfile, "w") as fp:
fp.write("quit\n")
try:
status, output = subproc(
'openssl s_client -connect "%s:%s" -showcerts < "%s"' %