FIX: Use context managers and explicit encoding

This commit is contained in:
Eric89GXL 2015-02-04 13:52:36 -08:00
parent 8e780a5a10
commit 8bd28f6af1

View File

@ -137,9 +137,7 @@ def generate_autosummary_docs(sources, output_dir=None, suffix='.rst',
new_files.append(fn) new_files.append(fn)
f = open(fn, 'w') with open(fn, 'w') as f:
try:
doc = get_documenter(obj, parent) doc = get_documenter(obj, parent)
if template_name is not None: if template_name is not None:
@ -201,8 +199,6 @@ def generate_autosummary_docs(sources, output_dir=None, suffix='.rst',
rendered = template.render(**ns) rendered = template.render(**ns)
f.write(rendered) f.write(rendered)
finally:
f.close()
# descend recursively to new files # descend recursively to new files
if new_files: if new_files:
@ -221,12 +217,13 @@ def find_autosummary_in_files(filenames):
""" """
documented = [] documented = []
for filename in filenames: for filename in filenames:
f = open(filename, 'r') with open(filename, 'r', encoding='utf-8') as f:
lines = f.read().splitlines() lines = f.read().splitlines()
documented.extend(find_autosummary_in_lines(lines, filename=filename)) documented.extend(find_autosummary_in_lines(lines,
f.close() filename=filename))
return documented return documented
def find_autosummary_in_docstring(name, module=None, filename=None): def find_autosummary_in_docstring(name, module=None, filename=None):
"""Find out what items are documented in the given object's docstring. """Find out what items are documented in the given object's docstring.
@ -245,6 +242,7 @@ def find_autosummary_in_docstring(name, module=None, filename=None):
"statement and it might call sys.exit()." % name) "statement and it might call sys.exit()." % name)
return [] return []
def find_autosummary_in_lines(lines, module=None, filename=None): def find_autosummary_in_lines(lines, module=None, filename=None):
"""Find out what items appear in autosummary:: directives in the """Find out what items appear in autosummary:: directives in the
given lines. given lines.