Skip to content

Tidy up Python file handlers #735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions utils/apply-fixit-edits.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def apply_edits(path):

edits_set = set()
for remap_file in remap_files:
json_data = open(remap_file).read()
with open(remap_file) as f:
json_data = f.read()
json_data = json_data.replace(",\n }", "\n }")
json_data = json_data.replace(",\n]", "\n]")
curr_edits = json.loads(json_data)
Expand All @@ -55,13 +56,15 @@ def apply_edits(path):
for fname, edits in edits_per_file.iteritems():
print('Updating', fname)
edits.sort(reverse=True)
file_data = open(fname).read()
with open(fname) as f:
file_data = f.read()
for ed in edits:
offset = ed[0]
length = ed[1]
text = ed[2]
file_data = file_data[:offset] + str(text) + file_data[offset+length:]
open(fname, 'w').write(file_data)
with open(fname, 'w') as f:
f.write(file_data)
return 0

def main():
Expand Down
8 changes: 6 additions & 2 deletions utils/gyb.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,13 @@ class ParseContext:
tokens = None # The rest of the tokens
closeLines = False

def __init__(self, filename, template = None):
def __init__(self, filename, template=None):
self.filename = os.path.abspath(filename)
self.template = template or open(filename).read()
if template is None:
with open(filename) as f:
self.template = f.read()
else:
self.template = template
self.lineStarts = getLineStarts(self.template)
self.tokens = self.tokenGenerator(tokenizeTemplate(self.template))
self.nextToken()
Expand Down
8 changes: 5 additions & 3 deletions utils/pre-commit-benchmark
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ def collectBenchmarks(exeNames, treeish = None, repeat = 3, build_script_args =

rebuilt = True
else:
timingsText = open(timingsFile).read()
with open(timingsFile) as f:
timingsText = f.read()
oldRepeat = timingsText.count('\nTotal')
if oldRepeat < repeat:
print('Only %s repeats in existing %s timings file' % (oldRepeat, exe))
Expand All @@ -146,8 +147,9 @@ def collectBenchmarks(exeNames, treeish = None, repeat = 3, build_script_args =
output = check_output(os.path.join(binDir, exe))
print(output)
timingsText += output

open(timingsFile, 'w').write(timingsText)

with open(timingsFile, 'w') as outfile:
outfile.write(timingsText)
print('done.')

return cacheDir
Expand Down
3 changes: 2 additions & 1 deletion utils/protocol_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ def bodyLines(bodyText):
comments = r'//.* | /[*] (.|\n)*? [*]/' # FIXME: doesn't respect strings or comment nesting)

# read source, stripping all comments
sourceSansComments = re.sub(comments, '', open(args[1]).read(), flags=reFlags)
with open(args[1]) as src:
sourceSansComments = re.sub(comments, '', src.read(), flags=reFlags)

genericParameterConstraint = interpolate(r' (%(identifier)s) \s* : \s* (%(identifier)s) ')

Expand Down
5 changes: 2 additions & 3 deletions utils/sil-opt-verify-all-modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ def run_commands_in_parallel(commands):
makefile += "all: " + " ".join(targets) + "\n"

temp_dir = tempfile.mkdtemp(prefix="swift-testsuite-main")
makefile_file = open(os.path.join(temp_dir, 'Makefile'), 'w')
makefile_file.write(makefile)
makefile_file.close()
with open(os.path.join(temp_dir, 'Makefile'), 'w') as makefile_file:
makefile_file.write(makefile)

max_processes = multiprocessing.cpu_count()
subprocess.check_call([
Expand Down
15 changes: 6 additions & 9 deletions utils/swift-bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,8 @@ def processSource(self, name):
"""

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is list(f) preferable to f.readlines()?

output = header
lookingForCurlyBrace = False
testNames = []
Expand Down Expand Up @@ -190,9 +189,8 @@ def processSource(self, name):
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

Expand All @@ -210,9 +208,8 @@ def compileOpaqueCFile(self):
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'])

Expand Down
40 changes: 19 additions & 21 deletions utils/viewcfg
Original file line number Diff line number Diff line change
Expand Up @@ -138,27 +138,25 @@ def main():
# Write the output dot file.

fileName = tempfile.gettempdir() + "/viewcfg" + suffix + ".dot"
outFile = open(fileName, "w")

outFile.write('digraph "CFG" {\n')
for name, block in blocks.iteritems():
if block.content is not None:
outFile.write("\tNode" + str(block.index) +
" [shape=record,label=\"{" + block.content + "}\"];\n")
else:
outFile.write("\tNode" + str(block.index) +
" [shape=record,color=gray,fontcolor=gray,label=\"{" +
block.name + "}\"];\n")

for succName in block.getSuccs():
succBlock = blocks[succName]
outFile.write("\tNode" + str(block.index) + " -> Node" +
str(succBlock.index) + ";\n")

outFile.write("}\n")
outFile.flush()
os.fsync(outFile.fileno())
outFile.close
with open(fileName 'w') as outFile:
outFile = open(fileName, "w")

outFile.write('digraph "CFG" {\n')
for name, block in blocks.iteritems():
if block.content is not None:
outFile.write("\tNode" + str(block.index) +
" [shape=record,label=\"{" + block.content + "}\"];\n")
else:
outFile.write("\tNode" + str(block.index) +
" [shape=record,color=gray,fontcolor=gray,label=\"{" +
block.name + "}\"];\n")

for succName in block.getSuccs():
succBlock = blocks[succName]
outFile.write("\tNode" + str(block.index) + " -> Node" +
str(succBlock.index) + ";\n")

outFile.write("}\n")

# Open the dot file (should be done with Graphviz).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how this ever worked.


Expand Down
10 changes: 2 additions & 8 deletions validation-test/stdlib/Slice/Inputs/GenerateSliceTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
]:
Base = '%s%s%sCollection' % (base_kind, traversal, 'Mutable' if mutable else '')
testFilename = WrapperType + '_Of_' + Base + '_' + name + '.swift'
testFile = open(testFilename + '.gyb', 'w')
testFile.write("""
with open(testFilename + '.gyb', 'w') as testFile:
testFile.write("""
//// Automatically Generated From validation-test/stdlib/Inputs/GenerateSliceTests.py
//////// Do Not Edit Directly!
// -*- swift -*-
Expand Down Expand Up @@ -69,9 +69,3 @@
prefix=prefix,
suffix=suffix
))
testFile.close()