gclient_utils: Make FileRead always return a Unicode string.

Previously, it returned bytes if the file did not contain valid Unicode data,
and a Unicode string otherwise.
It is confusing to reason about such a function, and no current caller needs
bytes data AFAICT, so make FileRead always return Unicode strings.

Bug: 1009814
Change-Id: I89dd1935e5d4fcaf9af71585b85bda6c47695950
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1880013
Reviewed-by: Anthony Polito <apolito@google.com>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
This commit is contained in:
Edward Lemur
2019-10-25 22:17:49 +00:00
committed by Commit Bot
parent b964662972
commit 419c92f1bc
2 changed files with 28 additions and 10 deletions

View File

@@ -164,19 +164,16 @@ class PrintableObject(object):
return output
def FileRead(filename, mode='rU'):
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 == 'rU' and sys.version_info.major == 3:
mode = 'r'
if mode == 'rbU' and sys.version_info.major == 3:
mode = 'rb'
with open(filename, mode=mode) as f:
# codecs.open() has different behavior than open() on python 2.6 so use
# open() and decode manually.
s = f.read()
try:
return s.decode('utf-8')
# AttributeError is for Py3 compatibility
except (UnicodeDecodeError, AttributeError):
return s
if isinstance(s, bytes):
return s.decode('utf-8', 'replace')
return s
def FileWrite(filename, content, mode='w'):