[cpplint] Use `with` statement to ensure file descriptor is closed

When running on macOS under python3 as part of PRESUBMIT_test.py,
a ResourceWarning is printed. This is due to the `open(..).read()`
idiom.

Use a `with` statement to ensure the file is properly closed.

Bug: none
Change-Id: I5643db0b77e6896b4b86177d2c1fe8dea04b58ae
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/4742422
Reviewed-by: Gavin Mak <gavinmak@google.com>
Auto-Submit: Sylvain Defresne <sdefresne@chromium.org>
Commit-Queue: Gavin Mak <gavinmak@google.com>
changes/22/4742422/2
Sylvain Defresne 2 years ago committed by LUCI CQ
parent 43083529de
commit 071df9a729

3
cpplint.py vendored

@ -6244,7 +6244,8 @@ def ProcessFile(filename, vlevel, extra_check_functions=[]):
codecs.getwriter('utf8'),
'replace').read().split('\n')
else:
lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n')
with codecs.open(filename, 'r', 'utf8', 'replace') as stream:
lines = stream.read().split('\n')
# Remove trailing '\r'.
# The -1 accounts for the extra trailing blank line we get from split()

Loading…
Cancel
Save