use open() strictly as context manager

This commit is contained in:
The MMGen Project 2022-01-06 20:24:21 +00:00
commit 0880229e94
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
14 changed files with 55 additions and 26 deletions

View file

@ -47,7 +47,8 @@ def exec_wrapper_write_traceback():
c = exec_wrapper_get_colors()
sys.stdout.write('{}{}'.format(c.yellow(''.join(lines)),c.red(exc)))
open('my.err','w').write(''.join(lines+[exc]))
with open('my.err','w') as fp:
fp.write(''.join(lines+[exc]))
def exec_wrapper_end_msg():
if os.getenv('EXEC_WRAPPER_SPAWN') and not os.getenv('MMGEN_TEST_SUITE_DETERMINISTIC'):
@ -61,7 +62,9 @@ exec_wrapper_tstart = time.time()
try:
sys.argv.pop(0)
exec_wrapper_execed_file = sys.argv[0]
exec(open(sys.argv[0]).read())
with open(sys.argv[0]) as fp:
text = fp.read()
exec(text)
except SystemExit as e:
if e.code != 0 and not os.getenv('EXEC_WRAPPER_NO_TRACEBACK'):
exec_wrapper_write_traceback()

View file

@ -24,7 +24,8 @@ translate = {
}
def cleanup_file(fn):
data = open(fn).read()
with open(fn) as fp:
data = fp.read()
def gen_text():
for line in data.splitlines():
line = re.sub('\r\n','\n',line) # DOS CRLF to Unix LF
@ -32,8 +33,10 @@ def cleanup_file(fn):
line = re.sub(r'\s+$','',line) # trailing whitespace
yield line
ret = list(gen_text())
open(fn+'.orig','w').write(data)
open(fn,'w').write('\n'.join(ret))
with open(fn+'.orig','w') as fp:
fp.write(data)
with open(fn,'w') as fp:
fp.write('\n'.join(ret))
return ret
cleaned_texts = [cleanup_file(fn) for fn in fns]