Handle more file closing with "with"

This commit is contained in:
Ville Skyttä 2016-07-07 18:53:34 +03:00
parent 593708a39c
commit e8a8be5788
6 changed files with 17 additions and 33 deletions

View File

@ -138,11 +138,8 @@ else:
domain + '.js')) domain + '.js'))
for js_file, (locale, po_file) in zip(js_files, po_files): for js_file, (locale, po_file) in zip(js_files, po_files):
infile = open(po_file, 'r') with open(po_file, 'r') as infile:
try:
catalog = read_po(infile, locale) catalog = read_po(infile, locale)
finally:
infile.close()
if catalog.fuzzy and not self.use_fuzzy: if catalog.fuzzy and not self.use_fuzzy:
continue continue
@ -159,8 +156,7 @@ else:
msgid = msgid[0] msgid = msgid[0]
jscatalog[msgid] = message.string jscatalog[msgid] = message.string
outfile = open(js_file, 'wb') with open(js_file, 'wb') as outfile:
try:
outfile.write('Documentation.addTranslations(') outfile.write('Documentation.addTranslations(')
dump(dict( dump(dict(
messages=jscatalog, messages=jscatalog,
@ -168,8 +164,6 @@ else:
locale=str(catalog.locale) locale=str(catalog.locale)
), outfile, sort_keys=True) ), outfile, sort_keys=True)
outfile.write(');') outfile.write(');')
finally:
outfile.close()
cmdclass['compile_catalog'] = compile_catalog_plusjs cmdclass['compile_catalog'] = compile_catalog_plusjs

View File

@ -590,9 +590,8 @@ class StandaloneHTMLBuilder(Builder):
self.info(bold('copying static files... '), nonl=True) self.info(bold('copying static files... '), nonl=True)
ensuredir(path.join(self.outdir, '_static')) ensuredir(path.join(self.outdir, '_static'))
# first, create pygments style file # first, create pygments style file
f = open(path.join(self.outdir, '_static', 'pygments.css'), 'w') with open(path.join(self.outdir, '_static', 'pygments.css'), 'w') as f:
f.write(self.highlighter.get_stylesheet()) f.write(self.highlighter.get_stylesheet())
f.close()
# then, copy translations JavaScript file # then, copy translations JavaScript file
if self.config.language is not None: if self.config.language is not None:
jsfile = self._get_translations_js() jsfile = self._get_translations_js()

View File

@ -133,9 +133,8 @@ class Theme(object):
dirname = path.dirname(name) dirname = path.dirname(name)
if not path.isdir(path.join(self.themedir, dirname)): if not path.isdir(path.join(self.themedir, dirname)):
os.makedirs(path.join(self.themedir, dirname)) os.makedirs(path.join(self.themedir, dirname))
fp = open(path.join(self.themedir, name), 'wb') with open(path.join(self.themedir, name), 'wb') as fp:
fp.write(tinfo.read(name)) fp.write(tinfo.read(name))
fp.close()
self.themeconf = configparser.RawConfigParser() self.themeconf = configparser.RawConfigParser()
self.themeconf.read(path.join(self.themedir, THEMECONF)) self.themeconf.read(path.join(self.themedir, THEMECONF))

View File

@ -472,10 +472,9 @@ class coverage:
def save(self): def save(self):
if self.usecache and self.cache: if self.usecache and self.cache:
self.canonicalize_filenames() self.canonicalize_filenames()
cache = open(self.cache, 'wb')
import marshal import marshal
with open(self.cache, 'wb') as cache:
marshal.dump(self.cexecuted, cache) marshal.dump(self.cexecuted, cache)
cache.close()
# restore(). Restore coverage data from the coverage cache (if it exists). # restore(). Restore coverage data from the coverage cache (if it exists).
@ -488,10 +487,9 @@ class coverage:
def restore_file(self, file_name): def restore_file(self, file_name):
try: try:
cache = open(file_name, 'rb')
import marshal import marshal
with open(file_name, 'rb') as cache:
cexecuted = marshal.load(cache) cexecuted = marshal.load(cache)
cache.close()
if isinstance(cexecuted, dict): if isinstance(cexecuted, dict):
return cexecuted return cexecuted
else: else:
@ -614,7 +612,7 @@ class coverage:
) )
filename = filename[:-1] filename = filename[:-1]
if not source: if not source:
sourcef = open(filename, 'rU') with open(filename, 'rU') as sourcef:
source = sourcef.read() source = sourcef.read()
try: try:
lines, excluded_lines, line_map = self.find_executable_statements( lines, excluded_lines, line_map = self.find_executable_statements(
@ -625,8 +623,6 @@ class coverage:
"Couldn't parse '%s' as Python source: '%s' at line %d" % "Couldn't parse '%s' as Python source: '%s' at line %d" %
(filename, synerr.msg, synerr.lineno) (filename, synerr.msg, synerr.lineno)
) )
if sourcef:
sourcef.close()
result = filename, lines, excluded_lines, line_map result = filename, lines, excluded_lines, line_map
self.analysis_cache[morf] = result self.analysis_cache[morf] = result
return result return result

View File

@ -223,11 +223,8 @@ def main(argv):
print("Checking %s..." % fn) print("Checking %s..." % fn)
try: try:
f = open(fn, 'rb') with open(fn, 'rb') as f:
try:
lines = list(f) lines = list(f)
finally:
f.close()
except (IOError, OSError) as err: except (IOError, OSError) as err:
print("%s: cannot open: %s" % (fn, err)) print("%s: cannot open: %s" % (fn, err))
num += 1 num += 1

View File

@ -127,8 +127,8 @@ def check(file):
errprint("%s: I/O Error: %s" % (file, str(msg))) errprint("%s: I/O Error: %s" % (file, str(msg)))
return return
with f:
r = Reindenter(f) r = Reindenter(f)
f.close()
if r.run(): if r.run():
if verbose: if verbose:
print("changed.") print("changed.")
@ -140,9 +140,8 @@ def check(file):
shutil.copyfile(file, bak) shutil.copyfile(file, bak)
if verbose: if verbose:
print("backed up", file, "to", bak) print("backed up", file, "to", bak)
f = open(file, "w") with open(file, "w") as f:
r.write(f) r.write(f)
f.close()
if verbose: if verbose:
print("wrote new", file) print("wrote new", file)
return True return True