Commit 8b3bc259 authored by Josip Sokcevic's avatar Josip Sokcevic Committed by LUCI CQ

Fix gclient_utils.FileRead() behavior for both 2 and 3.

We always want gclient_utils.FileRead() to return a text
string these days, but the existing code didn't work
right for Python2 if you called it explicitly with mode='r'.

R=ehmaldonado@chromium.org

Bug: 1210746
Change-Id: If540746f31308b671f9101334b5cd9848a7e9257
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2930626Reviewed-by: 's avatarJosip Sokcevic <sokcevic@google.com>
Reviewed-by: 's avatarEdward Lesmes <ehmaldonado@chromium.org>
Commit-Queue: Josip Sokcevic <sokcevic@google.com>
parent a0b0f128
......@@ -171,14 +171,12 @@ def AskForData(message):
def FileRead(filename, mode='rbU'):
# Always decodes output to a Unicode string.
# On Python 3 newlines are converted to '\n' by default and 'U' is deprecated.
if mode == 'rbU' and sys.version_info.major == 3:
mode = 'rb'
with open(filename, mode=mode) as f:
# mode is ignored now; we always return unicode strings.
with open(filename, mode='rb') as f:
s = f.read()
if isinstance(s, bytes):
return s.decode('utf-8', 'replace')
try:
return s.decode('utf-8', 'replace')
except (UnicodeDecodeError, AttributeError):
return s
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment