diff options
author | Matti Picus <matti.picus@gmail.com> | 2020-01-22 08:52:50 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-22 08:52:50 +1100 |
commit | 1d05717115990a1bd5dd547b70cd960354063702 (patch) | |
tree | 6f09d3fee9ad16c8f304be51ec9315f352a5a032 /doc | |
parent | b781887a087adfa5df7205b4808dc2052fa52875 (diff) | |
parent | b54108238dbd1ce2673a042b0b270f666ea96e29 (diff) | |
download | numpy-1d05717115990a1bd5dd547b70cd960354063702.tar.gz |
Merge pull request #15375 from sethtroisi/misc_cleanups
STY: Use `with open` when possible
Diffstat (limited to 'doc')
-rwxr-xr-x | doc/cdoc/numpyfilter.py | 26 | ||||
-rwxr-xr-x | doc/postprocess.py | 10 |
2 files changed, 13 insertions, 23 deletions
diff --git a/doc/cdoc/numpyfilter.py b/doc/cdoc/numpyfilter.py index 65c801206..fe03fcef7 100755 --- a/doc/cdoc/numpyfilter.py +++ b/doc/cdoc/numpyfilter.py @@ -27,13 +27,12 @@ def main(): cache = load_cache() - f = open(args[0], 'r') try: - text = f.read() - text = comment_re.sub(lambda m: process_match(m, cache), text) - sys.stdout.write(text) + with open(args[0], 'r') as f: + text = f.read() + text = comment_re.sub(lambda m: process_match(m, cache), text) + sys.stdout.write(text) finally: - f.close() save_cache(cache) def filter_comment(text): @@ -67,23 +66,18 @@ def process_match(m, cache=None): def load_cache(): if os.path.exists(CACHE_FILE): - f = open(CACHE_FILE, 'rb') - try: - cache = pickle.load(f) - except Exception: - cache = {} - finally: - f.close() + with open(CACHE_FILE, 'rb') as f: + try: + cache = pickle.load(f) + except Exception: + cache = {} else: cache = {} return cache def save_cache(cache): - f = open(CACHE_FILE + '.new', 'wb') - try: + with open(CACHE_FILE + '.new', 'wb') as f: pickle.dump(cache, f) - finally: - f.close() os.rename(CACHE_FILE + '.new', CACHE_FILE) def render_html(text): diff --git a/doc/postprocess.py b/doc/postprocess.py index b6d067437..e4cbecf7b 100755 --- a/doc/postprocess.py +++ b/doc/postprocess.py @@ -23,18 +23,14 @@ def main(): p.error('unknown mode %s' % mode) for fn in args: - f = io.open(fn, 'r', encoding="utf-8") - try: + with io.open(fn, 'r', encoding="utf-8") as f: if mode == 'html': lines = process_html(fn, f.readlines()) elif mode == 'tex': lines = process_tex(f.readlines()) - finally: - f.close() - f = io.open(fn, 'w', encoding="utf-8") - f.write("".join(lines)) - f.close() + with io.open(fn, 'w', encoding="utf-8") as f: + f.write("".join(lines)) def process_html(fn, lines): return lines |