Tidy up Python file handlers

Rather than using `f = open(path).read()`, which leaves the file open
for an indeterminate period of time, switch to the `with open(path) as f`
idiom, which ensures the file is always closed correctly.
This commit is contained in:
Alex Chan
2015-12-22 22:33:18 +00:00
parent 37b827c8c5
commit 1f2a39c5f3
8 changed files with 45 additions and 47 deletions

View File

@@ -154,9 +154,8 @@ main()
"""
benchRE = re.compile("^\s*func\s\s*bench_([a-zA-Z0-9_]+)\s*\(\s*\)\s*->\s*Int\s*({)?\s*$")
f = open(name)
lines = f.readlines()
f.close()
with open(name) as f:
lines = list(f)
output = header
lookingForCurlyBrace = False
testNames = []
@@ -190,9 +189,8 @@ main()
output += mainBody % (n, n)
processedName = 'processed_' + os.path.basename(name)
output += mainEnd
f = open(processedName, 'w')
f.write(output)
f.close()
with open(processedName, 'w') as f:
f.write(output)
for n in testNames:
self.tests[name+":"+n].processedSource = processedName
@@ -210,9 +208,8 @@ main()
extern "C" int32_t opaqueGetInt32(int32_t x) { return x; }
extern "C" int64_t opaqueGetInt64(int64_t x) { return x; }
"""
f = open('opaque.cpp', 'w')
f.write(fileBody)
f.close()
with open('opaque.cpp', 'w') as f:
f.write(fileBody)
# TODO: Handle subprocess.CalledProcessError for this call:
self.runCommand(['clang++', 'opaque.cpp', '-o', 'opaque.o', '-c', '-O2'])