[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>
diff --git a/cpplint.py b/cpplint.py
index 60b9c2b..04fd675 100755
--- a/cpplint.py
+++ b/cpplint.py
@@ -6244,7 +6244,8 @@
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()