Utilize Python context managers for opening/closing files

In some of these cases, files were not being closed
This commit is contained in:
Corey Farwell 2015-08-21 11:15:17 -04:00
parent 7c45ff8e05
commit 2ab43bea5d
5 changed files with 18 additions and 21 deletions

View file

@ -39,9 +39,8 @@ def main():
webIDLFile = os.path.normpath(args[2])
# Load the parsing results
f = open('ParserResults.pkl', 'rb')
parserData = cPickle.load(f)
f.close()
with open('ParserResults.pkl', 'rb') as f:
parserData = cPickle.load(f)
# Create the configuration data.
config = Configuration(configFile, parserData)

View file

@ -41,18 +41,16 @@ def replaceFileIfChanged(filename, newContents):
# oldFileContents = ""
# try:
# oldFile = open(filename, 'rb')
# oldFileContents = ''.join(oldFile.readlines())
# oldFile.close()
# with open(filename, 'rb') as oldFile:
# oldFileContents = ''.join(oldFile.readlines())
# except:
# pass
# if newContents == oldFileContents:
# return False
f = open(filename, 'wb')
f.write(newContents)
f.close()
with open(filename, 'wb') as f:
f.write(newContents)
return True

View file

@ -18,9 +18,8 @@ for [prop, pref] in propList:
props += " [%s] attribute DOMString %s;\n" % (", ".join(extendedAttrs),
prop)
idlFile = open(sys.argv[1], "r")
idlTemplate = idlFile.read()
idlFile.close()
with open(sys.argv[1], "r") as idlFile:
idlTemplate = idlFile.read()
print ("/* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT */\n\n" +
string.Template(idlTemplate).substitute({"props": props}))

View file

@ -47,16 +47,14 @@ def main():
parser = WebIDL.Parser(options.cachedir)
for filename in fileList:
fullPath = os.path.normpath(os.path.join(baseDir, filename))
f = open(fullPath, 'rb')
lines = f.readlines()
f.close()
with open(fullPath, 'rb') as f:
lines = f.readlines()
parser.parse(''.join(lines), fullPath)
parserResults = parser.finish()
# Write the parser results out to a pickle.
resultsFile = open('ParserResults.pkl', 'wb')
cPickle.dump(parserResults, resultsFile, -1)
resultsFile.close()
with open('ParserResults.pkl', 'wb') as resultsFile:
cPickle.dump(parserResults, resultsFile, -1)
# Load the configuration.
config = Configuration(configFile, parserResults)