Auto merge of #7305 - frewsxcv:python-context-managers, r=Ms2ger

Utilize Python context managers for opening/closing files

In some of these cases, files were not being closed

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7305)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2015-08-21 09:30:06 -06:00
commit 3a48e04caf
5 changed files with 18 additions and 21 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -73,7 +73,8 @@ class CommandBase(object):
config_path = path.join(context.topdir, ".servobuild") config_path = path.join(context.topdir, ".servobuild")
if path.exists(config_path): if path.exists(config_path):
self.config = toml.loads(open(config_path).read()) with open(config_path) as f:
self.config = toml.loads(f.read())
else: else:
self.config = {} self.config = {}
@ -121,14 +122,16 @@ class CommandBase(object):
def rust_snapshot_path(self): def rust_snapshot_path(self):
if self._rust_snapshot_path is None: if self._rust_snapshot_path is None:
filename = path.join(self.context.topdir, "rust-snapshot-hash") filename = path.join(self.context.topdir, "rust-snapshot-hash")
snapshot_hash = open(filename).read().strip() with open(filename) as f:
snapshot_hash = f.read().strip()
self._rust_snapshot_path = "%s-%s" % (snapshot_hash, host_triple()) self._rust_snapshot_path = "%s-%s" % (snapshot_hash, host_triple())
return self._rust_snapshot_path return self._rust_snapshot_path
def cargo_build_id(self): def cargo_build_id(self):
if self._cargo_build_id is None: if self._cargo_build_id is None:
filename = path.join(self.context.topdir, "cargo-nightly-build") filename = path.join(self.context.topdir, "cargo-nightly-build")
self._cargo_build_id = open(filename).read().strip() with open(filename) as f:
self._cargo_build_id = f.read().strip()
return self._cargo_build_id return self._cargo_build_id
def get_target_dir(self): def get_target_dir(self):