Skip to content

Commit 041be04

Browse files
committed
test: Explicitly work with binary data in PathSanitizingFileCheck
Python 3 uses the concepts of text and (binary) data instead of Unicode strings and 8-bit strings. This change makes it explicit in Python 2 and 3 that this utility works with binary data instead of strings. * print(bytes(r'(/|\\\\|\\\\\\\\)', encoding="ascii")) => b'(/|\\\\\\\\|\\\\\\\\\\\\\\\\)' * print(bytes(r'(/|\\\\)', encoding="ascii")) => b'(/|\\\\\\\\)' * print(bytes(r'/', encoding="ascii")) => b'/' * print(bytes(r'\\/', encoding="ascii")) => b'\\\\/'
1 parent ed28d8b commit 041be04

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

utils/PathSanitizingFileCheck

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from __future__ import print_function
1313

1414
import argparse
15+
import io
1516
import re
1617
import subprocess
1718
import sys
@@ -66,20 +67,20 @@ constants.""")
6667

6768
if args.enable_windows_compatibility:
6869
if args.enable_yaml_compatibility:
69-
slashes_re = r'(/|\\\\|\\\\\\\\)'
70+
slashes_re = b'(/|\\\\\\\\|\\\\\\\\\\\\\\\\)'
7071
else:
71-
slashes_re = r'(/|\\\\)'
72+
slashes_re = b'(/|\\\\\\\\)'
7273
else:
73-
slashes_re = r'/'
74+
slashes_re = b'/'
7475

75-
stdin = sys.stdin.read()
76+
stdin = io.open(sys.stdin.fileno(), 'rb').read()
7677

7778
for s in args.sanitize_strings:
78-
replacement, pattern = s.split('=', 1)
79+
replacement, pattern = s.encode(encoding="utf-8").split(b'=', 1)
7980
# Since we want to use pattern as a regex in some platforms, we need
8081
# to escape it first, and then replace the escaped slash
8182
# literal (r'\\/') for our platform-dependent slash regex.
82-
stdin = re.sub(re.sub(r'\\/', slashes_re, re.escape(pattern)),
83+
stdin = re.sub(re.sub(b'\\\\/', slashes_re, re.escape(pattern)),
8384
replacement,
8485
stdin)
8586

0 commit comments

Comments
 (0)