From 9a51e6a6df8a56aebede133687e91e519a186122 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Tue, 7 Oct 2014 14:10:28 +0200 Subject: Closes #980: fix DeprecationWarnings (mostly due to files closed by __del__) on Py3. Also fix a bunch of other uses of open() to use the with statement. --- scripts/find_codetags.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'scripts') diff --git a/scripts/find_codetags.py b/scripts/find_codetags.py index f8204e6e..5063f61f 100755 --- a/scripts/find_codetags.py +++ b/scripts/find_codetags.py @@ -39,12 +39,12 @@ def escape_html(text): def process_file(store, filename): try: - f = open(filename, 'r') + fp = open(filename, 'r') except (IOError, OSError): return False llmatch = 0 - try: - for lno, line in enumerate(f): + with fp: + for lno, line in enumerate(fp): # just some random heuristics to filter out binary files if lno < 100 and binary_re.search(line): return False @@ -69,8 +69,6 @@ def process_file(store, filename): continue llmatch = 0 return True - finally: - f.close() def main(): @@ -198,13 +196,13 @@ td { padding: 2px 5px 2px 5px; '%%(tag)s' '%%(who)s%%(what)s') - f = open(output, 'w') table = '\n'.join(TABLE % fname + '\n'.join(TR % (no % 2,) % entry for no, entry in enumerate(store[fname])) for fname in sorted(store)) - f.write(HTML % (', '.join(map(abspath, args)), table)) - f.close() + + with open(output, 'w') as fp: + fp.write(HTML % (', '.join(map(abspath, args)), table)) print("Report written to %s." % output) return 0 -- cgit v1.2.1