From 53ee708fe8135d10e651bf399dae06499b595d8d Mon Sep 17 00:00:00 2001 From: David Sanders Date: Mon, 5 Oct 2020 17:40:04 -0700 Subject: [PATCH] build: python3 compat and fix line endings on Win (#25767) --- script/check-trailing-whitespace.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/script/check-trailing-whitespace.py b/script/check-trailing-whitespace.py index 128853f2bc0e..e9cfe4609754 100755 --- a/script/check-trailing-whitespace.py +++ b/script/check-trailing-whitespace.py @@ -1,7 +1,8 @@ #!/usr/bin/env python -from __future__ import print_function +from __future__ import print_function, unicode_literals import argparse +import io import os import sys @@ -38,12 +39,11 @@ def main(): def hasTrailingWhiteSpace(filepath, fix): try: - f = open(filepath, 'r') - lines = f.read().splitlines() + with io.open(filepath, 'r', encoding='utf-8') as f: + lines = f.read().splitlines() except KeyboardInterrupt: print('Keyboard interruption while parsing. Please try again.') - finally: - f.close() + return fixed_lines = [] for num, line in enumerate(lines): @@ -53,7 +53,7 @@ def hasTrailingWhiteSpace(filepath, fix): num + 1, filepath)) return True if fix: - with open(filepath, 'w') as f: + with io.open(filepath, 'w', newline='\n', encoding='utf-8') as f: print(fixed_lines) f.writelines(fixed_lines)