Merge pull request #1707 from Eric89GXL/context-encoding

FIX: Use context managers and explicit encoding
This commit is contained in:
Georg Brandl 2015-02-08 18:48:18 +01:00
commit 4b396d4232

View File

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