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>
diff --git a/gclient_utils.py b/gclient_utils.py
index 52b2350..6939fff 100644
--- a/gclient_utils.py
+++ b/gclient_utils.py
@@ -164,19 +164,16 @@
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'):