Commit 3cc66082 authored by Vincent Pelletier's avatar Vincent Pelletier

Use a context manager for logfile resource management

parent 466146fb
...@@ -42,6 +42,7 @@ ...@@ -42,6 +42,7 @@
from html import escape from html import escape
from collections import defaultdict, Counter from collections import defaultdict, Counter
from contextlib import nullcontext
from datetime import datetime, timedelta, date, tzinfo from datetime import datetime, timedelta, date, tzinfo
from functools import partial from functools import partial
from operator import itemgetter from operator import itemgetter
...@@ -1520,6 +1521,7 @@ def main(): ...@@ -1520,6 +1521,7 @@ def main():
file=sys.stderr) file=sys.stderr)
if filename == '-': if filename == '-':
logfile = sys.stdin logfile = sys.stdin
logfile_context = nullcontext()
else: else:
for opener, exc in FILE_OPENER_LIST: for opener, exc in FILE_OPENER_LIST:
logfile = opener(filename, 'rt', encoding=INPUT_ENCODING, errors=INPUT_ENCODING_ERROR_HANDLER) logfile = opener(filename, 'rt', encoding=INPUT_ENCODING, errors=INPUT_ENCODING_ERROR_HANDLER)
...@@ -1531,7 +1533,14 @@ def main(): ...@@ -1531,7 +1533,14 @@ def main():
logfile.seek(0) logfile.seek(0)
break break
else: else:
logfile = open(filename, 'r', encoding=INPUT_ENCODING, errors=INPUT_ENCODING_ERROR_HANDLER) logfile = open( # pylint: disable=consider-using-with
filename,
'r',
encoding=INPUT_ENCODING,
errors=INPUT_ENCODING_ERROR_HANDLER,
)
logfile_context = logfile
with logfile_context:
lineno = 0 lineno = 0
for lineno, line in enumerate(logfile, 1): for lineno, line in enumerate(logfile, 1):
if show_progress and lineno % 5000 == 0: if show_progress and lineno % 5000 == 0:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment