From 93df7f4276f8b6507e61e2d51fc4c0d18fc6ae6a Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Mon, 29 Jun 2020 15:43:33 -0700 Subject: [PATCH] PathSanitizingFileCheck: improve Python3 compatibility Adjust the regex match to do a better job of sanitizing the paths. This improves the test coverage pass rate with Python 3. Furthermore, handle the unicode conversion properly that breaks with Python 3. This further improves the Python 3 test coverage pass rate. --- utils/PathSanitizingFileCheck | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/utils/PathSanitizingFileCheck b/utils/PathSanitizingFileCheck index 55cc4a5d286ac..3496895ec0d07 100755 --- a/utils/PathSanitizingFileCheck +++ b/utils/PathSanitizingFileCheck @@ -67,20 +67,21 @@ constants.""") if args.enable_windows_compatibility: if args.enable_yaml_compatibility: - slashes_re = b'(/|\\\\\\\\|\\\\\\\\\\\\\\\\)' + slashes_re = r'(/|\\\\|\\\\\\\\)' else: - slashes_re = b'(/|\\\\\\\\)' + slashes_re = r'(/|\\\\)' else: - slashes_re = b'/' + slashes_re = r'/' - stdin = io.open(sys.stdin.fileno(), 'rb').read() + stdin = io.open(sys.stdin.fileno(), 'r', encoding='utf-8', errors='ignore').read() for s in args.sanitize_strings: - replacement, pattern = s.encode(encoding="utf-8").split(b'=', 1) + replacement, pattern = s.split('=', 1) # Since we want to use pattern as a regex in some platforms, we need # to escape it first, and then replace the escaped slash # literal (r'\\/') for our platform-dependent slash regex. - stdin = re.sub(re.sub(b'\\\\/', slashes_re, re.escape(pattern)), + stdin = re.sub(re.sub('\\\\/' if sys.version_info[0] < 3 else r'[/\\]', + slashes_re, re.escape(pattern)), replacement, stdin) @@ -90,7 +91,7 @@ constants.""") else: p = subprocess.Popen( [args.file_check_path] + unknown_args, stdin=subprocess.PIPE) - stdout, stderr = p.communicate(stdin) + stdout, stderr = p.communicate(stdin.encode('utf-8')) if stdout is not None: print(stdout) if stderr is not None: