summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVille Skyttä <ville.skytta@iki.fi>2016-07-07 18:53:34 +0300
committerVille Skyttä <ville.skytta@iki.fi>2016-07-07 18:53:34 +0300
commite8a8be5788fcf761d61d2e584e25a7d08b93cc95 (patch)
tree5f458c979197ef94c9a4a73dd4b04ec5db1de508
parent593708a39cda458386b6c7352bebb62df6191af7 (diff)
downloadsphinx-git-e8a8be5788fcf761d61d2e584e25a7d08b93cc95.tar.gz
Handle more file closing with "with"
-rw-r--r--setup.py10
-rw-r--r--sphinx/builders/html.py5
-rw-r--r--sphinx/theming.py5
-rwxr-xr-xtests/coverage.py16
-rwxr-xr-xutils/check_sources.py5
-rwxr-xr-xutils/reindent.py9
6 files changed, 17 insertions, 33 deletions
diff --git a/setup.py b/setup.py
index d334d140e..c1aa346ba 100644
--- a/setup.py
+++ b/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
diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py
index f670c1399..462f5f00a 100644
--- a/sphinx/builders/html.py
+++ b/sphinx/builders/html.py
@@ -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()
diff --git a/sphinx/theming.py b/sphinx/theming.py
index 539184115..42e4448db 100644
--- a/sphinx/theming.py
+++ b/sphinx/theming.py
@@ -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))
diff --git a/tests/coverage.py b/tests/coverage.py
index f9341d8ba..cd36e218a 100755
--- a/tests/coverage.py
+++ b/tests/coverage.py
@@ -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
diff --git a/utils/check_sources.py b/utils/check_sources.py
index 16bc918cb..18d444057 100755
--- a/utils/check_sources.py
+++ b/utils/check_sources.py
@@ -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
diff --git a/utils/reindent.py b/utils/reindent.py
index 7679045cb..ee13a634a 100755
--- a/utils/reindent.py
+++ b/utils/reindent.py
@@ -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