mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Handle more file closing with "with"
This commit is contained in:
parent
593708a39c
commit
e8a8be5788
10
setup.py
10
setup.py
@ -138,11 +138,8 @@ else:
|
||||
domain + '.js'))
|
||||
|
||||
for js_file, (locale, po_file) in zip(js_files, po_files):
|
||||
infile = open(po_file, 'r')
|
||||
try:
|
||||
with open(po_file, 'r') as infile:
|
||||
catalog = read_po(infile, locale)
|
||||
finally:
|
||||
infile.close()
|
||||
|
||||
if catalog.fuzzy and not self.use_fuzzy:
|
||||
continue
|
||||
@ -159,8 +156,7 @@ else:
|
||||
msgid = msgid[0]
|
||||
jscatalog[msgid] = message.string
|
||||
|
||||
outfile = open(js_file, 'wb')
|
||||
try:
|
||||
with open(js_file, 'wb') as outfile:
|
||||
outfile.write('Documentation.addTranslations(')
|
||||
dump(dict(
|
||||
messages=jscatalog,
|
||||
@ -168,8 +164,6 @@ else:
|
||||
locale=str(catalog.locale)
|
||||
), outfile, sort_keys=True)
|
||||
outfile.write(');')
|
||||
finally:
|
||||
outfile.close()
|
||||
|
||||
cmdclass['compile_catalog'] = compile_catalog_plusjs
|
||||
|
||||
|
@ -590,9 +590,8 @@ class StandaloneHTMLBuilder(Builder):
|
||||
self.info(bold('copying static files... '), nonl=True)
|
||||
ensuredir(path.join(self.outdir, '_static'))
|
||||
# first, create pygments style file
|
||||
f = open(path.join(self.outdir, '_static', 'pygments.css'), 'w')
|
||||
f.write(self.highlighter.get_stylesheet())
|
||||
f.close()
|
||||
with open(path.join(self.outdir, '_static', 'pygments.css'), 'w') as f:
|
||||
f.write(self.highlighter.get_stylesheet())
|
||||
# then, copy translations JavaScript file
|
||||
if self.config.language is not None:
|
||||
jsfile = self._get_translations_js()
|
||||
|
@ -133,9 +133,8 @@ class Theme(object):
|
||||
dirname = path.dirname(name)
|
||||
if not path.isdir(path.join(self.themedir, dirname)):
|
||||
os.makedirs(path.join(self.themedir, dirname))
|
||||
fp = open(path.join(self.themedir, name), 'wb')
|
||||
fp.write(tinfo.read(name))
|
||||
fp.close()
|
||||
with open(path.join(self.themedir, name), 'wb') as fp:
|
||||
fp.write(tinfo.read(name))
|
||||
|
||||
self.themeconf = configparser.RawConfigParser()
|
||||
self.themeconf.read(path.join(self.themedir, THEMECONF))
|
||||
|
@ -472,10 +472,9 @@ class coverage:
|
||||
def save(self):
|
||||
if self.usecache and self.cache:
|
||||
self.canonicalize_filenames()
|
||||
cache = open(self.cache, 'wb')
|
||||
import marshal
|
||||
marshal.dump(self.cexecuted, cache)
|
||||
cache.close()
|
||||
with open(self.cache, 'wb') as cache:
|
||||
marshal.dump(self.cexecuted, cache)
|
||||
|
||||
# restore(). Restore coverage data from the coverage cache (if it exists).
|
||||
|
||||
@ -488,10 +487,9 @@ class coverage:
|
||||
|
||||
def restore_file(self, file_name):
|
||||
try:
|
||||
cache = open(file_name, 'rb')
|
||||
import marshal
|
||||
cexecuted = marshal.load(cache)
|
||||
cache.close()
|
||||
with open(file_name, 'rb') as cache:
|
||||
cexecuted = marshal.load(cache)
|
||||
if isinstance(cexecuted, dict):
|
||||
return cexecuted
|
||||
else:
|
||||
@ -614,8 +612,8 @@ class coverage:
|
||||
)
|
||||
filename = filename[:-1]
|
||||
if not source:
|
||||
sourcef = open(filename, 'rU')
|
||||
source = sourcef.read()
|
||||
with open(filename, 'rU') as sourcef:
|
||||
source = sourcef.read()
|
||||
try:
|
||||
lines, excluded_lines, line_map = self.find_executable_statements(
|
||||
source, exclude=self.exclude_re
|
||||
@ -625,8 +623,6 @@ class coverage:
|
||||
"Couldn't parse '%s' as Python source: '%s' at line %d" %
|
||||
(filename, synerr.msg, synerr.lineno)
|
||||
)
|
||||
if sourcef:
|
||||
sourcef.close()
|
||||
result = filename, lines, excluded_lines, line_map
|
||||
self.analysis_cache[morf] = result
|
||||
return result
|
||||
|
@ -223,11 +223,8 @@ def main(argv):
|
||||
print("Checking %s..." % fn)
|
||||
|
||||
try:
|
||||
f = open(fn, 'rb')
|
||||
try:
|
||||
with open(fn, 'rb') as f:
|
||||
lines = list(f)
|
||||
finally:
|
||||
f.close()
|
||||
except (IOError, OSError) as err:
|
||||
print("%s: cannot open: %s" % (fn, err))
|
||||
num += 1
|
||||
|
@ -127,8 +127,8 @@ def check(file):
|
||||
errprint("%s: I/O Error: %s" % (file, str(msg)))
|
||||
return
|
||||
|
||||
r = Reindenter(f)
|
||||
f.close()
|
||||
with f:
|
||||
r = Reindenter(f)
|
||||
if r.run():
|
||||
if verbose:
|
||||
print("changed.")
|
||||
@ -140,9 +140,8 @@ def check(file):
|
||||
shutil.copyfile(file, bak)
|
||||
if verbose:
|
||||
print("backed up", file, "to", bak)
|
||||
f = open(file, "w")
|
||||
r.write(f)
|
||||
f.close()
|
||||
with open(file, "w") as f:
|
||||
r.write(f)
|
||||
if verbose:
|
||||
print("wrote new", file)
|
||||
return True
|
||||
|
Loading…
Reference in New Issue
Block a user