mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
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:
@@ -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'])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user