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

Use a context manager for logfile resource management

parent 466146fb
......@@ -42,6 +42,7 @@
from html import escape
from collections import defaultdict, Counter
from contextlib import nullcontext
from datetime import datetime, timedelta, date, tzinfo
from functools import partial
from operator import itemgetter
......@@ -1520,6 +1521,7 @@ def main():
file=sys.stderr)
if filename == '-':
logfile = sys.stdin
logfile_context = nullcontext()
else:
for opener, exc in FILE_OPENER_LIST:
logfile = opener(filename, 'rt', encoding=INPUT_ENCODING, errors=INPUT_ENCODING_ERROR_HANDLER)
......@@ -1531,7 +1533,14 @@ def main():
logfile.seek(0)
break
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
for lineno, line in enumerate(logfile, 1):
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